mirror of
https://github.com/mrclay/minify.git
synced 2025-08-24 14:42:50 +02:00
JSMin.php : more comments, clearer code, simplified min()
This commit is contained in:
@@ -53,6 +53,9 @@
|
|||||||
class JSMin {
|
class JSMin {
|
||||||
const ORD_LF = 10;
|
const ORD_LF = 10;
|
||||||
const ORD_SPACE = 32;
|
const ORD_SPACE = 32;
|
||||||
|
const ACTION_KEEP_A = 1;
|
||||||
|
const ACTION_DELETE_A = 2;
|
||||||
|
const ACTION_DELETE_A_B = 3;
|
||||||
|
|
||||||
protected $a = "\n";
|
protected $a = "\n";
|
||||||
protected $b = '';
|
protected $b = '';
|
||||||
@@ -88,67 +91,61 @@ class JSMin {
|
|||||||
*/
|
*/
|
||||||
public function min()
|
public function min()
|
||||||
{
|
{
|
||||||
if ($this->output !== '') {
|
if ($this->output !== '') { // min already run
|
||||||
// min already run
|
|
||||||
return $this->output;
|
return $this->output;
|
||||||
}
|
}
|
||||||
$this->action(3);
|
$this->action(self::ACTION_DELETE_A_B);
|
||||||
|
|
||||||
while ($this->a !== null) {
|
while ($this->a !== null) {
|
||||||
// determine next action
|
// determine next command
|
||||||
|
$command = self::ACTION_KEEP_A; // default
|
||||||
if ($this->a === ' ') {
|
if ($this->a === ' ') {
|
||||||
$act = $this->isAlphaNum($this->b) ? 1 : 2;
|
if (! $this->isAlphaNum($this->b)) {
|
||||||
|
$command = self::ACTION_DELETE_A;
|
||||||
|
}
|
||||||
} elseif ($this->a === "\n") {
|
} elseif ($this->a === "\n") {
|
||||||
if ($this->b === ' ') {
|
if ($this->b === ' ') {
|
||||||
$act = 3;
|
$command = self::ACTION_DELETE_A_B;
|
||||||
} elseif (false !== strpos('{[(+-', $this->b)) {
|
} elseif (false === strpos('{[(+-', $this->b)
|
||||||
$act = 1;
|
&& ! $this->isAlphaNum($this->b)) {
|
||||||
} else {
|
$command = self::ACTION_DELETE_A;
|
||||||
$act = $this->isAlphaNum($this->b) ? 1 : 2;
|
|
||||||
}
|
}
|
||||||
} else {
|
} elseif (! $this->isAlphaNum($this->a)) {
|
||||||
if ($this->b === ' ') {
|
if ($this->b === ' '
|
||||||
$act = $this->isAlphaNum($this->a) ? 1 : 3;
|
|| ($this->b === "\n"
|
||||||
} elseif ($this->b === "\n") {
|
&& (false === strpos('}])+-"\'', $this->a)))) {
|
||||||
if (false !== strpos('}])+-"\'', $this->a)) {
|
$command = self::ACTION_DELETE_A_B;
|
||||||
$act = 1;
|
|
||||||
} else {
|
|
||||||
$act = $this->isAlphaNum($this->a) ? 1 : 3;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$act = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->action($act);
|
$this->action($command);
|
||||||
}
|
}
|
||||||
return $this->output;
|
return $this->output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1 = Output A. Copy B to A. Get the next B.
|
* ACTION_KEEP_A = Output A. Copy B to A. Get the next B.
|
||||||
* 2 = Copy B to A. Get the next B. (Delete A).
|
* ACTION_DELETE_A = Copy B to A. Get the next B.
|
||||||
* 3 = Get the next B. (Delete B).
|
* ACTION_DELETE_A_B = Get the next B.
|
||||||
*/
|
*/
|
||||||
protected function action($d)
|
protected function action($command)
|
||||||
{
|
{
|
||||||
switch ($d) {
|
switch ($command) {
|
||||||
case 1:
|
case self::ACTION_KEEP_A:
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case 2:
|
case self::ACTION_DELETE_A:
|
||||||
$this->a = $this->b;
|
$this->a = $this->b;
|
||||||
if ($this->a === "'" || $this->a === '"') {
|
if ($this->a === "'" || $this->a === '"') { // string literal
|
||||||
// string literal
|
|
||||||
$str = ''; // in case needed for exception
|
$str = ''; // in case needed for exception
|
||||||
while (true) {
|
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
|
||||||
// end quote
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (ord($this->a) <= self::ORD_LF) {
|
if (ord($this->a) <= self::ORD_LF) {
|
||||||
throw new JSMin_UnterminatedStringException('Contents: ' . $str);
|
throw new JSMin_UnterminatedStringException(
|
||||||
|
'Contents: ' . var_export($str, true));
|
||||||
}
|
}
|
||||||
$str .= $this->a;
|
$str .= $this->a;
|
||||||
if ($this->a === '\\') {
|
if ($this->a === '\\') {
|
||||||
@@ -159,49 +156,48 @@ class JSMin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case 3:
|
case self::ACTION_DELETE_A_B:
|
||||||
$this->b = $this->next();
|
$this->b = $this->next();
|
||||||
if ($this->b === '/' && $this->isRegexpLiteral()) {
|
if ($this->b === '/' && $this->isRegexpLiteral()) { // RegExp literal
|
||||||
// RegExp literal
|
|
||||||
$this->output .= $this->a . $this->b;
|
$this->output .= $this->a . $this->b;
|
||||||
$pattern = '/'; // in case needed for exception
|
$pattern = '/'; // in case needed for exception
|
||||||
while (true) {
|
while (true) {
|
||||||
$this->a = $this->get();
|
$this->a = $this->get();
|
||||||
$pattern .= $this->a;
|
$pattern .= $this->a;
|
||||||
if ($this->a === '/') {
|
if ($this->a === '/') { // end pattern
|
||||||
// end pattern
|
|
||||||
break; // while (true)
|
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;
|
$pattern .= $this->a;
|
||||||
} elseif (ord($this->a) <= self::ORD_LF) {
|
} elseif (ord($this->a) <= self::ORD_LF) {
|
||||||
throw new JSMin_UnterminatedRegExpException('Contents: '. $pattern);
|
throw new JSMin_UnterminatedRegExpException(
|
||||||
|
'Contents: '. var_export($pattern, true));
|
||||||
}
|
}
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
}
|
}
|
||||||
$this->b = $this->next();
|
$this->b = $this->next();
|
||||||
}
|
}
|
||||||
break; // switch ($d)
|
// end case ACTION_DELETE_A_B
|
||||||
// end case 3
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isRegexpLiteral()
|
protected function isRegexpLiteral()
|
||||||
{
|
{
|
||||||
if (false !== strpos("\n{;(,=:[!&|?", $this->a)) {
|
if (false !== strpos("\n{;(,=:[!&|?", $this->a)) { // we aren't dividing
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (' ' === $this->a) {
|
if (' ' === $this->a) {
|
||||||
// see if preceeded by keyword
|
|
||||||
$length = strlen($this->output);
|
$length = strlen($this->output);
|
||||||
if ($length < 2) {
|
if ($length < 2) { // weird edge case
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// you can't divide a keyword
|
||||||
if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
|
if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
|
||||||
if ($this->output === $m[0]) {
|
if ($this->output === $m[0]) { // odd but could happen
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// make sure it's a keyword, not end of an identifier
|
||||||
$charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
|
$charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
|
||||||
if (! $this->isAlphaNum($charBeforeKeyword)) {
|
if (! $this->isAlphaNum($charBeforeKeyword)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -229,8 +225,7 @@ class JSMin {
|
|||||||
if ($c === "\r" || $c === "\n") {
|
if ($c === "\r" || $c === "\n") {
|
||||||
return "\n";
|
return "\n";
|
||||||
}
|
}
|
||||||
if (ord($c) < self::ORD_SPACE) {
|
if (ord($c) < self::ORD_SPACE) { // control char
|
||||||
// control char
|
|
||||||
return ' ';
|
return ' ';
|
||||||
}
|
}
|
||||||
return $c;
|
return $c;
|
||||||
@@ -259,10 +254,9 @@ class JSMin {
|
|||||||
while (true) {
|
while (true) {
|
||||||
$get = $this->get();
|
$get = $this->get();
|
||||||
$comment .= $get;
|
$comment .= $get;
|
||||||
if (ord($get) <= self::ORD_LF) {
|
if (ord($get) <= self::ORD_LF) { // EOL reached
|
||||||
// EOL reached
|
// if IE conditional comment
|
||||||
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
|
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
|
||||||
// conditional comment, preserve it
|
|
||||||
return "/{$comment}";
|
return "/{$comment}";
|
||||||
}
|
}
|
||||||
return $get;
|
return $get;
|
||||||
@@ -277,21 +271,20 @@ class JSMin {
|
|||||||
while (true) {
|
while (true) {
|
||||||
$get = $this->get();
|
$get = $this->get();
|
||||||
if ($get === '*') {
|
if ($get === '*') {
|
||||||
if ($this->peek() === '/') {
|
if ($this->peek() === '/') { // end of comment reached
|
||||||
// end of comment reached
|
|
||||||
$this->get();
|
$this->get();
|
||||||
|
// if comment preserved by YUI Compressor
|
||||||
if (0 === strpos($comment, '!')) {
|
if (0 === strpos($comment, '!')) {
|
||||||
// is YUI Compressor style, keep it
|
|
||||||
return "\n/*" . substr($comment, 1) . "*/\n";
|
return "\n/*" . substr($comment, 1) . "*/\n";
|
||||||
}
|
}
|
||||||
|
// if IE conditional comment
|
||||||
if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
|
if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
|
||||||
// is IE conditional, keep it
|
|
||||||
return "/*{$comment}*/";
|
return "/*{$comment}*/";
|
||||||
}
|
}
|
||||||
return ' ';
|
return ' ';
|
||||||
}
|
}
|
||||||
} elseif ($get === null) {
|
} elseif ($get === null) {
|
||||||
throw new JSMin_UnterminatedCommentException('Contents: ' . $comment);
|
throw new JSMin_UnterminatedCommentException('Contents: ' . var_export($comment, true));
|
||||||
}
|
}
|
||||||
$comment .= $get;
|
$comment .= $get;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user