Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
David Scotson committed Mar 22, 2020
1 parent 9ba1b45 commit 08333b7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 57 deletions.
87 changes: 53 additions & 34 deletions src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,30 +295,60 @@ protected function importFiles($source, $content)
*/
public function execute($path = null, $parents = array())
{
$content = '';
$preservecommentpattern = '/(
# 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';

// loop CSS data (raw data and files)
foreach ($this->data as $source => $css) {
/*
* Let's first take out strings & comments, since we can't just
* remove whitespace anywhere. If whitespace occurs inside a string,
* we should leave it alone. E.g.:
* p { content: "a test" }
*/
$this->extractStrings();
$this->stripComments();
$this->extractCalcs();
$css = $this->replace($css);

$css = $this->stripWhitespace($css);
$css = $this->shortenColors($css);
$css = $this->shortenZeroes($css);
$css = $this->shortenFontWeights($css);
$css = $this->stripEmptyTags($css);

// restore the string we've extracted earlier
$css = $this->restoreExtractedData($css);

// Split JS on special comments.
$chunks = preg_split($preservecommentpattern, $css, -1, PREG_SPLIT_DELIM_CAPTURE );
$processed = [];
for ($i = 0; $i < count($chunks); $i += 2) {
$code = $chunks[$i];
$comment = '';
if (isset($chunks[$i + 1])) {
$comment = $chunks[$i + 1];
}
/*
* Let's first take out strings & other comments, since we can't just
* remove whitespace anywhere. If whitespace occurs inside a string,
* we should leave it alone. E.g.:
* p { content: "a test" }
*/
$this->extractStrings();
$this->stripComments();
$this->extractCalcs();
$code = $this->replace($code);

$code = $this->stripWhitespace($code);
$code = $this->shortenColors($code);
$code = $this->shortenZeroes($code);
$code = $this->shortenFontWeights($code);
$code = $this->stripEmptyTags($code);

// restore the string we've extracted earlier
$code = $this->restoreExtractedData($code);

$processed[] = $code;
$processed[] = $comment;
}
$css = implode($processed);
$source = is_int($source) ? '' : $source;
$parents = $source ? array_merge($parents, array($source)) : $parents;
$css = $this->combineImports($source, $css, $parents);
Expand All @@ -335,9 +365,9 @@ public function execute($path = null, $parents = array())
$css = $this->move($converter, $css);

// combine css
$content .= $css;
$content[] = $css;
}

$content = implode($content);
$content = $this->moveImportsToTop($content);

return $content;
Expand Down Expand Up @@ -627,17 +657,6 @@ 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', '');
}

Expand Down
67 changes: 44 additions & 23 deletions src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ public function __construct()
*/
public function execute($path = null)
{
$content = '';

/*
* Let's first take out strings, comments and regular expressions.
* All of these can contain JS code-like characters, and we should make
Expand All @@ -163,23 +161,56 @@ public function execute($path = null)
$this->stripComments();
$this->extractRegex();

$preservecommentpattern = '/(
# 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';
// loop files
foreach ($this->data as $source => $js) {
// take out strings, comments & regex (for which we've registered
// the regexes just a few lines earlier)
$js = $this->replace($js);

$js = $this->propertyNotation($js);
$js = $this->shortenBools($js);
$js = $this->stripWhitespace($js);
// Split JS on special comments.
$chunks = preg_split($preservecommentpattern, $js, -1, PREG_SPLIT_DELIM_CAPTURE );
$processed = [];
for ($i = 0; $i < count($chunks); $i += 2) {
$code = $chunks[$i];
$comment = '';
if (isset($chunks[$i + 1])) {
$comment = $chunks[$i + 1];
}

// take out strings, other comments & regex (for which we've registered
// the regexes just a few lines earlier)
$code = $this->replace($code);

// combine js: separating the scripts by a ;
$content .= $js.";";
}
$code = $this->propertyNotation($code);
$code = $this->shortenBools($code);
$code = $this->stripWhitespace($code);

$processed[] = $code;
$processed[] = $comment;
}
$file = implode($processed);
$file = preg_replace('/;$/s', '', $file);

$files[] = $file;
}
$content = implode(';', $files);
// clean up leftover `;`s from the combination of multiple scripts
$content = ltrim($content, ';');
$content = (string) substr($content, 0, -1);

/*
* Earlier, we extracted strings & regular expressions and replaced them
Expand All @@ -195,17 +226,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) {
$count = count($minifier->extracted);
$placeholder = '/*'.$count.'*/';
$minifier->extracted[$placeholder] = $match[0];

return $placeholder;
};
// multi-line comments
$this->registerPattern('/\n?\/\*(!|.*?@license|.*?@preserve).*?\*\/\n?/s', $callback);
$this->registerPattern('/\/\*.*?\*\//s', '');

// single-line comments
Expand Down Expand Up @@ -432,7 +453,7 @@ protected function stripWhitespace($content)
* script: ASI will kick in here & we're all about minifying.
* Semicolons at beginning of the file don't make any sense either.
*/
$content = preg_replace('/;(\}|$)/s', '\\1', $content);
$content = preg_replace('/;(\})/s', '\\1', $content);
$content = ltrim($content, ';');

// get rid of remaining whitespace af beginning/end
Expand Down

0 comments on commit 08333b7

Please sign in to comment.