1
0
mirror of https://github.com/mrclay/minify.git synced 2025-07-31 19:30:29 +02:00

Merge pull request #537 from mrclay/sync_jsmin

Sync JSMin with mrclay/jsmin-php
This commit is contained in:
Steve Clay
2016-06-28 14:51:40 -04:00
committed by GitHub
2 changed files with 32 additions and 26 deletions

View File

@@ -108,6 +108,11 @@ class JSMin {
$mbIntEnc = mb_internal_encoding(); $mbIntEnc = mb_internal_encoding();
mb_internal_encoding('8bit'); mb_internal_encoding('8bit');
} }
if (isset($this->input[0]) && $this->input[0] === "\xef") {
$this->input = substr($this->input, 3);
}
$this->input = str_replace("\r\n", "\n", $this->input); $this->input = str_replace("\r\n", "\n", $this->input);
$this->inputLength = strlen($this->input); $this->inputLength = strlen($this->input);
@@ -271,37 +276,39 @@ class JSMin {
protected function isRegexpLiteral() protected function isRegexpLiteral()
{ {
if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) { if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
// we obviously aren't dividing // we can't divide after these tokens
return true; return true;
} }
// we have to check for a preceding keyword, and we don't need to pattern // check if first non-ws token is "/" (see starts-regex.js)
// match over the whole output. $length = strlen($this->output);
$recentOutput = substr($this->output, -10); if ($this->a === ' ' || $this->a === "\n") {
if ($length < 2) { // weird edge case
// check if return/typeof directly precede a pattern without a space return true;
foreach (array('return', 'typeof') as $keyword) {
if ($this->a !== substr($keyword, -1)) {
// certainly wasn't keyword
continue;
}
if (preg_match("~(^|[\\s\\S])" . substr($keyword, 0, -1) . "$~", $recentOutput, $m)) {
if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
return true;
}
} }
} }
// check all keywords // if the "/" follows a keyword, it must be a regexp, otherwise it's best to assume division
if ($this->a === ' ' || $this->a === "\n") {
if (preg_match('~(^|[\\s\\S])(?:case|else|in|return|typeof)$~', $recentOutput, $m)) { $subject = $this->output . trim($this->a);
if ($m[1] === '' || !$this->isAlphaNum($m[1])) { if (!preg_match('/(?:case|else|in|return|typeof)$/', $subject, $m)) {
return true; // not a keyword
} return false;
}
} }
return false; // can't be sure it's a keyword yet (see not-regexp.js)
$charBeforeKeyword = substr($subject, 0 - strlen($m[0]) - 1, 1);
if ($this->isAlphaNum($charBeforeKeyword)) {
// this is really an identifier ending in a keyword, e.g. "xreturn"
return false;
}
// it's a regexp. Remove unneeded whitespace after keyword
if ($this->a === ' ' || $this->a === "\n") {
$this->a = '';
}
return true;
} }
/** /**

View File

@@ -1,3 +1,2 @@
function testIssue74(){return /'/;} function testIssue74(){return/'/;}
!function(s){return /^[£$€?.]/.test(s);}();typeof !function(s){return/^[£$€?.]/.test(s);}();typeof/ ' /;x=/ [/] /;1/foo;(2)/foo;function(){return/foo/};function(){return typeof/foo/};
/ ' /;x=/ [/] /;1/foo;(2)/foo;function(){return/foo/};function(){return typeof/foo/};