Skip to content

Commit

Permalink
Tighten legacy color conversion
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matthiasmullie committed Mar 15, 2024
1 parent e4d9ebd commit cb7a929
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/CSS/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit cb7a929

Please sign in to comment.