3 * jsmin.php - PHP implementation of Douglas Crockford's JSMin.
5 * This is pretty much a direct port of jsmin.c to PHP with just a few
6 * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
7 * outputs to stdout, this library accepts a string as input and returns another
10 * PHP 5 or higher is required.
12 * Permission is hereby granted to use this version of the library under the
13 * same terms as jsmin.c, which has the following license:
16 * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
18 * Permission is hereby granted, free of charge, to any person obtaining a copy of
19 * this software and associated documentation files (the "Software"), to deal in
20 * the Software without restriction, including without limitation the rights to
21 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
22 * of the Software, and to permit persons to whom the Software is furnished to do
23 * so, subject to the following conditions:
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
28 * The Software shall be used for Good, not Evil.
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 * @author Ryan Grove <ryan@wonko.com>
41 * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
42 * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
43 * @license http://opensource.org/licenses/mit-license.php MIT License
44 * @version 1.1.1 (2008-03-02)
45 * @link https://github.com/rgrove/jsmin-php/
51 const ACTION_KEEP_A = 1;
52 const ACTION_DELETE_A = 2;
53 const ACTION_DELETE_A_B = 3;
57 protected $input = '';
58 protected $inputIndex = 0;
59 protected $inputLength = 0;
60 protected $lookAhead = null;
61 protected $output = '';
63 // -- Public Static Methods --------------------------------------------------
70 * @param string $js Javascript to be minified
73 public static function minify($js) {
74 $jsmin = new JSMin($js);
78 // -- Public Instance Methods ------------------------------------------------
83 * @param string $input Javascript to be minified
85 public function __construct($input) {
86 $this->input = str_replace("\r\n", "\n", $input);
87 $this->inputLength = strlen($this->input);
90 // -- Protected Instance Methods ---------------------------------------------
93 * Action -- do something! What to do is determined by the $command argument.
95 * action treats a string as a single character. Wow!
96 * action recognizes a regular expression if it is preceded by ( or , or =.
100 * @throws JSMinException If parser errors are found:
101 * - Unterminated string literal
102 * - Unterminated regular expression set in regex literal
103 * - Unterminated regular expression literal
104 * @param int $command One of class constants:
105 * ACTION_KEEP_A Output A. Copy B to A. Get the next B.
106 * ACTION_DELETE_A Copy B to A. Get the next B. (Delete A).
107 * ACTION_DELETE_A_B Get the next B. (Delete B).
109 protected function action($command) {
111 case self::ACTION_KEEP_A:
112 $this->output .= $this->a;
114 case self::ACTION_DELETE_A:
117 if ($this->a === "'" || $this->a === '"') {
119 $this->output .= $this->a;
120 $this->a = $this->get();
122 if ($this->a === $this->b) {
126 if (ord($this->a) <= self::ORD_LF) {
127 throw new JSMinException('Unterminated string literal.');
130 if ($this->a === '\\') {
131 $this->output .= $this->a;
132 $this->a = $this->get();
137 case self::ACTION_DELETE_A_B:
138 $this->b = $this->next();
140 if ($this->b === '/' && (
141 $this->a === '(' || $this->a === ',' || $this->a === '=' ||
142 $this->a === ':' || $this->a === '[' || $this->a === '!' ||
143 $this->a === '&' || $this->a === '|' || $this->a === '?' ||
144 $this->a === '{' || $this->a === '}' || $this->a === ';' ||
145 $this->a === "\n" )) {
147 $this->output .= $this->a . $this->b;
150 $this->a = $this->get();
152 if ($this->a === '[') {
154 inside a regex [...] set, which MAY contain a '/' itself. Example: mootools Form.Validator near line 460:
155 return Form.Validator.getValidator('IsEmpty').test(element) || (/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(element.get('value'));
158 $this->output .= $this->a;
159 $this->a = $this->get();
161 if ($this->a === ']') {
163 } elseif ($this->a === '\\') {
164 $this->output .= $this->a;
165 $this->a = $this->get();
166 } elseif (ord($this->a) <= self::ORD_LF) {
167 throw new JSMinException('Unterminated regular expression set in regex literal.');
170 } elseif ($this->a === '/') {
172 } elseif ($this->a === '\\') {
173 $this->output .= $this->a;
174 $this->a = $this->get();
175 } elseif (ord($this->a) <= self::ORD_LF) {
176 throw new JSMinException('Unterminated regular expression literal.');
179 $this->output .= $this->a;
182 $this->b = $this->next();
188 * Get next char. Convert ctrl char to space.
190 * @return string|null
192 protected function get() {
193 $c = $this->lookAhead;
194 $this->lookAhead = null;
197 if ($this->inputIndex < $this->inputLength) {
198 $c = substr($this->input, $this->inputIndex, 1);
199 $this->inputIndex += 1;
209 if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
217 * Is $c a letter, digit, underscore, dollar sign, or non-ASCII character.
221 protected function isAlphaNum($c) {
222 return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
226 * Perform minification, return result
232 protected function min() {
234 $this->action(self::ACTION_DELETE_A_B);
236 while ($this->a !== null) {
239 if ($this->isAlphaNum($this->b)) {
240 $this->action(self::ACTION_KEEP_A);
242 $this->action(self::ACTION_DELETE_A);
253 $this->action(self::ACTION_KEEP_A);
257 $this->action(self::ACTION_DELETE_A_B);
261 if ($this->isAlphaNum($this->b)) {
262 $this->action(self::ACTION_KEEP_A);
265 $this->action(self::ACTION_DELETE_A);
273 if ($this->isAlphaNum($this->a)) {
274 $this->action(self::ACTION_KEEP_A);
278 $this->action(self::ACTION_DELETE_A_B);
290 $this->action(self::ACTION_KEEP_A);
294 if ($this->isAlphaNum($this->a)) {
295 $this->action(self::ACTION_KEEP_A);
298 $this->action(self::ACTION_DELETE_A_B);
304 $this->action(self::ACTION_KEEP_A);
310 return $this->output;
314 * Get the next character, skipping over comments. peek() is used to see
315 * if a '/' is followed by a '/' or '*'.
319 * @throws JSMinException On unterminated comment.
322 protected function next() {
326 switch($this->peek()) {
331 if (ord($c) <= self::ORD_LF) {
340 switch($this->get()) {
342 if ($this->peek() === '/') {
349 throw new JSMinException('Unterminated comment.');
362 * Get next char. If is ctrl character, translate to a space or newline.
365 * @return string|null
367 protected function peek() {
368 $this->lookAhead = $this->get();
369 return $this->lookAhead;
373 // -- Exceptions ---------------------------------------------------------------
374 class JSMinException extends Exception {}