1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-09 23:56:43 +02:00

JSMin.php : Overhaul for readability, more useful exceptions

This commit is contained in:
Steve Clay
2009-03-27 21:24:43 +00:00
parent 65a023cc23
commit 54ad187c49

View File

@@ -11,11 +11,6 @@
* comments that begin with "/*!" (for documentation purposes). In the latter case * comments that begin with "/*!" (for documentation purposes). In the latter case
* newlines are inserted around the comment to enhance readability. * newlines are inserted around the comment to enhance readability.
* *
* Known issue: regular expressions containing quote characters must be proceeded
* by one of the following characters: (,=:[!&|?
* E.g. JSMin will fail on the following: return /'/;
* The simple workaround is to wrap the expression in parenthesis: return (/'/);
*
* PHP 5 or higher is required. * PHP 5 or higher is required.
* *
* Permission is hereby granted to use this version of the library under the * Permission is hereby granted to use this version of the library under the
@@ -59,7 +54,7 @@ class JSMin {
const ORD_LF = 10; const ORD_LF = 10;
const ORD_SPACE = 32; const ORD_SPACE = 32;
protected $a = ''; protected $a = "\n";
protected $b = ''; protected $b = '';
protected $input = ''; protected $input = '';
protected $inputIndex = 0; protected $inputIndex = 0;
@@ -79,12 +74,61 @@ class JSMin {
return $jsmin->min(); return $jsmin->min();
} }
protected function __construct($input) /**
* Setup process
*/
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);
} }
/**
* Perform minification, return result
*/
public function min()
{
if ($this->output !== '') {
// min already run
return $this->output;
}
$this->action(3);
while ($this->a !== null) {
// determine next action
if ($this->a === ' ') {
$act = $this->isAlphaNum($this->b) ? 1 : 2;
} elseif ($this->a === "\n") {
if ($this->b === ' ') {
$act = 3;
} elseif (false !== strpos('{[(+-', $this->b)) {
$act = 1;
} else {
$act = $this->isAlphaNum($this->b) ? 1 : 2;
}
} else {
if ($this->b === ' ') {
$act = $this->isAlphaNum($this->a) ? 1 : 3;
} elseif ($this->b === "\n") {
if (false !== strpos('}])+-"\'', $this->a)) {
$act = 1;
} else {
$act = $this->isAlphaNum($this->a) ? 1 : 3;
}
} else {
$act = 1;
}
}
$this->action($act);
}
return $this->output;
}
/**
* 1 = Output A. Copy B to A. Get the next B.
* 2 = Copy B to A. Get the next B. (Delete A).
* 3 = Get the next B. (Delete B).
*/
protected function action($d) protected function action($d)
{ {
switch ($d) { switch ($d) {
@@ -94,65 +138,82 @@ class JSMin {
case 2: case 2:
$this->a = $this->b; $this->a = $this->b;
if ($this->a === "'" || $this->a === '"') { if ($this->a === "'" || $this->a === '"') {
for (;;) { // string literal
$str = ''; // in case needed for exception
while (true) {
$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) {
// end quote
break; break;
} }
if (ord($this->a) <= self::ORD_LF) { if (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated string literal.'); throw new JSMin_UnterminatedStringException('Contents: ' . $str);
} }
$str .= $this->a;
if ($this->a === '\\') { if ($this->a === '\\') {
$this->output .= $this->a; $this->output .= $this->a;
$this->a = $this->get(); $this->a = $this->get();
$str .= $this->a;
} }
} }
} }
// fallthrough // fallthrough
case 3: case 3:
$this->b = $this->next(); $this->b = $this->next();
if ($this->b === '/') { if ($this->b === '/' && $this->isRegexpLiteral()) {
switch ($this->a) { // RegExp literal
case "\n":
case ' ':
if (! $this->spaceBeforeRegExp($this->output)) {
break;
}
case '{':
case ';':
case '(':
case ',':
case '=':
case ':':
case '[':
case '!':
case '&':
case '|':
case '?':
$this->output .= $this->a . $this->b; $this->output .= $this->a . $this->b;
for (;;) { $pattern = '/'; // in case needed for exception
while (true) {
$this->a = $this->get(); $this->a = $this->get();
$pattern .= $this->a;
if ($this->a === '/') { if ($this->a === '/') {
break; // for (;;) // end pattern
break; // while (true)
} elseif ($this->a === '\\') { } elseif ($this->a === '\\') {
$this->output .= $this->a; $this->output .= $this->a;
$this->a = $this->get(); $this->a = $this->get();
$pattern .= $this->a;
} elseif (ord($this->a) <= self::ORD_LF) { } elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression literal.'); throw new JSMin_UnterminatedRegExpException('Contents: '. $pattern);
} }
$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) break; // switch ($d)
// end case 3 // end case 3
} }
} }
protected function isRegexpLiteral()
{
if (false !== strpos("\n{;(,=:[!&|?", $this->a)) {
return true;
}
if (' ' === $this->a) {
// see if preceeded by keyword
$length = strlen($this->output);
if ($length < 2) {
return true;
}
if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
if ($this->output === $m[0]) {
return true;
}
$charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
if (! $this->isAlphaNum($charBeforeKeyword)) {
return true;
}
}
}
return false;
}
/**
* Get next char. Convert ctrl char to space.
*/
protected function get() protected function get()
{ {
$c = $this->lookAhead; $c = $this->lookAhead;
@@ -162,170 +223,98 @@ class JSMin {
$c = $this->input[$this->inputIndex]; $c = $this->input[$this->inputIndex];
$this->inputIndex += 1; $this->inputIndex += 1;
} else { } else {
$c = null; return null;
} }
} }
return ($c === "\r") if ($c === "\r" || $c === "\n") {
? "\n" return "\n";
: ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE }
? $c if (ord($c) < self::ORD_SPACE) {
: ' '); // control char
} return ' ';
}
protected function isAlphaNum($c) return $c;
{
return (ord($c) > 126
|| $c === '\\'
|| preg_match('/^[\w\$]$/', $c) === 1);
}
protected function min()
{
$this->a = "\n";
$this->action(3);
while ($this->a !== null) {
switch ($this->a) {
case ' ':
if ($this->isAlphaNum($this->b)) {
$this->action(1);
} else {
$this->action(2);
}
break;
case "\n":
switch ($this->b) {
case '{':
case '[':
case '(':
case '+':
case '-':
$this->action(1);
break;
case ' ':
$this->action(3);
break;
default:
if ($this->isAlphaNum($this->b)) {
$this->action(1);
} else {
$this->action(2);
}
}
break;
default:
switch ($this->b) {
case ' ':
if ($this->isAlphaNum($this->a)) {
$this->action(1);
break; // switch ($this->b)
}
$this->action(3);
break; // switch ($this->b)
case "\n":
switch ($this->a) {
case '}':
case ']':
case ')':
case '+':
case '-':
case '"':
case "'":
$this->action(1);
break; // switch ($this->a)
default:
if ($this->isAlphaNum($this->a)) {
$this->action(1);
} else {
$this->action(3);
}
}
break; // switch ($this->b)
default:
$this->action(1);
break; // switch ($this->b)
}
// end default
}
}
return $this->output;
}
protected function next()
{
$get = $this->get();
if ($get === '/') {
$commentContents = '';
switch ($this->peek()) {
case '/':
// "//" comment
for (;;) {
$get = $this->get();
$commentContents .= $get;
if (ord($get) <= self::ORD_LF) {
return preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $commentContents)
? "/{$commentContents}"
: $get;
}
}
case '*':
// "/* */" comment
$this->get();
for (;;) {
$get = $this->get();
switch ($get) {
case '*':
if ($this->peek() === '/') {
$this->get();
if (0 === strpos($commentContents, '!')) {
// YUI Compressor style
return "\n/*" . substr($commentContents, 1) . "*/\n";
}
return preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $commentContents)
? "/*{$commentContents}*/" // IE conditional compilation
: ' ';
}
break;
case null:
throw new JSMinException('Unterminated comment.');
}
$commentContents .= $get;
}
default:
return $get;
}
}
return $get;
} }
/**
* Get next char. If is ctrl character, translate to a space or newline.
*/
protected function peek() protected function peek()
{ {
$this->lookAhead = $this->get(); $this->lookAhead = $this->get();
return $this->lookAhead; return $this->lookAhead;
} }
protected function spaceBeforeRegExp($output) /**
* Is $c a letter, digit, underscore, dollar sign, escape, or non-ASCII?
*/
protected function isAlphaNum($c)
{ {
$length = strlen($output); return (preg_match('/^[0-9a-zA-Z_\\$\\\\]$/', $c) || ord($c) > 126);
$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; protected function singleLineComment()
{
$comment = '';
while (true) {
$get = $this->get();
$comment .= $get;
if (ord($get) <= self::ORD_LF) {
// EOL reached
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
// conditional comment, preserve it
return "/{$comment}";
} }
return $get;
} }
return ($length < 2)
? true
: $isSpace;
} }
} }
class JSMinException extends Exception { protected function multipleLineComment()
{
$this->get();
$comment = '';
while (true) {
$get = $this->get();
if ($get === '*') {
if ($this->peek() === '/') {
// end of comment reached
$this->get();
if (0 === strpos($comment, '!')) {
// is YUI Compressor style, keep it
return "\n/*" . substr($comment, 1) . "*/\n";
} }
if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
// is IE conditional, keep it
return "/*{$comment}*/";
}
return ' ';
}
} elseif ($get === null) {
throw new JSMin_UnterminatedCommentException('Contents: ' . $comment);
}
$comment .= $get;
}
}
/**
* Get the next character, skipping over comments.
* Some comments may be preserved.
*/
protected function next()
{
$get = $this->get();
if ($get !== '/') {
return $get;
}
switch ($this->peek()) {
case '/': return $this->singleLineComment();
case '*': return $this->multipleLineComment();
default: return $get;
}
}
}
class JSMin_UnterminatedStringException extends Exception {}
class JSMin_UnterminatedCommentException extends Exception {}
class JSMin_UnterminatedRegExpException extends Exception {}