Skip to content

Commit 152837f

Browse files
committed
Update dependencies
1 parent 193ada1 commit 152837f

11 files changed

Lines changed: 347 additions & 188 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"10up/phpcs-composer": "dev-master",
2626
"coenjacobs/mozart": "^0.7.1",
2727
"matthiasmullie/minify": "^1.3",
28-
"deliciousbrains/wp-background-processing": "^1.4",
28+
"deliciousbrains/wp-background-processing": "dev-master#be0a5e62f2c1e7603ebe3dea75f91102f1a2c1cb",
2929
"voku/html-min": "^4.5"
3030
},
3131
"scripts": {

composer.lock

Lines changed: 222 additions & 129 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/classes/Dependencies/MatthiasMullie/Minify/CSS.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace PoweredCache\Dependencies\MatthiasMullie\Minify;
1414

1515
use PoweredCache\Dependencies\MatthiasMullie\Minify\Exceptions\FileImportException;
16+
use PoweredCache\Dependencies\MatthiasMullie\Minify\Exceptions\PatternMatchException;
1617
use PoweredCache\Dependencies\MatthiasMullie\PathConverter\Converter;
1718
use PoweredCache\Dependencies\MatthiasMullie\PathConverter\ConverterInterface;
1819

@@ -109,8 +110,8 @@ protected function moveImportsToTop($content)
109110
* \@import's will be loaded and their content merged into the original file,
110111
* to save HTTP requests.
111112
*
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
114115
* @param string[] $parents Parent paths, for circular reference checks
115116
*
116117
* @return string
@@ -245,7 +246,7 @@ protected function combineImports($source, $content, $parents)
245246
* @url(image.jpg) images will be loaded and their content merged into the
246247
* original file, to save HTTP requests.
247248
*
248-
* @param string $source The file to import files for
249+
* @param string $source The file to import files for
249250
* @param string $content The CSS content to import files for
250251
*
251252
* @return string
@@ -296,6 +297,8 @@ protected function importFiles($source, $content)
296297
* @param string[] $parents Parent paths, for circular reference checks
297298
*
298299
* @return string The minified data
300+
*
301+
* @throws PatternMatchException
299302
*/
300303
public function execute($path = null, $parents = array())
301304
{
@@ -357,7 +360,7 @@ public function execute($path = null, $parents = array())
357360
* (e.g. ../../images/image.gif, if the new CSS file is 1 folder deeper).
358361
*
359362
* @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
361364
*
362365
* @return string
363366
*/
@@ -590,6 +593,7 @@ protected function convertLegacyColors($content)
590593

591594
// convert `rgb` to `hex`
592595
$dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
596+
593597
return preg_replace_callback(
594598
"/rgb\($dec $dec $dec\)/i",
595599
function ($match) {
@@ -621,10 +625,10 @@ protected function cleanupModernColors($content)
621625
$tag = '(rgb|hsl|hwb|(?:(?:ok)?(?:lch|lab)))';
622626

623627
// 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);
625629

626630
// 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);
628632

629633
return $content;
630634
}
@@ -732,36 +736,59 @@ protected function stripComments()
732736
* @param string $content The CSS content to strip the whitespace for
733737
*
734738
* @return string
739+
*
740+
* @throws PatternMatchException
735741
*/
736742
protected function stripWhitespace($content)
737743
{
738744
// 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);
741747

742748
// replace newlines with a single space
743-
$content = preg_replace('/\s+/', ' ', $content);
749+
$content = $this->pregReplace('/\s+/', ' ', $content);
744750

745751
// remove whitespace around meta characters
746752
// 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);
751757

752758
// whitespace around + and - can only be stripped inside some pseudo-
753759
// classes, like `:nth-child(3+2n)`
754760
// not in things like `calc(3px + 2px)`, shorthands like `3px -2px`, or
755761
// selectors like `div.weird- p`
756762
$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);
758764

759765
// remove semicolon/whitespace followed by closing bracket
760766
$content = str_replace(';}', '}', $content);
761767

762768
return trim($content);
763769
}
764770

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+
765792
/**
766793
* Replace all occurrences of functions that may contain math, where
767794
* whitespace around operators needs to be preserved (e.g. calc, clamp).

includes/classes/Dependencies/MatthiasMullie/Minify/Exception.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717
*
1818
* @author Matthias Mullie <minify@mullie.eu>
1919
*/
20-
abstract class Exception extends \Exception
21-
{
22-
}
20+
abstract class Exception extends \Exception {}

includes/classes/Dependencies/MatthiasMullie/Minify/Exceptions/BasicException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@
1919
*
2020
* @author Matthias Mullie <minify@mullie.eu>
2121
*/
22-
abstract class BasicException extends Exception
23-
{
24-
}
22+
abstract class BasicException extends Exception {}

includes/classes/Dependencies/MatthiasMullie/Minify/Exceptions/FileImportException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717
*
1818
* @author Matthias Mullie <minify@mullie.eu>
1919
*/
20-
class FileImportException extends BasicException
21-
{
22-
}
20+
class FileImportException extends BasicException {}

includes/classes/Dependencies/MatthiasMullie/Minify/Exceptions/IOException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717
*
1818
* @author Matthias Mullie <minify@mullie.eu>
1919
*/
20-
class IOException extends BasicException
21-
{
22-
}
20+
class IOException extends BasicException {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Pattern match exception.
5+
*
6+
* Please report bugs on https://github.com/matthiasmullie/minify/issues
7+
*
8+
* @author Ere Maijala <ere.maijala@helsinki.fi>
9+
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
10+
* @license MIT License
11+
*/
12+
13+
namespace PoweredCache\Dependencies\MatthiasMullie\Minify\Exceptions;
14+
15+
/**
16+
* Pattern Match Exception Class.
17+
*
18+
* @author Ere Maijala <ere.maijala@helsinki.fi>
19+
*/
20+
class PatternMatchException extends BasicException
21+
{
22+
/**
23+
* Create an exception from preg_last_error.
24+
*
25+
* @param string $msg Error message
26+
*/
27+
public static function fromLastError($msg)
28+
{
29+
$msg .= ': Error ' . preg_last_error();
30+
if (PHP_MAJOR_VERSION >= 8) {
31+
$msg .= ' - ' . preg_last_error_msg();
32+
}
33+
34+
return new PatternMatchException($msg);
35+
}
36+
}

includes/classes/Dependencies/MatthiasMullie/Minify/JS.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ protected function stripWhitespace($content)
450450
* This will prepare the given array by escaping all characters.
451451
*
452452
* @param string[] $operators
453-
* @param string $delimiter
453+
* @param string $delimiter
454454
*
455455
* @return string[]
456456
*/
@@ -481,7 +481,7 @@ protected function getOperatorsForRegex(array $operators, $delimiter = '/')
481481
* This will prepare the given array by escaping all characters.
482482
*
483483
* @param string[] $keywords
484-
* @param string $delimiter
484+
* @param string $delimiter
485485
*
486486
* @return string[]
487487
*/

includes/classes/Dependencies/MatthiasMullie/Minify/Minify.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace PoweredCache\Dependencies\MatthiasMullie\Minify;
1414

1515
use PoweredCache\Dependencies\MatthiasMullie\Minify\Exceptions\IOException;
16+
use PoweredCache\Dependencies\MatthiasMullie\Minify\Exceptions\PatternMatchException;
1617
use Psr\Cache\CacheItemInterface;
1718

1819
/**
@@ -230,7 +231,7 @@ protected function load($data)
230231
* Save to file.
231232
*
232233
* @param string $content The minified data
233-
* @param string $path The path to save the minified data to
234+
* @param string $path The path to save the minified data to
234235
*
235236
* @throws IOException
236237
*/
@@ -249,7 +250,7 @@ protected function save($content, $path)
249250
* If $replacement is a string, it must be plain text. Placeholders like $1 or \2 don't work.
250251
* If you need that functionality, use a callback instead.
251252
*
252-
* @param string $pattern PCRE pattern
253+
* @param string $pattern PCRE pattern
253254
* @param string|callable $replacement Replacement value for matched pattern
254255
*/
255256
protected function registerPattern($pattern, $replacement = '')
@@ -265,23 +266,9 @@ protected function registerPattern($pattern, $replacement = '')
265266
*/
266267
protected function stripMultilineComments()
267268
{
268-
// First extract comments we want to keep, so they can be restored later
269-
// PHP only supports $this inside anonymous functions since 5.4
270269
$minifier = $this;
271-
$callback = function ($match) use ($minifier) {
272-
$count = count($minifier->extracted);
273-
$placeholder = '/*' . $count . '*/';
274-
$minifier->extracted[$placeholder] = $match[0];
275-
276-
return $placeholder;
277-
};
278-
$this->registerPattern('/
279-
# optional newline
280-
\n?
281-
282-
# start comment
283-
\/\*
284-
270+
// Pattern for matching comments that we want to preserve
271+
$keepPattern = '/^
285272
# comment content
286273
(?:
287274
# either starts with an !
@@ -293,14 +280,24 @@ protected function stripMultilineComments()
293280
# there is either a @license or @preserve tag
294281
@(?:license|preserve)
295282
)
283+
/ixs';
284+
$callback = function ($match) use ($minifier, $keepPattern) {
285+
if (preg_match($keepPattern, $match[1])) {
286+
// Preserve the comment
287+
$count = count($minifier->extracted);
288+
$placeholder = '/*' . $count . '*/';
289+
$minifier->extracted[$placeholder] = $match[0];
290+
} else {
291+
// Discard the comment but keep any single line feed
292+
$placeholder = strncmp($match[0], "\n", 1) === 0 || substr($match[0], -1) === "\n"
293+
? "\n"
294+
: '';
295+
}
296296

297-
# then match to the end of the comment
298-
.*?\*\/\n?
299-
300-
/ixs', $callback);
297+
return $placeholder;
298+
};
301299

302-
// Then strip all other comments
303-
$this->registerPattern('/\/\*.*?\*\//s', '');
300+
$this->registerPattern('/\n?\/\*(.*?)\*\/\n?/s', $callback);
304301
}
305302

306303
/**
@@ -314,6 +311,8 @@ protected function stripMultilineComments()
314311
* @param string $content The content to replace patterns in
315312
*
316313
* @return string The (manipulated) content
314+
*
315+
* @throws PatternMatchException
317316
*/
318317
protected function replace($content)
319318
{
@@ -341,14 +340,20 @@ protected function replace($content)
341340
}
342341

343342
$match = null;
344-
if (preg_match($pattern, $content, $match, PREG_OFFSET_CAPTURE, $processedOffset)) {
343+
$matchResult = preg_match($pattern, $content, $match, PREG_OFFSET_CAPTURE, $processedOffset);
344+
if ($matchResult) {
345345
$matches[$i] = $match;
346346

347347
// we'll store the match position as well; that way, we
348348
// don't have to redo all preg_matches after changing only
349349
// the first (we'll still know where those others are)
350350
$positions[$i] = $match[0][1];
351351
} else {
352+
if ($matchResult === false) {
353+
throw PatternMatchException::fromLastError(
354+
"Failed to match pattern '$pattern' at $processedOffset"
355+
);
356+
}
352357
// if the pattern couldn't be matched, there's no point in
353358
// executing it again in later runs on this same content;
354359
// ignore this one until we reach end of content
@@ -390,7 +395,7 @@ protected function replace($content)
390395
* If it's a string, just pass it through.
391396
*
392397
* @param string|callable $replacement Replacement value
393-
* @param array $match Match data, in PREG_OFFSET_CAPTURE form
398+
* @param array $match Match data, in PREG_OFFSET_CAPTURE form
394399
*
395400
* @return string
396401
*/
@@ -445,6 +450,11 @@ protected function extractStrings($chars = '\'"', $placeholderPrefix = '')
445450
};
446451

447452
/*
453+
* Quantifier {0,65535} is used instead of *? to avoid exceeding
454+
* backtrack limit with large strings. 65535 is the maximum allowed
455+
* (see https://www.php.net/manual/en/regexp.reference.repetition.php)
456+
* and should be well sufficient for string representations here.
457+
*
448458
* The \\ messiness explained:
449459
* * Don't count ' or " as end-of-string if it's escaped (has backslash
450460
* in front of it)
@@ -456,7 +466,8 @@ protected function extractStrings($chars = '\'"', $placeholderPrefix = '')
456466
* considered as escape-char (times 2) and to get it in the regex,
457467
* escaped (times 2)
458468
*/
459-
$this->registerPattern('/([' . $chars . '])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
469+
470+
$this->registerPattern('/([' . $chars . '])(.{0,65535}?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
460471
}
461472

462473
/**
@@ -532,8 +543,8 @@ protected function openFileForWriting($path)
532543
* Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
533544
*
534545
* @param resource $handler The resource to write to
535-
* @param string $content The content to write
536-
* @param string $path The path to the file (for exception printing only)
546+
* @param string $content The content to write
547+
* @param string $path The path to the file (for exception printing only)
537548
*
538549
* @throws IOException
539550
*/

0 commit comments

Comments
 (0)