From: Craig Duncan Date: Tue, 28 Mar 2017 22:39:01 +0100 Subject: [PATCH] Don't protect newlines before opening braces --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -181,7 +181,7 @@ class Minifier // new lines case "\n": // if the next line is something that can't stand alone preserve the newline - if ($this->b !== false && strpos('(-+{[@', $this->b) !== false) { + if ($this->b !== false && strpos('(-+[@', $this->b) !== false) { echo $this->a; $this->saveString(); break; From: Craig Duncan Date: Tue, 28 Mar 2017 22:24:40 +0100 Subject: [PATCH] Ensure that comments following other comments are handled --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -312,10 +312,14 @@ 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 +329,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 +339,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 +352,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) @@ -387,7 +386,9 @@ class Minifier $endPoint = ($this->index - 1) - $startIndex; echo substr($this->input, $startIndex, $endPoint); - return $char; + $this->c = $char; + + return; } } else { @@ -398,10 +399,7 @@ class Minifier 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; } /** From: Robert Hafner Date: Thu, 7 Dec 2017 16:03:40 -0800 Subject: [PATCH] Add support for template literals --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -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 @@ -442,7 +447,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; } @@ -471,7 +476,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 From: Robert Hafner Date: Thu, 7 Dec 2017 16:48:36 -0800 Subject: [PATCH] upgrade dependencies, php requirements, formatting --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -120,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. @@ -181,7 +179,6 @@ class Minifier protected function loop() { while ($this->a !== false && !is_null($this->a) && $this->a !== '') { - switch ($this->a) { // new lines case "\n": @@ -194,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; @@ -222,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 === '"')) { @@ -241,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(); + } } } @@ -272,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); @@ -287,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; } @@ -320,7 +323,6 @@ class Minifier $this->processOneLineComments($startIndex); return $this->getReal(); - } elseif ($this->c === '*') { $this->processMultiLineComments($startIndex); @@ -367,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. @@ -395,13 +396,13 @@ class Minifier 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 $this->c = $char; @@ -421,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; @@ -479,7 +480,7 @@ class Minifier if ($stringType === '`') { echo $this->a; } else { - throw new \RuntimeException('Unclosed string at position: ' . $startpos ); + throw new \RuntimeException('Unclosed string at position: ' . $startpos); } break; @@ -520,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; } @@ -590,5 +593,4 @@ class Minifier return $js; } - } From: Alex Date: Sat, 30 Jun 2018 12:50:18 +0300 Subject: [PATCH] Fixed infinite loop when reading invalid JavaScript. --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -459,11 +459,8 @@ class Minifier echo $this->a; // Loop until the string is done - while (true) { - - // Grab the very next character and load it into a - $this->a = $this->getChar(); - + // Grab the very next character and load it into a + while ($this->a = $this->getChar()) { switch ($this->a) { // If the string opener (single or double quote) is used From: Alex Date: Sat, 30 Jun 2018 12:59:50 +0300 Subject: [PATCH] Updated loop condition to strictly validate function result. --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -460,7 +460,7 @@ class Minifier // Loop until the string is done // Grab the very next character and load it into a - while ($this->a = $this->getChar()) { + while (($this->a = $this->getChar()) !== FALSE) { switch ($this->a) { // If the string opener (single or double quote) is used From: Alex Date: Mon, 2 Jul 2018 17:38:39 +0300 Subject: [PATCH] Fixed code formatting notices. --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -274,9 +274,8 @@ class Minifier if (isset($this->c)) { $char = $this->c; unset($this->c); - - // Otherwise we start pulling from the input. } else { + // Otherwise we start pulling from the input. $char = substr($this->input, $this->index, 1); // If the next character doesn't exist return false. @@ -460,7 +459,7 @@ class Minifier // Loop until the string is done // Grab the very next character and load it into a - while (($this->a = $this->getChar()) !== FALSE) { + while (($this->a = $this->getChar()) !== false) { switch ($this->a) { // If the string opener (single or double quote) is used From: Antti Hukkanen Date: Fri, 7 Jun 2019 00:24:25 +0300 Subject: [PATCH] Detect regular expression after arithmetic operation --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -243,7 +243,7 @@ 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(); } } From: Antti Hukkanen Date: Fri, 7 Jun 2019 00:37:27 +0300 Subject: [PATCH] Remove reduntant slash from the regular expression character match --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -243,7 +243,7 @@ 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(); } } From: Antti Hukkanen Date: Fri, 7 Jun 2019 00:39:02 +0300 Subject: [PATCH] Remove duplicate ampresand from the regular expression char match --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -243,7 +243,7 @@ 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(); } }