latest changes requested to remove ID number and date of birth
[namibia] / public / min / lib / JSMin.php
1 <?php
2 /**
3  * JSMin.php - modified PHP implementation of Douglas Crockford's JSMin.
4  *
5  * <code>
6  * $minifiedJs = JSMin::minify($js);
7  * </code>
8  *
9  * This is a modified port of jsmin.c. Improvements:
10  *
11  * Does not choke on some regexp literals containing quote characters. E.g. /'/
12  *
13  * Spaces are preserved after some add/sub operators, so they are not mistakenly
14  * converted to post-inc/dec. E.g. a + ++b -> a+ ++b
15  *
16  * Preserves multi-line comments that begin with /*!
17  *
18  * PHP 5 or higher is required.
19  *
20  * Permission is hereby granted to use this version of the library under the
21  * same terms as jsmin.c, which has the following license:
22  *
23  * --
24  * Copyright (c) 2002 Douglas Crockford  (www.crockford.com)
25  *
26  * Permission is hereby granted, free of charge, to any person obtaining a copy of
27  * this software and associated documentation files (the "Software"), to deal in
28  * the Software without restriction, including without limitation the rights to
29  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
30  * of the Software, and to permit persons to whom the Software is furnished to do
31  * so, subject to the following conditions:
32  *
33  * The above copyright notice and this permission notice shall be included in all
34  * copies or substantial portions of the Software.
35  *
36  * The Software shall be used for Good, not Evil.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44  * SOFTWARE.
45  * --
46  *
47  * @package JSMin
48  * @author Ryan Grove <ryan@wonko.com> (PHP port)
49  * @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
50  * @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
51  * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
52  * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
53  * @license http://opensource.org/licenses/mit-license.php MIT License
54  * @link http://code.google.com/p/jsmin-php/
55  */
56
57 class JSMin {
58     const ORD_LF            = 10;
59     const ORD_SPACE         = 32;
60     const ACTION_KEEP_A     = 1;
61     const ACTION_DELETE_A   = 2;
62     const ACTION_DELETE_A_B = 3;
63
64     protected $a           = "\n";
65     protected $b           = '';
66     protected $input       = '';
67     protected $inputIndex  = 0;
68     protected $inputLength = 0;
69     protected $lookAhead   = null;
70     protected $output      = '';
71     protected $lastByteOut  = '';
72     protected $keptComment = '';
73
74     /**
75      * Minify Javascript.
76      *
77      * @param string $js Javascript to be minified
78      *
79      * @return string
80      */
81     public static function minify($js)
82     {
83         $jsmin = new JSMin($js);
84         return $jsmin->min();
85     }
86
87     /**
88      * @param string $input
89      */
90     public function __construct($input)
91     {
92         $this->input = $input;
93     }
94
95     /**
96      * Perform minification, return result
97      *
98      * @return string
99      */
100     public function min()
101     {
102         if ($this->output !== '') { // min already run
103             return $this->output;
104         }
105
106         $mbIntEnc = null;
107         if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
108             $mbIntEnc = mb_internal_encoding();
109             mb_internal_encoding('8bit');
110         }
111         $this->input = str_replace("\r\n", "\n", $this->input);
112         $this->inputLength = strlen($this->input);
113
114         $this->action(self::ACTION_DELETE_A_B);
115
116         while ($this->a !== null) {
117             // determine next command
118             $command = self::ACTION_KEEP_A; // default
119             if ($this->a === ' ') {
120                 if (($this->lastByteOut === '+' || $this->lastByteOut === '-')
121                         && ($this->b === $this->lastByteOut)) {
122                     // Don't delete this space. If we do, the addition/subtraction
123                     // could be parsed as a post-increment
124                 } elseif (! $this->isAlphaNum($this->b)) {
125                     $command = self::ACTION_DELETE_A;
126                 }
127             } elseif ($this->a === "\n") {
128                 if ($this->b === ' ') {
129                     $command = self::ACTION_DELETE_A_B;
130
131                     // in case of mbstring.func_overload & 2, must check for null b,
132                     // otherwise mb_strpos will give WARNING
133                 } elseif ($this->b === null
134                           || (false === strpos('{[(+-!~', $this->b)
135                               && ! $this->isAlphaNum($this->b))) {
136                     $command = self::ACTION_DELETE_A;
137                 }
138             } elseif (! $this->isAlphaNum($this->a)) {
139                 if ($this->b === ' '
140                     || ($this->b === "\n"
141                         && (false === strpos('}])+-"\'', $this->a)))) {
142                     $command = self::ACTION_DELETE_A_B;
143                 }
144             }
145             $this->action($command);
146         }
147         $this->output = trim($this->output);
148
149         if ($mbIntEnc !== null) {
150             mb_internal_encoding($mbIntEnc);
151         }
152         return $this->output;
153     }
154
155     /**
156      * ACTION_KEEP_A = Output A. Copy B to A. Get the next B.
157      * ACTION_DELETE_A = Copy B to A. Get the next B.
158      * ACTION_DELETE_A_B = Get the next B.
159      *
160      * @param int $command
161      * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException
162      */
163     protected function action($command)
164     {
165         // make sure we don't compress "a + ++b" to "a+++b", etc.
166         if ($command === self::ACTION_DELETE_A_B
167             && $this->b === ' '
168             && ($this->a === '+' || $this->a === '-')) {
169             // Note: we're at an addition/substraction operator; the inputIndex
170             // will certainly be a valid index
171             if ($this->input[$this->inputIndex] === $this->a) {
172                 // This is "+ +" or "- -". Don't delete the space.
173                 $command = self::ACTION_KEEP_A;
174             }
175         }
176
177         switch ($command) {
178             case self::ACTION_KEEP_A: // 1
179                 $this->output .= $this->a;
180
181                 if ($this->keptComment) {
182                     $this->output = rtrim($this->output, "\n");
183                     $this->output .= $this->keptComment;
184                     $this->keptComment = '';
185                 }
186
187                 $this->lastByteOut = $this->a;
188
189                 // fallthrough intentional
190             case self::ACTION_DELETE_A: // 2
191                 $this->a = $this->b;
192                 if ($this->a === "'" || $this->a === '"') { // string literal
193                     $str = $this->a; // in case needed for exception
194                     for(;;) {
195                         $this->output .= $this->a;
196                         $this->lastByteOut = $this->a;
197
198                         $this->a = $this->get();
199                         if ($this->a === $this->b) { // end quote
200                             break;
201                         }
202                         if ($this->isEOF($this->a)) {
203                             throw new JSMin_UnterminatedStringException(
204                                 "JSMin: Unterminated String at byte {$this->inputIndex}: {$str}");
205                         }
206                         $str .= $this->a;
207                         if ($this->a === '\\') {
208                             $this->output .= $this->a;
209                             $this->lastByteOut = $this->a;
210
211                             $this->a       = $this->get();
212                             $str .= $this->a;
213                         }
214                     }
215                 }
216
217                 // fallthrough intentional
218             case self::ACTION_DELETE_A_B: // 3
219                 $this->b = $this->next();
220                 if ($this->b === '/' && $this->isRegexpLiteral()) {
221                     $this->output .= $this->a . $this->b;
222                     $pattern = '/'; // keep entire pattern in case we need to report it in the exception
223                     for(;;) {
224                         $this->a = $this->get();
225                         $pattern .= $this->a;
226                         if ($this->a === '[') {
227                             for(;;) {
228                                 $this->output .= $this->a;
229                                 $this->a = $this->get();
230                                 $pattern .= $this->a;
231                                 if ($this->a === ']') {
232                                     break;
233                                 }
234                                 if ($this->a === '\\') {
235                                     $this->output .= $this->a;
236                                     $this->a = $this->get();
237                                     $pattern .= $this->a;
238                                 }
239                                 if ($this->isEOF($this->a)) {
240                                     throw new JSMin_UnterminatedRegExpException(
241                                         "JSMin: Unterminated set in RegExp at byte "
242                                             . $this->inputIndex .": {$pattern}");
243                                 }
244                             }
245                         }
246
247                         if ($this->a === '/') { // end pattern
248                             break; // while (true)
249                         } elseif ($this->a === '\\') {
250                             $this->output .= $this->a;
251                             $this->a = $this->get();
252                             $pattern .= $this->a;
253                         } elseif ($this->isEOF($this->a)) {
254                             throw new JSMin_UnterminatedRegExpException(
255                                 "JSMin: Unterminated RegExp at byte {$this->inputIndex}: {$pattern}");
256                         }
257                         $this->output .= $this->a;
258                         $this->lastByteOut = $this->a;
259                     }
260                     $this->b = $this->next();
261                 }
262             // end case ACTION_DELETE_A_B
263         }
264     }
265
266     /**
267      * @return bool
268      */
269     protected function isRegexpLiteral()
270     {
271         if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
272             // we obviously aren't dividing
273             return true;
274         }
275         if ($this->a === ' ' || $this->a === "\n") {
276             $length = strlen($this->output);
277             if ($length < 2) { // weird edge case
278                 return true;
279             }
280             // you can't divide a keyword
281             if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
282                 if ($this->output === $m[0]) { // odd but could happen
283                     return true;
284                 }
285                 // make sure it's a keyword, not end of an identifier
286                 $charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
287                 if (! $this->isAlphaNum($charBeforeKeyword)) {
288                     return true;
289                 }
290             }
291         }
292         return false;
293     }
294
295     /**
296      * Return the next character from stdin. Watch out for lookahead. If the character is a control character,
297      * translate it to a space or linefeed.
298      *
299      * @return string
300      */
301     protected function get()
302     {
303         $c = $this->lookAhead;
304         $this->lookAhead = null;
305         if ($c === null) {
306             // getc(stdin)
307             if ($this->inputIndex < $this->inputLength) {
308                 $c = $this->input[$this->inputIndex];
309                 $this->inputIndex += 1;
310             } else {
311                 $c = null;
312             }
313         }
314         if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) {
315             return $c;
316         }
317         if ($c === "\r") {
318             return "\n";
319         }
320         return ' ';
321     }
322
323     /**
324      * Does $a indicate end of input?
325      *
326      * @param string $a
327      * @return bool
328      */
329     protected function isEOF($a)
330     {
331         return ord($a) <= self::ORD_LF;
332     }
333
334     /**
335      * Get next char (without getting it). If is ctrl character, translate to a space or newline.
336      *
337      * @return string
338      */
339     protected function peek()
340     {
341         $this->lookAhead = $this->get();
342         return $this->lookAhead;
343     }
344
345     /**
346      * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character.
347      *
348      * @param string $c
349      *
350      * @return bool
351      */
352     protected function isAlphaNum($c)
353     {
354         return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126);
355     }
356
357     /**
358      * Consume a single line comment from input (possibly retaining it)
359      */
360     protected function consumeSingleLineComment()
361     {
362         $comment = '';
363         while (true) {
364             $get = $this->get();
365             $comment .= $get;
366             if (ord($get) <= self::ORD_LF) { // end of line reached
367                 // if IE conditional comment
368                 if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
369                     $this->keptComment .= "/{$comment}";
370                 }
371                 return;
372             }
373         }
374     }
375
376     /**
377      * Consume a multiple line comment from input (possibly retaining it)
378      *
379      * @throws JSMin_UnterminatedCommentException
380      */
381     protected function consumeMultipleLineComment()
382     {
383         $this->get();
384         $comment = '';
385         for(;;) {
386             $get = $this->get();
387             if ($get === '*') {
388                 if ($this->peek() === '/') { // end of comment reached
389                     $this->get();
390                     if (0 === strpos($comment, '!')) {
391                         // preserved by YUI Compressor
392                         if (!$this->keptComment) {
393                             // don't prepend a newline if two comments right after one another
394                             $this->keptComment = "\n";
395                         }
396                         $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n";
397                     } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
398                         // IE conditional
399                         $this->keptComment .= "/*{$comment}*/";
400                     }
401                     return;
402                 }
403             } elseif ($get === null) {
404                 throw new JSMin_UnterminatedCommentException(
405                     "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}");
406             }
407             $comment .= $get;
408         }
409     }
410
411     /**
412      * Get the next character, skipping over comments. Some comments may be preserved.
413      *
414      * @return string
415      */
416     protected function next()
417     {
418         $get = $this->get();
419         if ($get === '/') {
420             switch ($this->peek()) {
421                 case '/':
422                     $this->consumeSingleLineComment();
423                     $get = "\n";
424                     break;
425                 case '*':
426                     $this->consumeMultipleLineComment();
427                     $get = ' ';
428                     break;
429             }
430         }
431         return $get;
432     }
433 }
434
435 class JSMin_UnterminatedStringException extends Exception {}
436 class JSMin_UnterminatedCommentException extends Exception {}
437 class JSMin_UnterminatedRegExpException extends Exception {}