| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | |
| 13 | 13 | namespace MatthiasMullie\Minify; |
| 14 | 14 | |
| 15 | 15 | use MatthiasMullie\Minify\Exceptions\FileImportException; |
| 16 | +use MatthiasMullie\Minify\Exceptions\PatternMatchException; | |
| 16 | 17 | use MatthiasMullie\PathConverter\Converter; |
| 17 | 18 | use MatthiasMullie\PathConverter\ConverterInterface; |
| 18 | 19 | |
| 19 | 20 | /** |
| @@ -108,10 +109,10 @@ | ||
| 108 | 109 | * |
| 109 | 110 | * \@import's will be loaded and their content merged into the original file, |
| 110 | 111 | * to save HTTP requests. |
| 111 | 112 | * |
| 112 | - * @param string $source The file to combine imports for | |
| 113 | - * @param string $content The CSS content to combine imports for | |
| 113 | + * @param string $source The file to combine imports for | |
| 114 | + * @param string $content The CSS content to combine imports for | |
| 114 | 115 | * @param string[] $parents Parent paths, for circular reference checks |
| 115 | 116 | * |
| 116 | 117 | * @return string |
| 117 | 118 | * |
| @@ -244,9 +245,9 @@ | ||
| 244 | 245 | * |
| 245 | 246 | * @url(image.jpg) images will be loaded and their content merged into the |
| 246 | 247 | * original file, to save HTTP requests. |
| 247 | 248 | * |
| 248 | - * @param string $source The file to import files for | |
| 249 | + * @param string $source The file to import files for | |
| 249 | 250 | * @param string $content The CSS content to import files for |
| 250 | 251 | * |
| 251 | 252 | * @return string |
| 252 | 253 | */ |
| @@ -295,8 +296,10 @@ | ||
| 295 | 296 | * @param string[optional] $path Path to write the data to |
| 296 | 297 | * @param string[] $parents Parent paths, for circular reference checks |
| 297 | 298 | * |
| 298 | 299 | * @return string The minified data |
| 300 | + * | |
| 301 | + * @throws PatternMatchException | |
| 299 | 302 | */ |
| 300 | 303 | public function execute($path = null, $parents = array()) |
| 301 | 304 | { |
| 302 | 305 | $content = ''; |
| @@ -356,9 +359,9 @@ | ||
| 356 | 359 | * will have to be updated when a file is being saved at another location |
| 357 | 360 | * (e.g. ../../images/image.gif, if the new CSS file is 1 folder deeper). |
| 358 | 361 | * |
| 359 | 362 | * @param ConverterInterface $converter Relative path converter |
| 360 | - * @param string $content The CSS content to update relative urls for | |
| 363 | + * @param string $content The CSS content to update relative urls for | |
| 361 | 364 | * |
| 362 | 365 | * @return string |
| 363 | 366 | */ |
| 364 | 367 | protected function move(ConverterInterface $converter, $content) |
| @@ -589,8 +592,9 @@ | ||
| 589 | 592 | $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); |
| 590 | 593 | |
| 591 | 594 | // convert `rgb` to `hex` |
| 592 | 595 | $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; |
| 596 | + | |
| 593 | 597 | return preg_replace_callback( |
| 594 | 598 | "/rgb\($dec $dec $dec\)/i", |
| 595 | 599 | function ($match) { |
| 596 | 600 | return sprintf('#%02x%02x%02x', $match[1], $match[2], $match[3]); |
| @@ -620,12 +624,12 @@ | ||
| 620 | 624 | */ |
| 621 | 625 | $tag = '(rgb|hsl|hwb|(?:(?:ok)?(?:lch|lab)))'; |
| 622 | 626 | |
| 623 | 627 | // remove alpha channel if it's pointless .. |
| 624 | - $content = preg_replace('/' . $tag . '\(\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+\/\s+1(?:(?:\.\d?)*|00%)?\s*\)/i', '$1($2 $3 $4)', $content); | |
| 628 | + $content = preg_replace('/' . $tag . '\(\s*([^\s)]+)\s+([^\s)]+)\s+([^\s)]+)\s+\/\s+1(?:(?:\.\d?)*|00%)?\s*\)/i', '$1($2 $3 $4)', $content); | |
| 625 | 629 | |
| 626 | 630 | // replace `transparent` with shortcut .. |
| 627 | - $content = preg_replace('/' . $tag . '\(\s*[^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\s*\)/i', '#fff0', $content); | |
| 631 | + $content = preg_replace('/' . $tag . '\(\s*[^\s)]+\s+[^\s)]+\s+[^\s)]+\s+\/\s+0(?:[\.0%]*)?\s*\)/i', '#fff0', $content); | |
| 628 | 632 | |
| 629 | 633 | return $content; |
| 630 | 634 | } |
| 631 | 635 | |
| @@ -731,24 +735,26 @@ | ||
| 731 | 735 | * |
| 732 | 736 | * @param string $content The CSS content to strip the whitespace for |
| 733 | 737 | * |
| 734 | 738 | * @return string |
| 739 | + * | |
| 740 | + * @throws PatternMatchException | |
| 735 | 741 | */ |
| 736 | 742 | protected function stripWhitespace($content) |
| 737 | 743 | { |
| 738 | 744 | // remove leading & trailing whitespace |
| 739 | - $content = preg_replace('/^\s*/m', '', $content); | |
| 740 | - $content = preg_replace('/\s*$/m', '', $content); | |
| 745 | + $content = $this->pregReplace('/^\s*/m', '', $content); | |
| 746 | + $content = $this->pregReplace('/\s*$/m', '', $content); | |
| 741 | 747 | |
| 742 | 748 | // replace newlines with a single space |
| 743 | - $content = preg_replace('/\s+/', ' ', $content); | |
| 749 | + $content = $this->pregReplace('/\s+/', ' ', $content); | |
| 744 | 750 | |
| 745 | 751 | // remove whitespace around meta characters |
| 746 | 752 | // inspired by stackoverflow.com/questions/15195750/minify-compress-css-with-regex |
| 747 | - $content = preg_replace('/\s*([\*$~^|]?+=|[{};,>~]|!important\b)\s*/', '$1', $content); | |
| 748 | - $content = preg_replace('/([\[(:>\+])\s+/', '$1', $content); | |
| 749 | - $content = preg_replace('/\s+([\]\)>\+])/', '$1', $content); | |
| 750 | - $content = preg_replace('/\s+(:)(?![^\}]*\{)/', '$1', $content); | |
| 753 | + $content = $this->pregReplace('/\s*([\*$~^|]?+=|[{};,>~]|!important\b)\s*/', '$1', $content); | |
| 754 | + $content = $this->pregReplace('/([\[(:>\+])\s+/', '$1', $content); | |
| 755 | + $content = $this->pregReplace('/\s+([\]\)>\+])/', '$1', $content); | |
| 756 | + $content = $this->pregReplace('/\s+(:)(?![^\}]*\{)/', '$1', $content); | |
| 751 | 757 | |
| 752 | 758 | // whitespace around + and - can only be stripped inside some pseudo- |
| 753 | 759 | // classes, like `:nth-child(3+2n)` |
| 754 | 760 | // not in things like `calc(3px + 2px)`, shorthands like `3px -2px`, or |
| @@ -753,14 +759,35 @@ | ||
| 753 | 759 | // classes, like `:nth-child(3+2n)` |
| 754 | 760 | // not in things like `calc(3px + 2px)`, shorthands like `3px -2px`, or |
| 755 | 761 | // selectors like `div.weird- p` |
| 756 | 762 | $pseudos = array('nth-child', 'nth-last-child', 'nth-last-of-type', 'nth-of-type'); |
| 757 | - $content = preg_replace('/:(' . implode('|', $pseudos) . ')\(\s*([+-]?)\s*(.+?)\s*([+-]?)\s*(.*?)\s*\)/', ':$1($2$3$4$5)', $content); | |
| 763 | + $content = $this->pregReplace('/:(' . implode('|', $pseudos) . ')\(\s*([+-]?)\s*(.+?)\s*([+-]?)\s*(.*?)\s*\)/', ':$1($2$3$4$5)', $content); | |
| 758 | 764 | |
| 759 | 765 | // remove semicolon/whitespace followed by closing bracket |
| 760 | 766 | $content = str_replace(';}', '}', $content); |
| 761 | 767 | |
| 762 | 768 | return trim($content); |
| 769 | + } | |
| 770 | + | |
| 771 | + /** | |
| 772 | + * Perform a preg_replace and check for errors. | |
| 773 | + * | |
| 774 | + * @param string $pattern Pattern | |
| 775 | + * @param string $replacement Replacement | |
| 776 | + * @param string $subject String to process | |
| 777 | + * | |
| 778 | + * @return string | |
| 779 | + * | |
| 780 | + * @throws PatternMatchException | |
| 781 | + */ | |
| 782 | + protected function pregReplace($pattern, $replacement, $subject) | |
| 783 | + { | |
| 784 | + $result = preg_replace($pattern, $replacement, $subject); | |
| 785 | + if ($result === null) { | |
| 786 | + throw PatternMatchException::fromLastError("Failed to replace with pattern '$pattern'"); | |
| 787 | + } | |
| 788 | + | |
| 789 | + return $result; | |
| 763 | 790 | } |
| 764 | 791 | |
| 765 | 792 | /** |
| 766 | 793 | * Replace all occurrences of functions that may contain math, where |