From cb7a9297b4ab070909cefade30ee95054d4ae87a Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Fri, 15 Mar 2024 11:27:09 +0100 Subject: [PATCH] Tighten legacy color conversion The existing regex was only separating on comma, but `var()` statements (which can be used as values) can also include a comma (after which follows a default value) This seemed to be the case in one instance in Bootstrap, causing it to be minified incorrectly. This replacement is more strict, and will only convert colors made up of their literal values, ignoring any that include `var()`s in there. Fixes #422 --- src/CSS.php | 13 +++++++------ tests/CSS/CSSTest.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index 77465ea..4f56320 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -583,12 +583,13 @@ protected function convertLegacyColors($content) */ // convert legacy color syntax - $content = preg_replace('/(rgb|hsl)a?\(([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^\s\)]+)\)/i', '$1($2 $3 $4 / $5)', $content); - $content = preg_replace('/(rgb|hsl)a?\(([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^,\s]+)\)/i', '$1($2 $3 $4)', $content); + $content = preg_replace('/(rgb)a?\(\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*,\s*([0,1]?(?:\.[0-9]*)?)\s*\)/i', '$1($2 $3 $4 / $5)', $content); + $content = preg_replace('/(rgb)a?\(\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*\)/i', '$1($2 $3 $4)', $content); + $content = preg_replace('/(hsl)a?\(\s*([0-9]+(?:deg|grad|rad|turn)?)\s*,\s*([0-9]{1,3}%)\s*,\s*([0-9]{1,3}%)\s*,\s*([0,1]?(?:\.[0-9]*)?)\s*\)/i', '$1($2 $3 $4 / $5)', $content); + $content = preg_replace('/(hsl)a?\(\s*([0-9]+(?:deg|grad|rad|turn)?)\s*,\s*([0-9]{1,3}%)\s*,\s*([0-9]{1,3}%)\s*\)/i', '$1($2 $3 $4)', $content); // convert `rgb` to `hex` - $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; // [000-255] THX @ https://www.regular-expressions.info/numericranges.html - + $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; return preg_replace_callback( "/rgb\($dec $dec $dec\)/i", function ($match) { @@ -620,10 +621,10 @@ protected function cleanupModernColors($content) $tag = '(rgb|hsl|hwb|(?:(?:ok)?(?:lch|lab)))'; // remove alpha channel if it's pointless .. - $content = preg_replace('/' . $tag . '\(([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+\/\s+1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); + $content = preg_replace('/' . $tag . '\(\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+\/\s+1(?:(?:\.\d?)*|00%)?\s*\)/i', '$1($2 $3 $4)', $content); // replace `transparent` with shortcut .. - $content = preg_replace('/' . $tag . '\([^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\)/i', '#fff0', $content); + $content = preg_replace('/' . $tag . '\(\s*[^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\s*\)/i', '#fff0', $content); return $content; } diff --git a/tests/CSS/CSSTest.php b/tests/CSS/CSSTest.php index 25f6b07..0046c4e 100644 --- a/tests/CSS/CSSTest.php +++ b/tests/CSS/CSSTest.php @@ -860,6 +860,23 @@ public static function dataProvider() 'background-position:right .8em bottom calc(50% - 5px),right .8em top calc(50% - 5px);', ); + // https://github.com/matthiasmullie/minify/issues/422 + $tests[] = array( + 'a { + color: rgba(var(--bs-link-color-rgb),var(--bs-link-opacity,1)); + text-decoration: none; + } + a:hover { + --bs-link-color-rgb: var(--bs-link-hover-color-rgb); + } + a:not([href]):not([class]), + a:not([href]):not([class]):hover { + color: inherit; + text-decoration: none; + }', + 'a{color:rgba(var(--bs-link-color-rgb),var(--bs-link-opacity,1));text-decoration:none}a:hover{--bs-link-color-rgb:var(--bs-link-hover-color-rgb)}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}', + ); + return $tests; }