mirror of
https://github.com/mrclay/minify.git
synced 2025-08-14 10:05:59 +02:00
JSMin.php : Fix Issue 74 (quotes in regexp literals) & code cleanup
Minify.php : phpdoc for logError() test_Minify_Lines.php : disabled cache before test test_environment.php : + note if SUBDOMAIN_DOCUMENT_ROOT detected
This commit is contained in:
@@ -28,6 +28,13 @@ $min_allowDebugFlag = false;
|
|||||||
$min_errorLogger = false;
|
$min_errorLogger = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow use of the Minify URI Builder app. If you no longer need
|
||||||
|
* this, set to false.
|
||||||
|
**/
|
||||||
|
$min_enableBuilder = true;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For best performance, specify your temp directory here. Otherwise Minify
|
* For best performance, specify your temp directory here. Otherwise Minify
|
||||||
* will have to load extra code to guess. Some examples below:
|
* will have to load extra code to guess. Some examples below:
|
||||||
@@ -44,10 +51,11 @@ $min_errorLogger = false;
|
|||||||
* E.g. '/home/accountname/public_html' or 'c:\\xampp\\htdocs'
|
* E.g. '/home/accountname/public_html' or 'c:\\xampp\\htdocs'
|
||||||
*
|
*
|
||||||
* If /min/ is directly inside your document root, just uncomment the
|
* If /min/ is directly inside your document root, just uncomment the
|
||||||
* second line:
|
* second line. The third line might work on some Apache servers.
|
||||||
*/
|
*/
|
||||||
$min_documentRoot = '';
|
$min_documentRoot = '';
|
||||||
//$min_documentRoot = substr(__FILE__, 0, strlen(__FILE__) - 15);
|
//$min_documentRoot = substr(__FILE__, 0, strlen(__FILE__) - 15);
|
||||||
|
//$min_documentRoot = $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,13 +65,6 @@ $min_documentRoot = '';
|
|||||||
$min_cacheFileLocking = true;
|
$min_cacheFileLocking = true;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allow use of the Minify URI Builder app. If you no longer need
|
|
||||||
* this, set to false.
|
|
||||||
**/
|
|
||||||
$min_enableBuilder = true;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maximum age of browser cache in seconds. After this period, the browser
|
* Maximum age of browser cache in seconds. After this period, the browser
|
||||||
* will send another conditional GET. Use a longer period for lower traffic
|
* will send another conditional GET. Use a longer period for lower traffic
|
||||||
@@ -82,7 +83,7 @@ $min_serveOptions['maxAge'] = 1800;
|
|||||||
* You will still need to include the directory in the
|
* You will still need to include the directory in the
|
||||||
* f or b GET parameters.
|
* f or b GET parameters.
|
||||||
*
|
*
|
||||||
* // = DOCUMENT_ROOT
|
* // = shortcut for DOCUMENT_ROOT
|
||||||
*/
|
*/
|
||||||
//$min_serveOptions['minApp']['allowDirs'] = array('//js', '//css');
|
//$min_serveOptions['minApp']['allowDirs'] = array('//js', '//css');
|
||||||
|
|
||||||
|
@@ -46,12 +46,12 @@
|
|||||||
* --
|
* --
|
||||||
*
|
*
|
||||||
* @package JSMin
|
* @package JSMin
|
||||||
* @author Ryan Grove <ryan@wonko.com>
|
* @author Ryan Grove <ryan@wonko.com> (PHP port)
|
||||||
* @author Steve Clay <steve@mrclay.org> (modifications)
|
* @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
|
||||||
|
* @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
|
||||||
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
|
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
|
||||||
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
|
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 1.1.1 (2008-03-02)
|
|
||||||
* @link http://code.google.com/p/jsmin-php/
|
* @link http://code.google.com/p/jsmin-php/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -67,85 +67,96 @@ class JSMin {
|
|||||||
protected $lookAhead = null;
|
protected $lookAhead = null;
|
||||||
protected $output = '';
|
protected $output = '';
|
||||||
|
|
||||||
// -- Public Static Methods --------------------------------------------------
|
/**
|
||||||
|
* Minify Javascript
|
||||||
public static function minify($js) {
|
*
|
||||||
|
* @param string $js Javascript to be minified
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function minify($js)
|
||||||
|
{
|
||||||
$jsmin = new JSMin($js);
|
$jsmin = new JSMin($js);
|
||||||
return $jsmin->min();
|
return $jsmin->min();
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Public Instance Methods ------------------------------------------------
|
protected 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Protected Instance Methods ---------------------------------------------
|
protected function action($d)
|
||||||
|
{
|
||||||
protected function action($d) {
|
switch ($d) {
|
||||||
switch($d) {
|
|
||||||
case 1:
|
case 1:
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
|
// fallthrough
|
||||||
case 2:
|
case 2:
|
||||||
$this->a = $this->b;
|
$this->a = $this->b;
|
||||||
|
|
||||||
if ($this->a === "'" || $this->a === '"') {
|
if ($this->a === "'" || $this->a === '"') {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
$this->a = $this->get();
|
$this->a = $this->get();
|
||||||
|
|
||||||
if ($this->a === $this->b) {
|
if ($this->a === $this->b) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ord($this->a) <= self::ORD_LF) {
|
if (ord($this->a) <= self::ORD_LF) {
|
||||||
throw new JSMinException('Unterminated string literal.');
|
throw new JSMinException('Unterminated string literal.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->a === '\\') {
|
if ($this->a === '\\') {
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
$this->a = $this->get();
|
$this->a = $this->get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// fallthrough
|
||||||
case 3:
|
case 3:
|
||||||
$this->b = $this->next();
|
$this->b = $this->next();
|
||||||
|
if ($this->b === '/') {
|
||||||
if ($this->b === '/' && (
|
switch ($this->a) {
|
||||||
$this->a === '(' || $this->a === ',' || $this->a === '=' ||
|
case "\n":
|
||||||
$this->a === ':' || $this->a === '[' || $this->a === '!' ||
|
case ' ':
|
||||||
$this->a === '&' || $this->a === '|' || $this->a === '?')) {
|
if (! $this->spaceBeforeRegExp($this->output)) {
|
||||||
|
break;
|
||||||
$this->output .= $this->a . $this->b;
|
}
|
||||||
|
case '{':
|
||||||
|
case ';':
|
||||||
|
case '(':
|
||||||
|
case ',':
|
||||||
|
case '=':
|
||||||
|
case ':':
|
||||||
|
case '[':
|
||||||
|
case '!':
|
||||||
|
case '&':
|
||||||
|
case '|':
|
||||||
|
case '?':
|
||||||
|
$this->output .= $this->a.$this->b;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
$this->a = $this->get();
|
$this->a = $this->get();
|
||||||
|
|
||||||
if ($this->a === '/') {
|
if ($this->a === '/') {
|
||||||
break;
|
break; // for (;;)
|
||||||
} elseif ($this->a === '\\') {
|
} elseif ($this->a === '\\') {
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
$this->a = $this->get();
|
$this->a = $this->get();
|
||||||
} elseif (ord($this->a) <= self::ORD_LF) {
|
} elseif (ord($this->a) <= self::ORD_LF) {
|
||||||
throw new JSMinException('Unterminated regular expression '.
|
throw new JSMinException('Unterminated regular expression literal.');
|
||||||
'literal.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->b = $this->next();
|
$this->b = $this->next();
|
||||||
|
break; // switch ($this->a)
|
||||||
|
// end case ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break; // switch ($d)
|
||||||
|
// end case 3
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function get() {
|
protected function get()
|
||||||
|
{
|
||||||
$c = $this->lookAhead;
|
$c = $this->lookAhead;
|
||||||
$this->lookAhead = null;
|
$this->lookAhead = null;
|
||||||
|
|
||||||
if ($c === null) {
|
if ($c === null) {
|
||||||
if ($this->inputIndex < $this->inputLength) {
|
if ($this->inputIndex < $this->inputLength) {
|
||||||
$c = $this->input[$this->inputIndex];
|
$c = $this->input[$this->inputIndex];
|
||||||
@@ -154,23 +165,22 @@ class JSMin {
|
|||||||
$c = null;
|
$c = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ($c === "\r")
|
||||||
if ($c === "\r") {
|
? "\n"
|
||||||
return "\n";
|
: ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE
|
||||||
|
? $c
|
||||||
|
: ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
|
protected function isAlphaNum($c)
|
||||||
return $c;
|
{
|
||||||
|
return (ord($c) > 126
|
||||||
|
|| $c === '\\'
|
||||||
|
|| preg_match('/^[\w\$]$/', $c) === 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ' ';
|
protected function min()
|
||||||
}
|
{
|
||||||
|
|
||||||
protected function isAlphaNum($c) {
|
|
||||||
return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function min() {
|
|
||||||
$this->a = "\n";
|
$this->a = "\n";
|
||||||
$this->action(3);
|
$this->action(3);
|
||||||
|
|
||||||
@@ -183,7 +193,6 @@ class JSMin {
|
|||||||
$this->action(2);
|
$this->action(2);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "\n":
|
case "\n":
|
||||||
switch ($this->b) {
|
switch ($this->b) {
|
||||||
case '{':
|
case '{':
|
||||||
@@ -193,32 +202,26 @@ class JSMin {
|
|||||||
case '-':
|
case '-':
|
||||||
$this->action(1);
|
$this->action(1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ' ':
|
case ' ':
|
||||||
$this->action(3);
|
$this->action(3);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if ($this->isAlphaNum($this->b)) {
|
if ($this->isAlphaNum($this->b)) {
|
||||||
$this->action(1);
|
$this->action(1);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$this->action(2);
|
$this->action(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
switch ($this->b) {
|
switch ($this->b) {
|
||||||
case ' ':
|
case ' ':
|
||||||
if ($this->isAlphaNum($this->a)) {
|
if ($this->isAlphaNum($this->a)) {
|
||||||
$this->action(1);
|
$this->action(1);
|
||||||
break;
|
break; // switch ($this->b)
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->action(3);
|
$this->action(3);
|
||||||
break;
|
break; // switch ($this->b)
|
||||||
|
|
||||||
case "\n":
|
case "\n":
|
||||||
switch ($this->a) {
|
switch ($this->a) {
|
||||||
case '}':
|
case '}':
|
||||||
@@ -229,34 +232,31 @@ class JSMin {
|
|||||||
case '"':
|
case '"':
|
||||||
case "'":
|
case "'":
|
||||||
$this->action(1);
|
$this->action(1);
|
||||||
break;
|
break; // switch ($this->a)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if ($this->isAlphaNum($this->a)) {
|
if ($this->isAlphaNum($this->a)) {
|
||||||
$this->action(1);
|
$this->action(1);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$this->action(3);
|
$this->action(3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break; // switch ($this->b)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$this->action(1);
|
$this->action(1);
|
||||||
break;
|
break; // switch ($this->b)
|
||||||
|
}
|
||||||
|
// end default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $this->output;
|
return $this->output;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function next() {
|
protected function next()
|
||||||
|
{
|
||||||
$get = $this->get();
|
$get = $this->get();
|
||||||
|
|
||||||
if ($get === '/') {
|
if ($get === '/') {
|
||||||
$commentContents = '';
|
$commentContents = '';
|
||||||
switch($this->peek()) {
|
switch ($this->peek()) {
|
||||||
case '/':
|
case '/':
|
||||||
// "//" comment
|
// "//" comment
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -268,13 +268,12 @@ class JSMin {
|
|||||||
: $get;
|
: $get;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case '*':
|
case '*':
|
||||||
// "/* */" comment
|
// "/* */" comment
|
||||||
$this->get();
|
$this->get();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
$get = $this->get();
|
$get = $this->get();
|
||||||
switch($get) {
|
switch ($get) {
|
||||||
case '*':
|
case '*':
|
||||||
if ($this->peek() === '/') {
|
if ($this->peek() === '/') {
|
||||||
$this->get();
|
$this->get();
|
||||||
@@ -287,27 +286,46 @@ class JSMin {
|
|||||||
: ' ';
|
: ' ';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case null:
|
case null:
|
||||||
throw new JSMinException('Unterminated comment.');
|
throw new JSMinException('Unterminated comment.');
|
||||||
}
|
}
|
||||||
$commentContents .= $get;
|
$commentContents .= $get;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return $get;
|
return $get;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $get;
|
return $get;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function peek() {
|
protected function peek()
|
||||||
|
{
|
||||||
$this->lookAhead = $this->get();
|
$this->lookAhead = $this->get();
|
||||||
return $this->lookAhead;
|
return $this->lookAhead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function spaceBeforeRegExp($output)
|
||||||
|
{
|
||||||
|
$length = strlen($output);
|
||||||
|
$isSpace = false;
|
||||||
|
$tmp = "";
|
||||||
|
foreach (array("case", "else", "in", "return", "typeof") as $word) {
|
||||||
|
if ($length === strlen($word)) {
|
||||||
|
$isSpace = ($word === $output);
|
||||||
|
} elseif ($length > strlen($word)) {
|
||||||
|
$tmp = substr($output, $length - strlen($word) - 1);
|
||||||
|
$isSpace = (substr($tmp, 1) === $word) && ! $this->isAlphaNum($tmp[0]);
|
||||||
|
}
|
||||||
|
if ($isSpace) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ($length < 2)
|
||||||
|
? true
|
||||||
|
: $isSpace;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Exceptions ---------------------------------------------------------------
|
class JSMinException extends Exception {
|
||||||
class JSMinException extends Exception {}
|
|
||||||
?>
|
}
|
||||||
|
@@ -369,6 +369,7 @@ class Minify {
|
|||||||
* an optional string label as the 2nd.
|
* an optional string label as the 2nd.
|
||||||
*
|
*
|
||||||
* @param mixed $obj or a "falsey" value to disable
|
* @param mixed $obj or a "falsey" value to disable
|
||||||
|
* @return null
|
||||||
*/
|
*/
|
||||||
public static function setLogger($obj = null) {
|
public static function setLogger($obj = null) {
|
||||||
self::$_logger = $obj
|
self::$_logger = $obj
|
||||||
@@ -376,6 +377,12 @@ class Minify {
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send message to the error log (if set)
|
||||||
|
*
|
||||||
|
* @param string $msg message to log
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
public static function logError($msg) {
|
public static function logError($msg) {
|
||||||
if (! self::$_logger) return;
|
if (! self::$_logger) return;
|
||||||
self::$_logger->log($msg, 'Minify');
|
self::$_logger->log($msg, 'Minify');
|
||||||
|
4
min_unit_tests/_test_files/js/issue74.js
Normal file
4
min_unit_tests/_test_files/js/issue74.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
function testIssue74() {
|
||||||
|
return /'/;
|
||||||
|
}
|
1
min_unit_tests/_test_files/js/issue74.min.js
vendored
Normal file
1
min_unit_tests/_test_files/js/issue74.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
function testIssue74(){return /'/;}
|
@@ -18,6 +18,18 @@ function test_Javascript()
|
|||||||
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
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/issue74.js');
|
||||||
|
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue74.min.js');
|
||||||
|
$minOutput = Minify_Javascript::minify($src);
|
||||||
|
|
||||||
|
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript : Quotes in RegExp literals (Issue 74)');
|
||||||
|
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
||||||
|
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
|
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test_Javascript();
|
test_Javascript();
|
||||||
|
@@ -9,6 +9,8 @@ function test_Lines()
|
|||||||
|
|
||||||
$exp = file_get_contents("{$thisDir}/_test_files/minify/lines_output.js");
|
$exp = file_get_contents("{$thisDir}/_test_files/minify/lines_output.js");
|
||||||
|
|
||||||
|
Minify::setCache(null); // no cache
|
||||||
|
|
||||||
$ret = Minify::serve('Files', array(
|
$ret = Minify::serve('Files', array(
|
||||||
'debug' => true
|
'debug' => true
|
||||||
,'quiet' => true
|
,'quiet' => true
|
||||||
|
@@ -30,8 +30,12 @@ function test_environment()
|
|||||||
,'environment : DOCUMENT_ROOT should be real path and contain this test file'
|
,'environment : DOCUMENT_ROOT should be real path and contain this test file'
|
||||||
);
|
);
|
||||||
if (! $noSlash || ! $goodRoot) {
|
if (! $noSlash || ! $goodRoot) {
|
||||||
echo "!NOTE: If you cannot modify DOCUMENT_ROOT, see this comment for a workaround:"
|
echo "!NOTE: environment : If you cannot modify DOCUMENT_ROOT, consider "
|
||||||
,"\n http://code.google.com/p/minify/issues/detail?id=68#c6\n";
|
. "setting \$min_documentRoot in config.php\n";
|
||||||
|
}
|
||||||
|
if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
|
||||||
|
echo "!NOTE: environment : \$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] is set. "
|
||||||
|
. "You may need to set \$min_documentRoot to this in config.php\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$thisUrl = 'http://'
|
$thisUrl = 'http://'
|
||||||
@@ -47,7 +51,8 @@ function test_environment()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ('1' === $oc) {
|
if ('1' === $oc) {
|
||||||
echo "!WARN: environment : zlib.output_compression is enabled in php.ini or .htaccess.\n";
|
echo "!WARN: environment : zlib.output_compression is enabled in php.ini"
|
||||||
|
. " or .htaccess.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$fp = fopen($thisUrl . '?hello=1', 'r', false, stream_context_create(array(
|
$fp = fopen($thisUrl . '?hello=1', 'r', false, stream_context_create(array(
|
||||||
|
Reference in New Issue
Block a user