Automatically make registered patterns match from string start

This commit is contained in:
Matthias Mullie 2014-10-12 18:02:37 +02:00
parent f1b565bc99
commit eb60ec7964
2 changed files with 9 additions and 9 deletions

View File

@ -183,7 +183,7 @@ class JS extends Minify
return $placeholder;
};
$this->registerPattern('/^([\'"])(.*?)(?<!\\\\)\\1/s', $callback);
$this->registerPattern('/([\'"])(.*?)(?<!\\\\)\\1/s', $callback);
}
/**
@ -192,10 +192,10 @@ class JS extends Minify
protected function stripComments()
{
// single-line comments
$this->registerPattern('/^\/\/.*$[\r\n]*/m', '');
$this->registerPattern('/\/\/.*$[\r\n]*/m', '');
// multi-line comments
$this->registerPattern('/^\/\*.*?\*\//s', '');
$this->registerPattern('/\/\*.*?\*\//s', '');
}
/**
@ -233,10 +233,10 @@ class JS extends Minify
// a = b / c; d = e / f
preg_match_all('/\[(.*?)\]/', $this->variable, $parts);
$last = $parts[1][1];
$this->registerPattern('/^[' . $last . '\}\]\)]\s*\/(?![\/\*])/u', '\\0');
$this->registerPattern('/[' . $last . '\}\]\)]\s*\/(?![\/\*])/u', '\\0');
// it's a regex if we can find an opening & (non-escaped) closing /
$this->registerPattern('/^\/(.*?(?<!\\\\)(\\\\\\\\)*)\//', $callback);
$this->registerPattern('/\/(.*?(?<!\\\\)(\\\\\\\\)*)\//', $callback);
}
/**

View File

@ -125,10 +125,10 @@ abstract class Minify
*/
protected function registerPattern($pattern, $replacement = '')
{
// doublecheck if pattern actually starts at beginning of content
if (substr($pattern, 1, 1) !== '^') {
throw new Exception('Pattern "' . $pattern . '" should start processing at the beginning of the string.');
}
// make sure pattern actually starts at beginning of content - we'll be
// looping the content character by character, executing patterns
// starting on exactly that character.
if ($pattern[0] !== '^') $pattern = $pattern[0] . '^' . substr($pattern, 1);
$this->patterns[] = array($pattern, $replacement);
}