Documentation

CSS extends Minify
in package

CSS minifier.

Please report bugs on https://github.com/matthiasmullie/minify/issues

Tags
author

Matthias Mullie minify@mullie.eu

author

Tijs Verkoyen minify@verkoyen.eu

copyright

Copyright (c) 2012, Matthias Mullie. All rights reserved

license

MIT License

Table of Contents

Properties

$data  : array<string|int, string>
The data to be minified.
$importExtensions  : array<string|int, string>
$maxImportSize  : int
$patterns  : array<string|int, string>
Array of patterns to match.

Methods

__construct()  : mixed
Init the minify class - optionally, code may be passed along already.
add()  : static
Add a file or straight-up code to be minified.
addFile()  : static
Add a file to be minified.
cache()  : CacheItemInterface
Minify the data & write it to a CacheItemInterface object.
execute()  : string
Minify the data.
gzip()  : string
Minify & gzip the data & (optionally) saves it to a file.
minify()  : string
Minify the data & (optionally) saves it to a file.
setImportExtensions()  : mixed
Set the type of extensions to be imported into the CSS (to save network connections).
setMaxImportSize()  : mixed
Set the maximum size if files to be imported.
canImportByPath()  : bool
Check if file a file can be imported, going by the path.
canImportBySize()  : bool
Check if file is small enough to be imported.
canImportFile()  : bool
Check if the path is a regular file and can be read.
combineImports()  : string
Combine CSS from import statements.
executeReplacement()  : string
If $replacement is a callback, execute it, passing in the match data.
extractCustomProperties()  : mixed
Replace custom properties, whose values may be used in scenarios where we wouldn't want them to be minified (e.g. inside calc).
extractMath()  : mixed
Replace all occurrences of functions that may contain math, where whitespace around operators needs to be preserved (e.g. calc, clamp).
extractStrings()  : mixed
Strings are a pattern we need to match, in order to ignore potential code-like content inside them, but we just want all of the string content to remain untouched.
getPathConverter()  : ConverterInterface
Return a converter to update relative paths to be relative to the new destination.
importFiles()  : string
Import files into the CSS, base64-ized.
load()  : string
Load data.
move()  : string
Moving a css file should update all relative urls.
moveImportsToTop()  : string
Move any import statements to the top.
openFileForWriting()  : resource
Attempts to open file specified by $path for writing.
registerPattern()  : mixed
Register a pattern to execute against the source content.
replace()  : string
We can't "just" run some regular expressions against JavaScript: it's a complex language. E.g. having an occurrence of // xyz would be a comment, unless it's used within a string. Of you could have something that looks like a 'string', but inside a comment.
restoreExtractedData()  : string
This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*(). The original content was saved in $this->extracted.
save()  : mixed
Save to file.
shortenColors()  : string
Shorthand hex color codes.
shortenFontWeights()  : string
Shorten CSS font weights.
shortenZeroes()  : string
Shorthand 0 values to plain 0, instead of e.g. -0em.
str_replace_first()  : mixed
stripComments()  : mixed
Strip comments from source code.
stripEmptyTags()  : string
Strip empty tags from source code.
stripMultilineComments()  : mixed
Both JS and CSS use the same form of multi-line comment, so putting the common code here.
stripWhitespace()  : string
Strip whitespace.
writeToFile()  : mixed
Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.

Properties

$data

The data to be minified.

protected array<string|int, string> $data = array()

$importExtensions

protected array<string|int, string> $importExtensions = array('gif' => 'data:image/gif', 'png' => 'data:image/png', 'jpe' => 'data:image/jpeg', 'jpg' => 'data:image/jpeg', 'jpeg' => 'data:image/jpeg', 'svg' => 'data:image/svg+xml', 'woff' => 'data:application/x-font-woff', 'woff2' => 'data:application/x-font-woff2', 'avif' => 'data:image/avif', 'apng' => 'data:image/apng', 'webp' => 'data:image/webp', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap')

valid import extensions

$maxImportSize

protected int $maxImportSize = 5

maximum import size in kB

$patterns

Array of patterns to match.

protected array<string|int, string> $patterns = array()

Methods

__construct()

Init the minify class - optionally, code may be passed along already.

public __construct() : mixed

add()

Add a file or straight-up code to be minified.

public add(string|array<string|int, string> $data) : static
Parameters
$data : string|array<string|int, string>
Return values
static

addFile()

Add a file to be minified.

public addFile(string|array<string|int, string> $data) : static
Parameters
$data : string|array<string|int, string>
Tags
throws
IOException
Return values
static

cache()

Minify the data & write it to a CacheItemInterface object.

public cache(CacheItemInterface $item) : CacheItemInterface
Parameters
$item : CacheItemInterface

Cache item to write the data to

Return values
CacheItemInterface

Cache item with the minifier data

execute()

Minify the data.

public execute([mixed $path = null ][, array<string|int, string> $parents = array() ]) : string

Perform CSS optimizations.

Parameters
$path : mixed = null
$parents : array<string|int, string> = array()

Parent paths, for circular reference checks

Return values
string

The minified data

gzip()

Minify & gzip the data & (optionally) saves it to a file.

public gzip([mixed $path = null ][, mixed $level = 9 ]) : string
Parameters
$path : mixed = null
$level : mixed = 9
Return values
string

The minified & gzipped data

minify()

Minify the data & (optionally) saves it to a file.

public minify([mixed $path = null ]) : string
Parameters
$path : mixed = null
Return values
string

The minified data

setImportExtensions()

Set the type of extensions to be imported into the CSS (to save network connections).

public setImportExtensions(array<string|int, string> $extensions) : mixed

Keys of the array should be the file extensions & respective values should be the data type.

Parameters
$extensions : array<string|int, string>

Array of file extensions

setMaxImportSize()

Set the maximum size if files to be imported.

public setMaxImportSize(int $size) : mixed

Files larger than this size (in kB) will not be imported into the CSS. Importing files into the CSS as data-uri will save you some connections, but we should only import relatively small decorative images so that our CSS file doesn't get too bulky.

Parameters
$size : int

Size in kB

canImportByPath()

Check if file a file can be imported, going by the path.

protected canImportByPath(string $path) : bool
Parameters
$path : string
Return values
bool

canImportBySize()

Check if file is small enough to be imported.

protected canImportBySize(string $path) : bool
Parameters
$path : string

The path to the file

Return values
bool

canImportFile()

Check if the path is a regular file and can be read.

protected canImportFile(string $path) : bool
Parameters
$path : string
Return values
bool

combineImports()

Combine CSS from import statements.

protected combineImports(string $source, string $content, array<string|int, string> $parents) : string

Import statements will be loaded and their content merged into the original file, to save HTTP requests.

Parameters
$source : string

The file to combine imports for

$content : string

The CSS content to combine imports for

$parents : array<string|int, string>

Parent paths, for circular reference checks

Tags
throws
FileImportException
Return values
string

executeReplacement()

If $replacement is a callback, execute it, passing in the match data.

protected executeReplacement(string|callable $replacement, array<string|int, mixed> $match) : string

If it's a string, just pass it through.

Parameters
$replacement : string|callable

Replacement value

$match : array<string|int, mixed>

Match data, in PREG_OFFSET_CAPTURE form

Return values
string

extractCustomProperties()

Replace custom properties, whose values may be used in scenarios where we wouldn't want them to be minified (e.g. inside calc).

protected extractCustomProperties() : mixed

extractMath()

Replace all occurrences of functions that may contain math, where whitespace around operators needs to be preserved (e.g. calc, clamp).

protected extractMath() : mixed

extractStrings()

Strings are a pattern we need to match, in order to ignore potential code-like content inside them, but we just want all of the string content to remain untouched.

protected extractStrings([mixed $chars = ''"' ][, mixed $placeholderPrefix = '' ]) : mixed

This method will replace all string content with simple STRING# placeholder text, so we've rid all strings from characters that may be misinterpreted. Original string content will be saved in $this->extracted and after doing all other minifying, we can restore the original content via restoreStrings().

Parameters
$chars : mixed = ''"'
$placeholderPrefix : mixed = ''

getPathConverter()

Return a converter to update relative paths to be relative to the new destination.

protected getPathConverter(string $source, string $target) : ConverterInterface
Parameters
$source : string
$target : string
Return values
ConverterInterface

importFiles()

Import files into the CSS, base64-ized.

protected importFiles(string $source, string $content) : string
Parameters
$source : string

The file to import files for

$content : string

The CSS content to import files for

Tags
url

(image.jpg) images will be loaded and their content merged into the original file, to save HTTP requests.

Return values
string

load()

Load data.

protected load(string $data) : string
Parameters
$data : string

Either a path to a file or the content itself

Return values
string

move()

Moving a css file should update all relative urls.

protected move(ConverterInterface $converter, string $content) : string

Relative references (e.g. ../images/image.gif) in a certain css file, will have to be updated when a file is being saved at another location (e.g. ../../images/image.gif, if the new CSS file is 1 folder deeper).

Parameters
$converter : ConverterInterface

Relative path converter

$content : string

The CSS content to update relative urls for

Return values
string

moveImportsToTop()

Move any import statements to the top.

protected moveImportsToTop(string $content) : string
Parameters
$content : string

Nearly finished CSS content

Return values
string

openFileForWriting()

Attempts to open file specified by $path for writing.

protected openFileForWriting(string $path) : resource
Parameters
$path : string

The path to the file

Tags
throws
IOException
Return values
resource

Specifier for the target file

registerPattern()

Register a pattern to execute against the source content.

protected registerPattern(string $pattern[, string|callable $replacement = '' ]) : mixed

If $replacement is a string, it must be plain text. Placeholders like $1 or \2 don't work. If you need that functionality, use a callback instead.

Parameters
$pattern : string

PCRE pattern

$replacement : string|callable = ''

Replacement value for matched pattern

replace()

We can't "just" run some regular expressions against JavaScript: it's a complex language. E.g. having an occurrence of // xyz would be a comment, unless it's used within a string. Of you could have something that looks like a 'string', but inside a comment.

protected replace(string $content) : string

The only way to accurately replace these pieces is to traverse the JS one character at a time and try to find whatever starts first.

Parameters
$content : string

The content to replace patterns in

Return values
string

The (manipulated) content

restoreExtractedData()

This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*(). The original content was saved in $this->extracted.

protected restoreExtractedData(string $content) : string
Parameters
$content : string
Return values
string

save()

Save to file.

protected save(string $content, string $path) : mixed
Parameters
$content : string

The minified data

$path : string

The path to save the minified data to

Tags
throws
IOException

shortenColors()

Shorthand hex color codes.

protected shortenColors(string $content) : string

#FF0000 -> #F00.

Parameters
$content : string

The CSS content to shorten the hex color codes for

Return values
string

shortenFontWeights()

Shorten CSS font weights.

protected shortenFontWeights(string $content) : string
Parameters
$content : string

The CSS content to shorten the font weights for

Return values
string

shortenZeroes()

Shorthand 0 values to plain 0, instead of e.g. -0em.

protected shortenZeroes(string $content) : string
Parameters
$content : string

The CSS content to shorten the zero values for

Return values
string

str_replace_first()

protected static str_replace_first(mixed $search, mixed $replace, mixed $subject) : mixed
Parameters
$search : mixed
$replace : mixed
$subject : mixed

stripComments()

Strip comments from source code.

protected stripComments() : mixed

stripEmptyTags()

Strip empty tags from source code.

protected stripEmptyTags(string $content) : string
Parameters
$content : string
Return values
string

stripMultilineComments()

Both JS and CSS use the same form of multi-line comment, so putting the common code here.

protected stripMultilineComments() : mixed

stripWhitespace()

Strip whitespace.

protected stripWhitespace(string $content) : string
Parameters
$content : string

The CSS content to strip the whitespace for

Return values
string

writeToFile()

Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.

protected writeToFile(resource $handler, string $content[, string $path = '' ]) : mixed
Parameters
$handler : resource

The resource to write to

$content : string

The content to write

$path : string = ''

The path to the file (for exception printing only)

Tags
throws
IOException

        
On this page

Search results