Merge branch 'MDL-76907-master' of https://github.com/sarjona/moodle

This commit is contained in:
Andrew Nicols 2023-03-05 21:54:42 +08:00
commit 668f65f281
9 changed files with 113 additions and 104 deletions

View File

@ -1,6 +1,7 @@
<?php
/**
* CSS Minifier
* CSS Minifier.
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
@ -12,15 +13,14 @@
namespace MatthiasMullie\Minify;
use MatthiasMullie\Minify\Exceptions\FileImportException;
use MatthiasMullie\PathConverter\ConverterInterface;
use MatthiasMullie\PathConverter\Converter;
use MatthiasMullie\PathConverter\ConverterInterface;
/**
* CSS minifier
* CSS minifier.
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
* @package Minify
* @author Matthias Mullie <minify@mullie.eu>
* @author Tijs Verkoyen <minify@verkoyen.eu>
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
@ -97,7 +97,7 @@ class CSS extends Minify
}
// add to top
$content = implode(';', $matches[2]).';'.trim($content, ';');
$content = implode(';', $matches[2]) . ';' . trim($content, ';');
}
return $content;
@ -106,8 +106,8 @@ class CSS extends Minify
/**
* Combine CSS from import statements.
*
* @import's will be loaded and their content merged into the original file,
* to save HTTP requests.
* Import statements will be loaded and their content merged into the original
* file, to save HTTP requests.
*
* @param string $source The file to combine imports for
* @param string $content The CSS content to combine imports for
@ -204,7 +204,7 @@ class CSS extends Minify
// loop the matches
foreach ($matches as $match) {
// get the path for the file that will be imported
$importPath = dirname($source).'/'.$match['path'];
$importPath = dirname($source) . '/' . $match['path'];
// only replace the import with the content if we can grab the
// content of the file
@ -215,7 +215,7 @@ class CSS extends Minify
// check if current file was not imported previously in the same
// import chain.
if (in_array($importPath, $parents)) {
throw new FileImportException('Failed to import file "'.$importPath.'": circular reference detected.');
throw new FileImportException('Failed to import file "' . $importPath . '": circular reference detected.');
}
// grab referenced file & minify it (which may include importing
@ -227,7 +227,7 @@ class CSS extends Minify
// check if this is only valid for certain media
if (!empty($match['media'])) {
$importContent = '@media '.$match['media'].'{'.$importContent.'}';
$importContent = '@media ' . $match['media'] . '{' . $importContent . '}';
}
// add to replacement array
@ -266,7 +266,7 @@ class CSS extends Minify
// get the path for the file that will be imported
$path = $match[2];
$path = dirname($source).'/'.$path;
$path = dirname($source) . '/' . $path;
// only replace the import with the content if we're able to get
// the content of the file, and it's relatively small
@ -277,7 +277,7 @@ class CSS extends Minify
// build replacement
$search[] = $match[0];
$replace[] = 'url('.$this->importExtensions[$extension].';base64,'.$importContent.')';
$replace[] = 'url(' . $this->importExtensions[$extension] . ';base64,' . $importContent . ')';
}
}
@ -293,7 +293,7 @@ class CSS extends Minify
* Perform CSS optimizations.
*
* @param string[optional] $path Path to write the data to
* @param string[] $parents Parent paths, for circular reference checks
* @param string[] $parents Parent paths, for circular reference checks
*
* @return string The minified data
*/
@ -469,9 +469,9 @@ class CSS extends Minify
// build replacement
$search[] = $match[0];
if ($type === 'url') {
$replace[] = 'url('.$url.')';
$replace[] = 'url(' . $url . ')';
} elseif ($type === 'import') {
$replace[] = '@import "'.$url.'"';
$replace[] = '@import "' . $url . '"';
}
}
@ -530,7 +530,7 @@ class CSS extends Minify
);
return preg_replace_callback(
'/(?<=[: ])('.implode('|', array_keys($colors)).')(?=[; }])/i',
'/(?<=[: ])(' . implode('|', array_keys($colors)) . ')(?=[; }])/i',
function ($match) use ($colors) {
return $colors[strtoupper($match[0])];
},
@ -553,10 +553,10 @@ class CSS extends Minify
);
$callback = function ($match) use ($weights) {
return $match[1].$weights[$match[2]];
return $match[1] . $weights[$match[2]];
};
return preg_replace_callback('/(font-weight\s*:\s*)('.implode('|', array_keys($weights)).')(?=[;}])/', $callback, $content);
return preg_replace_callback('/(font-weight\s*:\s*)(' . implode('|', array_keys($weights)) . ')(?=[;}])/', $callback, $content);
}
/**
@ -588,19 +588,19 @@ class CSS extends Minify
// practice, Webkit (especially Safari) seems to stumble over at least
// 0%, potentially other units as well. Only stripping 'px' for now.
// @see https://github.com/matthiasmullie/minify/issues/60
$content = preg_replace('/'.$before.'(-?0*(\.0+)?)(?<=0)px'.$after.'/', '\\1', $content);
$content = preg_replace('/' . $before . '(-?0*(\.0+)?)(?<=0)px' . $after . '/', '\\1', $content);
// strip 0-digits (.0 -> 0)
$content = preg_replace('/'.$before.'\.0+'.$units.'?'.$after.'/', '0\\1', $content);
$content = preg_replace('/' . $before . '\.0+' . $units . '?' . $after . '/', '0\\1', $content);
// strip trailing 0: 50.10 -> 50.1, 50.10px -> 50.1px
$content = preg_replace('/'.$before.'(-?[0-9]+\.[0-9]+)0+'.$units.'?'.$after.'/', '\\1\\2', $content);
$content = preg_replace('/' . $before . '(-?[0-9]+\.[0-9]+)0+' . $units . '?' . $after . '/', '\\1\\2', $content);
// strip trailing 0: 50.00 -> 50, 50.00px -> 50px
$content = preg_replace('/'.$before.'(-?[0-9]+)\.0+'.$units.'?'.$after.'/', '\\1\\2', $content);
$content = preg_replace('/' . $before . '(-?[0-9]+)\.0+' . $units . '?' . $after . '/', '\\1\\2', $content);
// strip leading 0: 0.1 -> .1, 01.1 -> 1.1
$content = preg_replace('/'.$before.'(-?)0+([0-9]*\.[0-9]+)'.$units.'?'.$after.'/', '\\1\\2\\3', $content);
$content = preg_replace('/' . $before . '(-?)0+([0-9]*\.[0-9]+)' . $units . '?' . $after . '/', '\\1\\2\\3', $content);
// strip negative zeroes (-0 -> 0) & truncate zeroes (00 -> 0)
$content = preg_replace('/'.$before.'-?0+'.$units.'?'.$after.'/', '0\\1', $content);
$content = preg_replace('/' . $before . '-?0+' . $units . '?' . $after . '/', '0\\1', $content);
// IE doesn't seem to understand a unitless flex-basis value (correct -
// it goes against the spec), so let's add it in again (make it `%`,
@ -636,7 +636,7 @@ class CSS extends Minify
$minifier = $this;
$callback = function ($match) use ($minifier) {
$count = count($minifier->extracted);
$placeholder = '/*'.$count.'*/';
$placeholder = '/*' . $count . '*/';
$minifier->extracted[$placeholder] = $match[0];
return $placeholder;
@ -702,7 +702,7 @@ class CSS extends Minify
// not in things like `calc(3px + 2px)`, shorthands like `3px -2px`, or
// selectors like `div.weird- p`
$pseudos = array('nth-child', 'nth-last-child', 'nth-last-of-type', 'nth-of-type');
$content = preg_replace('/:('.implode('|', $pseudos).')\(\s*([+-]?)\s*(.+?)\s*([+-]?)\s*(.*?)\s*\)/', ':$1($2$3$4$5)', $content);
$content = preg_replace('/:(' . implode('|', $pseudos) . ')\(\s*([+-]?)\s*(.+?)\s*([+-]?)\s*(.*?)\s*\)/', ':$1($2$3$4$5)', $content);
// remove semicolon/whitespace followed by closing bracket
$content = str_replace(';}', '}', $content);
@ -712,12 +712,12 @@ class CSS extends Minify
/**
* Replace all occurrences of functions that may contain math, where
* whitespace around operators needs to be preserved (e.g. calc, clamp)
* whitespace around operators needs to be preserved (e.g. calc, clamp).
*/
protected function extractMath()
{
$functions = array('calc', 'clamp', 'min', 'max');
$pattern = '/\b('. implode('|', $functions) .')(\(.+?)(?=$|;|})/m';
$pattern = '/\b(' . implode('|', $functions) . ')(\(.+?)(?=$|;|})/m';
// PHP only supports $this inside anonymous functions since 5.4
$minifier = $this;
@ -732,11 +732,11 @@ class CSS extends Minify
// instead, it'll match a larger portion of code to where it's certain that
// the calc() musts have ended, and we'll figure out which is the correct
// closing parenthesis here, by counting how many have opened
for ($i = 0; $i < $length; $i++) {
for ($i = 0; $i < $length; ++$i) {
$char = $match[2][$i];
$expr .= $char;
if ($char === '(') {
$opened++;
++$opened;
} elseif ($char === ')' && --$opened === 0) {
break;
}
@ -744,16 +744,16 @@ class CSS extends Minify
// now that we've figured out where the calc() starts and ends, extract it
$count = count($minifier->extracted);
$placeholder = 'math('.$count.')';
$minifier->extracted[$placeholder] = $function.'('.trim(substr($expr, 1, -1)).')';
$placeholder = 'math(' . $count . ')';
$minifier->extracted[$placeholder] = $function . '(' . trim(substr($expr, 1, -1)) . ')';
// and since we've captured more code than required, we may have some leftover
// calc() in here too - go recursive on the remaining but of code to go figure
// that out and extract what is needed
$rest = $minifier->str_replace_first($function.$expr, '', $match[0]);
$rest = $minifier->str_replace_first($function . $expr, '', $match[0]);
$rest = preg_replace_callback($pattern, $callback, $rest);
return $placeholder.$rest;
return $placeholder . $rest;
};
$this->registerPattern($pattern, $callback);
@ -761,20 +761,19 @@ class CSS extends Minify
/**
* Replace custom properties, whose values may be used in scenarios where
* we wouldn't want them to be minified (e.g. inside calc)
* we wouldn't want them to be minified (e.g. inside calc).
*/
protected function extractCustomProperties()
{
// PHP only supports $this inside anonymous functions since 5.4
$minifier = $this;
$this->registerPattern(
'/(?<=^|[;}{])\s*(--[^:;{}"\'\s]+)\s*:([^;{}]+)/m',
function ($match) use ($minifier) {
$placeholder = '--custom-'. count($minifier->extracted) . ':0';
$minifier->extracted[$placeholder] = $match[1] .':'. trim($match[2]);
return $placeholder;
$placeholder = '--custom-' . count($minifier->extracted) . ':0';
$minifier->extracted[$placeholder] = $match[1] . ':' . trim($match[2]);
return $placeholder;
}
);
}

View File

@ -1,18 +1,20 @@
<?php
/**
* Base Exception
* Base Exception.
*
* @deprecated Use Exceptions\BasicException instead
*
* @author Matthias Mullie <minify@mullie.eu>
*/
namespace MatthiasMullie\Minify;
/**
* Base Exception Class
* Base Exception Class.
*
* @deprecated Use Exceptions\BasicException instead
*
* @package Minify
* @author Matthias Mullie <minify@mullie.eu>
*/
abstract class Exception extends \Exception

View File

@ -1,6 +1,7 @@
<?php
/**
* Basic exception
* Basic exception.
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
@ -8,14 +9,14 @@
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
* @license MIT License
*/
namespace MatthiasMullie\Minify\Exceptions;
use MatthiasMullie\Minify\Exception;
/**
* Basic Exception Class
* Basic Exception Class.
*
* @package Minify\Exception
* @author Matthias Mullie <minify@mullie.eu>
*/
abstract class BasicException extends Exception

View File

@ -1,6 +1,7 @@
<?php
/**
* File Import Exception
* File Import Exception.
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
@ -8,12 +9,12 @@
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
* @license MIT License
*/
namespace MatthiasMullie\Minify\Exceptions;
/**
* File Import Exception Class
* File Import Exception Class.
*
* @package Minify\Exception
* @author Matthias Mullie <minify@mullie.eu>
*/
class FileImportException extends BasicException

View File

@ -1,6 +1,7 @@
<?php
/**
* IO Exception
* IO Exception.
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
@ -8,12 +9,12 @@
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
* @license MIT License
*/
namespace MatthiasMullie\Minify\Exceptions;
/**
* IO Exception Class
* IO Exception Class.
*
* @package Minify\Exception
* @author Matthias Mullie <minify@mullie.eu>
*/
class IOException extends BasicException

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
<?php
/**
* Abstract minifier class
* Abstract minifier class.
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
@ -8,6 +9,7 @@
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
* @license MIT License
*/
namespace MatthiasMullie\Minify;
use MatthiasMullie\Minify\Exceptions\IOException;
@ -18,7 +20,6 @@ use Psr\Cache\CacheItemInterface;
*
* Please report bugs on https://github.com/matthiasmullie/minify/issues
*
* @package Minify
* @author Matthias Mullie <minify@mullie.eu>
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved
* @license MIT License
@ -44,6 +45,8 @@ abstract class Minify
* been extracted from the JS source code, so we can reliably match "code",
* without having to worry about potential "code-like" characters inside.
*
* @internal
*
* @var string[]
*/
public $extracted = array();
@ -128,7 +131,7 @@ abstract class Minify
// check if we can read the file
if (!$this->canImportFile($path)) {
throw new IOException('The file "'.$path.'" could not be opened for reading. Check if PHP has enough permissions.');
throw new IOException('The file "' . $path . '" could not be opened for reading. Check if PHP has enough permissions.');
}
$this->add($path);
@ -357,6 +360,7 @@ abstract class Minify
foreach ($match as &$matchItem) {
$matchItem = $matchItem[0];
}
return $replacement($match);
}
@ -391,8 +395,8 @@ abstract class Minify
}
$count = count($minifier->extracted);
$placeholder = $match[1].$placeholderPrefix.$count.$match[1];
$minifier->extracted[$placeholder] = $match[1].$match[2].$match[1];
$placeholder = $match[1] . $placeholderPrefix . $count . $match[1];
$minifier->extracted[$placeholder] = $match[1] . $match[2] . $match[1];
return $placeholder;
};
@ -409,7 +413,7 @@ abstract class Minify
* considered as escape-char (times 2) and to get it in the regex,
* escaped (times 2)
*/
$this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
$this->registerPattern('/([' . $chars . '])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
}
/**
@ -469,7 +473,7 @@ abstract class Minify
protected function openFileForWriting($path)
{
if ($path === '' || ($handler = @fopen($path, 'w')) === false) {
throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.');
throw new IOException('The file "' . $path . '" could not be opened for writing. Check if PHP has enough permissions.');
}
return $handler;
@ -491,15 +495,17 @@ abstract class Minify
($result = @fwrite($handler, $content)) === false ||
($result < strlen($content))
) {
throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.');
throw new IOException('The file "' . $path . '" could not be written to. Check your disk space and file permissions.');
}
}
protected static function str_replace_first($search, $replace, $subject) {
protected static function str_replace_first($search, $replace, $subject)
{
$pos = strpos($subject, $search);
if ($pos !== false) {
return substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
}

View File

@ -1,14 +1,16 @@
Description of MatthiasMullie\Minify import into Moodle
1) Go to from https://github.com/matthiasmullie/minify/releases
1) Go to https://github.com/matthiasmullie/minify/releases
Download the latest minify "Source code (zip)" and unzip it:
Download lastest minify Source code (zip) and unzip
mv minify-X.Y.ZZ/src /path/to/moodle/lib/minify/matthiasmullie-minify/
mv minify-X.Y.ZZ/data /path/to/moodle/lib/minify/matthiasmullie-minify/
2) Go to https://github.com/matthiasmullie/path-converter/releases/A.B.C.zip and unzip
2) Go to https://github.com/matthiasmullie/path-converter/releases/ and unzip
Download the latest path-converter Source code (zip) and unzip it:
Download lastest path-converter Source code (zip) and unzip
mv path-converter-A.B.C/src/ /path/to/moodle/lib/minify/matthiasmullie-pathconverter/
3) Apply the following patches:
@ -20,9 +22,3 @@ MDL-68191: https://github.com/matthiasmullie/minify/issues/317 is a bug that sto
a few seconds. This is one of the reasons Behat runs in the browser are so slow.)
Whenever this library is updated check if the fix is included and remove this note.
NOTE: As of 2020/12/08, only the first commit was brought into Moodle
Note:
* 2023-02-10 Apply commit https://github.com/matthiasmullie/minify/commit/aa8010c2fa3c26f018874141d36025085754abff
to avoid PHP 8.2 deprecations. See MDL-76413 for more details.
Since new version already have this fix if someone executing the upgrading version and
it has already the patch please ignore this note.

View File

@ -82,7 +82,7 @@
<location>minify/matthiasmullie-minify</location>
<name>MatthiasMullie\Minify</name>
<description>CSS &amp; JavaScript minifier, in PHP</description>
<version>1.3.69</version>
<version>1.3.70</version>
<license>MIT</license>
<repository>https://github.com/matthiasmullie/minify</repository>
<customised/>