* css.cls.php - modified PHP implementation of Matthias Mullie's CSS minifier
* @author Matthias Mullie <minify@mullie.eu>
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
namespace LiteSpeed\Lib\CSS_JS_MIN\Minify;
use LiteSpeed\Lib\CSS_JS_MIN\Minify\Minify;
use LiteSpeed\Lib\CSS_JS_MIN\Minify\Exception\FileImportException;
use LiteSpeed\Lib\CSS_JS_MIN\PathConverter\Converter;
use LiteSpeed\Lib\CSS_JS_MIN\PathConverter\ConverterInterface;
defined( 'WPINC' ) || exit;
class CSS extends Minify {
* @var int maximum import size in kB
protected $maxImportSize = 5;
* @var string[] valid import extensions
protected $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',
'xbm' => 'image/x-xbitmap',
* Set the maximum size if files to be imported.
* 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.
* @param int $size Size in kB
public function setMaxImportSize( $size ) {
$this->maxImportSize = $size;
* Set the type of extensions to be imported into the CSS (to save network
* Keys of the array should be the file extensions & respective values
* should be the data type.
* @param string[] $extensions Array of file extensions
public function setImportExtensions( array $extensions ) {
$this->importExtensions = $extensions;
* Move any import statements to the top.
* @param string $content Nearly finished CSS content
public function moveImportsToTop( $content ) {
if ( preg_match_all( '/(;?)(@import (?<url>url\()?(?P<quotes>["\']?).+?(?P=quotes)(?(url)\)));?/', $content, $matches ) ) {
foreach ( $matches[0] as $import ) {
$content = str_replace( $import, '', $content );
$content = implode( ';', $matches[2] ) . ';' . trim( $content, ';' );
* Combine CSS from import statements.
* \@import's will be loaded and their content merged into the original file,
* @param string $source The file to combine imports for
* @param string $content The CSS content to combine imports for
* @param string[] $parents Parent paths, for circular reference checks
* @throws FileImportException
protected function combineImports( $source, $content, $parents ) {
# (optional) open path enclosure
# (optional) close path enclosure
# (optional) trailing whitespace
# (optional) media statement(s)
# (optional) trailing whitespace
# (optional) closing semi-colon
# (optional) trailing whitespace
# (optional) media statement(s)
# (optional) trailing whitespace
# (optional) closing semi-colon
// find all relative imports in css
foreach ( $importRegexes as $importRegex ) {
if ( preg_match_all( $importRegex, $content, $regexMatches, PREG_SET_ORDER ) ) {
$matches = array_merge( $matches, $regexMatches );
foreach ( $matches as $match ) {
// get the path for the file that will be imported
$importPath = dirname( $source ) . '/' . $match['path'];
// only replace the import with the content if we can grab the
if ( ! $this->canImportByPath( $match['path'] ) || ! $this->canImportFile( $importPath ) ) {
// check if current file was not imported previously in the same
if ( in_array( $importPath, $parents ) ) {
throw new FileImportException( 'Failed to import file "' . $importPath . '": circular reference detected.' );
// grab referenced file & minify it (which may include importing
// yet other @import statements recursively)
$minifier = new self( $importPath );
$minifier->setMaxImportSize( $this->maxImportSize );
$minifier->setImportExtensions( $this->importExtensions );
$importContent = $minifier->execute( $source, $parents );
// check if this is only valid for certain media
if ( ! empty( $match['media'] ) ) {
$importContent = '@media ' . $match['media'] . '{' . $importContent . '}';
// add to replacement array
$replace[] = $importContent;
// replace the import statements
return str_replace( $search, $replace, $content );
* Import files into the CSS, base64 encoded.
* Included images @url(image.jpg) will be loaded and their content merged into the
* original file, to save HTTP requests.
* @param string $source The file to import files for
* @param string $content The CSS content to import files for
protected function importFiles( $source, $content ) {
$regex = '/url\((["\']?)(.+?)\\1\)/i';
if ( $this->importExtensions && preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $match ) {
$extension = substr( strrchr( $match[2], '.' ), 1 );
if ( $extension && ! array_key_exists( $extension, $this->importExtensions ) ) {
// get the path for the file that will be imported
$path = dirname( $source ) . '/' . $path;
// only replace the import with the content if we're able to get
// the content of the file, and it's relatively small
if ( $this->canImportFile( $path ) && $this->canImportBySize( $path ) ) {
// grab content && base64-ize
$importContent = $this->load( $path );
$importContent = base64_encode( $importContent );
$replace[] = 'url(' . $this->importExtensions[ $extension ] . ';base64,' . $importContent . ')';
// replace the import statements
$content = str_replace( $search, $replace, $content );
* Perform CSS optimizations.
* @param string[optional] $path Path to write the data to
* @param string[] $parents Parent paths, for circular reference checks
* @return string The minified data
public function execute( $path = null, $parents = array() ) {
// loop CSS data (raw data and files)
foreach ( $this->data as $source => $css ) {
* Let's first take out strings & comments, since we can't just
* remove whitespace anywhere. If whitespace occurs inside a string,
* we should leave it alone. E.g.:
* p { content: "a test" }
$this->extractCustomProperties();
$css = $this->replace( $css );
$css = $this->stripWhitespace( $css );
$css = $this->convertLegacyColors( $css );
$css = $this->cleanupModernColors( $css );
$css = $this->shortenHEXColors( $css );
$css = $this->shortenZeroes( $css );
$css = $this->shortenFontWeights( $css );
$css = $this->stripEmptyTags( $css );
// restore the string we've extracted earlier
$css = $this->restoreExtractedData( $css );
$source = is_int( $source ) ? '' : $source;
$parents = $source ? array_merge( $parents, array( $source ) ) : $parents;
$css = $this->combineImports( $source, $css, $parents );
$css = $this->importFiles( $source, $css );
* If we'll save to a new path, we'll have to fix the relative paths
* to be relative no longer to the source file, but to the new path.
* If we don't write to a file, fall back to same path so no
* conversion happens (because we still want it to go through most
* of the move code, which also addresses url() & @import syntax...)
$converter = $this->getPathConverter( $source, $path ?: $source );
$css = $this->move( $converter, $css );
$content = $this->moveImportsToTop( $content );
* Moving a css file should update all relative urls.
* 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).
* @param ConverterInterface $converter Relative path converter
* @param string $content The CSS content to update relative urls for
protected function move( ConverterInterface $converter, $content ) {
* Relative path references will usually be enclosed by url(). @import
* is an exception, where url() is not necessary around the path (but is
* This *could* be 1 regular expression, where both regular expressions
* in this array are on different sides of a |. But we're using named
* patterns in both regexes, the same name on both regexes. This is only
* possible with a (?J) modifier, but that only works after a fairly
* recent PCRE version. That's why I'm doing 2 separate regular
* expressions & combining the matches after executing of both.
$relativeRegexes = array(
# we don\'t have to check for @import url(), because the
# condition above will already catch these
// find all relative urls in css
foreach ( $relativeRegexes as $relativeRegex ) {
if ( preg_match_all( $relativeRegex, $content, $regexMatches, PREG_SET_ORDER ) ) {
$matches = array_merge( $matches, $regexMatches );
foreach ( $matches as $match ) {
// determine if it's a url() or an @import match
$type = ( strpos( $match[0], '@import' ) === 0 ? 'import' : 'url' );
if ( $this->canImportByPath( $url ) ) {
// attempting to interpret GET-params makes no sense, so let's discard them for awhile
$params = strrchr( $url, '?' );
$url = $params ? substr( $url, 0, -strlen( $params ) ) : $url;
$url = $converter->convert( $url );
// now that the path has been converted, re-apply GET-params
* Urls with control characters above 0x7e should be quoted.
* According to Mozilla's parser, whitespace is only allowed at the
* Urls with `)` (as could happen with data: uris) should also be
* quoted to avoid being confused for the url() closing parentheses.
* And urls with a # have also been reported to cause issues.
* Urls with quotes inside should also remain escaped.
* @see https://developer.mozilla.org/nl/docs/Web/CSS/url#The_url()_functional_notation
* @see https://hg.mozilla.org/mozilla-central/rev/14abca4e7378
* @see https://github.com/matthiasmullie/minify/issues/193
if ( preg_match( '/[\s\)\'"#\x{7f}-\x{9f}]/u', $url ) ) {
$url = $match['quotes'] . $url . $match['quotes'];
$replace[] = 'url(' . $url . ')';
} elseif ( $type === 'import' ) {
$replace[] = '@import "' . $url . '"';
return str_replace( $search, $replace, $content );
* Shorthand HEX color codes.
* #FF0000FF -> #f00 -> red
* #FF00FF00 -> transparent.
* @param string $content The CSS content to shorten the HEX color codes for
protected function shortenHexColors( $content ) {
// shorten repeating patterns within HEX ..
$content = preg_replace( '/(?<=[: ])#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?(?=[; }])/i', '#$1$2$3$4', $content );
// remove alpha channel if it's pointless ..
$content = preg_replace( '/(?<=[: ])#([0-9a-f]{6})ff(?=[; }])/i', '#$1', $content );
$content = preg_replace( '/(?<=[: ])#([0-9a-f]{3})f(?=[; }])/i', '#$1', $content );
// replace `transparent` with shortcut ..
$content = preg_replace( '/(?<=[: ])#[0-9a-f]{6}00(?=[; }])/i', '#fff0', $content );
// make these more readable
// we can shorten some even more by replacing them with their color name