futtta /
autoptimize
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 <[email protected]> (PHP port) |
||
| 49 | * @author Steve Clay <[email protected]> (modifications + cleanup) |
||
| 50 | * @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp) |
||
| 51 | * @copyright 2002 Douglas Crockford <[email protected]> (jsmin.c) |
||
| 52 | * @copyright 2008 Ryan Grove <[email protected]> (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 | View Code Duplication | 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 | View Code Duplication | 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 === '-') |
||
|
0 ignored issues
–
show
|
|||
| 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 | View Code Duplication | 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 | View Code Duplication | 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 | throw new JSMin_UnterminatedStringException( |
||
| 204 | "JSMin: Unterminated String at byte {$this->inputIndex}: {$str}");
|
||
| 205 | } |
||
| 206 | $str .= $this->a; |
||
| 207 | View Code Duplication | if ($this->a === '\\') {
|
|
| 208 | $this->output .= $this->a; |
||
| 209 | $this->lastByteOut = $this->a; |
||
| 210 | |||
| 211 | $this->a = $this->get(); |
||
| 212 | $str .= $this->a; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | // fallthrough intentional |
||
| 218 | View Code Duplication | case self::ACTION_DELETE_A_B: // 3 |
|
| 219 | $this->b = $this->next(); |
||
| 220 | if ($this->b === '/' && $this->isRegexpLiteral()) {
|
||
| 221 | $this->output .= $this->a . $this->b; |
||
| 222 | $pattern = '/'; // keep entire pattern in case we need to report it in the exception |
||
| 223 | for(;;) {
|
||
| 224 | $this->a = $this->get(); |
||
| 225 | $pattern .= $this->a; |
||
| 226 | if ($this->a === '[') {
|
||
| 227 | for(;;) {
|
||
| 228 | $this->output .= $this->a; |
||
| 229 | $this->a = $this->get(); |
||
| 230 | $pattern .= $this->a; |
||
| 231 | if ($this->a === ']') {
|
||
| 232 | break; |
||
| 233 | } |
||
| 234 | if ($this->a === '\\') {
|
||
| 235 | $this->output .= $this->a; |
||
| 236 | $this->a = $this->get(); |
||
| 237 | $pattern .= $this->a; |
||
| 238 | } |
||
| 239 | if ($this->isEOF($this->a)) {
|
||
| 240 | throw new JSMin_UnterminatedRegExpException( |
||
| 241 | "JSMin: Unterminated set in RegExp at byte " |
||
| 242 | . $this->inputIndex .": {$pattern}");
|
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | if ($this->a === '/') { // end pattern
|
||
| 248 | break; // while (true) |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 249 | } elseif ($this->a === '\\') {
|
||
| 250 | $this->output .= $this->a; |
||
| 251 | $this->a = $this->get(); |
||
| 252 | $pattern .= $this->a; |
||
| 253 | } elseif ($this->isEOF($this->a)) {
|
||
| 254 | throw new JSMin_UnterminatedRegExpException( |
||
| 255 | "JSMin: Unterminated RegExp at byte {$this->inputIndex}: {$pattern}");
|
||
| 256 | } |
||
| 257 | $this->output .= $this->a; |
||
| 258 | $this->lastByteOut = $this->a; |
||
| 259 | } |
||
| 260 | $this->b = $this->next(); |
||
| 261 | } |
||
| 262 | // end case ACTION_DELETE_A_B |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | protected function isRegexpLiteral() |
||
| 270 | {
|
||
| 271 | if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
|
||
| 272 | // we obviously aren't dividing |
||
| 273 | return true; |
||
| 274 | } |
||
| 275 | |||
| 276 | // we have to check for a preceding keyword, and we don't need to pattern |
||
| 277 | // match over the whole output. |
||
| 278 | $recentOutput = substr($this->output, -10); |
||
| 279 | |||
| 280 | // check if return/typeof directly precede a pattern without a space |
||
| 281 | foreach (array('return', 'typeof') as $keyword) {
|
||
| 282 | if ($this->a !== substr($keyword, -1)) {
|
||
| 283 | // certainly wasn't keyword |
||
| 284 | continue; |
||
| 285 | } |
||
| 286 | if (preg_match("~(^|[\\s\\S])" . substr($keyword, 0, -1) . "$~", $recentOutput, $m)) {
|
||
| 287 | View Code Duplication | if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
|
|
| 288 | return true; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | // check all keywords |
||
| 294 | if ($this->a === ' ' || $this->a === "\n") {
|
||
| 295 | if (preg_match('~(^|[\\s\\S])(?:case|else|in|return|typeof)$~', $recentOutput, $m)) {
|
||
| 296 | View Code Duplication | if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
|
|
| 297 | return true; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | return false; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return the next character from stdin. Watch out for lookahead. If the character is a control character, |
||
| 307 | * translate it to a space or linefeed. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | View Code Duplication | protected function get() |
|
| 312 | {
|
||
| 313 | $c = $this->lookAhead; |
||
| 314 | $this->lookAhead = null; |
||
| 315 | if ($c === null) {
|
||
| 316 | // getc(stdin) |
||
| 317 | if ($this->inputIndex < $this->inputLength) {
|
||
| 318 | $c = $this->input[$this->inputIndex]; |
||
| 319 | $this->inputIndex += 1; |
||
| 320 | } else {
|
||
| 321 | $c = null; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) {
|
||
| 325 | return $c; |
||
| 326 | } |
||
| 327 | if ($c === "\r") {
|
||
| 328 | return "\n"; |
||
| 329 | } |
||
| 330 | return ' '; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Does $a indicate end of input? |
||
| 335 | * |
||
| 336 | * @param string $a |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | protected function isEOF($a) |
||
| 340 | {
|
||
| 341 | return ord($a) <= self::ORD_LF; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get next char (without getting it). If is ctrl character, translate to a space or newline. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | protected function peek() |
||
| 350 | {
|
||
| 351 | $this->lookAhead = $this->get(); |
||
| 352 | return $this->lookAhead; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character. |
||
| 357 | * |
||
| 358 | * @param string $c |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | protected function isAlphaNum($c) |
||
| 363 | {
|
||
| 364 | return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126);
|
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Consume a single line comment from input (possibly retaining it) |
||
| 369 | */ |
||
| 370 | View Code Duplication | protected function consumeSingleLineComment() |
|
| 371 | {
|
||
| 372 | $comment = ''; |
||
| 373 | while (true) {
|
||
| 374 | $get = $this->get(); |
||
| 375 | $comment .= $get; |
||
| 376 | if (ord($get) <= self::ORD_LF) { // end of line reached
|
||
| 377 | // if IE conditional comment |
||
| 378 | if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
|
||
| 379 | $this->keptComment .= "/{$comment}";
|
||
| 380 | } |
||
| 381 | return; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Consume a multiple line comment from input (possibly retaining it) |
||
| 388 | * |
||
| 389 | * @throws JSMin_UnterminatedCommentException |
||
| 390 | */ |
||
| 391 | View Code Duplication | protected function consumeMultipleLineComment() |
|
| 392 | {
|
||
| 393 | $this->get(); |
||
| 394 | $comment = ''; |
||
| 395 | for(;;) {
|
||
| 396 | $get = $this->get(); |
||
| 397 | if ($get === '*') {
|
||
| 398 | if ($this->peek() === '/') { // end of comment reached
|
||
| 399 | $this->get(); |
||
| 400 | if (0 === strpos($comment, '!')) {
|
||
| 401 | // preserved by YUI Compressor |
||
| 402 | if (!$this->keptComment) {
|
||
| 403 | // don't prepend a newline if two comments right after one another |
||
| 404 | $this->keptComment = "\n"; |
||
| 405 | } |
||
| 406 | $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n"; |
||
| 407 | } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
|
||
| 408 | // IE conditional |
||
| 409 | $this->keptComment .= "/*{$comment}*/";
|
||
| 410 | } |
||
| 411 | return; |
||
| 412 | } |
||
| 413 | } elseif ($get === null) {
|
||
| 414 | throw new JSMin_UnterminatedCommentException( |
||
| 415 | "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}");
|
||
| 416 | } |
||
| 417 | $comment .= $get; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get the next character, skipping over comments. Some comments may be preserved. |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | View Code Duplication | protected function next() |
|
| 427 | {
|
||
| 428 | $get = $this->get(); |
||
| 429 | if ($get === '/') {
|
||
| 430 | switch ($this->peek()) {
|
||
| 431 | case '/': |
||
| 432 | $this->consumeSingleLineComment(); |
||
| 433 | $get = "\n"; |
||
| 434 | break; |
||
| 435 | case '*': |
||
| 436 | $this->consumeMultipleLineComment(); |
||
| 437 | $get = ' '; |
||
| 438 | break; |
||
| 439 | } |
||
| 440 | } |
||
| 441 | return $get; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | class JSMin_UnterminatedStringException extends Exception {}
|
||
| 446 | class JSMin_UnterminatedCommentException extends Exception {}
|
||
| 447 | class JSMin_UnterminatedRegExpException extends Exception {} |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.