9e1c43225fe7b4fbf4ef88ceb3e847097e361963
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 '.
129 $this->output
.= $this->a
;
132 $this->b
= $this->next();
137 protected function get() {
138 $c = $this->lookAhead
;
139 $this->lookAhead
= null;
142 if ($this->inputIndex
< $this->inputLength
) {
143 $c = $this->input
[$this->inputIndex
];
144 $this->inputIndex +
= 1;
154 if ($c === null ||
$c === "\n" ||
ord($c) >= self
::ORD_SPACE
) {
161 protected function isAlphaNum($c) {
162 return ord($c) > 126 ||
$c === '\\' ||
preg_match('/^[\w\$]$/', $c) === 1;
165 protected function min() {
169 while ($this->a
!== null) {
172 if ($this->isAlphaNum($this->b
)) {
194 if ($this->isAlphaNum($this->b
)) {
206 if ($this->isAlphaNum($this->a
)) {
227 if ($this->isAlphaNum($this->a
)) {
243 return $this->output
;
246 protected function next() {
250 switch($this->peek()) {
255 if (ord($c) <= self
::ORD_LF
) {
264 switch($this->get()) {
266 if ($this->peek() === '/') {
273 throw new JSMinException('Unterminated comment.');
285 protected function peek() {
286 $this->lookAhead
= $this->get();
287 return $this->lookAhead
;
291 // -- Exceptions ---------------------------------------------------------------
292 class JSMinException
extends Exception
{}