1 /* jsmin.js - 2006-08-31
3 This work is an adaptation of jsminc.c published by Douglas Crockford.
4 Permission is hereby granted to use the Javascript version under the same
5 conditions as the jsmin.c on which it is based.
10 Copyright (c) 2002 Douglas Crockford (www.crockford.com)
12 Permission is hereby granted, free of charge, to any person obtaining a copy of
13 this software and associated documentation files (the "Software"), to deal in
14 the Software without restriction, including without limitation the rights to
15 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is furnished to do
17 so, subject to the following conditions:
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
22 The Software shall be used for Good, not Evil.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 1: minimal, keep linefeeds if single
35 2: normal, the standard algorithm
36 3: agressive, remove any linefeed and doesn't take care of potential
37 missing semicolons (can be regressive)
43 String.prototype.has = function(c) {
44 return this.indexOf(c) > -1;
47 function jsmin(comment, input, level) {
49 if (input === undefined) {
53 } else if (level === undefined || level < 1 || level > 3) {
57 if (comment.length > 0) {
64 LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
65 DIGITS = '0123456789',
66 ALNUM = LETTERS + DIGITS + '_$\\',
70 /* isAlphanum -- return true if the character is a letter, digit, underscore,
71 dollar sign, or non-ASCII character.
74 function isAlphanum(c) {
75 return c != EOF && (ALNUM.has(c) || c.charCodeAt(0) > 126);
79 /* get -- return the next character. Watch out for lookahead. If the
80 character is a control character, translate it to a space or
92 c = input.charAt(get.i);
95 if (c >= ' ' || c == '\n') {
105 get.l = input.length;
108 /* peek -- get the next character without getting it.
112 theLookahead = get();
117 /* next -- get the next character, excluding comments. peek() is used to see
118 if a '/' is followed by a '/' or '*'.
145 throw 'Error: Unterminated comment.';
157 /* action -- do something! What you do is determined by the argument:
158 1 Output A. Copy B to A. Get the next B.
159 2 Copy B to A. Get the next B. (Delete A).
160 3 Get the next B. (Delete B).
161 action treats a string as a single character. Wow!
162 action recognizes a regular expression if it is preceded by ( or , or =.
175 if (a == '\'' || a == '"') {
183 throw 'Error: unterminated string literal: ' + a;
195 if (b == '/' && '(,=:[!&|'.has(a)) {
202 } else if (a =='\\') {
205 } else if (a <= '\n') {
206 throw 'Error: unterminated Regular Expression literal';
217 /* m -- Copy the input to the output, deleting the characters which are
218 insignificant to JavaScript. Comments will be removed. Tabs will be
219 replaced with spaces. Carriage returns will be replaced with
221 Most spaces and linefeeds will be removed.
256 if (level == 1 && b != '\n') {
274 if (level == 1 && a != '\n') {
310 jsmin.oldSize = input.length;
312 jsmin.newSize = r.length;