From c7abc6890da680e4706c63b86edf7658fdfcea69 Mon Sep 17 00:00:00 2001 From: Marc DeTrano Date: Wed, 4 Oct 2023 09:15:06 -0600 Subject: [PATCH] implement fix to reduce amount of output examined for ends-in-keyword regular expression. --- src/JShrink/Minifier.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/JShrink/Minifier.php b/src/JShrink/Minifier.php index 3f07ad8..f0a078d 100644 --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -112,6 +112,8 @@ class Minifier protected static $keywords = ["delete", "do", "for", "in", "instanceof", "return", "typeof", "yield"]; + protected $max_keyword_len; + /** * Contains lock ids which are used to replace certain code patterns and * prevent them from being minified @@ -189,6 +191,8 @@ protected function initialize($js, $options) $this->b = "\n"; $this->last_char = "\n"; $this->output = ""; + + $this->max_keyword_len = max(array_map('strlen', static::$keywords)); } /** @@ -658,7 +662,8 @@ 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; + # Regular expression only needs to check final part of output for keyword. + $testOutput = substr($this->output . $this->a, -1 * ($this->max_keyword_len + 10)); foreach(static::$keywords as $keyword) { if (preg_match('/[^\w]'.$keyword.'[ ]?$/i', $testOutput) === 1) {