Skip to content

HTTPS clone URL

Subversion checkout URL

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