jsmin.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. ** node-jsmin - a js minifier based on Doug Crockford's jsmin.c, based
  3. ** on Franck Marica's jsmin.js by Peteris Krumins.
  4. */
  5. /*!
  6. jsmin.js - 2010-01-15
  7. Author: NanaLich (http://www.cnblogs.com/NanaLich)
  8. Another patched version for jsmin.js patched by Billy Hoffman,
  9. this version will try to keep CR LF pairs inside the important comments
  10. away from being changed into double LF pairs.
  11. jsmin.js - 2009-11-05
  12. Author: Billy Hoffman
  13. This is a patched version of jsmin.js created by Franck Marcia which
  14. supports important comments denoted with /*! ...
  15. Permission is hereby granted to use the Javascript version under the same
  16. conditions as the jsmin.js on which it is based.
  17. jsmin.js - 2006-08-31
  18. Author: Franck Marcia
  19. This work is an adaptation of jsminc.c published by Douglas Crockford.
  20. Permission is hereby granted to use the Javascript version under the same
  21. conditions as the jsmin.c on which it is based.
  22. jsmin.c
  23. 2006-05-04
  24. Copyright (c) 2002 Douglas Crockford (www.crockford.com)
  25. Permission is hereby granted, free of charge, to any person obtaining a copy of
  26. this software and associated documentation files (the "Software"), to deal in
  27. the Software without restriction, including without limitation the rights to
  28. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  29. of the Software, and to permit persons to whom the Software is furnished to do
  30. so, subject to the following conditions:
  31. The above copyright notice and this permission notice shall be included in all
  32. copies or substantial portions of the Software.
  33. The Software shall be used for Good, not Evil.
  34. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  40. SOFTWARE.
  41. Update:
  42. add level:
  43. 1: minimal, keep linefeeds if single
  44. 2: normal, the standard algorithm
  45. 3: agressive, remove any linefeed and doesn't take care of potential
  46. missing semicolons (can be regressive)
  47. */
  48. String.prototype.has = function(c) {
  49. return this.indexOf(c) > -1;
  50. };
  51. exports.jsmin = jsmin;
  52. function jsmin(input, level, comment) {
  53. if (!input) return '';
  54. if (!level) level = 2;
  55. if (!comment) comment = '';
  56. var a = '',
  57. b = '',
  58. EOF = -1,
  59. LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
  60. DIGITS = '0123456789',
  61. ALNUM = LETTERS + DIGITS + '_$\\',
  62. theLookahead = EOF;
  63. /* isAlphanum -- return true if the character is a letter, digit, underscore,
  64. dollar sign, or non-ASCII character.
  65. */
  66. function isAlphanum(c) {
  67. return c != EOF && (ALNUM.has(c) || c.charCodeAt(0) > 126);
  68. }
  69. /* getc(IC) -- return the next character. Watch out for lookahead. If the
  70. character is a control character, translate it to a space or
  71. linefeed.
  72. */
  73. var iChar = 0, lInput = input.length;
  74. function getc() {
  75. var c = theLookahead;
  76. if(iChar == lInput) {
  77. return EOF;
  78. }
  79. theLookahead = EOF;
  80. if(c == EOF) {
  81. c = input.charAt(iChar);
  82. ++iChar;
  83. }
  84. if(c >= ' ' || c == '\n') {
  85. return c;
  86. }
  87. if(c == '\r') {
  88. return '\n';
  89. }
  90. return ' ';
  91. }
  92. function getcIC() {
  93. var c = theLookahead;
  94. if(iChar == lInput) {
  95. return EOF;
  96. }
  97. theLookahead = EOF;
  98. if(c == EOF) {
  99. c = input.charAt(iChar);
  100. ++iChar;
  101. }
  102. if(c >= ' ' || c == '\n' || c == '\r') {
  103. return c;
  104. }
  105. return ' ';
  106. }
  107. /* peek -- get the next character without getting it.
  108. */
  109. function peek() {
  110. theLookahead = getc();
  111. return theLookahead;
  112. }
  113. /* next -- get the next character, excluding comments. peek() is used to see
  114. if a '/' is followed by a '/' or '*'.
  115. */
  116. function next() {
  117. var c = getc();
  118. if(c == '/') {
  119. switch(peek()) {
  120. case '/':
  121. for(; ; ) {
  122. c = getc();
  123. if(c <= '\n') {
  124. return c;
  125. }
  126. }
  127. break;
  128. case '*':
  129. //this is a comment. What kind?
  130. getc();
  131. if(peek() == '!') {
  132. // kill the extra one
  133. getc();
  134. //important comment
  135. var d = '/*!';
  136. for(; ; ) {
  137. c = getcIC(); // let it know it's inside an important comment
  138. switch(c) {
  139. case '*':
  140. if(peek() == '/') {
  141. getc();
  142. return d + '*/';
  143. }
  144. break;
  145. case EOF:
  146. throw 'Error: Unterminated comment.';
  147. default:
  148. //modern JS engines handle string concats much better than the
  149. //array+push+join hack.
  150. d += c;
  151. }
  152. }
  153. } else {
  154. //unimportant comment
  155. for(; ; ) {
  156. switch(getc()) {
  157. case '*':
  158. if(peek() == '/') {
  159. getc();
  160. return ' ';
  161. }
  162. break;
  163. case EOF:
  164. throw 'Error: Unterminated comment.';
  165. }
  166. }
  167. }
  168. break;
  169. default:
  170. return c;
  171. }
  172. }
  173. return c;
  174. }
  175. /* action -- do something! What you do is determined by the argument:
  176. 1 Output A. Copy B to A. Get the next B.
  177. 2 Copy B to A. Get the next B. (Delete A).
  178. 3 Get the next B. (Delete B).
  179. action treats a string as a single character. Wow!
  180. action recognizes a regular expression if it is preceded by ( or , or =.
  181. */
  182. function action(d) {
  183. var r = [];
  184. if(d == 1) {
  185. r.push(a);
  186. }
  187. if(d < 3) {
  188. a = b;
  189. if(a == '\'' || a == '"') {
  190. for(; ; ) {
  191. r.push(a);
  192. a = getc();
  193. if(a == b) {
  194. break;
  195. }
  196. if(a <= '\n') {
  197. throw 'Error: unterminated string literal: ' + a;
  198. }
  199. if(a == '\\') {
  200. r.push(a);
  201. a = getc();
  202. }
  203. }
  204. }
  205. }
  206. b = next();
  207. if(b == '/' && '(,=:[!&|'.has(a)) {
  208. r.push(a);
  209. r.push(b);
  210. for(; ; ) {
  211. a = getc();
  212. if(a == '/') {
  213. break;
  214. } else if(a == '\\') {
  215. r.push(a);
  216. a = getc();
  217. } else if(a <= '\n') {
  218. throw 'Error: unterminated Regular Expression literal';
  219. }
  220. r.push(a);
  221. }
  222. b = next();
  223. }
  224. return r.join('');
  225. }
  226. /* m -- Copy the input to the output, deleting the characters which are
  227. insignificant to JavaScript. Comments will be removed. Tabs will be
  228. replaced with spaces. Carriage returns will be replaced with
  229. linefeeds.
  230. Most spaces and linefeeds will be removed.
  231. */
  232. function m() {
  233. var r = [];
  234. a = '';
  235. r.push(action(3));
  236. while(a != EOF) {
  237. switch(a) {
  238. case ' ':
  239. if(isAlphanum(b)) {
  240. r.push(action(1));
  241. } else {
  242. r.push(action(2));
  243. }
  244. break;
  245. case '\n':
  246. switch(b) {
  247. case '{':
  248. case '[':
  249. case '(':
  250. case '+':
  251. case '-':
  252. r.push(action(1));
  253. break;
  254. case ' ':
  255. r.push(action(3));
  256. break;
  257. default:
  258. if(isAlphanum(b)) {
  259. r.push(action(1));
  260. } else {
  261. if(level == 1 && b != '\n') {
  262. r.push(action(1));
  263. } else {
  264. r.push(action(2));
  265. }
  266. }
  267. }
  268. break;
  269. default:
  270. switch(b) {
  271. case ' ':
  272. if(isAlphanum(a)) {
  273. r.push(action(1));
  274. break;
  275. }
  276. r.push(action(3));
  277. break;
  278. case '\n':
  279. if(level == 1 && a != '\n') {
  280. r.push(action(1));
  281. } else {
  282. switch(a) {
  283. case '}':
  284. case ']':
  285. case ')':
  286. case '+':
  287. case '-':
  288. case '"':
  289. case '\'':
  290. if(level == 3) {
  291. r.push(action(3));
  292. } else {
  293. r.push(action(1));
  294. }
  295. break;
  296. default:
  297. if(isAlphanum(a)) {
  298. r.push(action(1));
  299. } else {
  300. r.push(action(3));
  301. }
  302. }
  303. }
  304. break;
  305. default:
  306. r.push(action(1));
  307. break;
  308. }
  309. }
  310. }
  311. return r.join('');
  312. }
  313. ret = m(input);
  314. if (comment) {
  315. return comment + '\n' + ret;
  316. }
  317. return ret;
  318. }