Update JShrink

This commit is contained in:
Eric Lippmann 2018-06-25 16:05:36 +02:00
parent 46907735fe
commit 66132c330a
2 changed files with 53 additions and 44 deletions

View File

@ -74,6 +74,11 @@ class Minifier
*/
protected $options;
/**
* These characters are used to define strings.
*/
protected $stringDelimiters = ['\'', '"', '`'];
/**
* Contains the default options for minification. This array is merged with
* the one passed in by the user to create the request specific set of
@ -115,9 +120,7 @@ class Minifier
unset($jshrink);
return $js;
} catch (\Exception $e) {
if (isset($jshrink)) {
// Since the breakdownScript function probably wasn't finished
// we clean it out before discarding it.
@ -176,12 +179,11 @@ class Minifier
protected function loop()
{
while ($this->a !== false && !is_null($this->a) && $this->a !== '') {
switch ($this->a) {
// new lines
case "\n":
// if the next line is something that can't stand alone preserve the newline
if (strpos('(-+{[@', $this->b) !== false) {
if (strpos('(-+[@', $this->b) !== false) {
echo $this->a;
$this->saveString();
break;
@ -189,14 +191,17 @@ class Minifier
// if B is a space we skip the rest of the switch block and go down to the
// string/regex check below, resetting $this->b with getReal
if($this->b === ' ')
if ($this->b === ' ') {
break;
}
// otherwise we treat the newline like a space
// no break
case ' ':
if(static::isAlphaNumeric($this->b))
if (static::isAlphaNumeric($this->b)) {
echo $this->a;
}
$this->saveString();
break;
@ -217,9 +222,11 @@ class Minifier
break;
case ' ':
if(!static::isAlphaNumeric($this->a))
if (!static::isAlphaNumeric($this->a)) {
break;
}
// no break
default:
// check for some regex that breaks stuff
if ($this->a === '/' && ($this->b === '\'' || $this->b === '"')) {
@ -236,8 +243,9 @@ class Minifier
// do reg check of doom
$this->b = $this->getReal();
if(($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false))
if (($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false)) {
$this->saveRegex();
}
}
}
@ -267,7 +275,7 @@ class Minifier
$char = $this->c;
unset($this->c);
// Otherwise we start pulling from the input.
// Otherwise we start pulling from the input.
} else {
$char = substr($this->input, $this->index, 1);
@ -282,9 +290,9 @@ class Minifier
// Normalize all whitespace except for the newline character into a
// standard space.
if($char !== "\n" && ord($char) < 32)
if ($char !== "\n" && ord($char) < 32) {
return ' ';
}
return $char;
}
@ -312,10 +320,13 @@ class Minifier
$this->c = $this->getChar();
if ($this->c === '/') {
return $this->processOneLineComments($startIndex);
$this->processOneLineComments($startIndex);
return $this->getReal();
} elseif ($this->c === '*') {
return $this->processMultiLineComments($startIndex);
$this->processMultiLineComments($startIndex);
return $this->getReal();
}
return $char;
@ -325,8 +336,8 @@ class Minifier
* Removed one line comments, with the exception of some very specific types of
* conditional comments.
*
* @param int $startIndex The index point where "getReal" function started
* @return string
* @param int $startIndex The index point where "getReal" function started
* @return void
*/
protected function processOneLineComments($startIndex)
{
@ -335,17 +346,12 @@ class Minifier
// kill rest of line
$this->getNext("\n");
unset($this->c);
if ($thirdCommentString == '@') {
$endPoint = $this->index - $startIndex;
unset($this->c);
$char = "\n" . substr($this->input, $startIndex, $endPoint);
} else {
// first one is contents of $this->c
$this->getChar();
$char = $this->getChar();
$this->c = "\n" . substr($this->input, $startIndex, $endPoint);
}
return $char;
}
/**
@ -353,7 +359,7 @@ class Minifier
* Conditional comments and "license" style blocks are preserved.
*
* @param int $startIndex The index point where "getReal" function started
* @return bool|string False if there's no character
* @return void
* @throws \RuntimeException Unclosed comments will throw an error
*/
protected function processMultiLineComments($startIndex)
@ -363,14 +369,13 @@ class Minifier
// kill everything up to the next */ if it's there
if ($this->getNext('*/')) {
$this->getChar(); // get *
$this->getChar(); // get /
$char = $this->getChar(); // get next real character
// Now we reinsert conditional comments and YUI-style licensing comments
if (($this->options['flaggedComments'] && $thirdCommentString === '!')
|| ($thirdCommentString === '@') ) {
|| ($thirdCommentString === '@')) {
// If conditional comments or flagged comments are not the first thing in the script
// we need to echo a and fill it with a space before moving on.
@ -387,21 +392,20 @@ class Minifier
$endPoint = ($this->index - 1) - $startIndex;
echo substr($this->input, $startIndex, $endPoint);
return $char;
}
$this->c = $char;
return;
}
} else {
$char = false;
}
if($char === false)
if ($char === false) {
throw new \RuntimeException('Unclosed multiline comment at position: ' . ($this->index - 2));
}
// if we're here c is part of the comment and therefore tossed
if(isset($this->c))
unset($this->c);
return $char;
$this->c = $char;
}
/**
@ -418,9 +422,9 @@ class Minifier
$pos = strpos($this->input, $string, $this->index);
// If it's not there return false.
if($pos === false)
if ($pos === false) {
return false;
}
// Adjust position of index to jump ahead to the asked for string
$this->index = $pos;
@ -444,7 +448,7 @@ class Minifier
$this->a = $this->b;
// If this isn't a string we don't need to do anything.
if ($this->a !== "'" && $this->a !== '"') {
if (!in_array($this->a, $this->stringDelimiters)) {
return;
}
@ -473,7 +477,11 @@ class Minifier
// character, so those will be treated just fine using the switch
// block below.
case "\n":
throw new \RuntimeException('Unclosed string at position: ' . $startpos );
if ($stringType === '`') {
echo $this->a;
} else {
throw new \RuntimeException('Unclosed string at position: ' . $startpos);
}
break;
// Escaped characters get picked up here. If it's an escaped new line it's not really needed
@ -513,16 +521,18 @@ class Minifier
echo $this->a . $this->b;
while (($this->a = $this->getChar()) !== false) {
if($this->a === '/')
if ($this->a === '/') {
break;
}
if ($this->a === '\\') {
echo $this->a;
$this->a = $this->getChar();
}
if($this->a === "\n")
if ($this->a === "\n") {
throw new \RuntimeException('Unclosed regex pattern at position: ' . $this->index);
}
echo $this->a;
}
@ -583,5 +593,4 @@ class Minifier
return $js;
}
}

View File

@ -1,4 +1,4 @@
curl https://codeload.github.com/tedious/JShrink/tar.gz/v1.1.0 -o JShrink-1.1.0.tar.gz
tar xzf JShrink-1.1.0.tar.gz --strip-components 1 JShrink-1.1.0/LICENSE
tar xzf JShrink-1.1.0.tar.gz --strip-components 3 JShrink-1.1.0/src/JShrink/Minifier.php
rm JShrink-1.1.0.tar.gz
curl https://codeload.github.com/tedious/JShrink/tar.gz/v1.3.0 -o JShrink-1.3.0.tar.gz
tar xzf JShrink-1.3.0.tar.gz --strip-components 1 JShrink-1.3.0/LICENSE
tar xzf JShrink-1.3.0.tar.gz --strip-components 3 JShrink-1.3.0/src/JShrink/Minifier.php
rm JShrink-1.3.0.tar.gz