diff --git a/src/JShrink/Minifier.php b/src/JShrink/Minifier.php index 10b035f..4986c53 100644 --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -285,7 +285,14 @@ protected function loop() if ($this->b == '/') { $valid_tokens = "(,=:[!&|?\n"; - if (strpos($valid_tokens, $this->last_char) !== false || strpos($valid_tokens, $this->a) !== false) { + + # Find last "real" token, excluding spaces. + $last_token = $this->a; + if ($last_token == " ") { + $last_token = $this->last_char; + } + + if (strpos($valid_tokens, $last_token) !== false) { // Regex can appear unquoted after these symbols $this->saveRegex(); } else if ($this->endsInKeyword()) { @@ -644,11 +651,15 @@ protected static function isAlphaNumeric($char) } protected function endsInKeyword() { + + # When this function is called A is not yet assigned to output. + $testOutput = $this->output . $this->a; + foreach(static::$keywords as $keyword) { - if (str_ends_with($this->output, $keyword)) { + if (str_ends_with($testOutput, $keyword)) { return true; } - if (str_ends_with($this->output, $keyword . " ")) { + if (str_ends_with($testOutput, $keyword . " ")) { return true; } } diff --git a/tests/Resources/jshrink/input/regex_detection.js b/tests/Resources/jshrink/input/regex_detection.js new file mode 100644 index 0000000..6833207 --- /dev/null +++ b/tests/Resources/jshrink/input/regex_detection.js @@ -0,0 +1,5 @@ +function test () { + return this.indeterminate + ? 100 + : (100 * (this.options.value - this.min)) / (this.options.max - this.min) +} diff --git a/tests/Resources/jshrink/input/regex_division.js b/tests/Resources/jshrink/input/regex_division.js new file mode 100644 index 0000000..6d503e7 --- /dev/null +++ b/tests/Resources/jshrink/input/regex_division.js @@ -0,0 +1,3 @@ +if (D > (F/2)) { + console.log('a') +} diff --git a/tests/Resources/jshrink/input/regex_keyword_similarity.js b/tests/Resources/jshrink/input/regex_keyword_similarity.js new file mode 100644 index 0000000..9f79ba7 --- /dev/null +++ b/tests/Resources/jshrink/input/regex_keyword_similarity.js @@ -0,0 +1 @@ +var value = currentRating/other \ No newline at end of file diff --git a/tests/Resources/jshrink/output/regex_detection.js b/tests/Resources/jshrink/output/regex_detection.js new file mode 100644 index 0000000..be38d46 --- /dev/null +++ b/tests/Resources/jshrink/output/regex_detection.js @@ -0,0 +1 @@ +function test(){return this.indeterminate?100:(100*(this.options.value-this.min))/(this.options.max-this.min)} \ No newline at end of file diff --git a/tests/Resources/jshrink/output/regex_division.js b/tests/Resources/jshrink/output/regex_division.js new file mode 100644 index 0000000..70fa287 --- /dev/null +++ b/tests/Resources/jshrink/output/regex_division.js @@ -0,0 +1 @@ +if(D>(F/2)){console.log('a')} \ No newline at end of file diff --git a/tests/Resources/jshrink/output/regex_keyword_similarity.js b/tests/Resources/jshrink/output/regex_keyword_similarity.js new file mode 100644 index 0000000..24796c2 --- /dev/null +++ b/tests/Resources/jshrink/output/regex_keyword_similarity.js @@ -0,0 +1 @@ +var value=currentRating/other \ No newline at end of file