Preserve custom properties

Fixes #342
This commit is contained in:
Matthias Mullie 2020-12-21 15:03:51 +01:00
parent 8931f76af2
commit 26da9bbfc8
2 changed files with 27 additions and 0 deletions

View File

@ -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.
*

View File

@ -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;
}