| 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 |
// This is from https://github.com/mrclay/jsmin-php 2.3.2 |
| 58 |
|
| 59 |
class JSMin { |
| 60 |
const ORD_LF = 10; |
| 61 |
const ORD_SPACE = 32; |
| 62 |
const ACTION_KEEP_A = 1; |
| 63 |
const ACTION_DELETE_A = 2; |
| 64 |
const ACTION_DELETE_A_B = 3; |
| 65 |
|
| 66 |
protected $a = "\n"; |
| 67 |
protected $b = ''; |
| 68 |
protected $input = ''; |
| 69 |
protected $inputIndex = 0; |
| 70 |
protected $inputLength = 0; |
| 71 |
protected $lookAhead = null; |
| 72 |
protected $output = ''; |
| 73 |
protected $lastByteOut = ''; |
| 74 |
protected $keptComment = ''; |
| 75 |
|
| 76 |
/** |
| 77 |
* Minify Javascript. |
| 78 |
* |
| 79 |
* @param string $js Javascript to be minified |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public static function minify($js) |
| 84 |
{ |
| 85 |
$jsmin = new JSMin($js); |
| 86 |
return $jsmin->min(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @param string $input |
| 91 |
*/ |
| 92 |
public function __construct($input) |
| 93 |
{ |
| 94 |
$this->input = $input; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Perform minification, return result |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function min() |
| 103 |
{ |
| 104 |
if ($this->output !== '') { // min already run |
| 105 |
return $this->output; |
| 106 |
} |
| 107 |
|
| 108 |
$mbIntEnc = null; |
| 109 |
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { |
| 110 |
$mbIntEnc = mb_internal_encoding(); |
| 111 |
mb_internal_encoding('8bit'); |
| 112 |
} |
| 113 |
|
| 114 |
if (isset($this->input[0]) && $this->input[0] === "\xef") { |
| 115 |
$this->input = substr($this->input, 3); |
| 116 |
} |
| 117 |
|
| 118 |
$this->input = str_replace("\r\n", "\n", $this->input); |
| 119 |
$this->inputLength = strlen($this->input); |
| 120 |
|
| 121 |
$this->action(self::ACTION_DELETE_A_B); |
| 122 |
|
| 123 |
while ($this->a !== null) { |
| 124 |
// determine next command |
| 125 |
$command = self::ACTION_KEEP_A; // default |
| 126 |
if ($this->a === ' ') { |
| 127 |
if (($this->lastByteOut === '+' || $this->lastByteOut === '-') |
| 128 |
&& ($this->b === $this->lastByteOut)) { |
| 129 |
// Don't delete this space. If we do, the addition/subtraction |
| 130 |
// could be parsed as a post-increment |
| 131 |
} elseif (! $this->isAlphaNum($this->b)) { |
| 132 |
$command = self::ACTION_DELETE_A; |
| 133 |
} |
| 134 |
} elseif ($this->a === "\n") { |
| 135 |
if ($this->b === ' ') { |
| 136 |
$command = self::ACTION_DELETE_A_B; |
| 137 |
|
| 138 |
// in case of mbstring.func_overload & 2, must check for null b, |
| 139 |
// otherwise mb_strpos will give WARNING |
| 140 |
} elseif ($this->b === null |
| 141 |
|| (false === strpos('{[(+-!~', $this->b) |
| 142 |
&& ! $this->isAlphaNum($this->b))) { |
| 143 |
$command = self::ACTION_DELETE_A; |
| 144 |
} |
| 145 |
} elseif (! $this->isAlphaNum($this->a)) { |
| 146 |
if ($this->b === ' ' |
| 147 |
|| ($this->b === "\n" |
| 148 |
&& (false === strpos('}])+-"\'', $this->a)))) { |
| 149 |
$command = self::ACTION_DELETE_A_B; |
| 150 |
} |
| 151 |
} |
| 152 |
$this->action($command); |
| 153 |
} |
| 154 |
$this->output = trim($this->output); |
| 155 |
|
| 156 |
if ($mbIntEnc !== null) { |
| 157 |
mb_internal_encoding($mbIntEnc); |
| 158 |
} |
| 159 |
return $this->output; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* ACTION_KEEP_A = Output A. Copy B to A. Get the next B. |
| 164 |
* ACTION_DELETE_A = Copy B to A. Get the next B. |
| 165 |
* ACTION_DELETE_A_B = Get the next B. |
| 166 |
* |
| 167 |
* @param int $command |
| 168 |
* @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException |
| 169 |
*/ |
| 170 |
protected function action($command) |
| 171 |
{ |
| 172 |
// make sure we don't compress "a + ++b" to "a+++b", etc. |
| 173 |
if ($command === self::ACTION_DELETE_A_B |
| 174 |
&& $this->b === ' ' |
| 175 |
&& ($this->a === '+' || $this->a === '-')) { |
| 176 |
// Note: we're at an addition/substraction operator; the inputIndex |
| 177 |
// will certainly be a valid index |
| 178 |
if ($this->input[$this->inputIndex] === $this->a) { |
| 179 |
// This is "+ +" or "- -". Don't delete the space. |
| 180 |
$command = self::ACTION_KEEP_A; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
switch ($command) { |
| 185 |
case self::ACTION_KEEP_A: // 1 |
| 186 |
$this->output .= $this->a; |
| 187 |
|
| 188 |
if ($this->keptComment) { |
| 189 |
$this->output = rtrim($this->output, "\n"); |
| 190 |
$this->output .= $this->keptComment; |
| 191 |
$this->keptComment = ''; |
| 192 |
} |
| 193 |
|
| 194 |
$this->lastByteOut = $this->a; |
| 195 |
|
| 196 |
// fallthrough intentional |
| 197 |
case self::ACTION_DELETE_A: // 2 |
| 198 |
$this->a = $this->b; |
| 199 |
if ($this->a === "'" || $this->a === '"') { // string literal |
| 200 |
$str = $this->a; // in case needed for exception |
| 201 |
for(;;) { |
| 202 |
$this->output .= $this->a; |
| 203 |
$this->lastByteOut = $this->a; |
| 204 |
|
| 205 |
$this->a = $this->get(); |
| 206 |
if ($this->a === $this->b) { // end quote |
| 207 |
break; |
| 208 |
} |
| 209 |
if ($this->isEOF($this->a)) { |
| 210 |
$byte = $this->inputIndex - 1; |
| 211 |
throw new JSMin_UnterminatedStringException( |
| 212 |
"JSMin: Unterminated String at byte {$byte}: {$str}"); |
| 213 |
} |
| 214 |
$str .= $this->a; |
| 215 |
if ($this->a === '\\') { |
| 216 |
$this->output .= $this->a; |
| 217 |
$this->lastByteOut = $this->a; |
| 218 |
|
| 219 |
$this->a = $this->get(); |
| 220 |
$str .= $this->a; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// fallthrough intentional |
| 226 |
case self::ACTION_DELETE_A_B: // 3 |
| 227 |
$this->b = $this->next(); |
| 228 |
if ($this->b === '/' && $this->isRegexpLiteral()) { |
| 229 |
$this->output .= $this->a . $this->b; |
| 230 |
$pattern = '/'; // keep entire pattern in case we need to report it in the exception |
| 231 |
for(;;) { |
| 232 |
$this->a = $this->get(); |
| 233 |
$pattern .= $this->a; |
| 234 |
if ($this->a === '[') { |
| 235 |
for(;;) { |
| 236 |
$this->output .= $this->a; |
| 237 |
$this->a = $this->get(); |
| 238 |
$pattern .= $this->a; |
| 239 |
if ($this->a === ']') { |
| 240 |
break; |
| 241 |
} |
| 242 |
if ($this->a === '\\') { |
| 243 |
$this->output .= $this->a; |
| 244 |
$this->a = $this->get(); |
| 245 |
$pattern .= $this->a; |
| 246 |
} |
| 247 |
if ($this->isEOF($this->a)) { |
| 248 |
throw new JSMin_UnterminatedRegExpException( |
| 249 |
"JSMin: Unterminated set in RegExp at byte " |
| 250 |
. $this->inputIndex .": {$pattern}"); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ($this->a === '/') { // end pattern |
| 256 |
break; // while (true) |
| 257 |
} elseif ($this->a === '\\') { |
| 258 |
$this->output .= $this->a; |
| 259 |
$this->a = $this->get(); |
| 260 |
$pattern .= $this->a; |
| 261 |
} elseif ($this->isEOF($this->a)) { |
| 262 |
$byte = $this->inputIndex - 1; |
| 263 |
throw new JSMin_UnterminatedRegExpException( |
| 264 |
"JSMin: Unterminated RegExp at byte {$byte}: {$pattern}"); |
| 265 |
} |
| 266 |
$this->output .= $this->a; |
| 267 |
$this->lastByteOut = $this->a; |
| 268 |
} |
| 269 |
$this->b = $this->next(); |
| 270 |
} |
| 271 |
// end case ACTION_DELETE_A_B |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* @return bool |
| 277 |
*/ |
| 278 |
protected function isRegexpLiteral() |
| 279 |
{ |
| 280 |
if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) { |
| 281 |
// we can't divide after these tokens |
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
// check if first non-ws token is "/" (see starts-regex.js) |
| 286 |
$length = strlen($this->output); |
| 287 |
if ($this->a === ' ' || $this->a === "\n") { |
| 288 |
if ($length < 2) { // weird edge case |
| 289 |
return true; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
// if the "/" follows a keyword, it must be a regexp, otherwise it's best to assume division |
| 294 |
|
| 295 |
$subject = $this->output . trim($this->a); |
| 296 |
if (!preg_match('/(?:case|else|in|return|typeof)$/', $subject, $m)) { |
| 297 |
// not a keyword |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
// can't be sure it's a keyword yet (see not-regexp.js) |
| 302 |
$charBeforeKeyword = substr($subject, 0 - strlen($m[0]) - 1, 1); |
| 303 |
if ($this->isAlphaNum($charBeforeKeyword)) { |
| 304 |
// this is really an identifier ending in a keyword, e.g. "xreturn" |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
// it's a regexp. Remove unneeded whitespace after keyword |
| 309 |
if ($this->a === ' ' || $this->a === "\n") { |
| 310 |
$this->a = ''; |
| 311 |
} |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Return the next character from stdin. Watch out for lookahead. If the character is a control character, |
| 318 |
* translate it to a space or linefeed. |
| 319 |
* |
| 320 |
* @return string |
| 321 |
*/ |
| 322 |
protected function get() |
| 323 |
{ |
| 324 |
$c = $this->lookAhead; |
| 325 |
$this->lookAhead = null; |
| 326 |
if ($c === null) { |
| 327 |
// getc(stdin) |
| 328 |
if ($this->inputIndex < $this->inputLength) { |
| 329 |
$c = $this->input[$this->inputIndex]; |
| 330 |
$this->inputIndex += 1; |
| 331 |
} else { |
| 332 |
$c = null; |
| 333 |
} |
| 334 |
} |
| 335 |
if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) { |
| 336 |
return $c; |
| 337 |
} |
| 338 |
if ($c === "\r") { |
| 339 |
return "\n"; |
| 340 |
} |
| 341 |
return ' '; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Does $a indicate end of input? |
| 346 |
* |
| 347 |
* @param string $a |
| 348 |
* @return bool |
| 349 |
*/ |
| 350 |
protected function isEOF($a) |
| 351 |
{ |
| 352 |
return ord($a) <= self::ORD_LF; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Get next char (without getting it). If is ctrl character, translate to a space or newline. |
| 357 |
* |
| 358 |
* @return string |
| 359 |
*/ |
| 360 |
protected function peek() |
| 361 |
{ |
| 362 |
$this->lookAhead = $this->get(); |
| 363 |
return $this->lookAhead; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character. |
| 368 |
* |
| 369 |
* @param string $c |
| 370 |
* |
| 371 |
* @return bool |
| 372 |
*/ |
| 373 |
protected function isAlphaNum($c) |
| 374 |
{ |
| 375 |
return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Consume a single line comment from input (possibly retaining it) |
| 380 |
*/ |
| 381 |
protected function consumeSingleLineComment() |
| 382 |
{ |
| 383 |
$comment = ''; |
| 384 |
while (true) { |
| 385 |
$get = $this->get(); |
| 386 |
$comment .= $get; |
| 387 |
if (ord($get) <= self::ORD_LF) { // end of line reached |
| 388 |
// if IE conditional comment |
| 389 |
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
| 390 |
$this->keptComment .= "/{$comment}"; |
| 391 |
} |
| 392 |
return; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Consume a multiple line comment from input (possibly retaining it) |
| 399 |
* |
| 400 |
* @throws JSMin_UnterminatedCommentException |
| 401 |
*/ |
| 402 |
protected function consumeMultipleLineComment() |
| 403 |
{ |
| 404 |
$this->get(); |
| 405 |
$comment = ''; |
| 406 |
for(;;) { |
| 407 |
$get = $this->get(); |
| 408 |
if ($get === '*') { |
| 409 |
if ($this->peek() === '/') { // end of comment reached |
| 410 |
$this->get(); |
| 411 |
if (0 === strpos($comment, '!')) { |
| 412 |
// preserved by YUI Compressor |
| 413 |
if (!$this->keptComment) { |
| 414 |
// don't prepend a newline if two comments right after one another |
| 415 |
$this->keptComment = "\n"; |
| 416 |
} |
| 417 |
$this->keptComment .= "/*!" . substr($comment, 1) . "*/\n"; |
| 418 |
} else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
| 419 |
// IE conditional |
| 420 |
$this->keptComment .= "/*{$comment}*/"; |
| 421 |
} |
| 422 |
return; |
| 423 |
} |
| 424 |
} elseif ($get === null) { |
| 425 |
throw new JSMin_UnterminatedCommentException( |
| 426 |
"JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}"); |
| 427 |
} |
| 428 |
$comment .= $get; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Get the next character, skipping over comments. Some comments may be preserved. |
| 434 |
* |
| 435 |
* @return string |
| 436 |
*/ |
| 437 |
protected function next() |
| 438 |
{ |
| 439 |
$get = $this->get(); |
| 440 |
if ($get === '/') { |
| 441 |
switch ($this->peek()) { |
| 442 |
case '/': |
| 443 |
$this->consumeSingleLineComment(); |
| 444 |
$get = "\n"; |
| 445 |
break; |
| 446 |
case '*': |
| 447 |
$this->consumeMultipleLineComment(); |
| 448 |
$get = ' '; |
| 449 |
break; |
| 450 |
} |
| 451 |
} |
| 452 |
return $get; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
class JSMin_UnterminatedStringException extends Exception {} |
| 457 |
class JSMin_UnterminatedCommentException extends Exception {} |
| 458 |
class JSMin_UnterminatedRegExpException extends Exception {} |
| 459 |
|