mirror of
https://github.com/mrclay/minify.git
synced 2025-08-08 15:16:56 +02:00
Workaround for Issue 144
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* jsmin.php - PHP implementation of Douglas Crockford's JSMin.
|
* jsmin.php - extended PHP implementation of Douglas Crockford's JSMin.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* $minifiedJs = JSMin::minify($js);
|
||||||
|
* </code>
|
||||||
*
|
*
|
||||||
* This is a direct port of jsmin.c to PHP with a few PHP performance tweaks and
|
* 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
|
* modifications to preserve some comments (see below). Also, rather than using
|
||||||
@@ -73,16 +77,37 @@ class JSMin {
|
|||||||
*/
|
*/
|
||||||
public static function minify($js)
|
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);
|
$jsmin = new JSMin($js);
|
||||||
return $jsmin->min();
|
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)
|
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);
|
$this->inputLength = strlen($this->input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
min_unit_tests/_test_files/js/issue144.js
Normal file
1
min_unit_tests/_test_files/js/issue144.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
if(!a.id)a.id="dp"+ ++this.uuid;
|
1
min_unit_tests/_test_files/js/issue144.min.js
vendored
Normal file
1
min_unit_tests/_test_files/js/issue144.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
if(!a.id)a.id="dp"+ ++this.uuid;
|
@@ -19,6 +19,13 @@ function test_JSMin()
|
|||||||
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
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');
|
$src = file_get_contents($thisDir . '/_test_files/js/issue74.js');
|
||||||
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue74.min.js');
|
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue74.min.js');
|
||||||
$minOutput = JSMin::minify($src);
|
$minOutput = JSMin::minify($src);
|
||||||
|
Reference in New Issue
Block a user