Skip to content

Commit

Permalink
integrate most recent version of jsmin.php (2.4.1), _should_ fix #416
Browse files Browse the repository at this point in the history
  • Loading branch information
futtta committed May 9, 2024
1 parent a2693b2 commit 1605c57
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions classes/external/php/jsmin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

//namespace JSMin;

/**
* JSMin.php - modified PHP implementation of Douglas Crockford's JSMin.
*
Expand Down Expand Up @@ -53,12 +56,7 @@
* @license http://opensource.org/licenses/mit-license.php MIT License
* @link http://code.google.com/p/jsmin-php/
*/

// This is from https://github.com/mrclay/jsmin-php 2.3.2

class JSMin {
const ORD_LF = 10;
const ORD_SPACE = 32;
const ACTION_KEEP_A = 1;
const ACTION_DELETE_A = 2;
const ACTION_DELETE_A_B = 3;
Expand Down Expand Up @@ -123,16 +121,16 @@ public function min()
while ($this->a !== null) {
// determine next command
$command = self::ACTION_KEEP_A; // default
if ($this->a === ' ') {
if ($this->isWhiteSpace($this->a)) {
if (($this->lastByteOut === '+' || $this->lastByteOut === '-')
&& ($this->b === $this->lastByteOut)) {
// Don't delete this space. If we do, the addition/subtraction
// could be parsed as a post-increment
} elseif (! $this->isAlphaNum($this->b)) {
$command = self::ACTION_DELETE_A;
}
} elseif ($this->a === "\n") {
if ($this->b === ' ') {
} elseif ($this->isLineTerminator($this->a)) {
if ($this->isWhiteSpace($this->b)) {
$command = self::ACTION_DELETE_A_B;

// in case of mbstring.func_overload & 2, must check for null b,
Expand All @@ -143,8 +141,8 @@ public function min()
$command = self::ACTION_DELETE_A;
}
} elseif (! $this->isAlphaNum($this->a)) {
if ($this->b === ' '
|| ($this->b === "\n"
if ($this->isWhiteSpace($this->b)
|| ($this->isLineTerminator($this->b)
&& (false === strpos('}])+-"\'`', $this->a)))) {
$command = self::ACTION_DELETE_A_B;
}
Expand All @@ -165,7 +163,7 @@ public function min()
* ACTION_DELETE_A_B = Get the next B.
*
* @param int $command
* @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException
* @throws JSMin_UnterminatedRegExpException|UnterminatedStringException
*/
protected function action($command)
{
Expand Down Expand Up @@ -207,7 +205,7 @@ protected function action($command)
if ($this->a === $this->b) { // end quote
break;
}
if ($delimiter === '`' && $this->a === "\n") {
if ($delimiter === '`' && $this->isLineTerminator($this->a)) {
// leave the newline
} elseif ($this->isEOF($this->a)) {
$byte = $this->inputIndex - 1;
Expand Down Expand Up @@ -287,7 +285,7 @@ protected function isRegexpLiteral()

// check if first non-ws token is "/" (see starts-regex.js)
$length = strlen($this->output);
if ($this->a === ' ' || $this->a === "\n") {
if ($this->isWhiteSpace($this->a) || $this->isLineTerminator($this->a)) {
if ($length < 2) { // weird edge case
return true;
}
Expand All @@ -309,7 +307,7 @@ protected function isRegexpLiteral()
}

// it's a regexp. Remove unneeded whitespace after keyword
if ($this->a === ' ' || $this->a === "\n") {
if ($this->isWhiteSpace($this->a) || $this->isLineTerminator($this->a)) {
$this->a = '';
}

Expand All @@ -332,18 +330,13 @@ protected function get()
$c = $this->input[$this->inputIndex];
$this->inputIndex += 1;
} else {
return $c;
$c = null;
}
}
if ($c === null) {
return $c;
} else if (ord($c) >= self::ORD_SPACE || $c === "\n") {
return $c;
}
if ($c === "\r") {
return "\n";
}
return ' ';
return $c;
}

/**
Expand All @@ -354,7 +347,7 @@ protected function get()
*/
protected function isEOF($a)
{
return ord($a) <= self::ORD_LF;
return $a === null || $this->isLineTerminator($a);
}

/**
Expand Down Expand Up @@ -389,7 +382,7 @@ protected function consumeSingleLineComment()
while (true) {
$get = $this->get();
$comment .= $get;
if (ord($get) <= self::ORD_LF) { // end of line reached
if ($this->isEOF($get)) {
// if IE conditional comment
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
$this->keptComment .= "/{$comment}";
Expand Down Expand Up @@ -456,6 +449,16 @@ protected function next()
}
return $get;
}

protected function isWhiteSpace($s) {
// https://www.ecma-international.org/ecma-262/#sec-white-space
return $s !== null && strpos(" \t\v\f", $s) !== false;
}

protected function isLineTerminator($s) {
// https://www.ecma-international.org/ecma-262/#sec-line-terminators
return $s !== null && strpos("\n\r", $s) !== false;
}
}

class JSMin_UnterminatedStringException extends Exception {}
Expand Down

0 comments on commit 1605c57

Please sign in to comment.