diff --git a/src/CSS.php b/src/CSS.php index 44c61a1..3ba9e55 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -722,7 +722,7 @@ protected function extractMath() // and since we've captured more code than required, we may have some leftover // calc() in here too - go recursive on the remaining but of code to go figure // that out and extract what is needed - $rest = str_replace($function.$expr, '', $match[0]); + $rest = $minifier->str_replace_first($function.$expr, '', $match[0]); $rest = preg_replace_callback($pattern, $callback, $rest); return $placeholder.$rest; diff --git a/src/Minify.php b/src/Minify.php index 736ff11..2ff0c16 100644 --- a/src/Minify.php +++ b/src/Minify.php @@ -494,4 +494,12 @@ protected function writeToFile($handler, $content, $path = '') throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.'); } } + + protected static function str_replace_first($search, $replace, $subject) { + $pos = strpos($subject, $search); + if ($pos !== false) { + return substr_replace($subject, $replace, $pos, strlen($search)); + } + return $subject; + } } diff --git a/tests/css/CSSTest.php b/tests/css/CSSTest.php index 634f88b..a9e05eb 100644 --- a/tests/css/CSSTest.php +++ b/tests/css/CSSTest.php @@ -849,6 +849,12 @@ public function dataProvider() '.stk-block-columns>.stk-block-content{--stk-column-gap:0px;column-gap:var(--stk-column-gap,0)}' ); + // https://github.com/matthiasmullie/minify/issues/395 + $tests[] = array( + 'background-position: right 0.8em bottom calc(50% - 5px), right 0.8em top calc(50% - 5px);', + 'background-position:right .8em bottom calc(50% - 5px),right .8em top calc(50% - 5px);' + ); + return $tests; }