diff --git a/advagg_js_compress/jshrink.inc b/advagg_js_compress/jshrink.inc index 5c0699fc..94d38337 100644 --- a/advagg_js_compress/jshrink.inc +++ b/advagg_js_compress/jshrink.inc @@ -80,6 +80,13 @@ class Minifier */ protected $input; + /** + * Length of input javascript. + * + * @var int + */ + protected $len = 0; + /** * The location of the character (in the input string) that is next to be * processed. @@ -115,11 +122,11 @@ class Minifier * @var array */ protected $options; - + /** * These characters are used to define strings. */ - protected $stringDelimiters = array("'", '"', '`'); + protected $stringDelimiters = ['\'' => true, '"' => true, '`' => true]; /** * Contains the default options for minification. This array is merged with @@ -128,7 +135,7 @@ class Minifier * * @var array */ - protected static $defaultOptions = array('flaggedComments' => true); + protected static $defaultOptions = ['flaggedComments' => true]; /** * Contains lock ids which are used to replace certain code patterns and @@ -136,7 +143,7 @@ class Minifier * * @var array */ - protected $locks = array(); + protected $locks = []; /** * Takes a string containing javascript and removes unneeded characters in @@ -146,7 +153,7 @@ class Minifier * @param array $options Various runtime options in an associative array * @throws \Exception * @return bool|string - */ + */ public static function minify($js, $options = array()) { try { @@ -199,9 +206,7 @@ class Minifier protected function initialize($js, $options) { $this->options = array_merge(static::$defaultOptions, $options); - $js = str_replace("\r\n", "\n", $js); - $js = str_replace('/**/', '', $js); - $this->input = str_replace("\r", "\n", $js); + $this->input = str_replace(["\r\n", '/**/', "\r"], ["\n", "", "\n"], $js); // We add a newline to the end of the script to make it easier to deal // with comments at the bottom of the script- this prevents the unclosed @@ -214,6 +219,18 @@ class Minifier $this->b = $this->getReal(); } + /** + * Characters that can't stand alone preserve the newline. + * + * @var array + */ + protected $noNewLineCharacters = [ + '(' => true, + '-' => true, + '+' => true, + '[' => true, + '@' => true]; + /** * The primary action occurs here. This function loops through the input string, * outputting anything that's relevant and discarding anything that is not. @@ -225,7 +242,7 @@ class Minifier // new lines case "\n": // if the next line is something that can't stand alone preserve the newline - if ($this->b && strpos('(-+[@', $this->b) !== false) { + if ($this->b !== false && isset($this->noNewLineCharacters[$this->b])) { echo $this->a; $this->saveString(); break; @@ -319,7 +336,7 @@ class Minifier // Otherwise we start pulling from the input. } else { - $char = substr($this->input, $this->index, 1); + $char = $this->index < $this->len ? $this->input[$this->index] : false; // If the next character doesn't exist return false. if (isset($char) && $char === false) { @@ -332,7 +349,7 @@ class Minifier // Normalize all whitespace except for the newline character into a // standard space. - if ($char !== "\n" && ord($char) < 32) { + if ($char !== "\n" && $char < "\x20") { return ' '; } @@ -383,7 +400,7 @@ class Minifier */ protected function processOneLineComments($startIndex) { - $thirdCommentString = substr($this->input, $this->index, 1); + $thirdCommentString = $this->index < $this->len ? $this->input[$this->index] : false; // kill rest of line $this->getNext("\n"); @@ -472,7 +489,7 @@ class Minifier $this->index = $pos; // Return the first character of that string. - return substr($this->input, $this->index, 1); + return $this->index < $this->len ? $this->input[$this->index] : false; } /** @@ -501,10 +518,9 @@ class Minifier echo $this->a; // Loop until the string is done - while (true) { + while (($this->a = $this->getChar()) !== false) { // Grab the very next character and load it into a - $this->a = $this->getChar(); switch ($this->a) { @@ -603,7 +619,7 @@ class Minifier /* lock things like "asd" + ++x; */ $lock = '"LOCK---' . crc32(time()) . '"'; - $matches = array(); + $matches = []; preg_match('/([+-])(\s+)([+-])/S', $js, $matches); if (empty($matches)) { return $js;