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 http://github.com/rgrove/jsmin-php/
54 protected $input = '';
55 protected $inputIndex = 0;
56 protected $inputLength = 0;
57 protected $lookAhead = null;
58 protected $output = '';
60 // -- Public Static Methods --------------------------------------------------
62 public static function minify( $js ) {
63 $jsmin = new JSMin( $js );
68 // -- Public Instance Methods ------------------------------------------------
70 public function __construct( $input ) {
71 $this->input
= str_replace( "\r\n", "\n", $input );
72 $this->inputLength
= strlen( $this->input
);
75 // -- Protected Instance Methods ---------------------------------------------
77 protected function action( $d ) {
80 $this->output
.= $this->a
;
85 if ( $this->a
=== "'" ||
$this->a
=== '"' ) {
87 $this->output
.= $this->a
;
88 $this->a
= $this->get();
90 if ( $this->a
=== $this->b
) {
94 if ( ord( $this->a
) <= self
::ORD_LF
) {
95 throw new JSMinException( 'Unterminated string literal.' );
98 if ( $this->a
=== '\\' ) {
99 $this->output
.= $this->a
;
100 $this->a
= $this->get();
106 $this->b
= $this->next();
108 if ( $this->b
=== '/' && (
109 $this->a
=== '(' ||
$this->a
=== ',' ||
$this->a
=== '=' ||
110 $this->a
=== ':' ||
$this->a
=== '[' ||
$this->a
=== '!' ||
111 $this->a
=== '&' ||
$this->a
=== '|' ||
$this->a
=== '?' ) ) {
113 $this->output
.= $this->a
. $this->b
;
116 $this->a
= $this->get();
118 if ( $this->a
=== '/' ) {
120 } elseif ( $this->a
=== '\\' ) {
121 $this->output
.= $this->a
;
122 $this->a
= $this->get();
123 } elseif ( ord( $this->a
) <= self
::ORD_LF
) {
124 throw new JSMinException( 'Unterminated regular expression ' .
128 $this->output
.= $this->a
;
131 $this->b
= $this->next();
136 protected function get() {
137 $c = $this->lookAhead
;
138 $this->lookAhead
= null;
141 if ( $this->inputIndex
< $this->inputLength
) {
142 $c = substr( $this->input
, $this->inputIndex
, 1 );
143 $this->inputIndex +
= 1;
153 if ( $c === null ||
$c === "\n" ||
ord( $c ) >= self
::ORD_SPACE
) {
160 protected function isAlphaNum( $c ) {
161 return ord( $c ) > 126 ||
$c === '\\' ||
preg_match( '/^[\w\$]$/', $c ) === 1;
164 protected function min() {
168 while ( $this->a
!== null ) {
169 switch ( $this->a
) {
171 if ( $this->isAlphaNum( $this->b
) ) {
179 switch ( $this->b
) {
193 if ( $this->isAlphaNum( $this->b
) ) {
203 switch ( $this->b
) {
205 if ( $this->isAlphaNum( $this->a
) ) {
214 switch ( $this->a
) {
226 if ( $this->isAlphaNum( $this->a
) ) {
242 return $this->output
;
245 protected function next() {
249 switch( $this->peek() ) {
254 if ( ord( $c ) <= self
::ORD_LF
) {
263 switch( $this->get() ) {
265 if ( $this->peek() === '/' ) {
272 throw new JSMinException( 'Unterminated comment.' );
284 protected function peek() {
285 $this->lookAhead
= $this->get();
286 return $this->lookAhead
;
290 // -- Exceptions ---------------------------------------------------------------
291 class JSMinException
extends Exception
{}