3e0a00f8fe6ded337a14d44be3fa463605bd1f33
[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::stripHorizontalSpace( $script );
23 // If requested, make some vertical whitespace collapsing as well
24 if ( $stripVerticalSpace ) {
25 $script = self::stripVerticalSpace( $script );
26 }
27 // Done
28 return $script;
29 }
30
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" );
41 // Execute and return
42 return $parser->exec($script);
43 }
44
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 );
59 }
60
61 /*
62 * Creates an instance of ParseMaster and protects sensitive JavaScript regions.
63 *
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.
70 */
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' );
85 // Remove comments
86 $parser->add( '/\\/\\*(.|[\\r\\n])*?\\*\\//' );
87 // Preserve the newline after a C++-style comment -- bug 27046
88 $parser->add( '/\\/\\/[^\\r\\n]*([\\r\\n])/', '$2' );
89 return $parser;
90 }
91 }
92
93 /**
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/
97 *
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.
100 *
101 * Original Source: http://joliclic.free.fr/php/javascript-packer/en/
102 *
103 * Changes should be pushed back upstream.
104 */
105 class ParseMaster {
106 public $ignoreCase = false;
107 public $escapeChar = '';
108
109 // constants
110 const EXPRESSION = 0;
111 const REPLACEMENT = 1;
112 const LENGTH = 2;
113
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
122
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);
127
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))
139 ? '"' : "'";
140 $replacement = array(
141 'fn' => '_backReferences',
142 'data' => array(
143 'replacement' => $replacement,
144 'length' => $length,
145 'quote' => $quote
146 )
147 );
148 }
149 }
150 }
151 // pass the modified arguments
152 if (!empty($expression)) $this->_add($expression, $replacement, $length);
153 else $this->_add('/^$/', $replacement, $length);
154 }
155
156 public function exec($string) {
157 // execute the global replacement
158 $this->_escaped = array();
159
160 // simulate the _patterns.toSTring of Dean
161 $regexp = '/';
162 foreach ($this->_patterns as $reg) {
163 $regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|';
164 }
165 $regexp = substr($regexp, 0, -1) . '/S';
166 $regexp .= ($this->ignoreCase) ? 'i' : '';
167
168 $string = $this->_escape($string, $this->escapeChar);
169 $string = preg_replace_callback(
170 $regexp,
171 array(
172 &$this,
173 '_replacement'
174 ),
175 $string
176 );
177 $string = $this->_unescape($string, $this->escapeChar);
178
179 return preg_replace($this->DELETED, '', $string);
180 }
181
182 public function reset() {
183 // clear the patterns collection so that this object may be re-used
184 $this->_patterns = array();
185 }
186
187 // private
188 private $_escaped = array(); // escaped characters
189 private $_patterns = array(); // patterns stored by index
190
191 // create and add a new pattern to the patterns collection
192 private function _add() {
193 $arguments = func_get_args();
194 $this->_patterns[] = $arguments;
195 }
196
197 // this is the global replace function (it's quite complicated)
198 private function _replacement($arguments) {
199 if (empty($arguments)) return '';
200
201 $i = 1; $j = 0;
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];
208
209 if (is_array($replacement) && isset($replacement['fn'])) {
210
211 if (isset($replacement['data'])) $this->buffer = $replacement['data'];
212 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
213
214 } elseif (is_int($replacement)) {
215 return $arguments[$replacement + $i];
216
217 }
218 $delete = ($this->escapeChar == '' ||
219 strpos($arguments[$i], $this->escapeChar) === false)
220 ? '' : "\x01" . $arguments[$i] . "\x01";
221 return $delete . $replacement;
222
223 // skip over references to sub-expressions
224 } else {
225 $i += $pattern[self::LENGTH];
226 }
227 }
228 }
229
230 private function _backReferences($match, $offset) {
231 $replacement = $this->buffer['replacement'];
232 $quote = $this->buffer['quote'];
233 $i = $this->buffer['length'];
234 while ($i) {
235 $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
236 }
237 return $replacement;
238 }
239
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];
244 }
245
246 private function _replace_encoded($match, $offset) {
247 return $this->buffer[$match[$offset]];
248 }
249
250
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
253 private $buffer;
254
255 // encode escaped characters
256 private function _escape($string, $escapeChar) {
257 if ($escapeChar) {
258 $this->buffer = $escapeChar;
259 return preg_replace_callback(
260 '/\\' . $escapeChar . '(.)' .'/',
261 array(&$this, '_escapeBis'),
262 $string
263 );
264
265 } else {
266 return $string;
267 }
268 }
269 private function _escapeBis($match) {
270 $this->_escaped[] = $match[1];
271 return $this->buffer;
272 }
273
274 // decode escaped characters
275 private function _unescape($string, $escapeChar) {
276 if ($escapeChar) {
277 $regexp = '/'.'\\'.$escapeChar.'/';
278 $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
279 return preg_replace_callback
280 (
281 $regexp,
282 array(&$this, '_unescapeBis'),
283 $string
284 );
285
286 } else {
287 return $string;
288 }
289 }
290 private function _unescapeBis() {
291 if (isset($this->_escaped[$this->buffer['i']])
292 && $this->_escaped[$this->buffer['i']] != '')
293 {
294 $temp = $this->_escaped[$this->buffer['i']];
295 } else {
296 $temp = '';
297 }
298 $this->buffer['i']++;
299 return $this->buffer['escapeChar'] . $temp;
300 }
301
302 private function _internalEscape($string) {
303 return preg_replace($this->ESCAPE, '', $string);
304 }
305 }