Merge branch 'preserved-comments' into preserved-comments-merged

This commit is contained in:
Marlon Buella 2023-04-25 15:05:15 +08:00
commit 3d5b4205d6
5 changed files with 59 additions and 34 deletions

View File

@ -632,18 +632,7 @@ class CSS extends Minify
*/
protected function stripComments()
{
// PHP only supports $this inside anonymous functions since 5.4
$minifier = $this;
$callback = function ($match) use ($minifier) {
$count = count($minifier->extracted);
$placeholder = '/*' . $count . '*/';
$minifier->extracted[$placeholder] = $match[0];
return $placeholder;
};
$this->registerPattern('/\n?\/\*(!|.*?@license|.*?@preserve).*?\*\/\n?/s', $callback);
$this->registerPattern('/\/\*.*?\*\//s', '');
$this->stripMultilineComments();
}
/**

View File

@ -198,28 +198,7 @@ class JS extends Minify
*/
protected function stripComments()
{
// PHP only supports $this inside anonymous functions since 5.4
$minifier = $this;
$callback = function ($match) use ($minifier) {
if (
substr($match[2], 0, 1) === '!' ||
strpos($match[2], '@license') !== false ||
strpos($match[2], '@preserve') !== false
) {
// preserve multi-line comments that start with /*!
// or contain @license or @preserve annotations
$count = count($minifier->extracted);
$placeholder = '/*' . $count . '*/';
$minifier->extracted[$placeholder] = $match[0];
return $match[1] . $placeholder . $match[3];
}
return $match[1] . $match[3];
};
// multi-line comments
$this->registerPattern('/(\n?)\/\*(.*?)\*\/(\n?)/s', $callback);
$this->stripMultilineComments();
// single-line comments
$this->registerPattern('/\/\/.*$/m', '');

View File

@ -260,6 +260,49 @@ abstract class Minify
$this->patterns[] = array($pattern, $replacement);
}
/**
* Both JS and CSS use the same form of multi-line comment, so putting the common code here.
*/
protected function stripMultilineComments()
{
// First extract comments we want to keep, so they can be restored later
// PHP only supports $this inside anonymous functions since 5.4
$minifier = $this;
$callback = function ($match) use ($minifier) {
$count = count($minifier->extracted);
$placeholder = '/*'.$count.'*/';
$minifier->extracted[$placeholder] = $match[0];
return $placeholder;
};
$this->registerPattern('/
# optional newline
\n?
# start comment
\/\*
# comment content
(?:
# either starts with an !
!
|
# or, after some number of characters which do not end the comment
(?:(?!\*\/).)*?
# there is either a @license or @preserve tag
@(?:license|preserve)
)
# then match to the end of the comment
.*?\*\/\n?
/ixs', $callback);
// Then strip all other comments
$this->registerPattern('/\/\*.*?\*\//s', '');
}
/**
* We can't "just" run some regular expressions against JavaScript: it's a
* complex language. E.g. having an occurrence of // xyz would be a comment,

View File

@ -172,6 +172,13 @@ class CSSTest extends CompatTestCase
'/* @preserve This is a CSS comment */',
);
$tests[] = array(
'/* Do not preserve me */
body { color: red; }
/* @preserve This is a CSS comment */',
'body{color:red}/* @preserve This is a CSS comment */',
);
// strip whitespace
$tests[] = array(
'body { color: red; }',

View File

@ -183,6 +183,13 @@ class JSTest extends CompatTestCase
'/* @preserve This is a JS comment */',
);
$tests[] = array(
'/* Do not preserve me */
x = 1;
/* @preserve This is a JS comment */',
'x=1;/* @preserve This is a JS comment */',
);
// make sure no ; is added in places it shouldn't
$tests[] = array(
'if(true){}else{}',