substr($prev, -4) === 'url(' && preg_match('#\burl\($#', $prev) || // substr($prev, -1) === '=' && preg_match('#^' . $q . '[a-zA-Z_][\w-]*?' . $q . '$#', $part) ) ) { $part = self::trim_once($part, $q); // trim quote(s) } $output .= $part; } else { $output .= self::minify_css_union($part); } $prev = $part; } return trim($output); } public static function minify_css_union($input) { if (stripos($input, 'calc(') !== false) { // Keep important white–space(s) in `calc()` $input = preg_replace_callback('#\b(calc\()\s*(.*?)\s*\)#i', function($m) { return $m[1] . preg_replace('#\s+#', AMP_THEME_X, $m[2]) . ')'; }, $input); } $input = preg_replace([ // Fix case for `#foo[bar="baz"]`, `#foo*` and `#foo:first-child` [^1] '#(?<=[\w])\s+(\*|\[|:[\w-]+)#', // Fix case for `[bar="baz"].foo`, `*.foo`, `:nth-child(2).foo` and `@media(foo: bar)and(baz: qux)` [^2] '#([*\]\)])\s+(?=[\w\#.])#', '#\b\s+\(#', '#\)\s+\b#', // Minify HEX color code … [^3] '#\#([a-f\d])\1([a-f\d])\2([a-f\d])\3\b#i', // Remove white–space(s) around punctuation(s) [^4] '#\s*([~!@*\(\)+=\{\}\[\]:;,>\/])\s*#', // Replace zero unit(s) with `0` [^5] '#\b(?:0\.)?0([a-z]+\b)#i', // Replace `0.6` with `.6` [^6] '#\b0+\.(\d+)#', // Replace `:0 0`, `:0 0 0` and `:0 0 0 0` with `:0` [^7] '#:(0\s+){0,3}0(?=[!,;\)\}]|$)#', // Replace `background(?:-position)?:(0|none)` with `background$1:0 0` [^8] '#\b(background(?:-position)?):(?:0|none)([;,\}])#i', // Replace `(border(?:-radius)?|outline):none` with `$1:0` [^9] '#\b(border(?:-radius)?|outline):none\b#i', // Remove empty selector(s) [^10] '#(^|[\{\}])(?:[^\{\}]+)\{\}#', // Remove the last semi–colon and replace multiple semi–colon(s) with a semi–colon [^11] '#;+([;\}])#', // Replace multiple white–space(s) with a space [^12] '#\s+#' ], [ // [^1] AMP_THEME_X . '$1', // [^2] '$1' . AMP_THEME_X, AMP_THEME_X . '(', ')' . AMP_THEME_X, // [^3] '#$1$2$3', // [^4] '$1', // [^5] '0', // [^6] '.$1', // [^7] ':0', // [^8] '$1:0 0$2', // [^9] '$1:0', // [^10] '$1', // [^11] '$1', // [^12] ' ' ], $input); return trim(str_replace(AMP_THEME_X, ' ', $input)); } // normalize line–break(s) public static function normalize_line_break($s) { return str_replace(["\r\n", "\r"], "\n", $s); } // trim once public static function trim_once($a, $b) { if ($a && strpos($a, $b) === 0 && substr($a, -strlen($b)) === $b) { return substr(substr($a, strlen($b)), 0, -strlen($b)); } return $a; } } } ?>