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