Skip to content

Commit

Permalink
Handle all types of single byte whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
apeschar authored and glensc committed Dec 7, 2022
1 parent 80921e5 commit b45a0f9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/JSMin/JSMin.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,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 +143,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 Down Expand Up @@ -349,7 +349,7 @@ protected function get()
*/
protected function isEOF($a)
{
return ord($a) <= self::ORD_LF;
return $a === null || $this->isLineTerminator($a);
}

/**
Expand Down Expand Up @@ -451,4 +451,14 @@ protected function next()
}
return $get;
}

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

protected function isLineTerminator($s) {
// https://www.ecma-international.org/ecma-262/#sec-line-terminators
return strpos("\n\r", $s) !== false;
}
}
2 changes: 1 addition & 1 deletion tests/JSMinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testMinify($testName, $input, $expected, $actualFile)
}

public function testWhitespace() {
$this->assertEquals("hello;", JSMin::minify("\r\n\r\nhello;\r\n"));
$this->assertEquals("hello;", JSMin::minify("\r\n\r\n \t\v\f\xa0hello;\r\n"));
}

public function testBomRemoval() {
Expand Down
Binary file modified tests/Resources/minify/expected/control-chars.js
Binary file not shown.
Binary file modified tests/Resources/minify/input/control-chars.js
Binary file not shown.

0 comments on commit b45a0f9

Please sign in to comment.