mirror of
https://github.com/matthiasmullie/minify.git
synced 2025-02-22 12:13:01 +01:00
parent
9ba1b45982
commit
8931f76af2
29
src/CSS.php
29
src/CSS.php
@ -307,7 +307,7 @@ class CSS extends Minify
|
||||
*/
|
||||
$this->extractStrings();
|
||||
$this->stripComments();
|
||||
$this->extractCalcs();
|
||||
$this->extractMath();
|
||||
$css = $this->replace($css);
|
||||
|
||||
$css = $this->stripWhitespace($css);
|
||||
@ -678,19 +678,21 @@ class CSS extends Minify
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all `calc()` occurrences.
|
||||
* Replace all occurrences of functions that may contain math, where
|
||||
* whitespace around operators needs to be preserved (e.g. calc, clamp)
|
||||
*/
|
||||
protected function extractCalcs()
|
||||
protected function extractMath()
|
||||
{
|
||||
// PHP only supports $this inside anonymous functions since 5.4
|
||||
$minifier = $this;
|
||||
$callback = function ($match) use ($minifier) {
|
||||
$length = strlen($match[1]);
|
||||
$function = $match[1];
|
||||
$length = strlen($match[2]);
|
||||
$expr = '';
|
||||
$opened = 0;
|
||||
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$char = $match[1][$i];
|
||||
$char = $match[2][$i];
|
||||
$expr .= $char;
|
||||
if ($char === '(') {
|
||||
$opened++;
|
||||
@ -698,18 +700,25 @@ class CSS extends Minify
|
||||
break;
|
||||
}
|
||||
}
|
||||
$rest = str_replace($expr, '', $match[1]);
|
||||
$rest = str_replace($expr, '', $match[2]);
|
||||
$expr = trim(substr($expr, 1, -1));
|
||||
|
||||
$count = count($minifier->extracted);
|
||||
$placeholder = 'calc('.$count.')';
|
||||
$minifier->extracted[$placeholder] = 'calc('.$expr.')';
|
||||
$placeholder = 'math('.$count.')';
|
||||
$minifier->extracted[$placeholder] = $function.'('.$expr.')';
|
||||
|
||||
return $placeholder.$rest;
|
||||
};
|
||||
|
||||
$this->registerPattern('/calc(\(.+?)(?=$|;|}|calc\()/', $callback);
|
||||
$this->registerPattern('/calc(\(.+?)(?=$|;|}|calc\()/m', $callback);
|
||||
$functions = array('calc', 'clamp', 'min', 'max');
|
||||
$this->registerPattern(
|
||||
'/('. implode($functions, '|') .')(\(.+?)(?=$|;|}|('. implode($functions, '|') .')\()/',
|
||||
$callback
|
||||
);
|
||||
$this->registerPattern(
|
||||
'/('. implode($functions, '|') .')(\(.+?)(?=$|;|}|('. implode($functions, '|') .')\()/m',
|
||||
$callback
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -494,15 +494,15 @@ only screen and (min-device-pixel-ratio: 1.5) {
|
||||
'p{width:calc(35% + (10% + 0px + 10%))}',
|
||||
);
|
||||
|
||||
// https://github.com/matthiasmullie/minify/issues/274
|
||||
$tests[] = array(
|
||||
'.cvp-live-filter select {
|
||||
// https://github.com/matthiasmullie/minify/issues/274
|
||||
$tests[] = array(
|
||||
'.cvp-live-filter select {
|
||||
background-position:
|
||||
calc(100% - 20px) calc(1em + 2px),
|
||||
calc(100% - 15px) calc(1em + 2px),
|
||||
calc(100% - 2.5em) 0.5em;
|
||||
}',
|
||||
'.cvp-live-filter select{background-position:calc(100% - 20px) calc(1em + 2px),calc(100% - 15px) calc(1em + 2px),calc(100% - 2.5em) .5em}',
|
||||
'.cvp-live-filter select{background-position:calc(100% - 20px) calc(1em + 2px),calc(100% - 15px) calc(1em + 2px),calc(100% - 2.5em) .5em}',
|
||||
);
|
||||
|
||||
// https://github.com/matthiasmullie/minify/issues/301
|
||||
@ -800,6 +800,12 @@ body{font-family:sans-serif}',
|
||||
'ul p{padding-left:calc((var(--icon-size) / 2) + var(--horisontal-space))}',
|
||||
);
|
||||
|
||||
// https://github.com/matthiasmullie/minify/issues/351
|
||||
$tests[] = array(
|
||||
'clamp(2.5rem, 1rem + 4vw, 4rem)',
|
||||
'clamp(2.5rem, 1rem + 4vw, 4rem)',
|
||||
);
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user