Upgrade JShrink to 1.0

refs #6165
This commit is contained in:
Thomas Gelf 2014-05-09 10:37:38 +00:00
parent 681cdee68d
commit 64dd91e457
2 changed files with 212 additions and 121 deletions

View File

@ -84,22 +84,37 @@ class Minifier
protected static $defaultOptions = array('flaggedComments' => true); protected static $defaultOptions = array('flaggedComments' => true);
/** /**
* Minifier::minify takes a string containing javascript and removes * Contains lock ids which are used to replace certain code patterns and
* unneeded characters in order to shrink the code without altering it's * prevent them from being minified
* functionality. *
* @var array
*/
protected $locks = array();
/**
* Takes a string containing javascript and removes unneeded characters in
* order to shrink the code without altering it's functionality.
*
* @param string $js The raw javascript to be minified
* @param array $options Various runtime options in an associative array
* @throws \Exception
* @return bool|string
*/ */
public static function minify($js, $options = array()) public static function minify($js, $options = array())
{ {
try { try {
ob_start(); ob_start();
$currentOptions = array_merge(static::$defaultOptions, $options);
$jshrink = new Minifier(); $jshrink = new Minifier();
$jshrink->breakdownScript($js, $currentOptions); $js = $jshrink->lock($js);
unset($jshrink); $jshrink->minifyDirectToOutput($js, $options);
// Sometimes there's a leading new line, so we trim that out here. // Sometimes there's a leading new line, so we trim that out here.
return ltrim(ob_get_clean()); $js = ltrim(ob_get_clean());
$js = $jshrink->unlock($js);
unset($jshrink);
return $js;
} catch (\Exception $e) { } catch (\Exception $e) {
@ -121,12 +136,24 @@ class Minifier
* stripping out all unneeded characters. * stripping out all unneeded characters.
* *
* @param string $js The raw javascript to be minified * @param string $js The raw javascript to be minified
* @param array $currentOptions Various runtime options in an associative array * @param array $options Various runtime options in an associative array
*/ */
protected function breakdownScript($js, $currentOptions) protected function minifyDirectToOutput($js, $options)
{ {
$this->options = $currentOptions; $this->initialize($js, $options);
$this->loop();
$this->clean();
}
/**
* Initializes internal variables, normalizes new lines,
*
* @param string $js The raw javascript to be minified
* @param array $options Various runtime options in an associative array
*/
protected function initialize($js, $options)
{
$this->options = array_merge(static::$defaultOptions, $options);
$js = str_replace("\r\n", "\n", $js); $js = str_replace("\r\n", "\n", $js);
$this->input = str_replace("\r", "\n", $js); $this->input = str_replace("\r", "\n", $js);
@ -135,42 +162,32 @@ class Minifier
// comment error that can otherwise occur. // comment error that can otherwise occur.
$this->input .= PHP_EOL; $this->input .= PHP_EOL;
// Populate "a" with a new line, "b" with the first character, before
$this->a = $this->getReal(); // entering the loop
$this->a = "\n";
// the only time the length can be higher than 1 is if a conditional $this->b = $this->getReal();
// comment needs to be displayed and the only time that can happen for
// $a is on the very first run
while (strlen($this->a) > 1) {
echo $this->a;
$this->a = $this->getReal();
} }
$this->b = $this->getReal(); /**
* The primary action occurs here. This function loops through the input string,
* outputting anything that's relevant and discarding anything that is not.
*/
protected function loop()
{
while ($this->a !== false && !is_null($this->a) && $this->a !== '') { while ($this->a !== false && !is_null($this->a) && $this->a !== '') {
// now we give $b the same check for conditional comments we gave $a
// before we began looping
if (strlen($this->b) > 1) {
echo $this->a . $this->b;
$this->a = $this->getReal();
$this->b = $this->getReal();
continue;
}
switch ($this->a) { switch ($this->a) {
// new lines // new lines
case "\n": case "\n":
// if the next line is something that can't stand alone // if the next line is something that can't stand alone preserve the newline
// preserve the newline
if (strpos('(-+{[@', $this->b) !== false) { if (strpos('(-+{[@', $this->b) !== false) {
echo $this->a; echo $this->a;
$this->saveString(); $this->saveString();
break; break;
} }
// if its a space we move down to the string test below // if B is a space we skip the rest of the switch block and go down to the
// string/regex check below, resetting $this->b with getReal
if($this->b === ' ') if($this->b === ' ')
break; break;
@ -221,7 +238,20 @@ class Minifier
if(($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false)) if(($this->b == '/' && strpos('(,=:[!&|?', $this->a) !== false))
$this->saveRegex(); $this->saveRegex();
} }
$this->clean(); }
/**
* Resets attributes that do not need to be stored between requests so that
* the next request is ready to go. Another reason for this is to make sure
* the variables are cleared and are not taking up memory.
*/
protected function clean()
{
unset($this->input);
$this->index = 0;
$this->a = $this->b = '';
unset($this->c);
unset($this->options);
} }
/** /**
@ -264,61 +294,99 @@ class Minifier
* performance benefits as the skipping is done using native functions (ie, * performance benefits as the skipping is done using native functions (ie,
* c code) rather than in script php. * c code) rather than in script php.
* *
* @throws \RuntimeException *
* @return string Next 'real' character to be processed. * @return string Next 'real' character to be processed.
* @throws \RuntimeException
*/ */
protected function getReal() protected function getReal()
{ {
$startIndex = $this->index; $startIndex = $this->index;
$char = $this->getChar(); $char = $this->getChar();
// Check to see if we're potentially in a comment // Check to see if we're potentially in a comment
if ($char == '/') { if ($char !== '/') {
return $char;
}
$this->c = $this->getChar(); $this->c = $this->getChar();
if ($this->c == '/') { if ($this->c == '/') {
return $this->processOneLineComments($startIndex);
} elseif ($this->c == '*') {
return $this->processMultiLineComments($startIndex);
}
return $char;
}
/**
* Removed one line comments, with the exception of some very specific types of
* conditional comments.
*
* @param int $startIndex The index point where "getReal" function started
* @return string
*/
protected function processOneLineComments($startIndex)
{
$thirdCommentString = substr($this->input, $this->index, 1); $thirdCommentString = substr($this->input, $this->index, 1);
// kill rest of line // kill rest of line
$char = $this->getNext("\n"); $this->getNext("\n");
if ($thirdCommentString == '@') { if ($thirdCommentString == '@') {
$endPoint = ($this->index) - $startIndex; $endPoint = ($this->index) - $startIndex;
unset($this->c); unset($this->c);
$char = "\n" . substr($this->input, $startIndex, $endPoint); $char = "\n" . substr($this->input, $startIndex, $endPoint);
} else { } else {
$char = $this->getChar(); // first one is contents of $this->c
$this->getChar();
$char = $this->getChar(); $char = $this->getChar();
} }
} elseif ($this->c == '*') { return $char;
}
/**
* Skips multiline comments where appropriate, and includes them where needed.
* Conditional comments and "license" style blocks are preserved.
*
* @param int $startIndex The index point where "getReal" function started
* @return bool|string False if there's no character
* @throws \RuntimeException Unclosed comments will throw an error
*/
protected function processMultiLineComments($startIndex)
{
$this->getChar(); // current C $this->getChar(); // current C
$thirdCommentString = $this->getChar(); $thirdCommentString = $this->getChar();
if ($thirdCommentString == '@') { // kill everything up to the next */ if it's there
// conditional comment if ($this->getNext('*/')) {
// we're gonna back up a bit and and send the comment back,
// where the first char will be echoed and the rest will be
// treated like a string
$this->index = $this->index-2;
return '/';
} elseif ($this->getNext('*/')) {
// kill everything up to the next */
$this->getChar(); // get * $this->getChar(); // get *
$this->getChar(); // get / $this->getChar(); // get /
$char = $this->getChar(); // get next real character $char = $this->getChar(); // get next real character
// if YUI-style comments are enabled we reinsert it into the stream // Now we reinsert conditional comments and YUI-style licensing comments
if ($this->options['flaggedComments'] && $thirdCommentString == '!') { if (($this->options['flaggedComments'] && $thirdCommentString == '!')
|| ($thirdCommentString == '@') ) {
// If conditional comments or flagged comments are not the first thing in the script
// we need to echo a and fill it with a space before moving on.
if ($startIndex > 0) {
echo $this->a;
$this->a = " ";
// If the comment started on a new line we let it stay on the new line
if ($this->input[($startIndex - 1)] == "\n") {
echo "\n";
}
}
$endPoint = ($this->index - 1) - $startIndex; $endPoint = ($this->index - 1) - $startIndex;
echo "\n" . substr($this->input, $startIndex, $endPoint) . "\n"; echo substr($this->input, $startIndex, $endPoint);
return $char;
} }
} else { } else {
@ -331,8 +399,6 @@ class Minifier
// if we're here c is part of the comment and therefore tossed // if we're here c is part of the comment and therefore tossed
if(isset($this->c)) if(isset($this->c))
unset($this->c); unset($this->c);
}
}
return $char; return $char;
} }
@ -342,7 +408,7 @@ class Minifier
* is found the first character of the string is returned and the index is set * is found the first character of the string is returned and the index is set
* to it's position. * to it's position.
* *
* @param $string * @param string $string
* @return string|false Returns the first character of the string or false. * @return string|false Returns the first character of the string or false.
*/ */
protected function getNext($string) protected function getNext($string)
@ -366,6 +432,7 @@ class Minifier
* When a javascript string is detected this function crawls for the end of * When a javascript string is detected this function crawls for the end of
* it and saves the whole string. * it and saves the whole string.
* *
* @throws \RuntimeException Unclosed strings will throw an error
*/ */
protected function saveString() protected function saveString()
{ {
@ -386,7 +453,6 @@ class Minifier
// Echo out that starting quote // Echo out that starting quote
echo $this->a; echo $this->a;
// Loop until the string is done // Loop until the string is done
while (1) { while (1) {
@ -401,8 +467,6 @@ class Minifier
case $stringType: case $stringType:
break 2; break 2;
// New lines in strings without line delimiters are bad- actual // New lines in strings without line delimiters are bad- actual
// new lines will be represented by the string \n and not the actual // new lines will be represented by the string \n and not the actual
// character, so those will be treated just fine using the switch // character, so those will be treated just fine using the switch
@ -411,7 +475,6 @@ class Minifier
throw new \RuntimeException('Unclosed string at position: ' . $startpos ); throw new \RuntimeException('Unclosed string at position: ' . $startpos );
break; break;
// Escaped characters get picked up here. If it's an escaped new line it's not really needed // Escaped characters get picked up here. If it's an escaped new line it's not really needed
case '\\': case '\\':
@ -435,15 +498,14 @@ class Minifier
default: default:
echo $this->a; echo $this->a;
} }
// Echo a- it'll be set to the next char at the start of the loop
// echo $this->a;
} }
} }
/** /**
* When a regular expression is detected this function crawls for the end of * When a regular expression is detected this function crawls for the end of
* it and saves the whole regex. * it and saves the whole regex.
*
* @throws \RuntimeException Unclosed regex will throw an error
*/ */
protected function saveRegex() protected function saveRegex()
{ {
@ -466,24 +528,10 @@ class Minifier
$this->b = $this->getReal(); $this->b = $this->getReal();
} }
/**
* Resets attributes that do not need to be stored between requests so that
* the next request is ready to go. Another reason for this is to make sure
* the variables are cleared and are not taking up memory.
*/
protected function clean()
{
unset($this->input);
$this->index = 0;
$this->a = $this->b = '';
unset($this->c);
unset($this->options);
}
/** /**
* Checks to see if a character is alphanumeric. * Checks to see if a character is alphanumeric.
* *
* @param $char string Just one character * @param string $char Just one character
* @return bool * @return bool
*/ */
protected static function isAlphaNumeric($char) protected static function isAlphaNumeric($char)
@ -491,4 +539,48 @@ class Minifier
return preg_match('/^[\w\$]$/', $char) === 1 || $char == '/'; return preg_match('/^[\w\$]$/', $char) === 1 || $char == '/';
} }
/**
* Replace patterns in the given string and store the replacement
*
* @param string $js The string to lock
* @return bool
*/
protected function lock($js)
{
/* lock things like <code>"asd" + ++x;</code> */
$lock = '"LOCK---' . crc32(time()) . '"';
$matches = array();
preg_match('/([+-])(\s+)([+-])/', $js, $matches);
if (empty($matches)) {
return $js;
}
$this->locks[$lock] = $matches[2];
$js = preg_replace('/([+-])\s+([+-])/', "$1{$lock}$2", $js);
/* -- */
return $js;
}
/**
* Replace "locks" with the original characters
*
* @param string $js The string to unlock
* @return bool
*/
protected function unlock($js)
{
if (!count($this->locks)) {
return $js;
}
foreach ($this->locks as $lock => $replacement) {
$js = str_replace($lock, $replacement, $js);
}
return $js;
}
} }

View File

@ -1,2 +1 @@
https://github.com/tedivm/JShrink.git https://github.com/tedivm/JShrink/releases/tag/v1.0.0
d9238750fdf763dc4f38631be64866fd84fe5ec8