5 * Author: Dean Edwards, Nicholas Martin, Trevor Parscal
8 class JavaScriptDistiller
{
13 * Removes most of the white-space from JavaScript code.
15 * This code came from the first pass of Dean Edwards' JavaScript Packer. Compared to using
16 * JSMin::minify, this produces < 1% larger output (after gzip) in approx. 25% of the time.
18 * @param $script String: JavaScript code to minify
19 * @param $stripVerticalSpace Boolean: Try to remove as much vertical whitespace as possible
21 public static function stripWhiteSpace( $script, $stripVerticalSpace = false ) {
22 // Try to avoid segfaulting
23 // I saw segfaults with a limit of 10000, 1000 seems to work
24 $oldLimit = ini_get( 'pcre.recursion_limit' );
25 if ( intval( $oldLimit ) > 1000 ) {
26 ini_set( 'pcre.recursion_limit', '1000' );
29 $script = self
::stripHorizontalSpace( $script );
30 // If requested, make some vertical whitespace collapsing as well
31 if ( $stripVerticalSpace ) {
32 $script = self
::stripVerticalSpace( $script );
35 ini_set( 'pcre.recursion_limit', $oldLimit );
39 public static function stripHorizontalSpace( $script ) {
40 $parser = self
::createParser();
41 // Collapse horizontal whitespaces between variable names into a single space
42 $parser->add( '(\b|\$) [ \t]+ (\b|\$)', '$2 $3' );
43 // Collapse horizontal whitespaces between unary operators into a single space
44 $parser->add( '([+\-]) [ \t]+ ([+\-])', '$2 $3' );
45 // Remove all remaining un-protected horizontal whitespace
46 $parser->add( '[ \t]+');
47 // Collapse multiple vertical whitespaces with some horizontal spaces between them
48 $parser->add( '[\r\n]+ [ \t]* [\r\n]+', "\n" );
50 return $parser->exec($script);
53 public static function stripVerticalSpace( $script ) {
54 $parser = self
::createParser();
55 // Collapse whitespaces between and after a ){ pair (function definitions)
56 $parser->add( '\) \s+ \{ \s+', '){' );
57 // Collapse whitespaces between and after a ({ pair (JSON argument)
58 $parser->add( '\( \s+ \{ \s+', '({' );
59 // Collapse whitespaces between a parenthesis and a period (call chaining)
60 $parser->add( '\) \s+ \.', ').');
61 // Collapse vertical whitespaces which come directly after a semicolon or a comma
62 $parser->add( '( [;,] ) \s+', '$2' );
63 // Collapse whitespaces between multiple parenthesis/brackets of similar direction
64 $parser->add( '( [\)\}] ) \s+ ( [\)\}] )', '$2$3' );
65 $parser->add( '( [\(\{] ) \s+ ( [\(\{] )', '$2$3' );
66 return $parser->exec( $script );
70 * Creates an instance of ParseMaster and protects sensitive JavaScript regions.
72 * This parser is based on regular expressions, which all get or'd together, so rules take
73 * precedence in the order they are added. We can use it to minify by armoring certain regions
74 * by matching them and replacing them with the full match, leaving the remaining regions around
75 * for further matching and replacing. When creating rules please note that because ParseMaster
76 * "or"s all of the rules together in a single pattern, encapsulating them in parenthesis, $1
77 * represents the whole match for a given rule, and $2 is the first submatch.
79 private static function createParser() {
80 $parser = new ParseMaster();
81 // There is a bug in ParseMaster that causes a backslash at the end of a line to be changed
82 // to \s if we use a backslash as the escape character. We work around this by using an
83 // obscure escape character that we hope will never appear at the end of a line.
84 $parser->escapeChar
= chr( 1 );
86 // C-style comment: use non-greedy repetition to find the end
87 $parser->add( '\/ \* .*? \* \/' );
89 // Preserve the newline after a C++-style comment -- bug 27046
90 $parser->add( '\/ \/ [^\r\n]* ( [\r\n] )', '$2' );
92 // Protect strings. The original code had [^\'\\v] here, but that didn't armor multiline
93 // strings correctly. This also armors multiline strings that don't have backslashes at the
94 // end of the line (these are invalid), but that's fine because we're just armoring here.
99 '[^\'\\\\]*' . // a run of non-special characters
101 '\\\\ .' . // a backslash followed by any character
102 '[^\'\\\\]*' . // a run of non-special characters
103 ')*' . // any number of the above
107 // Double quotes: same as above
108 $parser->add( '" [^"\\\\]* (?: \\\\ . [^"\\\\]* )* "', '$1' );
110 // Protect regular expressions
112 '(?<= [ \t] | [^\w\$\/\'"*)\?:] )' . // assert that whitespace or punctuation precedes
113 '\/' . // start slash
114 '[^\r\n\*]' . // not a comment-start or line ending
115 '[^\/\r\n\\\\]*' . // a sequence of non-special characters
117 '\\\\ .' . // a backslash followed by any character
118 '[^\/\r\n\\\\]*' . // a sequence of non-special characters
119 ')*' . // any number of the above
120 '\/[ig]*' , // pattern end, optional modifier
127 * ParseMaster, version 1.0.2 (2005-08-19) Copyright 2005, Dean Edwards
128 * A multi-pattern parser.
129 * License: http://creativecommons.org/licenses/LGPL/2.1/
131 * This is the PHP version of the ParseMaster component of Dean Edwards' (http://dean.edwards.name/)
132 * Packer, which was originally written in JavaScript. It was ported to PHP by Nicolas Martin.
134 * Original Source: http://joliclic.free.fr/php/javascript-packer/en/
136 * Changes should be pushed back upstream.
139 public $ignoreCase = false;
140 public $escapeChar = '';
143 const EXPRESSION
= 0;
144 const REPLACEMENT
= 1;
147 // used to determine nesting levels
148 private $GROUPS = '/\( (?! \? ) /x';//g
149 private $SUB_REPLACE = '/\$\d/';
150 private $INDEXED = '/^\$\d+$/';
151 private $ESCAPE = '/\\\./';//g
152 private $QUOTE = '/\'/';
153 private $DELETED = '/\x01[^\x01]*\x01/';//g
155 public function add($expression, $replacement = '') {
156 // count the number of sub-expressions
157 // - add one because each pattern is itself a sub-expression
158 $length = 1 +
preg_match_all($this->GROUPS
, $this->_internalEscape((string)$expression), $out);
160 // treat only strings $replacement
161 if (is_string($replacement)) {
162 // does the pattern deal with sub-expressions?
163 if (preg_match($this->SUB_REPLACE
, $replacement)) {
164 // a simple lookup? (e.g. "$2")
165 if (preg_match($this->INDEXED
, $replacement)) {
166 // store the index (used for fast retrieval of matched strings)
167 $replacement = (int)(substr($replacement, 1)) - 1;
168 } else { // a complicated lookup (e.g. "Hello $2 $1")
169 // build a function to do the lookup
170 $quote = preg_match($this->QUOTE
, $this->_internalEscape($replacement))
172 $replacement = array(
173 'fn' => '_backReferences',
175 'replacement' => $replacement,
183 // pass the modified arguments
184 if (!empty($expression)) $this->_add($expression, $replacement, $length);
185 else $this->_add('/^$/', $replacement, $length);
188 public function exec($string) {
189 // execute the global replacement
190 $this->_escaped
= array();
192 // simulate the _patterns.toSTring of Dean
194 foreach ($this->_patterns
as $reg) {
195 $regexp .= '(' . $reg[self
::EXPRESSION
] . ")|\n";
197 $regexp = substr($regexp, 0, -2) . '/Sxs';
198 $regexp .= ($this->ignoreCase
) ?
'i' : '';
200 $string = $this->_escape($string, $this->escapeChar
);
201 $string = preg_replace_callback(
209 $string = $this->_unescape($string, $this->escapeChar
);
211 return preg_replace($this->DELETED
, '', $string);
214 public function reset() {
215 // clear the patterns collection so that this object may be re-used
216 $this->_patterns
= array();
220 private $_escaped = array(); // escaped characters
221 private $_patterns = array(); // patterns stored by index
223 // create and add a new pattern to the patterns collection
224 private function _add() {
225 $arguments = func_get_args();
226 $this->_patterns
[] = $arguments;
229 // this is the global replace function (it's quite complicated)
230 private function _replacement($arguments) {
231 if (empty($arguments)) return '';
234 // loop through the patterns
235 while (isset($this->_patterns
[$j])) {
236 $pattern = $this->_patterns
[$j++
];
237 // do we have a result?
238 if (isset($arguments[$i]) && ($arguments[$i] != '')) {
239 $replacement = $pattern[self
::REPLACEMENT
];
241 if (is_array($replacement) && isset($replacement['fn'])) {
243 if (isset($replacement['data'])) $this->buffer
= $replacement['data'];
244 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
246 } elseif (is_int($replacement)) {
247 return $arguments[$replacement +
$i];
250 $delete = ($this->escapeChar
== '' ||
251 strpos($arguments[$i], $this->escapeChar
) === false)
252 ?
'' : "\x01" . $arguments[$i] . "\x01";
253 return $delete . $replacement;
255 // skip over references to sub-expressions
257 $i +
= $pattern[self
::LENGTH
];
262 private function _backReferences($match, $offset) {
263 $replacement = $this->buffer
['replacement'];
264 //$quote = $this->buffer['quote'];
265 $i = $this->buffer
['length'];
267 $replacement = str_replace('$'.$i--, $match[$offset +
$i], $replacement);
272 private function _replace_name($match, $offset){
273 $length = strlen($match[$offset +
2]);
274 $start = $length - max($length - strlen($match[$offset +
3]), 0);
275 return substr($match[$offset +
1], $start, $length) . $match[$offset +
4];
278 private function _replace_encoded($match, $offset) {
279 return $this->buffer
[$match[$offset]];
283 // php : we cannot pass additional data to preg_replace_callback,
284 // and we cannot use &$this in create_function, so let's go to lower level
287 // encode escaped characters
288 private function _escape($string, $escapeChar) {
290 $this->buffer
= $escapeChar;
291 return preg_replace_callback(
292 '/\\' . $escapeChar . '(.)' .'/',
293 array(&$this, '_escapeBis'),
301 private function _escapeBis($match) {
302 $this->_escaped
[] = $match[1];
303 return $this->buffer
;
306 // decode escaped characters
307 private function _unescape($string, $escapeChar) {
309 $regexp = '/'.'\\'.$escapeChar.'/';
310 $this->buffer
= array('escapeChar'=> $escapeChar, 'i' => 0);
311 return preg_replace_callback
314 array(&$this, '_unescapeBis'),
322 private function _unescapeBis() {
323 if (isset($this->_escaped
[$this->buffer
['i']])
324 && $this->_escaped
[$this->buffer
['i']] != '')
326 $temp = $this->_escaped
[$this->buffer
['i']];
330 $this->buffer
['i']++
;
331 return $this->buffer
['escapeChar'] . $temp;
334 private function _internalEscape($string) {
335 return preg_replace($this->ESCAPE
, '', $string);