95c275b621d1241b2913487b9d84b1533a5f08c8
3 /* jsmin.js - 2006-08-31
5 This work is an adaptation of jsminc.c published by Douglas Crockford.
6 Permission is hereby granted to use the Javascript version under the same
7 conditions as the jsmin.c on which it is based.
12 Copyright (c) 2002 Douglas Crockford (www.crockford.com)
14 Permission is hereby granted, free of charge, to any person obtaining a copy of
15 this software and associated documentation files (the "Software"), to deal in
16 the Software without restriction, including without limitation the rights to
17 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18 of the Software, and to permit persons to whom the Software is furnished to do
19 so, subject to the following conditions:
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
24 The Software shall be used for Good, not Evil.
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 1: minimal, keep linefeeds if single
37 2: normal, the standard algorithm
38 3: agressive, remove any linefeed and doesn't take care of potential
39 missing semicolons (can be regressive)
45 String
.prototype.has = function(c
) {
46 return this.indexOf(c
) > -1;
49 function jsmin(comment
, input
, level
) {
51 if (input
=== undefined) {
55 } else if (level
=== undefined || level
< 1 || level
> 3) {
59 if (comment
.length
> 0) {
66 LETTERS
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
67 DIGITS
= '0123456789',
68 ALNUM
= LETTERS
+ DIGITS
+ '_$\\',
72 /* isAlphanum -- return true if the character is a letter, digit, underscore,
73 dollar sign, or non-ASCII character.
76 function isAlphanum(c
) {
77 return c
!= EOF
&& (ALNUM
.has(c
) || c
.charCodeAt(0) > 126);
81 /* get -- return the next character. Watch out for lookahead. If the
82 character is a control character, translate it to a space or
94 c
= input
.charAt(get.i
);
97 if (c
>= ' ' || c
== '\n') {
107 get.l
= input
.length
;
110 /* peek -- get the next character without getting it.
114 theLookahead
= get();
119 /* next -- get the next character, excluding comments. peek() is used to see
120 if a '/' is followed by a '/' or '*'.
127 if (c
=== ";" && peek() === ";") {
157 throw 'Error: Unterminated comment.';
169 /* action -- do something! What you do is determined by the argument:
170 1 Output A. Copy B to A. Get the next B.
171 2 Copy B to A. Get the next B. (Delete A).
172 3 Get the next B. (Delete B).
173 action treats a string as a single character. Wow!
174 action recognizes a regular expression if it is preceded by ( or , or =.
187 if (a
== '\'' || a
== '"') {
195 throw 'Error: unterminated string literal: ' + a
;
207 if (b
== '/' && '(,=:[!&|'.has(a
)) {
214 } else if (a
=='\\') {
217 } else if (a
<= '\n') {
218 throw 'Error: unterminated Regular Expression literal';
229 /* m -- Copy the input to the output, deleting the characters which are
230 insignificant to JavaScript. Comments will be removed. Tabs will be
231 replaced with spaces. Carriage returns will be replaced with
233 Most spaces and linefeeds will be removed.
268 if (level
== 1 && b
!= '\n') {
286 if (level
== 1 && a
!= '\n') {
322 jsmin
.oldSize
= input
.length
;
324 jsmin
.newSize
= ret
.length
;
326 return comment
+ ret
;
330 importPackage(java
.io
);
332 // in is a reserved javascript word, so we need to use [] for access
333 var readingIn
= new BufferedReader(new InputStreamReader(java
.lang
.System
["in"]));
337 sInput
+= str
+ '\n';
339 str
= readingIn
.readLine();
341 print(jsmin(sInput
));