PluginProbe
Autoptimize / trunk
Autoptimize vtrunk
3.1.16 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 All 108 releases
← All changes | classes/external/php/jsmin.php +27 -22 2.7.2 → trunk View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +
3 +//namespace JSMin;
4 +
2 5 /**
3 6 * JSMin.php - modified PHP implementation of Douglas Crockford's JSMin.
4 7 *
5 8 * <code>
@@ -52,14 +55,9 @@
52 55 * @copyright 2008 Ryan Grove <[email protected]> (PHP port)
53 56 * @license http://opensource.org/licenses/mit-license.php MIT License
54 57 * @link http://code.google.com/p/jsmin-php/
55 58 */
56 -
57 -// This is from https://github.com/mrclay/jsmin-php 2.3.2
58 -
59 59 class JSMin {
60 - const ORD_LF = 10;
61 - const ORD_SPACE = 32;
62 60 const ACTION_KEEP_A = 1;
63 61 const ACTION_DELETE_A = 2;
64 62 const ACTION_DELETE_A_B = 3;
65 63
@@ -122,9 +120,9 @@
122 120
123 121 while ($this->a !== null) {
124 122 // determine next command
125 123 $command = self::ACTION_KEEP_A; // default
126 - if ($this->a === ' ') {
124 + if ($this->isWhiteSpace($this->a)) {
127 125 if (($this->lastByteOut === '+' || $this->lastByteOut === '-')
128 126 && ($this->b === $this->lastByteOut)) {
129 127 // Don't delete this space. If we do, the addition/subtraction
130 128 // could be parsed as a post-increment
@@ -130,23 +128,23 @@
130 128 // could be parsed as a post-increment
131 129 } elseif (! $this->isAlphaNum($this->b)) {
132 130 $command = self::ACTION_DELETE_A;
133 131 }
134 - } elseif ($this->a === "\n") {
135 - if ($this->b === ' ') {
132 + } elseif ($this->isLineTerminator($this->a)) {
133 + if ($this->isWhiteSpace($this->b)) {
136 134 $command = self::ACTION_DELETE_A_B;
137 135
138 136 // in case of mbstring.func_overload & 2, must check for null b,
139 137 // otherwise mb_strpos will give WARNING
140 138 } elseif ($this->b === null
141 - || (false === strpos('{[(+-!~', $this->b)
139 + || (false === strpos('{[(+-!~#', $this->b)
142 140 && ! $this->isAlphaNum($this->b))) {
143 141 $command = self::ACTION_DELETE_A;
144 142 }
145 143 } elseif (! $this->isAlphaNum($this->a)) {
146 - if ($this->b === ' '
147 - || ($this->b === "\n"
148 - && (false === strpos('}])+-"\'', $this->a)))) {
144 + if ($this->isWhiteSpace($this->b)
145 + || ($this->isLineTerminator($this->b)
146 + && (false === strpos('}])+-"\'`', $this->a)))) {
149 147 $command = self::ACTION_DELETE_A_B;
150 148 }
151 149 }
152 150 $this->action($command);
@@ -164,9 +162,9 @@
164 162 * ACTION_DELETE_A = Copy B to A. Get the next B.
165 163 * ACTION_DELETE_A_B = Get the next B.
166 164 *
167 165 * @param int $command
168 - * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException
166 + * @throws JSMin_UnterminatedRegExpException|UnterminatedStringException
169 167 */
170 168 protected function action($command)
171 169 {
172 170 // make sure we don't compress "a + ++b" to "a+++b", etc.
@@ -206,9 +204,9 @@
206 204 $this->a = $this->get();
207 205 if ($this->a === $this->b) { // end quote
208 206 break;
209 207 }
210 - if ($delimiter === '`' && $this->a === "\n") {
208 + if ($delimiter === '`' && $this->isLineTerminator($this->a)) {
211 209 // leave the newline
212 210 } elseif ($this->isEOF($this->a)) {
213 211 $byte = $this->inputIndex - 1;
214 212 throw new JSMin_UnterminatedStringException(
@@ -286,9 +284,9 @@
286 284 }
287 285
288 286 // check if first non-ws token is "/" (see starts-regex.js)
289 287 $length = strlen($this->output);
290 - if ($this->a === ' ' || $this->a === "\n") {
288 + if ($this->isWhiteSpace($this->a) || $this->isLineTerminator($this->a)) {
291 289 if ($length < 2) { // weird edge case
292 290 return true;
293 291 }
294 292 }
@@ -308,9 +306,9 @@
308 306 return false;
309 307 }
310 308
311 309 // it's a regexp. Remove unneeded whitespace after keyword
312 - if ($this->a === ' ' || $this->a === "\n") {
310 + if ($this->isWhiteSpace($this->a) || $this->isLineTerminator($this->a)) {
313 311 $this->a = '';
314 312 }
315 313
316 314 return true;
@@ -334,15 +332,12 @@
334 332 } else {
335 333 $c = null;
336 334 }
337 335 }
338 - if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) {
339 - return $c;
340 - }
341 336 if ($c === "\r") {
342 337 return "\n";
343 338 }
344 - return ' ';
339 + return $c;
345 340 }
346 341
347 342 /**
348 343 * Does $a indicate end of input?
@@ -351,9 +346,9 @@
351 346 * @return bool
352 347 */
353 348 protected function isEOF($a)
354 349 {
355 - return ord($a) <= self::ORD_LF;
350 + return $a === null || $this->isLineTerminator($a);
356 351 }
357 352
358 353 /**
359 354 * Get next char (without getting it). If is ctrl character, translate to a space or newline.
@@ -386,9 +381,9 @@
386 381 $comment = '';
387 382 while (true) {
388 383 $get = $this->get();
389 384 $comment .= $get;
390 - if (ord($get) <= self::ORD_LF) { // end of line reached
385 + if ($this->isEOF($get)) {
391 386 // if IE conditional comment
392 387 if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
393 388 $this->keptComment .= "/{$comment}";
394 389 }
@@ -452,8 +447,18 @@
452 447 break;
453 448 }
454 449 }
455 450 return $get;
451 + }
452 +
453 + protected function isWhiteSpace($s) {
454 + // https://www.ecma-international.org/ecma-262/#sec-white-space
455 + return $s !== null && strpos(" \t\v\f", $s) !== false;
456 + }
457 +
458 + protected function isLineTerminator($s) {
459 + // https://www.ecma-international.org/ecma-262/#sec-line-terminators
460 + return $s !== null && strpos("\n\r", $s) !== false;
456 461 }
457 462 }
458 463
459 464 class JSMin_UnterminatedStringException extends Exception {}