From 149fee434af715f2b2319fe48edef5a19c027908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Wed, 8 Sep 2021 18:09:42 +0200 Subject: [PATCH 1/8] [OPT] shortenHEXColors --- src/CSS.php | 92 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index 1b354ad..1963854 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -316,7 +316,7 @@ public function execute($path = null, $parents = array()) $css = $this->replace($css); $css = $this->stripWhitespace($css); - $css = $this->shortenColors($css); + $css = $this->shortenHEXColors($css); $css = $this->shortenZeroes($css); $css = $this->shortenFontWeights($css); $css = $this->stripEmptyTags($css); @@ -480,59 +480,83 @@ protected function move(ConverterInterface $converter, $content) } /** - * Shorthand hex color codes. - * #FF0000 -> #F00. + * Shorthand HEX color codes. + * #FF0000 -> #f00 -> red * - * @param string $content The CSS content to shorten the hex color codes for + * @param string $content The CSS content to shorten the HEX color codes for * * @return string */ - protected function shortenColors($content) + protected function shortenHexColors($content) { - $content = preg_replace('/(?<=[: ])#([0-9a-z])\\1([0-9a-z])\\2([0-9a-z])\\3(?:([0-9a-z])\\4)?(?=[; }])/i', '#$1$2$3$4', $content); + // shorten repeating patterns within HEX .. + $content = preg_replace('/#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?/i', '#$1$2$3$4', $content); - // remove alpha channel if it's pointless... - $content = preg_replace('/(?<=[: ])#([0-9a-z]{6})ff?(?=[; }])/i', '#$1', $content); - $content = preg_replace('/(?<=[: ])#([0-9a-z]{3})f?(?=[; }])/i', '#$1', $content); + // remove alpha channel if it's pointless .. + $content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content); + $content = preg_replace('/#([0-9a-f]{3})f/i', '#$1', $content); + + // replace `transparent` with shortcut .. + $content = preg_replace('/#[0-9a-f]{6}00/i', '#0000', $content); $colors = array( + // make these more readable + '#00f' => 'blue', + '#dc143c' => 'crimson', + '#0ff' => 'cyan', + '#8b0000' => 'darkred', + '#696969' => 'dimgray', + '#ff69b4' => 'hotpink', + '#0f0' => 'lime', + '#fdf5e6' => 'oldlace', + '#87ceeb' => 'skyblue', + '#d8bfd8' => 'thistle', // we can shorten some even more by replacing them with their color name - '#F0FFFF' => 'azure', - '#F5F5DC' => 'beige', - '#A52A2A' => 'brown', - '#FF7F50' => 'coral', - '#FFD700' => 'gold', + '#f0ffff' => 'azure', + '#f5f5dc' => 'beige', + '#ffe4c4' => 'bisque', + '#a52a2a' => 'brown', + '#ff7f50' => 'coral', + '#ffd700' => 'gold', '#808080' => 'gray', '#008000' => 'green', - '#4B0082' => 'indigo', - '#FFFFF0' => 'ivory', - '#F0E68C' => 'khaki', - '#FAF0E6' => 'linen', + '#4b0082' => 'indigo', + '#fffff0' => 'ivory', + '#f0e68c' => 'khaki', + '#faf0e6' => 'linen', '#800000' => 'maroon', '#000080' => 'navy', '#808000' => 'olive', - '#CD853F' => 'peru', - '#FFC0CB' => 'pink', - '#DDA0DD' => 'plum', + '#ffa500' => 'orange', + '#da70d6' => 'orchid', + '#cd853f' => 'peru', + '#ffc0cb' => 'pink', + '#dda0dd' => 'plum', '#800080' => 'purple', - '#F00' => 'red', - '#FA8072' => 'salmon', - '#A0522D' => 'sienna', - '#C0C0C0' => 'silver', - '#FFFAFA' => 'snow', - '#D2B48C' => 'tan', - '#FF6347' => 'tomato', - '#EE82EE' => 'violet', - '#F5DEB3' => 'wheat', + '#f00' => 'red', + '#fa8072' => 'salmon', + '#a0522d' => 'sienna', + '#c0c0c0' => 'silver', + '#fffafa' => 'snow', + '#d2b48c' => 'tan', + '#008080' => 'teal', + '#ff6347' => 'tomato', + '#ee82ee' => 'violet', + '#f5deb3' => 'wheat', // or the other way around - 'WHITE' => '#fff', - 'BLACK' => '#000', + 'black' => '#000', + 'fuchsia' => '#f0f', + 'magenta' => '#f0f', + 'white' => '#fff', + 'yellow' => '#ff0', + // and also `transparent` + #'transparent' => '#0000' CHECK safari ); return preg_replace_callback( - '/(?<=[: ])('.implode('|', array_keys($colors)).')(?=[; }])/i', + '/('.implode('|', array_keys($colors)).')/i', function ($match) use ($colors) { - return $colors[strtoupper($match[0])]; + return $colors[strtolower($match[0])]; }, $content ); From 6bf1dfe0d71311158f71439a7f126a1d2addff86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Wed, 8 Sep 2021 18:11:22 +0200 Subject: [PATCH 2/8] [ADD] shortenRGBColors --- src/CSS.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/CSS.php b/src/CSS.php index 1963854..140c179 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -316,6 +316,7 @@ public function execute($path = null, $parents = array()) $css = $this->replace($css); $css = $this->stripWhitespace($css); + $css = $this->shortenRGBColors($css); $css = $this->shortenHEXColors($css); $css = $this->shortenZeroes($css); $css = $this->shortenFontWeights($css); @@ -562,6 +563,38 @@ function ($match) use ($colors) { ); } + /** + * Shorthand RGB color codes. + * rgb(255,0,0) -> #f00. + * + * @param string $content The CSS content to shorten the RGB color codes for + * + * @return string + */ + protected function shortenRGBColors($content) + { + /* + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb() + */ + // THX @ https://www.regular-expressions.info/numericranges.html + $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';// [000-255] + + // remove alpha channel if it's pointless .. + $content = preg_replace("/(rgb)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i", '$1($2 $3 $4)', $content); + + // replace `transparent` with shortcut .. + #$content = preg_replace("/rgba?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i", '#0000', $content); CHECK safari + + return preg_replace_callback( + "/rgb\($dec[,\s]$dec[,\s]$dec\)/i", + function ($match) + { + return sprintf('#%02x%02x%02x', $match[1],$match[2],$match[3]); + }, + $content + ); + } + /** * Shorten CSS font weights. * From c63bd510fb45447a18c7e3e9f4e08175585c4dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Wed, 8 Sep 2021 18:24:37 +0200 Subject: [PATCH 3/8] [WIP] cleanupNEWColors --- src/CSS.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/CSS.php b/src/CSS.php index 140c179..7dd56d0 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -316,6 +316,7 @@ public function execute($path = null, $parents = array()) $css = $this->replace($css); $css = $this->stripWhitespace($css); + $css = $this->cleanupNEWColors($css); $css = $this->shortenRGBColors($css); $css = $this->shortenHEXColors($css); $css = $this->shortenZeroes($css); @@ -595,6 +596,49 @@ function ($match) ); } + /** + * Cleanup HSL|HWB|LCH|LAB + * + * @param string $content The CSS content to cleanup HSL|HWB|LCH|LAB + * + * @return string + */ + protected function cleanupNEWColors($content) + { + /* + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl() + */ + $hue = '([-+]?(?:(?:[012]?[0-9]?[0-9]|3[0-5][0-9])(\.[0-9]+)|360(?:\.0+))(?:degs|rads|grads|turn)?)';// -/+ [000.*-360] def + $pct = '((100(?:\.0+)|0?[0-9]?[0-9])(\.[0-9]+)%)';// [000.*-100] % + + // remove alpha channel if it's pointless .. + $content = preg_replace("/(hsl)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i", '$1($2 $3 $4)', $content); + + // replace `transparent` with shortcut .. + #$content = preg_replace("/hsla?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i", '#0000', $content); CHECK safari + + # ToRGB ?: https://github.com/mexitek/phpColors/blob/a74808eaf7cd918681aab0cd30f142025556bc8a/src/Mexitek/PHPColors/Color.php#L124 + #"/(hsl)a?\($hue[,\s]$pct[,\s]$pct[,\/]1(?:[\.\d]*|00%)?\)/i" + + /* + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hwb() + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lch() + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lab() + https://drafts.csswg.org/css-color/#color + */ + + // remove alpha channel if it's pointless .. + $content = preg_replace("/(lch|lab|hwba?)\(([^\s]+)\s([^\s]+)\s([^\s]+)\s?\/\s?1(?:[\.\d]*|00%)?\)/i", '$1($2 $3 $4)', $content); + + // replace `transparent` with shortcut .. + #$content = preg_replace("/(lch|lab)\([^\s]+\s[^\s]+\s[^\s]+\s?\/\s?0(?:[\.0%]*)?\)/i", '#0000', $content); CHECK safari + + #"/(hwb)a?\($hue\s$pct\s$pct\s?\/\s?1(?:[\.\d]*|00%)?\)/i" + #"/(lch|lab)\($pct\s$VAR\s$VAR\s?\/\s?1(?:[\.\d]*|00%)?\)/i" + + return $content; + } + /** * Shorten CSS font weights. * From 68b1059831cfb5a70277c123f363fedbe4f2696f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Tue, 16 Nov 2021 15:40:20 +0100 Subject: [PATCH 4/8] [CLS] ColorFunct.RegEx --- src/CSS.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index 7dd56d0..5a421c4 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -495,11 +495,11 @@ protected function shortenHexColors($content) $content = preg_replace('/#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?/i', '#$1$2$3$4', $content); // remove alpha channel if it's pointless .. - $content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content); - $content = preg_replace('/#([0-9a-f]{3})f/i', '#$1', $content); + $content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content); + $content = preg_replace('/#([0-9a-f]{3})f(?=[ ;}\)])/i', '#$1', $content); // replace `transparent` with shortcut .. - $content = preg_replace('/#[0-9a-f]{6}00/i', '#0000', $content); + $content = preg_replace('/#[0-9a-f]{6}00/i', '#fff0', $content); $colors = array( // make these more readable @@ -552,7 +552,7 @@ protected function shortenHexColors($content) 'white' => '#fff', 'yellow' => '#ff0', // and also `transparent` - #'transparent' => '#0000' CHECK safari + 'transparent' => '#fff0' ); return preg_replace_callback( @@ -581,10 +581,10 @@ protected function shortenRGBColors($content) $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';// [000-255] // remove alpha channel if it's pointless .. - $content = preg_replace("/(rgb)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i", '$1($2 $3 $4)', $content); + $content = preg_replace('/(rgb)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); // replace `transparent` with shortcut .. - #$content = preg_replace("/rgba?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i", '#0000', $content); CHECK safari + $content = preg_replace('/rgba?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); return preg_replace_callback( "/rgb\($dec[,\s]$dec[,\s]$dec\)/i", @@ -612,10 +612,10 @@ protected function cleanupNEWColors($content) $pct = '((100(?:\.0+)|0?[0-9]?[0-9])(\.[0-9]+)%)';// [000.*-100] % // remove alpha channel if it's pointless .. - $content = preg_replace("/(hsl)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i", '$1($2 $3 $4)', $content); + $content = preg_replace('/(hsl)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); // replace `transparent` with shortcut .. - #$content = preg_replace("/hsla?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i", '#0000', $content); CHECK safari + $content = preg_replace('/hsla?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); # ToRGB ?: https://github.com/mexitek/phpColors/blob/a74808eaf7cd918681aab0cd30f142025556bc8a/src/Mexitek/PHPColors/Color.php#L124 #"/(hsl)a?\($hue[,\s]$pct[,\s]$pct[,\/]1(?:[\.\d]*|00%)?\)/i" @@ -628,10 +628,10 @@ protected function cleanupNEWColors($content) */ // remove alpha channel if it's pointless .. - $content = preg_replace("/(lch|lab|hwba?)\(([^\s]+)\s([^\s]+)\s([^\s]+)\s?\/\s?1(?:[\.\d]*|00%)?\)/i", '$1($2 $3 $4)', $content); + $content = preg_replace('/(lch|lab|hwba?)\(([^\s]+)\s([^\s]+)\s([^\s]+)\s?\/\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); // replace `transparent` with shortcut .. - #$content = preg_replace("/(lch|lab)\([^\s]+\s[^\s]+\s[^\s]+\s?\/\s?0(?:[\.0%]*)?\)/i", '#0000', $content); CHECK safari + $content = preg_replace('/(lch|lab)\([^\s]+\s[^\s]+\s[^\s]+\s?\/\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); #"/(hwb)a?\($hue\s$pct\s$pct\s?\/\s?1(?:[\.\d]*|00%)?\)/i" #"/(lch|lab)\($pct\s$VAR\s$VAR\s?\/\s?1(?:[\.\d]*|00%)?\)/i" From 65b7ebf7ea3f43c65d41969ddbd8cca289a1d3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Sat, 27 Nov 2021 21:08:37 +0100 Subject: [PATCH 5/8] [DEACTIVATE] --- src/CSS.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index 5a421c4..e967f3b 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -495,11 +495,11 @@ protected function shortenHexColors($content) $content = preg_replace('/#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?/i', '#$1$2$3$4', $content); // remove alpha channel if it's pointless .. - $content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content); - $content = preg_replace('/#([0-9a-f]{3})f(?=[ ;}\)])/i', '#$1', $content); + $content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content); + $content = preg_replace('/#([0-9a-f]{3})f(?=[^\w])/i', '#$1', $content); - // replace `transparent` with shortcut .. - $content = preg_replace('/#[0-9a-f]{6}00/i', '#fff0', $content); + #// replace `transparent` with shortcut .. + #$content = preg_replace('/#[0-9a-f]{6}00/i', '#fff0', $content); $colors = array( // make these more readable @@ -583,8 +583,8 @@ protected function shortenRGBColors($content) // remove alpha channel if it's pointless .. $content = preg_replace('/(rgb)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); - // replace `transparent` with shortcut .. - $content = preg_replace('/rgba?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); + #// replace `transparent` with shortcut .. + #$content = preg_replace('/rgba?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); return preg_replace_callback( "/rgb\($dec[,\s]$dec[,\s]$dec\)/i", @@ -614,8 +614,8 @@ protected function cleanupNEWColors($content) // remove alpha channel if it's pointless .. $content = preg_replace('/(hsl)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); - // replace `transparent` with shortcut .. - $content = preg_replace('/hsla?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); + #// replace `transparent` with shortcut .. + #$content = preg_replace('/hsla?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); # ToRGB ?: https://github.com/mexitek/phpColors/blob/a74808eaf7cd918681aab0cd30f142025556bc8a/src/Mexitek/PHPColors/Color.php#L124 #"/(hsl)a?\($hue[,\s]$pct[,\s]$pct[,\/]1(?:[\.\d]*|00%)?\)/i" From 7f64dadc0cc7084c9eacb1dfa3d37d5d6bb3de83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Thu, 29 Feb 2024 15:16:09 +0100 Subject: [PATCH 6/8] [OPT] legacy/modern colors --- src/CSS.php | 81 +++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 46 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index c1b0376..b0e65ee 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -316,8 +316,8 @@ public function execute($path = null, $parents = array()) $css = $this->replace($css); $css = $this->stripWhitespace($css); - $css = $this->cleanupNEWColors($css); - $css = $this->shortenRGBColors($css); + $css = $this->convertLegacyColors($css); + $css = $this->cleanupModernColors($css); $css = $this->shortenHEXColors($css); $css = $this->shortenZeroes($css); $css = $this->shortenFontWeights($css); @@ -483,7 +483,8 @@ protected function move(ConverterInterface $converter, $content) /** * Shorthand HEX color codes. - * #FF0000 -> #f00 -> red + * #FF0000FF -> #f00 -> red + * #FF00FF00 -> transparent * * @param string $content The CSS content to shorten the HEX color codes for * @@ -492,14 +493,14 @@ protected function move(ConverterInterface $converter, $content) protected function shortenHexColors($content) { // shorten repeating patterns within HEX .. - $content = preg_replace('/#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?/i', '#$1$2$3$4', $content); + $content = preg_replace('/(?<=[: ])#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?(?=[; }])/i', '#$1$2$3$4', $content); // remove alpha channel if it's pointless .. - $content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content); - $content = preg_replace('/#([0-9a-f]{3})f(?=[^\w])/i', '#$1', $content); + $content = preg_replace('/(?<=[: ])#([0-9a-f]{6})ff(?=[; }])/i', '#$1', $content); + $content = preg_replace('/(?<=[: ])#([0-9a-f]{3})f(?=[; }])/i', '#$1', $content); - #// replace `transparent` with shortcut .. - #$content = preg_replace('/#[0-9a-f]{6}00/i', '#fff0', $content); + // replace `transparent` with shortcut .. + $content = preg_replace('/(?<=[: ])#[0-9a-f]{6}00(?=[; }])/i', '#fff0', $content); $colors = array( // make these more readable @@ -556,7 +557,7 @@ protected function shortenHexColors($content) ); return preg_replace_callback( - '/('.implode('|', array_keys($colors)).')/i', + '/(?<=[: ])(' . implode('|', array_keys($colors)) . ')(?=[; }])/i', function ($match) use ($colors) { return $colors[strtolower($match[0])]; }, @@ -565,29 +566,30 @@ function ($match) use ($colors) { } /** - * Shorthand RGB color codes. + * Convert RGB|HSL color codes. + * rgb(255,0,0,.5) -> rgb(255 0 0 / .5). * rgb(255,0,0) -> #f00. * * @param string $content The CSS content to shorten the RGB color codes for * * @return string */ - protected function shortenRGBColors($content) + protected function convertLegacyColors($content) { /* - https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb() + https://drafts.csswg.org/css-color/#color-syntax-legacy + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl */ - // THX @ https://www.regular-expressions.info/numericranges.html - $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';// [000-255] - - // remove alpha channel if it's pointless .. - $content = preg_replace('/(rgb)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); - #// replace `transparent` with shortcut .. - #$content = preg_replace('/rgba?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $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); + // 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 return preg_replace_callback( - "/rgb\($dec[,\s]$dec[,\s]$dec\)/i", + "/rgb\($dec $dec $dec\)/i", function ($match) { return sprintf('#%02x%02x%02x', $match[1],$match[2],$match[3]); @@ -597,44 +599,31 @@ function ($match) } /** - * Cleanup HSL|HWB|LCH|LAB + * Cleanup RGB|HSL|HWB|LCH|LAB + * rgb(255 0 0 / 1) -> rgb(255 0 0). + * rgb(255 0 0 / 0) -> transparent. * * @param string $content The CSS content to cleanup HSL|HWB|LCH|LAB * * @return string */ - protected function cleanupNEWColors($content) + protected function cleanupModernColors($content) { /* - https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl() + https://drafts.csswg.org/css-color/#color-syntax-modern + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hwb + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lch + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lab + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklch + https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklab */ - $hue = '([-+]?(?:(?:[012]?[0-9]?[0-9]|3[0-5][0-9])(\.[0-9]+)|360(?:\.0+))(?:degs|rads|grads|turn)?)';// -/+ [000.*-360] def - $pct = '((100(?:\.0+)|0?[0-9]?[0-9])(\.[0-9]+)%)';// [000.*-100] % + $tag = '(rgb|hsl|hwb|(?:(?:ok)?(?:lch|lab)))'; // remove alpha channel if it's pointless .. - $content = preg_replace('/(hsl)a?\(([^,\s]+)[,\s]([^,\s]+)[,\s]([^,\s]+)\s?[,\/]\s?1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); - - #// replace `transparent` with shortcut .. - #$content = preg_replace('/hsla?\([^,\s]+[,\s][^,\s]+[,\s][^,\s]+\s?[,\/]\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); - - # ToRGB ?: https://github.com/mexitek/phpColors/blob/a74808eaf7cd918681aab0cd30f142025556bc8a/src/Mexitek/PHPColors/Color.php#L124 - #"/(hsl)a?\($hue[,\s]$pct[,\s]$pct[,\/]1(?:[\.\d]*|00%)?\)/i" - - /* - https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hwb() - https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lch() - https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lab() - https://drafts.csswg.org/css-color/#color - */ - - // remove alpha channel if it's pointless .. - $content = preg_replace('/(lch|lab|hwba?)\(([^\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+1(?:[\.\d]*|00%)?\)/i', '$1($2 $3 $4)', $content); // replace `transparent` with shortcut .. - $content = preg_replace('/(lch|lab)\([^\s]+\s[^\s]+\s[^\s]+\s?\/\s?0(?:[\.0%]*)?\)/i', '#fff0', $content); - - #"/(hwb)a?\($hue\s$pct\s$pct\s?\/\s?1(?:[\.\d]*|00%)?\)/i" - #"/(lch|lab)\($pct\s$VAR\s$VAR\s?\/\s?1(?:[\.\d]*|00%)?\)/i" + $content = preg_replace('/'.$tag.'\([^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\)/i', '#fff0', $content); return $content; } From 0127dd0f3cfa1dabfa2735eadeaa7b3e37be0522 Mon Sep 17 00:00:00 2001 From: Julien Date: Thu, 25 Jan 2024 10:21:56 +0100 Subject: [PATCH 7/8] Intercept openbasedir exceptions when detecting check for files --- src/Minify.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Minify.php b/src/Minify.php index 29de517..c643c2d 100644 --- a/src/Minify.php +++ b/src/Minify.php @@ -501,7 +501,13 @@ protected function canImportFile($path) return false; } - return strlen($path) < PHP_MAXPATHLEN && @is_file($path) && is_readable($path); + try { + return strlen($path) < PHP_MAXPATHLEN && @is_file($path) && is_readable($path); + } + // catch openbasedir exceptions which are not caught by @ on is_file() + catch(\Exception $e) { + return false; + } } /** From d710fe0d8420b19af29f4a4ee6a9d70b3962e4ca Mon Sep 17 00:00:00 2001 From: Rocco Howard Date: Mon, 29 Mar 2021 11:27:18 +1100 Subject: [PATCH 8/8] Backslashed @import comment (#1) reflection-docblock treats this as a tag "does not seem to be wellformed, please check it for errors" Backlashing ensures this non-import is ignored. --- src/CSS.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index b0e65ee..f56fc5e 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -106,8 +106,8 @@ protected function moveImportsToTop($content) /** * Combine CSS from import statements. * - * Import statements will be loaded and their content merged into the original - * file, to save HTTP requests. + * \@import's will be loaded and their content merged into the original file, + * to save HTTP requests. * * @param string $source The file to combine imports for * @param string $content The CSS content to combine imports for