3 if ( !defined( 'MEDIAWIKI' ) ) die( 1 );
5 * jsmin.php - PHP implementation of Douglas Crockford's JSMin.
7 * This is pretty much a direct port of jsmin.c to PHP with just a few
8 * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
9 * outputs to stdout, this library accepts a string as input and returns another
12 * PHP 5 or higher is required.
14 * Permission is hereby granted to use this version of the library under the
15 * same terms as jsmin.c, which has the following license:
18 * Copyright (c) 2002 Douglas Crockford (www.crockford.com)
20 * Permission is hereby granted, free of charge, to any person obtaining a copy of
21 * this software and associated documentation files (the "Software"), to deal in
22 * the Software without restriction, including without limitation the rights to
23 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
24 * of the Software, and to permit persons to whom the Software is furnished to do
25 * so, subject to the following conditions:
27 * The above copyright notice and this permission notice shall be included in all
28 * copies or substantial portions of the Software.
30 * The Software shall be used for Good, not Evil.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42 * @author Ryan Grove <ryan@wonko.com>
43 * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
44 * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
45 * @license http://opensource.org/licenses/mit-license.php MIT License
46 * @version 1.1.1 (2008-03-02)
47 * @link http://code.google.com/p/jsmin-php/
56 protected $input = '';
57 protected $inputIndex = 0;
58 protected $inputLength = 0;
59 protected $lookAhead = null;
60 protected $output = '';
62 // -- Public Static Methods --------------------------------------------------
64 public static function minify( $js ) {
65 $jsmin = new JSMin( $js );
69 // -- Public Instance Methods ------------------------------------------------
71 public function __construct( $input ) {
72 $this->input
= str_replace( "\r\n", "\n", $input );
73 $this->inputLength
= strlen( $this->input
);
76 // -- Protected Instance Methods ---------------------------------------------
78 protected function action( $d ) {
81 $this->output
.= $this->a
;
86 if( $this->a
=== "'" ||
$this->a
=== '"' ) {
88 $this->output
.= $this->a
;
89 $this->a
= $this->get();
91 if( $this->a
=== $this->b
) {
95 if( ord( $this->a
) <= self
::ORD_LF
) {
96 throw new JSMinException( 'Unterminated string literal.' );
99 if( $this->a
=== '\\' ) {
100 $this->output
.= $this->a
;
101 $this->a
= $this->get();
107 $this->b
= $this->next();
109 if( $this->b
=== '/' && (
110 $this->a
=== '(' ||
$this->a
=== ',' ||
$this->a
=== '=' ||
111 $this->a
=== ':' ||
$this->a
=== '[' ||
$this->a
=== '!' ||
112 $this->a
=== '&' ||
$this->a
=== '|' ||
$this->a
=== '?' ) ) {
114 $this->output
.= $this->a
. $this->b
;
117 $this->a
= $this->get();
119 if( $this->a
=== '/' ) {
121 } elseif( $this->a
=== '\\' ) {
122 $this->output
.= $this->a
;
123 $this->a
= $this->get();
124 } elseif( ord( $this->a
) <= self
::ORD_LF
) {
125 throw new JSMinException( 'Unterminated regular expression literal.' );
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 = $this->input
[$this->inputIndex
];
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 ) {
171 if( $this->isAlphaNum( $this->b
) ) {
193 if( $this->isAlphaNum( $this->b
) ) {
204 if( $this->isAlphaNum( $this->a
) ) {
225 if( $this->isAlphaNum( $this->a
) ) {
240 return $this->output
;
243 protected function next() {
247 switch( $this->peek() ) {
252 if( ord( $c ) <= self
::ORD_LF
) {
261 switch( $this->get() ) {
263 if( $this->peek() === '/' ) {
270 throw new JSMinException( 'Unterminated comment.' );
282 protected function peek() {
283 $this->lookAhead
= $this->get();
284 return $this->lookAhead
;
288 // -- Exceptions ---------------------------------------------------------------
289 class JSMinException
extends Exception
{}