Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / php / minify / JSMin.php
blobf8760e4f0396454a4aa99ff5e9598c57041bf692
1 <?php
3 if ( !defined( 'MEDIAWIKI' ) ) die( 1 );
4 /**
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
10 * string as output.
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:
17 * --
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
38 * SOFTWARE.
39 * --
41 * @package JSMin
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/
50 class JSMin {
51 const ORD_LF = 10;
52 const ORD_SPACE = 32;
54 protected $a = '';
55 protected $b = '';
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 );
66 return $jsmin->min();
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 ) {
79 switch( $d ) {
80 case 1:
81 $this->output .= $this->a;
83 case 2:
84 $this->a = $this->b;
86 if( $this->a === "'" || $this->a === '"' ) {
87 for (;;) {
88 $this->output .= $this->a;
89 $this->a = $this->get();
91 if( $this->a === $this->b ) {
92 break;
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();
106 case 3:
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;
116 for (;;) {
117 $this->a = $this->get();
119 if( $this->a === '/' ) {
120 break;
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;
140 if( $c === null ) {
141 if( $this->inputIndex < $this->inputLength ) {
142 $c = $this->input[$this->inputIndex];
143 $this->inputIndex += 1;
144 } else {
145 $c = null;
149 if( $c === "\r" ) {
150 return "\n";
153 if( $c === null || $c === "\n" || ord( $c ) >= self::ORD_SPACE ) {
154 return $c;
157 return ' ';
160 protected function isAlphaNum( $c ) {
161 return ord( $c ) > 126 || $c === '\\' || preg_match( '/^[\w\$]$/', $c ) === 1;
164 protected function min() {
165 $this->a = "\n";
166 $this->action( 3 );
168 while( $this->a !== null ) {
169 switch( $this->a ) {
170 case ' ':
171 if( $this->isAlphaNum( $this->b ) ) {
172 $this->action( 1 );
173 } else {
174 $this->action( 2 );
176 break;
178 case "\n":
179 switch( $this->b ) {
180 case '{':
181 case '[':
182 case '(':
183 case '+':
184 case '-':
185 $this->action( 1 );
186 break;
188 case ' ':
189 $this->action( 3 );
190 break;
192 default:
193 if( $this->isAlphaNum( $this->b ) ) {
194 $this->action( 1 );
195 } else {
196 $this->action( 2 );
199 break;
201 default:
202 switch( $this->b ) {
203 case ' ':
204 if( $this->isAlphaNum( $this->a ) ) {
205 $this->action( 1 );
206 break;
209 $this->action( 3 );
210 break;
212 case "\n":
213 switch( $this->a ) {
214 case '}':
215 case ']':
216 case ')':
217 case '+':
218 case '-':
219 case '"':
220 case "'":
221 $this->action( 1 );
222 break;
224 default:
225 if( $this->isAlphaNum( $this->a ) ) {
226 $this->action( 1 );
227 } else {
228 $this->action( 3 );
231 break;
233 default:
234 $this->action( 1 );
235 break;
240 return $this->output;
243 protected function next() {
244 $c = $this->get();
246 if( $c === '/' ) {
247 switch( $this->peek() ) {
248 case '/':
249 for (;;) {
250 $c = $this->get();
252 if( ord( $c ) <= self::ORD_LF ) {
253 return $c;
257 case '*':
258 $this->get();
260 for (;;) {
261 switch( $this->get() ) {
262 case '*':
263 if( $this->peek() === '/' ) {
264 $this->get();
265 return ' ';
267 break;
269 case null:
270 throw new JSMinException( 'Unterminated comment.' );
274 default:
275 return $c;
279 return $c;
282 protected function peek() {
283 $this->lookAhead = $this->get();
284 return $this->lookAhead;
288 // -- Exceptions ---------------------------------------------------------------
289 class JSMinException extends Exception {}