diff --git a/min/lib/JSMin.php b/min/lib/JSMin.php index 75e8e10..011eca3 100644 --- a/min/lib/JSMin.php +++ b/min/lib/JSMin.php @@ -10,7 +10,7 @@ * modifications to preserve some comments (see below). Also, rather than using * stdin/stdout, JSMin::minify() accepts a string as input and returns another * string as output. - * + * * Comments containing IE conditional compilation are preserved, as are multi-line * comments that begin with "/*!" (for documentation purposes). In the latter case * newlines are inserted around the comment to enhance readability. @@ -60,7 +60,7 @@ class JSMin { const ACTION_KEEP_A = 1; const ACTION_DELETE_A = 2; const ACTION_DELETE_A_B = 3; - + protected $a = "\n"; protected $b = ''; protected $input = ''; @@ -70,36 +70,32 @@ class JSMin { protected $output = ''; /** - * Minify Javascript + * Minify Javascript. * * @param string $js Javascript to be minified * @return string */ public static function minify($js) { - // look out for syntax like "++ +" and "- ++" - $p = '\\+'; - $m = '\\-'; - if (preg_match("/([$p$m])(?:\\1 [$p$m]| (?:$p$p|$m$m))/", $js)) { - // likely pre-minified and would be broken by JSMin - return $js; - } $jsmin = new JSMin($js); return $jsmin->min(); } - /* - * Don't create a JSMin instance, instead use the static function minify, - * which checks for mb_string function overloading and avoids errors - * trying to re-minify the output of Closure Compiler - * - * @private + /** + * @param string $input */ public function __construct($input) { $this->input = $input; + // look out for syntax like "++ +" and "- ++" + $p = '\\+'; + $m = '\\-'; + if (preg_match("/([$p$m])(?:\\1 [$p$m]| (?:$p$p|$m$m))/", $input)) { + // likely pre-minified and would be broken by JSMin + $this->output = $input; + } } - + /** * Perform minification, return result */ @@ -118,7 +114,7 @@ class JSMin { $this->inputLength = strlen($this->input); $this->action(self::ACTION_DELETE_A_B); - + while ($this->a !== null) { // determine next command $command = self::ACTION_KEEP_A; // default @@ -138,7 +134,7 @@ class JSMin { } } elseif (! $this->isAlphaNum($this->a)) { if ($this->b === ' ' - || ($this->b === "\n" + || ($this->b === "\n" && (false === strpos('}])+-"\'', $this->a)))) { $command = self::ACTION_DELETE_A_B; } @@ -152,7 +148,7 @@ class JSMin { } return $this->output; } - + /** * ACTION_KEEP_A = Output A. Copy B to A. Get the next B. * ACTION_DELETE_A = Copy B to A. Get the next B. @@ -214,7 +210,7 @@ class JSMin { // end case ACTION_DELETE_A_B } } - + protected function isRegexpLiteral() { if (false !== strpos("\n{;(,=:[!&|?", $this->a)) { // we aren't dividing @@ -239,7 +235,7 @@ class JSMin { } return false; } - + /** * Get next char. Convert ctrl char to space. */ @@ -263,7 +259,7 @@ class JSMin { } return $c; } - + /** * Get next char. If is ctrl character, translate to a space or newline. */ @@ -272,7 +268,7 @@ class JSMin { $this->lookAhead = $this->get(); return $this->lookAhead; } - + /** * Is $c a letter, digit, underscore, dollar sign, escape, or non-ASCII? */ @@ -280,7 +276,7 @@ class JSMin { { return (preg_match('/^[0-9a-zA-Z_\\$\\\\]$/', $c) || ord($c) > 126); } - + protected function singleLineComment() { $comment = ''; @@ -296,7 +292,7 @@ class JSMin { } } } - + protected function multipleLineComment() { $this->get(); @@ -324,7 +320,7 @@ class JSMin { $comment .= $get; } } - + /** * Get the next character, skipping over comments. * Some comments may be preserved.