|
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 |
|
@@ -296,6 +297,8 @@ protected function importFiles($source, $content) |
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 | { |
@@ -733,36 +736,59 @@ protected function stripComments() |
733 | 736 | * @param string $content The CSS content to strip the whitespace for |
734 | 737 | * |
735 | 738 | * @return string |
| 739 | + * |
| 740 | + * @throws PatternMatchException |
736 | 741 | */ |
737 | 742 | protected function stripWhitespace($content) |
738 | 743 | { |
739 | 744 | // remove leading & trailing whitespace |
740 | | - $content = preg_replace('/^\s*/m', '', $content); |
741 | | - $content = preg_replace('/\s*$/m', '', $content); |
| 745 | + $content = $this->pregReplace('/^\s*/m', '', $content); |
| 746 | + $content = $this->pregReplace('/\s*$/m', '', $content); |
742 | 747 |
|
743 | 748 | // replace newlines with a single space |
744 | | - $content = preg_replace('/\s+/', ' ', $content); |
| 749 | + $content = $this->pregReplace('/\s+/', ' ', $content); |
745 | 750 |
|
746 | 751 | // remove whitespace around meta characters |
747 | 752 | // inspired by stackoverflow.com/questions/15195750/minify-compress-css-with-regex |
748 | | - $content = preg_replace('/\s*([\*$~^|]?+=|[{};,>~]|!important\b)\s*/', '$1', $content); |
749 | | - $content = preg_replace('/([\[(:>\+])\s+/', '$1', $content); |
750 | | - $content = preg_replace('/\s+([\]\)>\+])/', '$1', $content); |
751 | | - $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); |
752 | 757 |
|
753 | 758 | // whitespace around + and - can only be stripped inside some pseudo- |
754 | 759 | // classes, like `:nth-child(3+2n)` |
755 | 760 | // not in things like `calc(3px + 2px)`, shorthands like `3px -2px`, or |
756 | 761 | // selectors like `div.weird- p` |
757 | 762 | $pseudos = array('nth-child', 'nth-last-child', 'nth-last-of-type', 'nth-of-type'); |
758 | | - $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); |
759 | 764 |
|
760 | 765 | // remove semicolon/whitespace followed by closing bracket |
761 | 766 | $content = str_replace(';}', '}', $content); |
762 | 767 |
|
763 | 768 | return trim($content); |
764 | 769 | } |
765 | 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; |
| 790 | + } |
| 791 | + |
766 | 792 | /** |
767 | 793 | * Replace all occurrences of functions that may contain math, where |
768 | 794 | * whitespace around operators needs to be preserved (e.g. calc, clamp). |
|
0 commit comments