| @@ -6,50 +6,14 @@ | ||
| 6 | 6 | defined( 'ABSPATH' ) || exit; |
| 7 | 7 | |
| 8 | 8 | class CSSMinifier { |
| 9 | 9 | |
| 10 | - public static function minify($input): string { | |
| 11 | - if(trim($input) === "") { | |
| 12 | - return $input; | |
| 13 | - } | |
| 14 | - $replaced = preg_replace( | |
| 15 | - array( | |
| 16 | - // Remove comment(s) | |
| 17 | - '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s', | |
| 18 | - // Remove unused white-space(s) | |
| 19 | - '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~]|\s(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si', | |
| 20 | - // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0` | |
| 21 | - '#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si', | |
| 22 | - // Replace `:0 0 0 0` with `:0` | |
| 23 | - '#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i', | |
| 24 | - // Replace `background-position:0` with `background-position:0 0` | |
| 25 | - '#(background-position):0(?=[;\}])#si', | |
| 26 | - // Replace `0.6` with `.6`, but only when preceded by `:`, `,`, `-` or a white-space | |
| 27 | - '#(?<=[\s:,\-])0+\.(\d+)#s', | |
| 28 | - // Minify string value | |
| 29 | - '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si', | |
| 30 | - '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si', | |
| 31 | - // Minify HEX color code | |
| 32 | - '#(?<=[\s:,\-]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i', | |
| 33 | - // Replace `(border|outline):none` with `(border|outline):0` | |
| 34 | - '#(?<=[\{;])(border|outline):none(?=[;\}\!])#', | |
| 35 | - // Remove empty selector(s) | |
| 36 | - '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s' | |
| 37 | - ), | |
| 38 | - array( | |
| 39 | - '$1', | |
| 40 | - '$1$2$3$4$5$6$7', | |
| 41 | - '$1', | |
| 42 | - ':0', | |
| 43 | - '$1:0 0', | |
| 44 | - '.$1', | |
| 45 | - '$1$3', | |
| 46 | - '$1$2$4$5', | |
| 47 | - '$1$2$3', | |
| 48 | - '$1:0', | |
| 49 | - '$1$2' | |
| 50 | - ), | |
| 51 | - $input); | |
| 52 | - return str_replace('fill=none', '', $replaced); // fix for url("data:image/svg+xml,%3Csvg ... fill='none' | |
| 10 | + public static function minify($css): string { | |
| 11 | + $css = preg_replace('/\/\*(.*?)\*\//s', '', $css); // Remove comments | |
| 12 | + $css = preg_replace('/\s+/', ' ', $css); // Remove whitespace | |
| 13 | + $css = preg_replace('/;\s*/', ';', $css); // Remove spaces after semicolons | |
| 14 | + $css = preg_replace('/: /', ':', $css); // Remove spaces after colons | |
| 15 | + $css = preg_replace('/\s{2,}/', ' ', $css); // Remove multiple spaces | |
| 16 | + return trim($css); | |
| 53 | 17 | } |
| 54 | 18 | |
| 55 | 19 | } |