Skip to content

Commit

Permalink
Merge pull request #141 from mdetrano/enhancement_endsInKeyword_139
Browse files Browse the repository at this point in the history
implement fix to reduce amount of output examined for ends-in-keyword
  • Loading branch information
tedivm authored Oct 4, 2023
2 parents 35a83e0 + c7abc68 commit 7a35f5a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/JShrink/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7a35f5a

Please sign in to comment.