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; protected $options;
/**
* These characters are used to define strings.
*/
protected $stringDelimiters = ['\'', '"', '`'];
/** /**
* Contains the default options for minification. This array is merged with * 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 * the one passed in by the user to create the request specific set of
@ -115,9 +120,7 @@ class Minifier
unset($jshrink); unset($jshrink);
return $js; return $js;
} catch (\Exception $e) { } catch (\Exception $e) {
if (isset($jshrink)) { if (isset($jshrink)) {
// Since the breakdownScript function probably wasn't finished // Since the breakdownScript function probably wasn't finished
// we clean it out before discarding it. // we clean it out before discarding it.
@ -176,12 +179,11 @@ class Minifier
protected function loop() protected function loop()
{ {
while ($this->a !== false && !is_null($this->a) && $this->a !== '') { while ($this->a !== false && !is_null($this->a) && $this->a !== '') {
switch ($this->a) { switch ($this->a) {
// new lines // new lines
case "\n": case "\n":
// if the next line is something that can't stand alone preserve the newline // 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; echo $this->a;
$this->saveString(); $this->saveString();
break; 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 // 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 // string/regex check below, resetting $this->b with getReal
if($this->b === ' ') if ($this->b === ' ') {
break; break;
}
// otherwise we treat the newline like a space // otherwise we treat the newline like a space
// no break
case ' ': case ' ':
if(static::isAlphaNumeric($this->b)) if (static::isAlphaNumeric($this->b)) {
echo $this->a; echo $this->a;
}
$this->saveString(); $this->saveString();
break; break;
@ -217,9 +222,11 @@ class Minifier
break; break;
case ' ': case ' ':
if(!static::isAlphaNumeric($this->a)) if (!static::isAlphaNumeric($this->a)) {
break; break;
}
// no break
default: default:
// check for some regex that breaks stuff // check for some regex that breaks stuff
if ($this->a === '/' && ($this->b === '\'' || $this->b === '"')) { if ($this->a === '/' && ($this->b === '\'' || $this->b === '"')) {
@ -236,10 +243,11 @@ class Minifier
// do reg check of doom // do reg check of doom
$this->b = $this->getReal(); $this->b = $this->getReal();
if(($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false)) if (($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false)) {
$this->saveRegex(); $this->saveRegex();
} }
} }
}
/** /**
* Resets attributes that do not need to be stored between requests so that * Resets attributes that do not need to be stored between requests so that
@ -282,9 +290,9 @@ class Minifier
// Normalize all whitespace except for the newline character into a // Normalize all whitespace except for the newline character into a
// standard space. // standard space.
if($char !== "\n" && ord($char) < 32) if ($char !== "\n" && ord($char) < 32) {
return ' '; return ' ';
}
return $char; return $char;
} }
@ -312,10 +320,13 @@ class Minifier
$this->c = $this->getChar(); $this->c = $this->getChar();
if ($this->c === '/') { if ($this->c === '/') {
return $this->processOneLineComments($startIndex); $this->processOneLineComments($startIndex);
return $this->getReal();
} elseif ($this->c === '*') { } elseif ($this->c === '*') {
return $this->processMultiLineComments($startIndex); $this->processMultiLineComments($startIndex);
return $this->getReal();
} }
return $char; return $char;
@ -326,7 +337,7 @@ class Minifier
* conditional comments. * conditional comments.
* *
* @param int $startIndex The index point where "getReal" function started * @param int $startIndex The index point where "getReal" function started
* @return string * @return void
*/ */
protected function processOneLineComments($startIndex) protected function processOneLineComments($startIndex)
{ {
@ -335,17 +346,12 @@ class Minifier
// kill rest of line // kill rest of line
$this->getNext("\n"); $this->getNext("\n");
unset($this->c);
if ($thirdCommentString == '@') { if ($thirdCommentString == '@') {
$endPoint = $this->index - $startIndex; $endPoint = $this->index - $startIndex;
unset($this->c); $this->c = "\n" . substr($this->input, $startIndex, $endPoint);
$char = "\n" . substr($this->input, $startIndex, $endPoint);
} else {
// first one is contents of $this->c
$this->getChar();
$char = $this->getChar();
} }
return $char;
} }
/** /**
@ -353,7 +359,7 @@ class Minifier
* Conditional comments and "license" style blocks are preserved. * Conditional comments and "license" style blocks are preserved.
* *
* @param int $startIndex The index point where "getReal" function started * @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 * @throws \RuntimeException Unclosed comments will throw an error
*/ */
protected function processMultiLineComments($startIndex) protected function processMultiLineComments($startIndex)
@ -363,7 +369,6 @@ class Minifier
// kill everything up to the next */ if it's there // kill everything up to the next */ if it's there
if ($this->getNext('*/')) { if ($this->getNext('*/')) {
$this->getChar(); // get * $this->getChar(); // get *
$this->getChar(); // get / $this->getChar(); // get /
$char = $this->getChar(); // get next real character $char = $this->getChar(); // get next real character
@ -387,21 +392,20 @@ class Minifier
$endPoint = ($this->index - 1) - $startIndex; $endPoint = ($this->index - 1) - $startIndex;
echo substr($this->input, $startIndex, $endPoint); echo substr($this->input, $startIndex, $endPoint);
return $char; $this->c = $char;
}
return;
}
} else { } else {
$char = false; $char = false;
} }
if($char === false) if ($char === false) {
throw new \RuntimeException('Unclosed multiline comment at position: ' . ($this->index - 2)); 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 we're here c is part of the comment and therefore tossed
if(isset($this->c)) $this->c = $char;
unset($this->c);
return $char;
} }
/** /**
@ -418,9 +422,9 @@ class Minifier
$pos = strpos($this->input, $string, $this->index); $pos = strpos($this->input, $string, $this->index);
// If it's not there return false. // If it's not there return false.
if($pos === false) if ($pos === false) {
return false; return false;
}
// Adjust position of index to jump ahead to the asked for string // Adjust position of index to jump ahead to the asked for string
$this->index = $pos; $this->index = $pos;
@ -444,7 +448,7 @@ class Minifier
$this->a = $this->b; $this->a = $this->b;
// If this isn't a string we don't need to do anything. // 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; return;
} }
@ -473,7 +477,11 @@ class Minifier
// character, so those will be treated just fine using the switch // character, so those will be treated just fine using the switch
// block below. // block below.
case "\n": case "\n":
if ($stringType === '`') {
echo $this->a;
} else {
throw new \RuntimeException('Unclosed string at position: ' . $startpos); throw new \RuntimeException('Unclosed string at position: ' . $startpos);
}
break; break;
// Escaped characters get picked up here. If it's an escaped new line it's not really needed // 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; echo $this->a . $this->b;
while (($this->a = $this->getChar()) !== false) { while (($this->a = $this->getChar()) !== false) {
if($this->a === '/') if ($this->a === '/') {
break; break;
}
if ($this->a === '\\') { if ($this->a === '\\') {
echo $this->a; echo $this->a;
$this->a = $this->getChar(); $this->a = $this->getChar();
} }
if($this->a === "\n") if ($this->a === "\n") {
throw new \RuntimeException('Unclosed regex pattern at position: ' . $this->index); throw new \RuntimeException('Unclosed regex pattern at position: ' . $this->index);
}
echo $this->a; echo $this->a;
} }
@ -583,5 +593,4 @@ class Minifier
return $js; 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 curl https://codeload.github.com/tedious/JShrink/tar.gz/v1.3.0 -o JShrink-1.3.0.tar.gz
tar xzf JShrink-1.1.0.tar.gz --strip-components 1 JShrink-1.1.0/LICENSE tar xzf JShrink-1.3.0.tar.gz --strip-components 1 JShrink-1.3.0/LICENSE
tar xzf JShrink-1.1.0.tar.gz --strip-components 3 JShrink-1.1.0/src/JShrink/Minifier.php tar xzf JShrink-1.3.0.tar.gz --strip-components 3 JShrink-1.3.0/src/JShrink/Minifier.php
rm JShrink-1.1.0.tar.gz rm JShrink-1.3.0.tar.gz