Index: trunk/phase3/includes/JSMin.php |
— | — | @@ -46,246 +46,246 @@ |
47 | 47 | */ |
48 | 48 | |
49 | 49 | class JSMin { |
50 | | - const ORD_LF = 10; |
51 | | - const ORD_SPACE = 32; |
| 50 | + const ORD_LF = 10; |
| 51 | + const ORD_SPACE = 32; |
52 | 52 | |
53 | | - protected $a = ''; |
54 | | - protected $b = ''; |
55 | | - protected $input = ''; |
56 | | - protected $inputIndex = 0; |
57 | | - protected $inputLength = 0; |
58 | | - protected $lookAhead = null; |
59 | | - protected $output = ''; |
| 53 | + protected $a = ''; |
| 54 | + protected $b = ''; |
| 55 | + protected $input = ''; |
| 56 | + protected $inputIndex = 0; |
| 57 | + protected $inputLength = 0; |
| 58 | + protected $lookAhead = null; |
| 59 | + protected $output = ''; |
60 | 60 | |
61 | | - // -- Public Static Methods -------------------------------------------------- |
| 61 | + // -- Public Static Methods -------------------------------------------------- |
62 | 62 | |
63 | | - public static function minify($js) { |
64 | | - $jsmin = new JSMin($js); |
65 | | - return $jsmin->min(); |
66 | | - } |
| 63 | + public static function minify( $js ) { |
| 64 | + $jsmin = new JSMin( $js ); |
| 65 | + return $jsmin->min(); |
| 66 | + } |
67 | 67 | |
68 | | - // -- Public Instance Methods ------------------------------------------------ |
| 68 | + // -- Public Instance Methods ------------------------------------------------ |
69 | 69 | |
70 | | - public function __construct($input) { |
71 | | - $this->input = str_replace("\r\n", "\n", $input); |
72 | | - $this->inputLength = strlen($this->input); |
73 | | - } |
| 70 | + public function __construct( $input ) { |
| 71 | + $this->input = str_replace( "\r\n", "\n", $input ); |
| 72 | + $this->inputLength = strlen( $this->input ); |
| 73 | + } |
74 | 74 | |
75 | | - // -- Protected Instance Methods --------------------------------------------- |
| 75 | + // -- Protected Instance Methods --------------------------------------------- |
76 | 76 | |
77 | | - protected function action($d) { |
78 | | - switch($d) { |
79 | | - case 1: |
80 | | - $this->output .= $this->a; |
| 77 | + protected function action( $d ) { |
| 78 | + switch( $d ) { |
| 79 | + case 1: |
| 80 | + $this->output .= $this->a; |
81 | 81 | |
82 | | - case 2: |
83 | | - $this->a = $this->b; |
| 82 | + case 2: |
| 83 | + $this->a = $this->b; |
84 | 84 | |
85 | | - if ($this->a === "'" || $this->a === '"') { |
86 | | - for (;;) { |
87 | | - $this->output .= $this->a; |
88 | | - $this->a = $this->get(); |
| 85 | + if ( $this->a === "'" || $this->a === '"' ) { |
| 86 | + for ( ; ; ) { |
| 87 | + $this->output .= $this->a; |
| 88 | + $this->a = $this->get(); |
89 | 89 | |
90 | | - if ($this->a === $this->b) { |
91 | | - break; |
92 | | - } |
| 90 | + if ( $this->a === $this->b ) { |
| 91 | + break; |
| 92 | + } |
93 | 93 | |
94 | | - if (ord($this->a) <= self::ORD_LF) { |
95 | | - throw new JSMinException('Unterminated string literal.'); |
96 | | - } |
| 94 | + if ( ord( $this->a ) <= self::ORD_LF ) { |
| 95 | + throw new JSMinException( 'Unterminated string literal.' ); |
| 96 | + } |
97 | 97 | |
98 | | - if ($this->a === '\\') { |
99 | | - $this->output .= $this->a; |
100 | | - $this->a = $this->get(); |
101 | | - } |
102 | | - } |
103 | | - } |
| 98 | + if ( $this->a === '\\' ) { |
| 99 | + $this->output .= $this->a; |
| 100 | + $this->a = $this->get(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
104 | 104 | |
105 | | - case 3: |
106 | | - $this->b = $this->next(); |
| 105 | + case 3: |
| 106 | + $this->b = $this->next(); |
107 | 107 | |
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 === '?')) { |
| 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 === '?' ) ) { |
112 | 112 | |
113 | | - $this->output .= $this->a . $this->b; |
| 113 | + $this->output .= $this->a . $this->b; |
114 | 114 | |
115 | | - for (;;) { |
116 | | - $this->a = $this->get(); |
| 115 | + for ( ; ; ) { |
| 116 | + $this->a = $this->get(); |
117 | 117 | |
118 | | - if ($this->a === '/') { |
119 | | - break; |
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 '. |
125 | | - 'literal.'); |
126 | | - } |
| 118 | + if ( $this->a === '/' ) { |
| 119 | + break; |
| 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 ' . |
| 125 | + 'literal.' ); |
| 126 | + } |
127 | 127 | |
128 | | - $this->output .= $this->a; |
129 | | - } |
| 128 | + $this->output .= $this->a; |
| 129 | + } |
130 | 130 | |
131 | | - $this->b = $this->next(); |
132 | | - } |
133 | | - } |
134 | | - } |
| 131 | + $this->b = $this->next(); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
135 | 135 | |
136 | | - protected function get() { |
137 | | - $c = $this->lookAhead; |
138 | | - $this->lookAhead = null; |
| 136 | + protected function get() { |
| 137 | + $c = $this->lookAhead; |
| 138 | + $this->lookAhead = null; |
139 | 139 | |
140 | | - if ($c === null) { |
141 | | - if ($this->inputIndex < $this->inputLength) { |
142 | | - $c = substr($this->input, $this->inputIndex, 1); |
143 | | - $this->inputIndex += 1; |
144 | | - } else { |
145 | | - $c = null; |
146 | | - } |
147 | | - } |
| 140 | + if ( $c === null ) { |
| 141 | + if ( $this->inputIndex < $this->inputLength ) { |
| 142 | + $c = substr( $this->input, $this->inputIndex, 1 ); |
| 143 | + $this->inputIndex += 1; |
| 144 | + } else { |
| 145 | + $c = null; |
| 146 | + } |
| 147 | + } |
148 | 148 | |
149 | | - if ($c === "\r") { |
150 | | - return "\n"; |
151 | | - } |
| 149 | + if ( $c === "\r" ) { |
| 150 | + return "\n"; |
| 151 | + } |
152 | 152 | |
153 | | - if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) { |
154 | | - return $c; |
155 | | - } |
| 153 | + if ( $c === null || $c === "\n" || ord( $c ) >= self::ORD_SPACE ) { |
| 154 | + return $c; |
| 155 | + } |
156 | 156 | |
157 | | - return ' '; |
158 | | - } |
| 157 | + return ' '; |
| 158 | + } |
159 | 159 | |
160 | | - protected function isAlphaNum($c) { |
161 | | - return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1; |
162 | | - } |
| 160 | + protected function isAlphaNum( $c ) { |
| 161 | + return ord( $c ) > 126 || $c === '\\' || preg_match( '/^[\w\$]$/', $c ) === 1; |
| 162 | + } |
163 | 163 | |
164 | | - protected function min() { |
165 | | - $this->a = "\n"; |
166 | | - $this->action(3); |
| 164 | + protected function min() { |
| 165 | + $this->a = "\n"; |
| 166 | + $this->action( 3 ); |
167 | 167 | |
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); |
175 | | - } |
176 | | - break; |
| 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 ); |
| 175 | + } |
| 176 | + break; |
177 | 177 | |
178 | | - case "\n": |
179 | | - switch ($this->b) { |
180 | | - case '{': |
181 | | - case '[': |
182 | | - case '(': |
183 | | - case '+': |
184 | | - case '-': |
185 | | - $this->action(1); |
186 | | - break; |
| 178 | + case "\n": |
| 179 | + switch ( $this->b ) { |
| 180 | + case '{': |
| 181 | + case '[': |
| 182 | + case '(': |
| 183 | + case '+': |
| 184 | + case '-': |
| 185 | + $this->action( 1 ); |
| 186 | + break; |
187 | 187 | |
188 | | - case ' ': |
189 | | - $this->action(3); |
190 | | - break; |
| 188 | + case ' ': |
| 189 | + $this->action( 3 ); |
| 190 | + break; |
191 | 191 | |
192 | | - default: |
193 | | - if ($this->isAlphaNum($this->b)) { |
194 | | - $this->action(1); |
195 | | - } |
196 | | - else { |
197 | | - $this->action(2); |
198 | | - } |
199 | | - } |
200 | | - break; |
| 192 | + default: |
| 193 | + if ( $this->isAlphaNum( $this->b ) ) { |
| 194 | + $this->action( 1 ); |
| 195 | + } |
| 196 | + else { |
| 197 | + $this->action( 2 ); |
| 198 | + } |
| 199 | + } |
| 200 | + break; |
201 | 201 | |
202 | | - default: |
203 | | - switch ($this->b) { |
204 | | - case ' ': |
205 | | - if ($this->isAlphaNum($this->a)) { |
206 | | - $this->action(1); |
207 | | - break; |
208 | | - } |
| 202 | + default: |
| 203 | + switch ( $this->b ) { |
| 204 | + case ' ': |
| 205 | + if ( $this->isAlphaNum( $this->a ) ) { |
| 206 | + $this->action( 1 ); |
| 207 | + break; |
| 208 | + } |
209 | 209 | |
210 | | - $this->action(3); |
211 | | - break; |
| 210 | + $this->action( 3 ); |
| 211 | + break; |
212 | 212 | |
213 | | - case "\n": |
214 | | - switch ($this->a) { |
215 | | - case '}': |
216 | | - case ']': |
217 | | - case ')': |
218 | | - case '+': |
219 | | - case '-': |
220 | | - case '"': |
221 | | - case "'": |
222 | | - $this->action(1); |
223 | | - break; |
| 213 | + case "\n": |
| 214 | + switch ( $this->a ) { |
| 215 | + case '}': |
| 216 | + case ']': |
| 217 | + case ')': |
| 218 | + case '+': |
| 219 | + case '-': |
| 220 | + case '"': |
| 221 | + case "'": |
| 222 | + $this->action( 1 ); |
| 223 | + break; |
224 | 224 | |
225 | | - default: |
226 | | - if ($this->isAlphaNum($this->a)) { |
227 | | - $this->action(1); |
228 | | - } |
229 | | - else { |
230 | | - $this->action(3); |
231 | | - } |
232 | | - } |
233 | | - break; |
| 225 | + default: |
| 226 | + if ( $this->isAlphaNum( $this->a ) ) { |
| 227 | + $this->action( 1 ); |
| 228 | + } |
| 229 | + else { |
| 230 | + $this->action( 3 ); |
| 231 | + } |
| 232 | + } |
| 233 | + break; |
234 | 234 | |
235 | | - default: |
236 | | - $this->action(1); |
237 | | - break; |
238 | | - } |
239 | | - } |
240 | | - } |
| 235 | + default: |
| 236 | + $this->action( 1 ); |
| 237 | + break; |
| 238 | + } |
| 239 | + } |
| 240 | + } |
241 | 241 | |
242 | | - return $this->output; |
243 | | - } |
| 242 | + return $this->output; |
| 243 | + } |
244 | 244 | |
245 | | - protected function next() { |
246 | | - $c = $this->get(); |
| 245 | + protected function next() { |
| 246 | + $c = $this->get(); |
247 | 247 | |
248 | | - if ($c === '/') { |
249 | | - switch($this->peek()) { |
250 | | - case '/': |
251 | | - for (;;) { |
252 | | - $c = $this->get(); |
| 248 | + if ( $c === '/' ) { |
| 249 | + switch( $this->peek() ) { |
| 250 | + case '/': |
| 251 | + for ( ; ; ) { |
| 252 | + $c = $this->get(); |
253 | 253 | |
254 | | - if (ord($c) <= self::ORD_LF) { |
255 | | - return $c; |
256 | | - } |
257 | | - } |
| 254 | + if ( ord( $c ) <= self::ORD_LF ) { |
| 255 | + return $c; |
| 256 | + } |
| 257 | + } |
258 | 258 | |
259 | | - case '*': |
260 | | - $this->get(); |
| 259 | + case '*': |
| 260 | + $this->get(); |
261 | 261 | |
262 | | - for (;;) { |
263 | | - switch($this->get()) { |
264 | | - case '*': |
265 | | - if ($this->peek() === '/') { |
266 | | - $this->get(); |
267 | | - return ' '; |
268 | | - } |
269 | | - break; |
| 262 | + for ( ; ; ) { |
| 263 | + switch( $this->get() ) { |
| 264 | + case '*': |
| 265 | + if ( $this->peek() === '/' ) { |
| 266 | + $this->get(); |
| 267 | + return ' '; |
| 268 | + } |
| 269 | + break; |
270 | 270 | |
271 | | - case null: |
272 | | - throw new JSMinException('Unterminated comment.'); |
273 | | - } |
274 | | - } |
| 271 | + case null: |
| 272 | + throw new JSMinException( 'Unterminated comment.' ); |
| 273 | + } |
| 274 | + } |
275 | 275 | |
276 | | - default: |
277 | | - return $c; |
278 | | - } |
279 | | - } |
| 276 | + default: |
| 277 | + return $c; |
| 278 | + } |
| 279 | + } |
280 | 280 | |
281 | | - return $c; |
282 | | - } |
| 281 | + return $c; |
| 282 | + } |
283 | 283 | |
284 | | - protected function peek() { |
285 | | - $this->lookAhead = $this->get(); |
286 | | - return $this->lookAhead; |
287 | | - } |
| 284 | + protected function peek() { |
| 285 | + $this->lookAhead = $this->get(); |
| 286 | + return $this->lookAhead; |
| 287 | + } |
288 | 288 | } |
289 | 289 | |
290 | 290 | // -- Exceptions --------------------------------------------------------------- |
291 | 291 | class JSMinException extends Exception {} |
292 | | -?> |
\ No newline at end of file |
| 292 | +?> |