diff --git a/src/CSS.php b/src/CSS.php index ece9c6d..1528666 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -308,6 +308,7 @@ class CSS extends Minify $this->extractStrings(); $this->stripComments(); $this->extractMath(); + $this->extractCustomProperties(); $css = $this->replace($css); $css = $this->stripWhitespace($css); @@ -721,6 +722,25 @@ class CSS extends Minify ); } + /** + * Replace custom properties, whose values may be used in scenarios where + * we wouldn't want them to be minified (e.g. inside calc) + */ + protected function extractCustomProperties() + { + // PHP only supports $this inside anonymous functions since 5.4 + $minifier = $this; + $this->registerPattern( + '/(?<=^|[;}])(--[^:;{}"\'\s]+)\s*:([^;{}]+)/m', + function ($match) use ($minifier) { + $placeholder = '--custom-'. count($minifier->extracted) . ':0'; + $minifier->extracted[$placeholder] = $match[1] .':'. trim($match[2]); + return $placeholder; + + } + ); + } + /** * Check if file is small enough to be imported. * diff --git a/tests/css/CSSTest.php b/tests/css/CSSTest.php index f8e61ae..89f3868 100644 --- a/tests/css/CSSTest.php +++ b/tests/css/CSSTest.php @@ -806,6 +806,13 @@ body{font-family:sans-serif}', 'clamp(2.5rem, 1rem + 4vw, 4rem)', ); + // https://github.com/matthiasmullie/minify/issues/342 + $tests[] = array( + '--headlineFontSize: 16px + var(--multiplicator); +font-size: calc(var(--headlineFontSize));', + '--headlineFontSize:16px + var(--multiplicator);font-size:calc(var(--headlineFontSize));', + ); + return $tests; }