Skip to content

Commit

Permalink
Merge branch 'mbuella-preserved-comments-merged'
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmullie committed Apr 25, 2023
2 parents da99a84 + 0adaa00 commit a07165a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 34 deletions.
13 changes: 1 addition & 12 deletions src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,18 +632,7 @@ protected function stripEmptyTags($content)
*/
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();
}

/**
Expand Down
23 changes: 1 addition & 22 deletions src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,7 @@ public function execute($path = null)
*/
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', '');
Expand Down
43 changes: 43 additions & 0 deletions src/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,49 @@ protected function registerPattern($pattern, $replacement = '')
$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,
Expand Down
7 changes: 7 additions & 0 deletions tests/CSS/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ public static function dataProvider()
'/* @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; }',
Expand Down
7 changes: 7 additions & 0 deletions tests/JS/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ public static function dataProvider()
'/* @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{}',
Expand Down

0 comments on commit a07165a

Please sign in to comment.