9e1c43225fe7b4fbf4ef88ceb3e847097e361963
[lhc/web/wiklou.git] / js2 / mwEmbed / php / minify / JSMin.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) die( 1 );
4 /**
5 * jsmin.php - PHP implementation of Douglas Crockford's JSMin.
6 *
7 * This is pretty much a direct port of jsmin.c to PHP with just a few
8 * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
9 * outputs to stdout, this library accepts a string as input and returns another
10 * string as output.
11 *
12 * PHP 5 or higher is required.
13 *
14 * Permission is hereby granted to use this version of the library under the
15 * same terms as jsmin.c, which has the following license:
16 *
17 * --
18 * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining a copy of
21 * this software and associated documentation files (the "Software"), to deal in
22 * the Software without restriction, including without limitation the rights to
23 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
24 * of the Software, and to permit persons to whom the Software is furnished to do
25 * so, subject to the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be included in all
28 * copies or substantial portions of the Software.
29 *
30 * The Software shall be used for Good, not Evil.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * SOFTWARE.
39 * --
40 *
41 * @package JSMin
42 * @author Ryan Grove <ryan@wonko.com>
43 * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
44 * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
45 * @license http://opensource.org/licenses/mit-license.php MIT License
46 * @version 1.1.1 (2008-03-02)
47 * @link http://code.google.com/p/jsmin-php/
48 */
49
50 class JSMin {
51 const ORD_LF = 10;
52 const ORD_SPACE = 32;
53
54 protected $a = '';
55 protected $b = '';
56 protected $input = '';
57 protected $inputIndex = 0;
58 protected $inputLength = 0;
59 protected $lookAhead = null;
60 protected $output = '';
61
62 // -- Public Static Methods --------------------------------------------------
63
64 public static function minify($js) {
65 $jsmin = new JSMin($js);
66 return $jsmin->min();
67 }
68
69 // -- Public Instance Methods ------------------------------------------------
70
71 public function __construct($input) {
72 $this->input = str_replace("\r\n", "\n", $input);
73 $this->inputLength = strlen($this->input);
74 }
75
76 // -- Protected Instance Methods ---------------------------------------------
77
78 protected function action($d) {
79 switch($d) {
80 case 1:
81 $this->output .= $this->a;
82
83 case 2:
84 $this->a = $this->b;
85
86 if ($this->a === "'" || $this->a === '"') {
87 for (;;) {
88 $this->output .= $this->a;
89 $this->a = $this->get();
90
91 if ($this->a === $this->b) {
92 break;
93 }
94
95 if (ord($this->a) <= self::ORD_LF) {
96 throw new JSMinException('Unterminated string literal.');
97 }
98
99 if ($this->a === '\\') {
100 $this->output .= $this->a;
101 $this->a = $this->get();
102 }
103 }
104 }
105
106 case 3:
107 $this->b = $this->next();
108
109 if ($this->b === '/' && (
110 $this->a === '(' || $this->a === ',' || $this->a === '=' ||
111 $this->a === ':' || $this->a === '[' || $this->a === '!' ||
112 $this->a === '&' || $this->a === '|' || $this->a === '?')) {
113
114 $this->output .= $this->a . $this->b;
115
116 for (;;) {
117 $this->a = $this->get();
118
119 if ($this->a === '/') {
120 break;
121 } elseif ($this->a === '\\') {
122 $this->output .= $this->a;
123 $this->a = $this->get();
124 } elseif (ord($this->a) <= self::ORD_LF) {
125 throw new JSMinException('Unterminated regular expression '.
126 'literal.');
127 }
128
129 $this->output .= $this->a;
130 }
131
132 $this->b = $this->next();
133 }
134 }
135 }
136
137 protected function get() {
138 $c = $this->lookAhead;
139 $this->lookAhead = null;
140
141 if ($c === null) {
142 if ($this->inputIndex < $this->inputLength) {
143 $c = $this->input[$this->inputIndex];
144 $this->inputIndex += 1;
145 } else {
146 $c = null;
147 }
148 }
149
150 if ($c === "\r") {
151 return "\n";
152 }
153
154 if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
155 return $c;
156 }
157
158 return ' ';
159 }
160
161 protected function isAlphaNum($c) {
162 return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
163 }
164
165 protected function min() {
166 $this->a = "\n";
167 $this->action(3);
168
169 while ($this->a !== null) {
170 switch ($this->a) {
171 case ' ':
172 if ($this->isAlphaNum($this->b)) {
173 $this->action(1);
174 } else {
175 $this->action(2);
176 }
177 break;
178
179 case "\n":
180 switch ($this->b) {
181 case '{':
182 case '[':
183 case '(':
184 case '+':
185 case '-':
186 $this->action(1);
187 break;
188
189 case ' ':
190 $this->action(3);
191 break;
192
193 default:
194 if ($this->isAlphaNum($this->b)) {
195 $this->action(1);
196 }
197 else {
198 $this->action(2);
199 }
200 }
201 break;
202
203 default:
204 switch ($this->b) {
205 case ' ':
206 if ($this->isAlphaNum($this->a)) {
207 $this->action(1);
208 break;
209 }
210
211 $this->action(3);
212 break;
213
214 case "\n":
215 switch ($this->a) {
216 case '}':
217 case ']':
218 case ')':
219 case '+':
220 case '-':
221 case '"':
222 case "'":
223 $this->action(1);
224 break;
225
226 default:
227 if ($this->isAlphaNum($this->a)) {
228 $this->action(1);
229 }
230 else {
231 $this->action(3);
232 }
233 }
234 break;
235
236 default:
237 $this->action(1);
238 break;
239 }
240 }
241 }
242
243 return $this->output;
244 }
245
246 protected function next() {
247 $c = $this->get();
248
249 if ($c === '/') {
250 switch($this->peek()) {
251 case '/':
252 for (;;) {
253 $c = $this->get();
254
255 if (ord($c) <= self::ORD_LF) {
256 return $c;
257 }
258 }
259
260 case '*':
261 $this->get();
262
263 for (;;) {
264 switch($this->get()) {
265 case '*':
266 if ($this->peek() === '/') {
267 $this->get();
268 return ' ';
269 }
270 break;
271
272 case null:
273 throw new JSMinException('Unterminated comment.');
274 }
275 }
276
277 default:
278 return $c;
279 }
280 }
281
282 return $c;
283 }
284
285 protected function peek() {
286 $this->lookAhead = $this->get();
287 return $this->lookAhead;
288 }
289 }
290
291 // -- Exceptions ---------------------------------------------------------------
292 class JSMinException extends Exception {}
293 ?>