Followup r83891: don't insert a newline before ++ or -- . Patch by Paul Copperman
[lhc/web/wiklou.git] / includes / libs / JavaScriptDistiller.php
1 <?php
2 /**
3 * JavaScript Distiller
4 *
5 * Author: Dean Edwards, Nicholas Martin, Trevor Parscal
6 * License: LGPL
7 */
8 class JavaScriptDistiller {
9
10 /* Static Methods */
11
12 /**
13 * Removes most of the white-space from JavaScript code.
14 *
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.
17 *
18 * @param $script String: JavaScript code to minify
19 * @param $stripVerticalSpace Boolean: Try to remove as much vertical whitespace as possible
20 */
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' );
27 }
28
29 $script = self::stripHorizontalSpace( $script );
30 // If requested, make some vertical whitespace collapsing as well
31 if ( $stripVerticalSpace ) {
32 $script = self::stripVerticalSpace( $script );
33 }
34 // Done
35 ini_set( 'pcre.recursion_limit', $oldLimit );
36 return $script;
37 }
38
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" );
49 // Execute and return
50 return $parser->exec($script);
51 }
52
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 );
67 }
68
69 /*
70 * Creates an instance of ParseMaster and protects sensitive JavaScript regions.
71 *
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.
78 */
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 );
85
86 // C-style comment: use non-greedy repetition to find the end
87 $parser->add( '\/ \* .*? \* \/' );
88
89 // Preserve the newline after a C++-style comment -- bug 27046
90 $parser->add( '\/ \/ [^\r\n]* ( [\r\n] )', '$2' );
91
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.
95
96 // Single quotes
97 $parser->add(
98 '\'' . // start quote
99 '[^\'\\\\]*' . // a run of non-special characters
100 '(?:' .
101 '\\\\ .' . // a backslash followed by any character
102 '[^\'\\\\]*' . // a run of non-special characters
103 ')*' . // any number of the above
104 '\'', // end quote
105 '$1' );
106
107 // Double quotes: same as above
108 $parser->add( '" [^"\\\\]* (?: \\\\ . [^"\\\\]* )* "', '$1' );
109
110 // Protect regular expressions
111 $parser->add(
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
116 '(?:' .
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
121 '$1' );
122 return $parser;
123 }
124 }
125
126 /**
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/
130 *
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.
133 *
134 * Original Source: http://joliclic.free.fr/php/javascript-packer/en/
135 *
136 * Changes should be pushed back upstream.
137 */
138 class ParseMaster {
139 public $ignoreCase = false;
140 public $escapeChar = '';
141
142 // constants
143 const EXPRESSION = 0;
144 const REPLACEMENT = 1;
145 const LENGTH = 2;
146
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
154
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);
159
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))
171 ? '"' : "'";
172 $replacement = array(
173 'fn' => '_backReferences',
174 'data' => array(
175 'replacement' => $replacement,
176 'length' => $length,
177 'quote' => $quote
178 )
179 );
180 }
181 }
182 }
183 // pass the modified arguments
184 if (!empty($expression)) $this->_add($expression, $replacement, $length);
185 else $this->_add('/^$/', $replacement, $length);
186 }
187
188 public function exec($string) {
189 // execute the global replacement
190 $this->_escaped = array();
191
192 // simulate the _patterns.toSTring of Dean
193 $regexp = '/';
194 foreach ($this->_patterns as $reg) {
195 $regexp .= '(' . $reg[self::EXPRESSION] . ")|\n";
196 }
197 $regexp = substr($regexp, 0, -2) . '/Sxs';
198 $regexp .= ($this->ignoreCase) ? 'i' : '';
199
200 $string = $this->_escape($string, $this->escapeChar);
201 $string = preg_replace_callback(
202 $regexp,
203 array(
204 &$this,
205 '_replacement'
206 ),
207 $string
208 );
209 $string = $this->_unescape($string, $this->escapeChar);
210
211 return preg_replace($this->DELETED, '', $string);
212 }
213
214 public function reset() {
215 // clear the patterns collection so that this object may be re-used
216 $this->_patterns = array();
217 }
218
219 // private
220 private $_escaped = array(); // escaped characters
221 private $_patterns = array(); // patterns stored by index
222
223 // create and add a new pattern to the patterns collection
224 private function _add() {
225 $arguments = func_get_args();
226 $this->_patterns[] = $arguments;
227 }
228
229 // this is the global replace function (it's quite complicated)
230 private function _replacement($arguments) {
231 if (empty($arguments)) return '';
232
233 $i = 1; $j = 0;
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];
240
241 if (is_array($replacement) && isset($replacement['fn'])) {
242
243 if (isset($replacement['data'])) $this->buffer = $replacement['data'];
244 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
245
246 } elseif (is_int($replacement)) {
247 return $arguments[$replacement + $i];
248
249 }
250 $delete = ($this->escapeChar == '' ||
251 strpos($arguments[$i], $this->escapeChar) === false)
252 ? '' : "\x01" . $arguments[$i] . "\x01";
253 return $delete . $replacement;
254
255 // skip over references to sub-expressions
256 } else {
257 $i += $pattern[self::LENGTH];
258 }
259 }
260 }
261
262 private function _backReferences($match, $offset) {
263 $replacement = $this->buffer['replacement'];
264 //$quote = $this->buffer['quote'];
265 $i = $this->buffer['length'];
266 while ($i) {
267 $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
268 }
269 return $replacement;
270 }
271
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];
276 }
277
278 private function _replace_encoded($match, $offset) {
279 return $this->buffer[$match[$offset]];
280 }
281
282
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
285 private $buffer;
286
287 // encode escaped characters
288 private function _escape($string, $escapeChar) {
289 if ($escapeChar) {
290 $this->buffer = $escapeChar;
291 return preg_replace_callback(
292 '/\\' . $escapeChar . '(.)' .'/',
293 array(&$this, '_escapeBis'),
294 $string
295 );
296
297 } else {
298 return $string;
299 }
300 }
301 private function _escapeBis($match) {
302 $this->_escaped[] = $match[1];
303 return $this->buffer;
304 }
305
306 // decode escaped characters
307 private function _unescape($string, $escapeChar) {
308 if ($escapeChar) {
309 $regexp = '/'.'\\'.$escapeChar.'/';
310 $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
311 return preg_replace_callback
312 (
313 $regexp,
314 array(&$this, '_unescapeBis'),
315 $string
316 );
317
318 } else {
319 return $string;
320 }
321 }
322 private function _unescapeBis() {
323 if (isset($this->_escaped[$this->buffer['i']])
324 && $this->_escaped[$this->buffer['i']] != '')
325 {
326 $temp = $this->_escaped[$this->buffer['i']];
327 } else {
328 $temp = '';
329 }
330 $this->buffer['i']++;
331 return $this->buffer['escapeChar'] . $temp;
332 }
333
334 private function _internalEscape($string) {
335 return preg_replace($this->ESCAPE, '', $string);
336 }
337 }