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
::stripComments( $script );
23 $script = self
::stripHorizontalSpace( $script );
24 // If requested, make some vertical whitespace collapsing as well
25 if ( $stripVerticalSpace ) {
26 $script = self
::stripVerticalSpace( $script );
32 private static function stripComments( $script ) {
33 $parser = self
::createParser();
35 $parser->add( '/\\/\\/[^\\r\\n]*[\\r\\n]/' );
36 $parser->add( '/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//' );
38 return $parser->exec( $script );
41 private static function stripHorizontalSpace( $script ) {
42 $parser = self
::createParser();
43 // Collapse horizontal whitespaces between variable names into a single space
44 $parser->add( '/(\\b|\\$)[ \\t]+(\\b|\\$)/', '$2 $3' );
45 // Collapse horizontal whitespaces between unary operators into a single space
46 $parser->add( '/([+\\-])[ \\t]+([+\\-])/', '$2 $3' );
47 // Remove all remaining un-protected horizontal whitespace
48 $parser->add( '/[ \\t]+/');
49 // Collapse multiple vertical whitespaces with some horizontal spaces between them
50 $parser->add( '/[\\r\\n]+[ \\t]*[\\r\\n]+/', "\n" );
52 return $parser->exec($script);
55 private static function stripVerticalSpace( $script ) {
56 $parser = self
::createParser();
57 // Collapse whitespaces between and after a ){ pair (function definitions)
58 $parser->add( '/\\)\\s+\\{\\s+/', '){' );
59 // Collapse whitespaces between and after a ({ pair (JSON argument)
60 $parser->add( '/\\(\\s+\\{\\s+/', '({' );
61 // Collapse whitespaces between a parenthesis and a period (call chaining)
62 $parser->add( '/\\)\\s+\\./', ').');
63 // Collapse vertical whitespaces which come directly after a semicolon or a comma
64 $parser->add( '/([;,])\\s+/', '$2' );
65 // Collapse whitespaces between multiple parenthesis/brackets of similar direction
66 $parser->add( '/([\\)\\}])\\s+([\\)\\}])/', '$2$3' );
67 $parser->add( '/([\\(\\{])\\s+([\\(\\{])/', '$2$3' );
68 return $parser->exec( $script );
72 * Creates an instance of ParseMaster and protects sensitive JavaScript regions.
74 * This parser is based on regular expressions, which all get or'd together, so rules take
75 * precedence in the order they are added. We can use it to minify by armoring certain regions
76 * by matching them and replacing them with the full match, leaving the remaining regions around
77 * for further matching and replacing. When creating rules please note that because ParseMaster
78 * "or"s all of the rules together in a single pattern, encapsulating them in parenthesis, $1
79 * represents the whole match for a given rule, and $2 is the first submatch.
81 private static function createParser() {
82 $parser = new ParseMaster();
83 // There is a bug in ParseMaster that causes a backslash at the end of a line to be changed
84 // to \s if we use a backslash as the escape character. We work around this by using an
85 // obscure escape character that we hope will never appear at the end of a line.
86 $parser->escapeChar
= chr( 1 );
87 // Protect strings. The original code had [^\'\\v] here, but that didn't armor multiline
88 // strings correctly. This also armors multiline strings that don't have backslashes at the
89 // end of the line (these are invalid), but that's fine because we're just armoring here.
90 $parser->add( '/\'[^\']*\'/', '$1' );
91 $parser->add( '/"[^"]*"/', '$1' );
92 // Protect regular expressions
93 $parser->add( '/[ \\t]+(\\/[^\\/\\r\\n\\*][^\\/\\r\\n]*\\/g?i?)/', '$2' );
94 $parser->add( '/[^\\w\\$\\/\'"*)\\?:]\\/[^\\/\\r\\n\\*][^\\/\\r\\n]*\\/g?i?/', '$1' );
100 * ParseMaster, version 1.0.2 (2005-08-19) Copyright 2005, Dean Edwards
101 * A multi-pattern parser.
102 * License: http://creativecommons.org/licenses/LGPL/2.1/
104 * This is the PHP version of the ParseMaster component of Dean Edwards' (http://dean.edwards.name/)
105 * Packer, which was originally written in JavaScript. It was ported to PHP by Nicolas Martin.
107 * Original Source: http://joliclic.free.fr/php/javascript-packer/en/
109 * Changes should be pushed back upstream.
112 public $ignoreCase = false;
113 public $escapeChar = '';
116 const EXPRESSION
= 0;
117 const REPLACEMENT
= 1;
120 // used to determine nesting levels
121 private $GROUPS = '/\\(/';//g
122 private $SUB_REPLACE = '/\\$\\d/';
123 private $INDEXED = '/^\\$\\d+$/';
124 private $TRIM = '/([\'"])\\1\\.(.*)\\.\\1\\1$/';
125 private $ESCAPE = '/\\\./';//g
126 private $QUOTE = '/\'/';
127 private $DELETED = '/\\x01[^\\x01]*\\x01/';//g
129 public function add($expression, $replacement = '') {
130 // count the number of sub-expressions
131 // - add one because each pattern is itself a sub-expression
132 $length = 1 +
preg_match_all($this->GROUPS
, $this->_internalEscape((string)$expression), $out);
134 // treat only strings $replacement
135 if (is_string($replacement)) {
136 // does the pattern deal with sub-expressions?
137 if (preg_match($this->SUB_REPLACE
, $replacement)) {
138 // a simple lookup? (e.g. "$2")
139 if (preg_match($this->INDEXED
, $replacement)) {
140 // store the index (used for fast retrieval of matched strings)
141 $replacement = (int)(substr($replacement, 1)) - 1;
142 } else { // a complicated lookup (e.g. "Hello $2 $1")
143 // build a function to do the lookup
144 $quote = preg_match($this->QUOTE
, $this->_internalEscape($replacement))
146 $replacement = array(
147 'fn' => '_backReferences',
149 'replacement' => $replacement,
157 // pass the modified arguments
158 if (!empty($expression)) $this->_add($expression, $replacement, $length);
159 else $this->_add('/^$/', $replacement, $length);
162 public function exec($string) {
163 // execute the global replacement
164 $this->_escaped
= array();
166 // simulate the _patterns.toSTring of Dean
168 foreach ($this->_patterns
as $reg) {
169 $regexp .= '(' . substr($reg[self
::EXPRESSION
], 1, -1) . ')|';
171 $regexp = substr($regexp, 0, -1) . '/';
172 $regexp .= ($this->ignoreCase
) ?
'i' : '';
174 $string = $this->_escape($string, $this->escapeChar
);
175 $string = preg_replace_callback(
183 $string = $this->_unescape($string, $this->escapeChar
);
185 return preg_replace($this->DELETED
, '', $string);
188 public function reset() {
189 // clear the patterns collection so that this object may be re-used
190 $this->_patterns
= array();
194 private $_escaped = array(); // escaped characters
195 private $_patterns = array(); // patterns stored by index
197 // create and add a new pattern to the patterns collection
198 private function _add() {
199 $arguments = func_get_args();
200 $this->_patterns
[] = $arguments;
203 // this is the global replace function (it's quite complicated)
204 private function _replacement($arguments) {
205 if (empty($arguments)) return '';
208 // loop through the patterns
209 while (isset($this->_patterns
[$j])) {
210 $pattern = $this->_patterns
[$j++
];
211 // do we have a result?
212 if (isset($arguments[$i]) && ($arguments[$i] != '')) {
213 $replacement = $pattern[self
::REPLACEMENT
];
215 if (is_array($replacement) && isset($replacement['fn'])) {
217 if (isset($replacement['data'])) $this->buffer
= $replacement['data'];
218 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
220 } elseif (is_int($replacement)) {
221 return $arguments[$replacement +
$i];
224 $delete = ($this->escapeChar
== '' ||
225 strpos($arguments[$i], $this->escapeChar
) === false)
226 ?
'' : "\x01" . $arguments[$i] . "\x01";
227 return $delete . $replacement;
229 // skip over references to sub-expressions
231 $i +
= $pattern[self
::LENGTH
];
236 private function _backReferences($match, $offset) {
237 $replacement = $this->buffer
['replacement'];
238 $quote = $this->buffer
['quote'];
239 $i = $this->buffer
['length'];
241 $replacement = str_replace('$'.$i--, $match[$offset +
$i], $replacement);
246 private function _replace_name($match, $offset){
247 $length = strlen($match[$offset +
2]);
248 $start = $length - max($length - strlen($match[$offset +
3]), 0);
249 return substr($match[$offset +
1], $start, $length) . $match[$offset +
4];
252 private function _replace_encoded($match, $offset) {
253 return $this->buffer
[$match[$offset]];
257 // php : we cannot pass additional data to preg_replace_callback,
258 // and we cannot use &$this in create_function, so let's go to lower level
261 // encode escaped characters
262 private function _escape($string, $escapeChar) {
264 $this->buffer
= $escapeChar;
265 return preg_replace_callback(
266 '/\\' . $escapeChar . '(.)' .'/',
267 array(&$this, '_escapeBis'),
275 private function _escapeBis($match) {
276 $this->_escaped
[] = $match[1];
277 return $this->buffer
;
280 // decode escaped characters
281 private function _unescape($string, $escapeChar) {
283 $regexp = '/'.'\\'.$escapeChar.'/';
284 $this->buffer
= array('escapeChar'=> $escapeChar, 'i' => 0);
285 return preg_replace_callback
288 array(&$this, '_unescapeBis'),
296 private function _unescapeBis() {
297 if (isset($this->_escaped
[$this->buffer
['i']])
298 && $this->_escaped
[$this->buffer
['i']] != '')
300 $temp = $this->_escaped
[$this->buffer
['i']];
304 $this->buffer
['i']++
;
305 return $this->buffer
['escapeChar'] . $temp;
308 private function _internalEscape($string) {
309 return preg_replace($this->ESCAPE
, '', $string);