PluginProbe
GetResponse Forms by Optin Cat / 1.3.5
GetResponse Forms by Optin Cat v1.3.5
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / skelet / core / lib / JSMin.php

JSMin.php in GetResponse Forms by Optin Cat 1.3.5, at includes/skelet/core/lib/JSMin.php

450 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $byte = $this->inputIndex - 1;
204 throw new JSMin_UnterminatedStringException(
205 "JSMin: Unterminated String at byte {$byte}: {$str}");
206 }
207 $str .= $this->a;
208 if ($this->a === '\\') {
209 $this->output .= $this->a;
210 $this->lastByteOut = $this->a;
211
212 $this->a = $this->get();
213 $str .= $this->a;
214 }
215 }
216 }
217
218 // fallthrough intentional
219 case self::ACTION_DELETE_A_B: // 3
220 $this->b = $this->next();
221 if ($this->b === '/' && $this->isRegexpLiteral()) {
222 $this->output .= $this->a . $this->b;
223 $pattern = '/'; // keep entire pattern in case we need to report it in the exception
224 for(;;) {
225 $this->a = $this->get();
226 $pattern .= $this->a;
227 if ($this->a === '[') {
228 for(;;) {
229 $this->output .= $this->a;
230 $this->a = $this->get();
231 $pattern .= $this->a;
232 if ($this->a === ']') {
233 break;
234 }
235 if ($this->a === '\\') {
236 $this->output .= $this->a;
237 $this->a = $this->get();
238 $pattern .= $this->a;
239 }
240 if ($this->isEOF($this->a)) {
241 throw new JSMin_UnterminatedRegExpException(
242 "JSMin: Unterminated set in RegExp at byte "
243 . $this->inputIndex .": {$pattern}");
244 }
245 }
246 }
247
248 if ($this->a === '/') { // end pattern
249 break; // while (true)
250 } elseif ($this->a === '\\') {
251 $this->output .= $this->a;
252 $this->a = $this->get();
253 $pattern .= $this->a;
254 } elseif ($this->isEOF($this->a)) {
255 $byte = $this->inputIndex - 1;
256 throw new JSMin_UnterminatedRegExpException(
257 "JSMin: Unterminated RegExp at byte {$byte}: {$pattern}");
258 }
259 $this->output .= $this->a;
260 $this->lastByteOut = $this->a;
261 }
262 $this->b = $this->next();
263 }
264 // end case ACTION_DELETE_A_B
265 }
266 }
267
268 /**
269 * @return bool
270 */
271 protected function isRegexpLiteral()
272 {
273 if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
274 // we obviously aren't dividing
275 return true;
276 }
277
278 // we have to check for a preceding keyword, and we don't need to pattern
279 // match over the whole output.
280 $recentOutput = substr($this->output, -10);
281
282 // check if return/typeof directly precede a pattern without a space
283 foreach (array('return', 'typeof') as $keyword) {
284 if ($this->a !== substr($keyword, -1)) {
285 // certainly wasn't keyword
286 continue;
287 }
288 if (preg_match("~(^|[\\s\\S])" . substr($keyword, 0, -1) . "$~", $recentOutput, $m)) {
289 if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
290 return true;
291 }
292 }
293 }
294
295 // check all keywords
296 if ($this->a === ' ' || $this->a === "\n") {
297 if (preg_match('~(^|[\\s\\S])(?:case|else|in|return|typeof)$~', $recentOutput, $m)) {
298 if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
299 return true;
300 }
301 }
302 }
303
304 return false;
305 }
306
307 /**
308 * Return the next character from stdin. Watch out for lookahead. If the character is a control character,
309 * translate it to a space or linefeed.
310 *
311 * @return string
312 */
313 protected function get()
314 {
315 $c = $this->lookAhead;
316 $this->lookAhead = null;
317 if ($c === null) {
318 // getc(stdin)
319 if ($this->inputIndex < $this->inputLength) {
320 $c = $this->input[$this->inputIndex];
321 $this->inputIndex += 1;
322 } else {
323 $c = null;
324 }
325 }
326 if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) {
327 return $c;
328 }
329 if ($c === "\r") {
330 return "\n";
331 }
332 return ' ';
333 }
334
335 /**
336 * Does $a indicate end of input?
337 *
338 * @param string $a
339 * @return bool
340 */
341 protected function isEOF($a)
342 {
343 return ord($a) <= self::ORD_LF;
344 }
345
346 /**
347 * Get next char (without getting it). If is ctrl character, translate to a space or newline.
348 *
349 * @return string
350 */
351 protected function peek()
352 {
353 $this->lookAhead = $this->get();
354 return $this->lookAhead;
355 }
356
357 /**
358 * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character.
359 *
360 * @param string $c
361 *
362 * @return bool
363 */
364 protected function isAlphaNum($c)
365 {
366 return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126);
367 }
368
369 /**
370 * Consume a single line comment from input (possibly retaining it)
371 */
372 protected function consumeSingleLineComment()
373 {
374 $comment = '';
375 while (true) {
376 $get = $this->get();
377 $comment .= $get;
378 if (ord($get) <= self::ORD_LF) { // end of line reached
379 // if IE conditional comment
380 if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
381 $this->keptComment .= "/{$comment}";
382 }
383 return;
384 }
385 }
386 }
387
388 /**
389 * Consume a multiple line comment from input (possibly retaining it)
390 *
391 * @throws JSMin_UnterminatedCommentException
392 */
393 protected function consumeMultipleLineComment()
394 {
395 $this->get();
396 $comment = '';
397 for(;;) {
398 $get = $this->get();
399 if ($get === '*') {
400 if ($this->peek() === '/') { // end of comment reached
401 $this->get();
402 if (0 === strpos($comment, '!')) {
403 // preserved by YUI Compressor
404 if (!$this->keptComment) {
405 // don't prepend a newline if two comments right after one another
406 $this->keptComment = "\n";
407 }
408 $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n";
409 } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
410 // IE conditional
411 $this->keptComment .= "/*{$comment}*/";
412 }
413 return;
414 }
415 } elseif ($get === null) {
416 throw new JSMin_UnterminatedCommentException(
417 "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}");
418 }
419 $comment .= $get;
420 }
421 }
422
423 /**
424 * Get the next character, skipping over comments. Some comments may be preserved.
425 *
426 * @return string
427 */
428 protected function next()
429 {
430 $get = $this->get();
431 if ($get === '/') {
432 switch ($this->peek()) {
433 case '/':
434 $this->consumeSingleLineComment();
435 $get = "\n";
436 break;
437 case '*':
438 $this->consumeMultipleLineComment();
439 $get = ' ';
440 break;
441 }
442 }
443 return $get;
444 }
445 }
446
447 class JSMin_UnterminatedStringException extends Exception {}
448 class JSMin_UnterminatedCommentException extends Exception {}
449 class JSMin_UnterminatedRegExpException extends Exception {}
450