mirror of
https://github.com/matthiasmullie/minify.git
synced 2025-01-17 20:58:26 +01:00
Merge branch 'preserved-comments' into preserved-comments-merged
This commit is contained in:
commit
3d5b4205d6
13
src/CSS.php
13
src/CSS.php
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
23
src/JS.php
23
src/JS.php
@ -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', '');
|
||||
|
@ -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,
|
||||
|
@ -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; }',
|
||||
|
@ -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{}',
|
||||
|
Loading…
x
Reference in New Issue
Block a user