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 $script = self
::stripHorizontalSpace( $script );
23 // If requested, make some vertical whitespace collapsing as well
24 if ( $stripVerticalSpace ) {
25 $script = self
::stripVerticalSpace( $script );
31 private static function stripHorizontalSpace( $script ) {
32 $parser = self
::createParser();
33 // Collapse horizontal whitespaces between variable names into a single space
34 $parser->add( '/(\\b|\\$)[ \\t]+(\\b|\\$)/', '$2 $3' );
35 // Collapse horizontal whitespaces between unary operators into a single space
36 $parser->add( '/([+\\-])[ \\t]+([+\\-])/', '$2 $3' );
37 // Remove all remaining un-protected horizontal whitespace
38 $parser->add( '/[ \\t]+/');
39 // Collapse multiple vertical whitespaces with some horizontal spaces between them
40 $parser->add( '/[\\r\\n]+[ \\t]*[\\r\\n]+/', "\n" );
42 return $parser->exec($script);
45 private static function stripVerticalSpace( $script ) {
46 $parser = self
::createParser();
47 // Collapse whitespaces between and after a ){ pair (function definitions)
48 $parser->add( '/\\)\\s+\\{\\s+/', '){' );
49 // Collapse whitespaces between and after a ({ pair (JSON argument)
50 $parser->add( '/\\(\\s+\\{\\s+/', '({' );
51 // Collapse whitespaces between a parenthesis and a period (call chaining)
52 $parser->add( '/\\)\\s+\\./', ').');
53 // Collapse vertical whitespaces which come directly after a semicolon or a comma
54 $parser->add( '/([;,])\\s+/', '$2' );
55 // Collapse whitespaces between multiple parenthesis/brackets of similar direction
56 $parser->add( '/([\\)\\}])\\s+([\\)\\}])/', '$2$3' );
57 $parser->add( '/([\\(\\{])\\s+([\\(\\{])/', '$2$3' );
58 return $parser->exec( $script );
62 * Creates an instance of ParseMaster and protects sensitive JavaScript regions.
64 * This parser is based on regular expressions, which all get or'd together, so rules take
65 * precedence in the order they are added. We can use it to minify by armoring certain regions
66 * by matching them and replacing them with the full match, leaving the remaining regions around
67 * for further matching and replacing. When creating rules please note that because ParseMaster
68 * "or"s all of the rules together in a single pattern, encapsulating them in parenthesis, $1
69 * represents the whole match for a given rule, and $2 is the first submatch.
71 private static function createParser() {
72 $parser = new ParseMaster();
73 // There is a bug in ParseMaster that causes a backslash at the end of a line to be changed
74 // to \s if we use a backslash as the escape character. We work around this by using an
75 // obscure escape character that we hope will never appear at the end of a line.
76 $parser->escapeChar
= chr( 1 );
77 // Protect strings. The original code had [^\'\\v] here, but that didn't armor multiline
78 // strings correctly. This also armors multiline strings that don't have backslashes at the
79 // end of the line (these are invalid), but that's fine because we're just armoring here.
80 $parser->add( '/\'[^\']*\'/', '$1' );
81 $parser->add( '/"[^"]*"/', '$1' );
82 // Protect regular expressions
83 $parser->add( '/[ \\t]+(\\/[^\\/\\r\\n\\*][^\\/\\r\\n]*\\/g?i?)/', '$2' );
84 $parser->add( '/[^\\w\\$\\/\'"*)\\?:]\\/[^\\/\\r\\n\\*][^\\/\\r\\n]*\\/g?i?/', '$1' );
86 $parser->add( '/\\/\\*(.|[\\r\\n])*?\\*\\//' );
87 // Preserve the newline after a C++-style comment -- bug 27046
88 $parser->add( '/\\/\\/[^\\r\\n]*([\\r\\n])/', '$2' );
94 * ParseMaster, version 1.0.2 (2005-08-19) Copyright 2005, Dean Edwards
95 * A multi-pattern parser.
96 * License: http://creativecommons.org/licenses/LGPL/2.1/
98 * This is the PHP version of the ParseMaster component of Dean Edwards' (http://dean.edwards.name/)
99 * Packer, which was originally written in JavaScript. It was ported to PHP by Nicolas Martin.
101 * Original Source: http://joliclic.free.fr/php/javascript-packer/en/
103 * Changes should be pushed back upstream.
106 public $ignoreCase = false;
107 public $escapeChar = '';
110 const EXPRESSION
= 0;
111 const REPLACEMENT
= 1;
114 // used to determine nesting levels
115 private $GROUPS = '/\\(/';//g
116 private $SUB_REPLACE = '/\\$\\d/';
117 private $INDEXED = '/^\\$\\d+$/';
118 private $TRIM = '/([\'"])\\1\\.(.*)\\.\\1\\1$/';
119 private $ESCAPE = '/\\\./';//g
120 private $QUOTE = '/\'/';
121 private $DELETED = '/\\x01[^\\x01]*\\x01/';//g
123 public function add($expression, $replacement = '') {
124 // count the number of sub-expressions
125 // - add one because each pattern is itself a sub-expression
126 $length = 1 +
preg_match_all($this->GROUPS
, $this->_internalEscape((string)$expression), $out);
128 // treat only strings $replacement
129 if (is_string($replacement)) {
130 // does the pattern deal with sub-expressions?
131 if (preg_match($this->SUB_REPLACE
, $replacement)) {
132 // a simple lookup? (e.g. "$2")
133 if (preg_match($this->INDEXED
, $replacement)) {
134 // store the index (used for fast retrieval of matched strings)
135 $replacement = (int)(substr($replacement, 1)) - 1;
136 } else { // a complicated lookup (e.g. "Hello $2 $1")
137 // build a function to do the lookup
138 $quote = preg_match($this->QUOTE
, $this->_internalEscape($replacement))
140 $replacement = array(
141 'fn' => '_backReferences',
143 'replacement' => $replacement,
151 // pass the modified arguments
152 if (!empty($expression)) $this->_add($expression, $replacement, $length);
153 else $this->_add('/^$/', $replacement, $length);
156 public function exec($string) {
157 // execute the global replacement
158 $this->_escaped
= array();
160 // simulate the _patterns.toSTring of Dean
162 foreach ($this->_patterns
as $reg) {
163 $regexp .= '(' . substr($reg[self
::EXPRESSION
], 1, -1) . ')|';
165 $regexp = substr($regexp, 0, -1) . '/S';
166 $regexp .= ($this->ignoreCase
) ?
'i' : '';
168 $string = $this->_escape($string, $this->escapeChar
);
169 $string = preg_replace_callback(
177 $string = $this->_unescape($string, $this->escapeChar
);
179 return preg_replace($this->DELETED
, '', $string);
182 public function reset() {
183 // clear the patterns collection so that this object may be re-used
184 $this->_patterns
= array();
188 private $_escaped = array(); // escaped characters
189 private $_patterns = array(); // patterns stored by index
191 // create and add a new pattern to the patterns collection
192 private function _add() {
193 $arguments = func_get_args();
194 $this->_patterns
[] = $arguments;
197 // this is the global replace function (it's quite complicated)
198 private function _replacement($arguments) {
199 if (empty($arguments)) return '';
202 // loop through the patterns
203 while (isset($this->_patterns
[$j])) {
204 $pattern = $this->_patterns
[$j++
];
205 // do we have a result?
206 if (isset($arguments[$i]) && ($arguments[$i] != '')) {
207 $replacement = $pattern[self
::REPLACEMENT
];
209 if (is_array($replacement) && isset($replacement['fn'])) {
211 if (isset($replacement['data'])) $this->buffer
= $replacement['data'];
212 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
214 } elseif (is_int($replacement)) {
215 return $arguments[$replacement +
$i];
218 $delete = ($this->escapeChar
== '' ||
219 strpos($arguments[$i], $this->escapeChar
) === false)
220 ?
'' : "\x01" . $arguments[$i] . "\x01";
221 return $delete . $replacement;
223 // skip over references to sub-expressions
225 $i +
= $pattern[self
::LENGTH
];
230 private function _backReferences($match, $offset) {
231 $replacement = $this->buffer
['replacement'];
232 $quote = $this->buffer
['quote'];
233 $i = $this->buffer
['length'];
235 $replacement = str_replace('$'.$i--, $match[$offset +
$i], $replacement);
240 private function _replace_name($match, $offset){
241 $length = strlen($match[$offset +
2]);
242 $start = $length - max($length - strlen($match[$offset +
3]), 0);
243 return substr($match[$offset +
1], $start, $length) . $match[$offset +
4];
246 private function _replace_encoded($match, $offset) {
247 return $this->buffer
[$match[$offset]];
251 // php : we cannot pass additional data to preg_replace_callback,
252 // and we cannot use &$this in create_function, so let's go to lower level
255 // encode escaped characters
256 private function _escape($string, $escapeChar) {
258 $this->buffer
= $escapeChar;
259 return preg_replace_callback(
260 '/\\' . $escapeChar . '(.)' .'/',
261 array(&$this, '_escapeBis'),
269 private function _escapeBis($match) {
270 $this->_escaped
[] = $match[1];
271 return $this->buffer
;
274 // decode escaped characters
275 private function _unescape($string, $escapeChar) {
277 $regexp = '/'.'\\'.$escapeChar.'/';
278 $this->buffer
= array('escapeChar'=> $escapeChar, 'i' => 0);
279 return preg_replace_callback
282 array(&$this, '_unescapeBis'),
290 private function _unescapeBis() {
291 if (isset($this->_escaped
[$this->buffer
['i']])
292 && $this->_escaped
[$this->buffer
['i']] != '')
294 $temp = $this->_escaped
[$this->buffer
['i']];
298 $this->buffer
['i']++
;
299 return $this->buffer
['escapeChar'] . $temp;
302 private function _internalEscape($string) {
303 return preg_replace($this->ESCAPE
, '', $string);