2011-01-20 21:57:01 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* JavaScript Distiller
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-20 21:57:01 +00:00
|
|
|
* Author: Dean Edwards, Nicholas Martin, Trevor Parscal
|
|
|
|
|
* License: LGPL
|
|
|
|
|
*/
|
|
|
|
|
class JavaScriptDistiller {
|
|
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes most of the white-space from JavaScript code.
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-20 21:57:01 +00:00
|
|
|
* This code came from the first pass of Dean Edwards' JavaScript Packer. Compared to using
|
|
|
|
|
* JSMin::minify, this produces < 1% larger output (after gzip) in approx. 25% of the time.
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-20 21:57:01 +00:00
|
|
|
* @param $script String: JavaScript code to minify
|
2011-01-21 00:03:58 +00:00
|
|
|
* @param $stripVerticalSpace Boolean: Try to remove as much vertical whitespace as possible
|
2011-01-20 21:57:01 +00:00
|
|
|
*/
|
2011-01-21 00:03:58 +00:00
|
|
|
public static function stripWhiteSpace( $script, $stripVerticalSpace = false ) {
|
2011-02-18 14:09:03 +00:00
|
|
|
// Try to avoid segfaulting
|
|
|
|
|
// I saw segfaults with a limit of 10000, 1000 seems to work
|
|
|
|
|
$oldLimit = ini_get( 'pcre.recursion_limit' );
|
|
|
|
|
if ( intval( $oldLimit ) > 1000 ) {
|
|
|
|
|
ini_set( 'pcre.recursion_limit', '1000' );
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
$script = self::stripHorizontalSpace( $script );
|
|
|
|
|
// If requested, make some vertical whitespace collapsing as well
|
2011-01-21 17:23:24 +00:00
|
|
|
if ( $stripVerticalSpace ) {
|
2011-01-21 00:03:58 +00:00
|
|
|
$script = self::stripVerticalSpace( $script );
|
|
|
|
|
}
|
|
|
|
|
// Done
|
2011-02-18 14:09:03 +00:00
|
|
|
ini_set( 'pcre.recursion_limit', $oldLimit );
|
2011-01-21 00:03:58 +00:00
|
|
|
return $script;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 06:54:46 +00:00
|
|
|
public static function stripHorizontalSpace( $script ) {
|
2011-01-21 00:03:58 +00:00
|
|
|
$parser = self::createParser();
|
|
|
|
|
// Collapse horizontal whitespaces between variable names into a single space
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '(\b|\$) [ \t]+ (\b|\$)', '$2 $3' );
|
2011-01-21 00:03:58 +00:00
|
|
|
// Collapse horizontal whitespaces between unary operators into a single space
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '([+\-]) [ \t]+ ([+\-])', '$2 $3' );
|
2011-01-21 00:03:58 +00:00
|
|
|
// Remove all remaining un-protected horizontal whitespace
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '[ \t]+');
|
2011-01-21 00:03:58 +00:00
|
|
|
// Collapse multiple vertical whitespaces with some horizontal spaces between them
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '[\r\n]+ [ \t]* [\r\n]+', "\n" );
|
2011-01-21 00:03:58 +00:00
|
|
|
// Execute and return
|
|
|
|
|
return $parser->exec($script);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 06:54:46 +00:00
|
|
|
public static function stripVerticalSpace( $script ) {
|
2011-01-21 00:03:58 +00:00
|
|
|
$parser = self::createParser();
|
|
|
|
|
// Collapse whitespaces between and after a ){ pair (function definitions)
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '\) \s+ \{ \s+', '){' );
|
2011-01-21 00:03:58 +00:00
|
|
|
// Collapse whitespaces between and after a ({ pair (JSON argument)
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '\( \s+ \{ \s+', '({' );
|
2011-01-21 00:03:58 +00:00
|
|
|
// Collapse whitespaces between a parenthesis and a period (call chaining)
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '\) \s+ \.', ').');
|
2011-01-21 00:03:58 +00:00
|
|
|
// Collapse vertical whitespaces which come directly after a semicolon or a comma
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '( [;,] ) \s+', '$2' );
|
2011-01-21 00:03:58 +00:00
|
|
|
// Collapse whitespaces between multiple parenthesis/brackets of similar direction
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add( '( [\)\}] ) \s+ ( [\)\}] )', '$2$3' );
|
|
|
|
|
$parser->add( '( [\(\{] ) \s+ ( [\(\{] )', '$2$3' );
|
2011-01-21 00:03:58 +00:00
|
|
|
return $parser->exec( $script );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Creates an instance of ParseMaster and protects sensitive JavaScript regions.
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-21 00:03:58 +00:00
|
|
|
* This parser is based on regular expressions, which all get or'd together, so rules take
|
|
|
|
|
* precedence in the order they are added. We can use it to minify by armoring certain regions
|
|
|
|
|
* by matching them and replacing them with the full match, leaving the remaining regions around
|
|
|
|
|
* for further matching and replacing. When creating rules please note that because ParseMaster
|
|
|
|
|
* "or"s all of the rules together in a single pattern, encapsulating them in parenthesis, $1
|
|
|
|
|
* represents the whole match for a given rule, and $2 is the first submatch.
|
|
|
|
|
*/
|
|
|
|
|
private static function createParser() {
|
2011-01-20 21:57:01 +00:00
|
|
|
$parser = new ParseMaster();
|
|
|
|
|
// There is a bug in ParseMaster that causes a backslash at the end of a line to be changed
|
|
|
|
|
// to \s if we use a backslash as the escape character. We work around this by using an
|
|
|
|
|
// obscure escape character that we hope will never appear at the end of a line.
|
|
|
|
|
$parser->escapeChar = chr( 1 );
|
2011-02-18 14:09:03 +00:00
|
|
|
|
|
|
|
|
// C-style comment: use non-greedy repetition to find the end
|
|
|
|
|
$parser->add( '\/ \* .*? \* \/' );
|
|
|
|
|
|
|
|
|
|
// Preserve the newline after a C++-style comment -- bug 27046
|
|
|
|
|
$parser->add( '\/ \/ [^\r\n]* ( [\r\n] )', '$2' );
|
|
|
|
|
|
2011-01-20 21:57:01 +00:00
|
|
|
// Protect strings. The original code had [^\'\\v] here, but that didn't armor multiline
|
|
|
|
|
// strings correctly. This also armors multiline strings that don't have backslashes at the
|
|
|
|
|
// end of the line (these are invalid), but that's fine because we're just armoring here.
|
2011-02-18 07:36:11 +00:00
|
|
|
|
|
|
|
|
// Single quotes
|
2011-02-18 14:09:03 +00:00
|
|
|
$parser->add(
|
|
|
|
|
'\'' . // start quote
|
2011-02-18 07:36:11 +00:00
|
|
|
'[^\'\\\\]*' . // a run of non-special characters
|
2011-02-18 14:09:03 +00:00
|
|
|
'(?:' .
|
2011-02-18 14:18:26 +00:00
|
|
|
'\\\\ .' . // a backslash followed by any character
|
2011-02-18 07:36:11 +00:00
|
|
|
'[^\'\\\\]*' . // a run of non-special characters
|
|
|
|
|
')*' . // any number of the above
|
2011-02-18 14:09:03 +00:00
|
|
|
'\'', // end quote
|
2011-02-18 07:36:11 +00:00
|
|
|
'$1' );
|
|
|
|
|
|
|
|
|
|
// Double quotes: same as above
|
2011-02-18 14:09:03 +00:00
|
|
|
$parser->add( '" [^"\\\\]* (?: \\\\ . [^"\\\\]* )* "', '$1' );
|
2011-02-18 07:36:11 +00:00
|
|
|
|
2011-01-20 21:57:01 +00:00
|
|
|
// Protect regular expressions
|
2011-02-18 07:36:11 +00:00
|
|
|
$parser->add(
|
2011-02-18 14:09:03 +00:00
|
|
|
'(?<= [ \t] | [^\w\$\/\'"*)\?:] )' . // assert that whitespace or punctuation precedes
|
|
|
|
|
'\/' . // start slash
|
2011-02-18 07:36:11 +00:00
|
|
|
'[^\r\n\*]' . // not a comment-start or line ending
|
|
|
|
|
'[^\/\r\n\\\\]*' . // a sequence of non-special characters
|
2011-02-18 14:09:03 +00:00
|
|
|
'(?:' .
|
2011-02-18 14:18:26 +00:00
|
|
|
'\\\\ .' . // a backslash followed by any character
|
2011-02-18 07:36:11 +00:00
|
|
|
'[^\/\r\n\\\\]*' . // a sequence of non-special characters
|
|
|
|
|
')*' . // any number of the above
|
2011-02-18 14:09:03 +00:00
|
|
|
'\/[ig]*' , // pattern end, optional modifier
|
2011-02-18 07:36:11 +00:00
|
|
|
'$1' );
|
2011-01-21 00:03:58 +00:00
|
|
|
return $parser;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ParseMaster, version 1.0.2 (2005-08-19) Copyright 2005, Dean Edwards
|
|
|
|
|
* A multi-pattern parser.
|
|
|
|
|
* License: http://creativecommons.org/licenses/LGPL/2.1/
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-21 00:03:58 +00:00
|
|
|
* This is the PHP version of the ParseMaster component of Dean Edwards' (http://dean.edwards.name/)
|
|
|
|
|
* Packer, which was originally written in JavaScript. It was ported to PHP by Nicolas Martin.
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-21 00:03:58 +00:00
|
|
|
* Original Source: http://joliclic.free.fr/php/javascript-packer/en/
|
2011-02-12 04:06:22 +00:00
|
|
|
*
|
2011-01-21 00:03:58 +00:00
|
|
|
* Changes should be pushed back upstream.
|
|
|
|
|
*/
|
|
|
|
|
class ParseMaster {
|
|
|
|
|
public $ignoreCase = false;
|
|
|
|
|
public $escapeChar = '';
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// constants
|
|
|
|
|
const EXPRESSION = 0;
|
|
|
|
|
const REPLACEMENT = 1;
|
|
|
|
|
const LENGTH = 2;
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// used to determine nesting levels
|
2011-02-18 14:09:03 +00:00
|
|
|
private $GROUPS = '/\( (?! \? ) /x';//g
|
2011-02-18 06:54:46 +00:00
|
|
|
private $SUB_REPLACE = '/\$\d/';
|
|
|
|
|
private $INDEXED = '/^\$\d+$/';
|
2011-01-21 00:03:58 +00:00
|
|
|
private $ESCAPE = '/\\\./';//g
|
|
|
|
|
private $QUOTE = '/\'/';
|
2011-02-18 06:54:46 +00:00
|
|
|
private $DELETED = '/\x01[^\x01]*\x01/';//g
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
public function add($expression, $replacement = '') {
|
|
|
|
|
// count the number of sub-expressions
|
|
|
|
|
// - add one because each pattern is itself a sub-expression
|
|
|
|
|
$length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((string)$expression), $out);
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// treat only strings $replacement
|
|
|
|
|
if (is_string($replacement)) {
|
|
|
|
|
// does the pattern deal with sub-expressions?
|
|
|
|
|
if (preg_match($this->SUB_REPLACE, $replacement)) {
|
|
|
|
|
// a simple lookup? (e.g. "$2")
|
|
|
|
|
if (preg_match($this->INDEXED, $replacement)) {
|
|
|
|
|
// store the index (used for fast retrieval of matched strings)
|
|
|
|
|
$replacement = (int)(substr($replacement, 1)) - 1;
|
|
|
|
|
} else { // a complicated lookup (e.g. "Hello $2 $1")
|
|
|
|
|
// build a function to do the lookup
|
|
|
|
|
$quote = preg_match($this->QUOTE, $this->_internalEscape($replacement))
|
2011-02-12 04:06:22 +00:00
|
|
|
? '"' : "'";
|
2011-01-21 00:03:58 +00:00
|
|
|
$replacement = array(
|
|
|
|
|
'fn' => '_backReferences',
|
|
|
|
|
'data' => array(
|
|
|
|
|
'replacement' => $replacement,
|
|
|
|
|
'length' => $length,
|
|
|
|
|
'quote' => $quote
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-20 21:57:01 +00:00
|
|
|
}
|
2011-01-21 00:03:58 +00:00
|
|
|
// pass the modified arguments
|
|
|
|
|
if (!empty($expression)) $this->_add($expression, $replacement, $length);
|
|
|
|
|
else $this->_add('/^$/', $replacement, $length);
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
public function exec($string) {
|
|
|
|
|
// execute the global replacement
|
|
|
|
|
$this->_escaped = array();
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// simulate the _patterns.toSTring of Dean
|
|
|
|
|
$regexp = '/';
|
|
|
|
|
foreach ($this->_patterns as $reg) {
|
2011-02-18 14:09:03 +00:00
|
|
|
$regexp .= '(' . $reg[self::EXPRESSION] . ")|\n";
|
2011-01-21 00:03:58 +00:00
|
|
|
}
|
2011-02-18 14:09:03 +00:00
|
|
|
$regexp = substr($regexp, 0, -2) . '/Sxs';
|
2011-01-21 00:03:58 +00:00
|
|
|
$regexp .= ($this->ignoreCase) ? 'i' : '';
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
$string = $this->_escape($string, $this->escapeChar);
|
|
|
|
|
$string = preg_replace_callback(
|
|
|
|
|
$regexp,
|
|
|
|
|
array(
|
|
|
|
|
&$this,
|
|
|
|
|
'_replacement'
|
|
|
|
|
),
|
|
|
|
|
$string
|
|
|
|
|
);
|
|
|
|
|
$string = $this->_unescape($string, $this->escapeChar);
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
return preg_replace($this->DELETED, '', $string);
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
public function reset() {
|
|
|
|
|
// clear the patterns collection so that this object may be re-used
|
|
|
|
|
$this->_patterns = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private
|
|
|
|
|
private $_escaped = array(); // escaped characters
|
|
|
|
|
private $_patterns = array(); // patterns stored by index
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// create and add a new pattern to the patterns collection
|
|
|
|
|
private function _add() {
|
|
|
|
|
$arguments = func_get_args();
|
|
|
|
|
$this->_patterns[] = $arguments;
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// this is the global replace function (it's quite complicated)
|
|
|
|
|
private function _replacement($arguments) {
|
|
|
|
|
if (empty($arguments)) return '';
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
$i = 1; $j = 0;
|
|
|
|
|
// loop through the patterns
|
|
|
|
|
while (isset($this->_patterns[$j])) {
|
|
|
|
|
$pattern = $this->_patterns[$j++];
|
|
|
|
|
// do we have a result?
|
|
|
|
|
if (isset($arguments[$i]) && ($arguments[$i] != '')) {
|
|
|
|
|
$replacement = $pattern[self::REPLACEMENT];
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
if (is_array($replacement) && isset($replacement['fn'])) {
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
if (isset($replacement['data'])) $this->buffer = $replacement['data'];
|
|
|
|
|
return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
} elseif (is_int($replacement)) {
|
|
|
|
|
return $arguments[$replacement + $i];
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
}
|
|
|
|
|
$delete = ($this->escapeChar == '' ||
|
2011-02-12 04:06:22 +00:00
|
|
|
strpos($arguments[$i], $this->escapeChar) === false)
|
|
|
|
|
? '' : "\x01" . $arguments[$i] . "\x01";
|
2011-01-21 00:03:58 +00:00
|
|
|
return $delete . $replacement;
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// skip over references to sub-expressions
|
|
|
|
|
} else {
|
|
|
|
|
$i += $pattern[self::LENGTH];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
private function _backReferences($match, $offset) {
|
|
|
|
|
$replacement = $this->buffer['replacement'];
|
2011-02-18 10:32:11 +00:00
|
|
|
//$quote = $this->buffer['quote'];
|
2011-01-21 00:03:58 +00:00
|
|
|
$i = $this->buffer['length'];
|
|
|
|
|
while ($i) {
|
|
|
|
|
$replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
|
|
|
|
|
}
|
|
|
|
|
return $replacement;
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
private function _replace_name($match, $offset){
|
|
|
|
|
$length = strlen($match[$offset + 2]);
|
|
|
|
|
$start = $length - max($length - strlen($match[$offset + 3]), 0);
|
|
|
|
|
return substr($match[$offset + 1], $start, $length) . $match[$offset + 4];
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
private function _replace_encoded($match, $offset) {
|
|
|
|
|
return $this->buffer[$match[$offset]];
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
|
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// php : we cannot pass additional data to preg_replace_callback,
|
|
|
|
|
// and we cannot use &$this in create_function, so let's go to lower level
|
|
|
|
|
private $buffer;
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// encode escaped characters
|
|
|
|
|
private function _escape($string, $escapeChar) {
|
|
|
|
|
if ($escapeChar) {
|
|
|
|
|
$this->buffer = $escapeChar;
|
|
|
|
|
return preg_replace_callback(
|
|
|
|
|
'/\\' . $escapeChar . '(.)' .'/',
|
|
|
|
|
array(&$this, '_escapeBis'),
|
|
|
|
|
$string
|
|
|
|
|
);
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
} else {
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private function _escapeBis($match) {
|
|
|
|
|
$this->_escaped[] = $match[1];
|
|
|
|
|
return $this->buffer;
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
// decode escaped characters
|
|
|
|
|
private function _unescape($string, $escapeChar) {
|
|
|
|
|
if ($escapeChar) {
|
|
|
|
|
$regexp = '/'.'\\'.$escapeChar.'/';
|
|
|
|
|
$this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
|
|
|
|
|
return preg_replace_callback
|
|
|
|
|
(
|
|
|
|
|
$regexp,
|
|
|
|
|
array(&$this, '_unescapeBis'),
|
|
|
|
|
$string
|
|
|
|
|
);
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
} else {
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private function _unescapeBis() {
|
|
|
|
|
if (isset($this->_escaped[$this->buffer['i']])
|
|
|
|
|
&& $this->_escaped[$this->buffer['i']] != '')
|
|
|
|
|
{
|
|
|
|
|
$temp = $this->_escaped[$this->buffer['i']];
|
|
|
|
|
} else {
|
|
|
|
|
$temp = '';
|
|
|
|
|
}
|
|
|
|
|
$this->buffer['i']++;
|
|
|
|
|
return $this->buffer['escapeChar'] . $temp;
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2011-01-21 00:03:58 +00:00
|
|
|
private function _internalEscape($string) {
|
|
|
|
|
return preg_replace($this->ESCAPE, '', $string);
|
2011-01-20 21:57:01 +00:00
|
|
|
}
|
|
|
|
|
}
|