3 * JSMin.php - modified PHP implementation of Douglas Crockford's JSMin.
6 * $minifiedJs = JSMin::minify($js);
9 * This is a modified port of jsmin.c. Improvements:
11 * Does not choke on some regexp literals containing quote characters. E.g. /'/
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
16 * Preserves multi-line comments that begin with /*!
18 * PHP 5 or higher is required.
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:
24 * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
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:
33 * The above copyright notice and this permission notice shall be included in all
34 * copies or substantial portions of the Software.
36 * The Software shall be used for Good, not Evil.
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
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/
60 const ACTION_KEEP_A = 1;
61 const ACTION_DELETE_A = 2;
62 const ACTION_DELETE_A_B = 3;
66 protected $input = '';
67 protected $inputIndex = 0;
68 protected $inputLength = 0;
69 protected $lookAhead = null;
70 protected $output = '';
71 protected $lastByteOut = '';
72 protected $keptComment = '';
77 * @param string $js Javascript to be minified
81 public static function minify($js)
83 $jsmin = new JSMin($js);
88 * @param string $input
90 public function __construct($input)
92 $this->input = $input;
96 * Perform minification, return result
100 public function min()
102 if ($this->output !== '') { // min already run
103 return $this->output;
107 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
108 $mbIntEnc = mb_internal_encoding();
109 mb_internal_encoding('8bit');
111 $this->input = str_replace("\r\n", "\n", $this->input);
112 $this->inputLength = strlen($this->input);
114 $this->action(self::ACTION_DELETE_A_B);
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;
127 } elseif ($this->a === "\n") {
128 if ($this->b === ' ') {
129 $command = self::ACTION_DELETE_A_B;
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;
138 } elseif (! $this->isAlphaNum($this->a)) {
140 || ($this->b === "\n"
141 && (false === strpos('}])+-"\'', $this->a)))) {
142 $command = self::ACTION_DELETE_A_B;
145 $this->action($command);
147 $this->output = trim($this->output);
149 if ($mbIntEnc !== null) {
150 mb_internal_encoding($mbIntEnc);
152 return $this->output;
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.
160 * @param int $command
161 * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException
163 protected function action($command)
165 // make sure we don't compress "a + ++b" to "a+++b", etc.
166 if ($command === self::ACTION_DELETE_A_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;
178 case self::ACTION_KEEP_A: // 1
179 $this->output .= $this->a;
181 if ($this->keptComment) {
182 $this->output = rtrim($this->output, "\n");
183 $this->output .= $this->keptComment;
184 $this->keptComment = '';
187 $this->lastByteOut = $this->a;
189 // fallthrough intentional
190 case self::ACTION_DELETE_A: // 2
192 if ($this->a === "'" || $this->a === '"') { // string literal
193 $str = $this->a; // in case needed for exception
195 $this->output .= $this->a;
196 $this->lastByteOut = $this->a;
198 $this->a = $this->get();
199 if ($this->a === $this->b) { // end quote
202 if ($this->isEOF($this->a)) {
203 throw new JSMin_UnterminatedStringException(
204 "JSMin: Unterminated String at byte {$this->inputIndex}: {$str}");
207 if ($this->a === '\\') {
208 $this->output .= $this->a;
209 $this->lastByteOut = $this->a;
211 $this->a = $this->get();
217 // fallthrough intentional
218 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
224 $this->a = $this->get();
225 $pattern .= $this->a;
226 if ($this->a === '[') {
228 $this->output .= $this->a;
229 $this->a = $this->get();
230 $pattern .= $this->a;
231 if ($this->a === ']') {
234 if ($this->a === '\\') {
235 $this->output .= $this->a;
236 $this->a = $this->get();
237 $pattern .= $this->a;
239 if ($this->isEOF($this->a)) {
240 throw new JSMin_UnterminatedRegExpException(
241 "JSMin: Unterminated set in RegExp at byte "
242 . $this->inputIndex .": {$pattern}");
247 if ($this->a === '/') { // end pattern
248 break; // while (true)
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}");
257 $this->output .= $this->a;
258 $this->lastByteOut = $this->a;
260 $this->b = $this->next();
262 // end case ACTION_DELETE_A_B
269 protected function isRegexpLiteral()
271 if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
272 // we obviously aren't dividing
275 if ($this->a === ' ' || $this->a === "\n") {
276 $length = strlen($this->output);
277 if ($length < 2) { // weird edge case
280 // you can't divide a keyword
281 if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
282 if ($this->output === $m[0]) { // odd but could happen
285 // make sure it's a keyword, not end of an identifier
286 $charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
287 if (! $this->isAlphaNum($charBeforeKeyword)) {
296 * Return the next character from stdin. Watch out for lookahead. If the character is a control character,
297 * translate it to a space or linefeed.
301 protected function get()
303 $c = $this->lookAhead;
304 $this->lookAhead = null;
307 if ($this->inputIndex < $this->inputLength) {
308 $c = $this->input[$this->inputIndex];
309 $this->inputIndex += 1;
314 if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) {
324 * Does $a indicate end of input?
329 protected function isEOF($a)
331 return ord($a) <= self::ORD_LF;
335 * Get next char (without getting it). If is ctrl character, translate to a space or newline.
339 protected function peek()
341 $this->lookAhead = $this->get();
342 return $this->lookAhead;
346 * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character.
352 protected function isAlphaNum($c)
354 return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126);
358 * Consume a single line comment from input (possibly retaining it)
360 protected function consumeSingleLineComment()
366 if (ord($get) <= self::ORD_LF) { // end of line reached
367 // if IE conditional comment
368 if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
369 $this->keptComment .= "/{$comment}";
377 * Consume a multiple line comment from input (possibly retaining it)
379 * @throws JSMin_UnterminatedCommentException
381 protected function consumeMultipleLineComment()
388 if ($this->peek() === '/') { // end of comment reached
390 if (0 === strpos($comment, '!')) {
391 // preserved by YUI Compressor
392 if (!$this->keptComment) {
393 // don't prepend a newline if two comments right after one another
394 $this->keptComment = "\n";
396 $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n";
397 } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
399 $this->keptComment .= "/*{$comment}*/";
403 } elseif ($get === null) {
404 throw new JSMin_UnterminatedCommentException(
405 "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}");
412 * Get the next character, skipping over comments. Some comments may be preserved.
416 protected function next()
420 switch ($this->peek()) {
422 $this->consumeSingleLineComment();
426 $this->consumeMultipleLineComment();
435 class JSMin_UnterminatedStringException extends Exception {}
436 class JSMin_UnterminatedCommentException extends Exception {}
437 class JSMin_UnterminatedRegExpException extends Exception {}