Fix r80666: botched variable rename
[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 $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 );
27 }
28 // Done
29 return $script;
30 }
31
32 private static function stripComments( $script ) {
33 $parser = self::createParser();
34 // Remove comments
35 $parser->add( '/\\/\\/[^\\r\\n]*[\\r\\n]/' );
36 $parser->add( '/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//' );
37 // Execute and return
38 return $parser->exec( $script );
39 }
40
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" );
51 // Execute and return
52 return $parser->exec($script);
53 }
54
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 );
69 }
70
71 /*
72 * Creates an instance of ParseMaster and protects sensitive JavaScript regions.
73 *
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.
80 */
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' );
95 return $parser;
96 }
97 }
98
99 /**
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/
103 *
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.
106 *
107 * Original Source: http://joliclic.free.fr/php/javascript-packer/en/
108 *
109 * Changes should be pushed back upstream.
110 */
111 class ParseMaster {
112 public $ignoreCase = false;
113 public $escapeChar = '';
114
115 // constants
116 const EXPRESSION = 0;
117 const REPLACEMENT = 1;
118 const LENGTH = 2;
119
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
128
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);
133
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))
145 ? '"' : "'";
146 $replacement = array(
147 'fn' => '_backReferences',
148 'data' => array(
149 'replacement' => $replacement,
150 'length' => $length,
151 'quote' => $quote
152 )
153 );
154 }
155 }
156 }
157 // pass the modified arguments
158 if (!empty($expression)) $this->_add($expression, $replacement, $length);
159 else $this->_add('/^$/', $replacement, $length);
160 }
161
162 public function exec($string) {
163 // execute the global replacement
164 $this->_escaped = array();
165
166 // simulate the _patterns.toSTring of Dean
167 $regexp = '/';
168 foreach ($this->_patterns as $reg) {
169 $regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|';
170 }
171 $regexp = substr($regexp, 0, -1) . '/';
172 $regexp .= ($this->ignoreCase) ? 'i' : '';
173
174 $string = $this->_escape($string, $this->escapeChar);
175 $string = preg_replace_callback(
176 $regexp,
177 array(
178 &$this,
179 '_replacement'
180 ),
181 $string
182 );
183 $string = $this->_unescape($string, $this->escapeChar);
184
185 return preg_replace($this->DELETED, '', $string);
186 }
187
188 public function reset() {
189 // clear the patterns collection so that this object may be re-used
190 $this->_patterns = array();
191 }
192
193 // private
194 private $_escaped = array(); // escaped characters
195 private $_patterns = array(); // patterns stored by index
196
197 // create and add a new pattern to the patterns collection
198 private function _add() {
199 $arguments = func_get_args();
200 $this->_patterns[] = $arguments;
201 }
202
203 // this is the global replace function (it's quite complicated)
204 private function _replacement($arguments) {
205 if (empty($arguments)) return '';
206
207 $i = 1; $j = 0;
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];
214
215 if (is_array($replacement) && isset($replacement['fn'])) {
216
217 if (isset($replacement['data'])) $this->buffer = $replacement['data'];
218 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
219
220 } elseif (is_int($replacement)) {
221 return $arguments[$replacement + $i];
222
223 }
224 $delete = ($this->escapeChar == '' ||
225 strpos($arguments[$i], $this->escapeChar) === false)
226 ? '' : "\x01" . $arguments[$i] . "\x01";
227 return $delete . $replacement;
228
229 // skip over references to sub-expressions
230 } else {
231 $i += $pattern[self::LENGTH];
232 }
233 }
234 }
235
236 private function _backReferences($match, $offset) {
237 $replacement = $this->buffer['replacement'];
238 $quote = $this->buffer['quote'];
239 $i = $this->buffer['length'];
240 while ($i) {
241 $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
242 }
243 return $replacement;
244 }
245
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];
250 }
251
252 private function _replace_encoded($match, $offset) {
253 return $this->buffer[$match[$offset]];
254 }
255
256
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
259 private $buffer;
260
261 // encode escaped characters
262 private function _escape($string, $escapeChar) {
263 if ($escapeChar) {
264 $this->buffer = $escapeChar;
265 return preg_replace_callback(
266 '/\\' . $escapeChar . '(.)' .'/',
267 array(&$this, '_escapeBis'),
268 $string
269 );
270
271 } else {
272 return $string;
273 }
274 }
275 private function _escapeBis($match) {
276 $this->_escaped[] = $match[1];
277 return $this->buffer;
278 }
279
280 // decode escaped characters
281 private function _unescape($string, $escapeChar) {
282 if ($escapeChar) {
283 $regexp = '/'.'\\'.$escapeChar.'/';
284 $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
285 return preg_replace_callback
286 (
287 $regexp,
288 array(&$this, '_unescapeBis'),
289 $string
290 );
291
292 } else {
293 return $string;
294 }
295 }
296 private function _unescapeBis() {
297 if (isset($this->_escaped[$this->buffer['i']])
298 && $this->_escaped[$this->buffer['i']] != '')
299 {
300 $temp = $this->_escaped[$this->buffer['i']];
301 } else {
302 $temp = '';
303 }
304 $this->buffer['i']++;
305 return $this->buffer['escapeChar'] . $temp;
306 }
307
308 private function _internalEscape($string) {
309 return preg_replace($this->ESCAPE, '', $string);
310 }
311 }