diff --git a/min/lib/JSMin.php b/min/lib/JSMin.php index 770e1c6..e53f99c 100644 --- a/min/lib/JSMin.php +++ b/min/lib/JSMin.php @@ -1,6 +1,10 @@ + * $minifiedJs = JSMin::minify($js); + * * * This is a direct port of jsmin.c to PHP with a few PHP performance tweaks and * modifications to preserve some comments (see below). Also, rather than using @@ -64,7 +68,7 @@ class JSMin { protected $inputLength = 0; protected $lookAhead = null; protected $output = ''; - + /** * Minify Javascript * @@ -73,16 +77,37 @@ class JSMin { */ 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; + } + if (function_exists('mb_strlen') + && (ini_get('mbstring.func_overload') !== '') + && ((int)ini_get('mbstring.func_overload') & 2)) { + $enc = mb_internal_encoding(); + mb_internal_encoding('8bit'); + $jsmin = new JSMin($js); + $ret = $jsmin->min(); + mb_internal_encoding($enc); + return $ret; + } $jsmin = new JSMin($js); return $jsmin->min(); } - - /** - * Setup process + + /* + * 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 */ public function __construct($input) { - $this->input = str_replace("\r\n", "\n", $input); + $this->input = str_replace("\r\n", "\n", $input); $this->inputLength = strlen($this->input); } diff --git a/min_unit_tests/_test_files/js/issue144.js b/min_unit_tests/_test_files/js/issue144.js new file mode 100644 index 0000000..cccb826 --- /dev/null +++ b/min_unit_tests/_test_files/js/issue144.js @@ -0,0 +1 @@ +if(!a.id)a.id="dp"+ ++this.uuid; \ No newline at end of file diff --git a/min_unit_tests/_test_files/js/issue144.min.js b/min_unit_tests/_test_files/js/issue144.min.js new file mode 100644 index 0000000..cccb826 --- /dev/null +++ b/min_unit_tests/_test_files/js/issue144.min.js @@ -0,0 +1 @@ +if(!a.id)a.id="dp"+ ++this.uuid; \ No newline at end of file diff --git a/min_unit_tests/test_JSMin.php b/min_unit_tests/test_JSMin.php index 56d07a2..a492324 100644 --- a/min_unit_tests/test_JSMin.php +++ b/min_unit_tests/test_JSMin.php @@ -19,6 +19,13 @@ function test_JSMin() echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n"; } + $src = file_get_contents($thisDir . '/_test_files/js/issue144.js'); + $minExpected = file_get_contents($thisDir . '/_test_files/js/issue144.min.js'); + $minOutput = JSMin::minify($src); + + $passed = assertTrue($minExpected == $minOutput, 'JSMin : Don\'t minify files with + ++ (Issue 144)'); + + $src = file_get_contents($thisDir . '/_test_files/js/issue74.js'); $minExpected = file_get_contents($thisDir . '/_test_files/js/issue74.min.js'); $minOutput = JSMin::minify($src);