Skip to content

Commit cb7a929

Browse files
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
1 parent e4d9ebd commit cb7a929

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/CSS.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,13 @@ protected function convertLegacyColors($content)
583583
*/
584584

585585
// convert legacy color syntax
586-
$content = preg_replace('/(rgb|hsl)a?\(([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^\s\)]+)\)/i', '$1($2 $3 $4 / $5)', $content);
587-
$content = preg_replace('/(rgb|hsl)a?\(([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^,\s]+)\)/i', '$1($2 $3 $4)', $content);
586+
$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);
587+
$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);
588+
$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);
589+
$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);
588590

589591
// convert `rgb` to `hex`
590-
$dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; // [000-255] THX @ https://www.regular-expressions.info/numericranges.html
591-
592+
$dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
592593
return preg_replace_callback(
593594
"/rgb\($dec $dec $dec\)/i",
594595
function ($match) {
@@ -620,10 +621,10 @@ protected function cleanupModernColors($content)
620621
$tag = '(rgb|hsl|hwb|(?:(?:ok)?(?:lch|lab)))';
621622

622623
// remove alpha channel if it's pointless ..
623-
$content = preg_replace('/' . $tag . '\(([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+\/\s+1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content);
624+
$content = preg_replace('/' . $tag . '\(\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+\/\s+1(?:(?:\.\d?)*|00%)?\s*\)/i', '$1($2 $3 $4)', $content);
624625

625626
// replace `transparent` with shortcut ..
626-
$content = preg_replace('/' . $tag . '\([^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\)/i', '#fff0', $content);
627+
$content = preg_replace('/' . $tag . '\(\s*[^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\s*\)/i', '#fff0', $content);
627628

628629
return $content;
629630
}

tests/CSS/CSSTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,23 @@ public static function dataProvider()
860860
'background-position:right .8em bottom calc(50% - 5px),right .8em top calc(50% - 5px);',
861861
);
862862

863+
// https://github.com/matthiasmullie/minify/issues/422
864+
$tests[] = array(
865+
'a {
866+
color: rgba(var(--bs-link-color-rgb),var(--bs-link-opacity,1));
867+
text-decoration: none;
868+
}
869+
a:hover {
870+
--bs-link-color-rgb: var(--bs-link-hover-color-rgb);
871+
}
872+
a:not([href]):not([class]),
873+
a:not([href]):not([class]):hover {
874+
color: inherit;
875+
text-decoration: none;
876+
}',
877+
'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}',
878+
);
879+
863880
return $tests;
864881
}
865882

0 commit comments

Comments
 (0)