-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJavaScriptCompressor.cs
524 lines (437 loc) · 16.9 KB
/
JavaScriptCompressor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#region Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// Copyright (c) 2007 Atif Aziz. All rights reserved.
// http://www.raboof.com
//
// Portion Copyright (c) 2001 Douglas Crockford
// http://www.crockford.com
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#endregion
namespace MudBlazor.JSCompiler
{
#region Imports
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
#endregion
#region About JavaScriptCompressor (JSMin)
/*
JavaScriptCompressor is a C# port of jsmin.c that was originally written
by Douglas Crockford.
jsmin.c
04-Dec-2003
(c) 2001 Douglas Crockford
http://www.crockford.com
C# port written by Atif Aziz
12-Apr-2005
http://www.raboof.com
The following documentation is a minimal adaption from the original
found at:
http://www.crockford.com/javascript/jsmin.html
The documentation therefore still refers to JSMin, but equally
applies to this C# port since the code implementation has not been
changed or enhanced in any way. Some passages have been omitted
since they don't apply. For example, the original documentation has
a comment about character set. This does not apply to this port
since JavaScriptCompressor works with TextReader and TextWriter
from the Base Class Library (BCL). The character set responsibility
is therefore pushed back to the user of this class.
What JSMin Does
---------------
JSMin is a filter that omits or modifies some characters. This does
not change the behavior of the program that it is minifying. The
result may be harder to debug. It will definitely be harder to read.
JSMin first replaces carriage returns ('\r') with linefeeds ('\n').
It replaces all other control characters (including tab) with spaces.
It replaces comments in the // form with linefeeds. It replaces
comments with spaces. All runs of spaces are replaced with a single
space. All runs of linefeeds are replaced with a single linefeed.
It omits spaces except when a space is preceded or followed by a
non-ASCII character or by an ASCII letter or digit, or by one of
these characters:
\ $ _
It is more conservative in omitting linefeeds, because linefeeds are
sometimes treated as semicolons. A linefeed is not omitted if it
precedes a non-ASCII character or an ASCII letter or digit or one of
these characters:
\ $ _ { [ ( + -
and if it follows a non-ASCII character or an ASCII letter or digit
or one of these characters:
\ $ _ } ] ) + - " '
No other characters are omitted or modified.
JSMin knows to not modify quoted strings and regular expression
literals.
JSMin does not obfuscate, but it does uglify.
Before:
// is.js
// (c) 2001 Douglas Crockford
// 2001 June 3
// is
// The -is- object is used to identify the browser. Every browser edition
// identifies itself, but there is no standard way of doing it, and some of
// the identification is deceptive. This is because the authors of web
// browsers are liars. For example, Microsoft's IE browsers claim to be
// Mozilla 4. Netscape 6 claims to be version 5.
var is = {
ie: navigator.appName == 'Microsoft Internet Explorer',
java: navigator.javaEnabled(),
ns: navigator.appName == 'Netscape',
ua: navigator.userAgent.toLowerCase(),
version: parseFloat(navigator.appVersion.substr(21)) ||
parseFloat(navigator.appVersion),
win: navigator.platform == 'Win32'
}
is.mac = is.ua.indexOf('mac') >= 0;
if (is.ua.indexOf('opera') >= 0) {
is.ie = is.ns = false;
is.opera = true;
}
if (is.ua.indexOf('gecko') >= 0) {
is.ie = is.ns = false;
is.gecko = true;
}
After:
var is={ie:navigator.appName=='MicrosoftInternetExplorer',java:navigator.javaEnabled(),ns:navigator.appName=='Netscape',ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=='Win32'}
is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}
Caution
-------
Do not put raw control characters inside a quoted string. That is an
extremely bad practice. Use \xhh notation instead. JSMin will replace
control characters with spaces or linefeeds.
Use parens with confusing sequences of + or -. For example, minification
changes
a + ++b
into
a+++b
which is interpreted as
a++ + b
which is wrong. You can avoid this by using parens:
a + (++b)
JSLint (http://www.jslint.com/) checks for all of these problems. It is
suggested that JSLint be used before using JSMin.
Errors
------
JSMin can detect and produce three error messages:
- Unterminated comment.
- Unterminated string constant.
- Unterminated regular expression.
It ignores all other errors that may be present in your source program.
*/
#endregion
internal sealed class JavaScriptCompressor
{
//
// Public functions
//
public static string Compress(string source)
{
StringWriter writer = new StringWriter();
Compress(new StringReader(source), writer);
return writer.ToString();
}
public static void Compress(TextReader reader, TextWriter writer)
{
if (reader == null)
throw new ArgumentNullException("reader");
if (writer == null)
throw new ArgumentNullException("writer");
JavaScriptCompressor compressor = new JavaScriptCompressor(reader, writer);
compressor.Compress();
}
//
// Private implementation
//
private int aa;
private int bb;
private int lookahead = eof;
private TextReader reader = Console.In;
private TextWriter writer = Console.Out;
private const int eof = -1;
private JavaScriptCompressor(TextReader reader, TextWriter writer)
{
Debug.Assert(reader != null);
Debug.Assert(writer != null);
this.reader = reader;
this.writer = writer;
}
/* Compress -- Copy the input to the output, deleting the characters which are
insignificant to JavaScript. Comments will be removed. Tabs will be
replaced with spaces. Carriage returns will be replaced with linefeeds.
Most spaces and linefeeds will be removed.
*/
private void Compress()
{
aa = '\n';
Action(3);
while (aa != eof)
{
switch (aa)
{
case ' ':
if (IsAlphanum(bb))
{
Action(1);
}
else
{
Action(2);
}
break;
case '\n':
switch (bb)
{
case '{':
case '[':
case '(':
case '+':
case '-':
Action(1);
break;
case ' ':
Action(3);
break;
default:
if (IsAlphanum(bb))
{
Action(1);
}
else
{
Action(2);
}
break;
}
break;
default:
switch (bb)
{
case ' ':
if (IsAlphanum(aa))
{
Action(1);
break;
}
Action(3);
break;
case '\n':
switch (aa)
{
case '}':
case ']':
case ')':
case '+':
case '-':
case '"':
case '\'':
Action(1);
break;
default:
if (IsAlphanum(aa))
{
Action(1);
}
else
{
Action(3);
}
break;
}
break;
default:
Action(1);
break;
}
break;
}
}
}
/* Get -- return the next character from stdin. Watch out for lookahead. If
the character is a control character, translate it to a space or
linefeed.
*/
private int Get()
{
int ch = lookahead;
lookahead = eof;
if (ch == eof)
{
ch = reader.Read();
}
if (ch >= ' ' || ch == '\n' || ch == eof)
{
return ch;
}
if (ch == '\r')
{
return '\n';
}
return ' ';
}
/* Peek -- get the next character without getting it.
*/
private int Peek()
{
lookahead = Get();
return lookahead;
}
/* Next -- get the next character, excluding comments. Peek() is used to see
if a '/' is followed by a '/' or '*'.
*/
private int Next()
{
int ch = Get();
if (ch == '/')
{
switch (Peek())
{
case '/':
for (;; )
{
ch = Get();
if (ch <= '\n')
{
return ch;
}
}
case '*':
Get();
for (;; )
{
switch (Get())
{
case '*':
if (Peek() == '/')
{
Get();
return ' ';
}
break;
case eof:
throw new Exception("Unterminated comment.");
}
}
default:
return ch;
}
}
return ch;
}
/* Action -- do something! What you do is determined by the argument:
1 Output A. Copy A to B. Get the next B.
2 Copy B to A. Get the next B. (Delete A).
3 Get the next B. (Delete B).
Action treats a string as a single character. Wow!
Action recognizes a regular expression if it is preceded by ( or , or =.
*/
private void Action(int d)
{
switch (d)
{
case 1:
Write(aa);
goto case 2;
case 2:
aa = bb;
if (aa == '\'' || aa == '"')
{
for (;; )
{
Write(aa);
aa = Get();
if (aa == bb)
{
break;
}
if (aa <= '\n')
{
string message = string.Format("Unterminated string literal: '{0}'.", aa);
throw new Exception(message);
}
if (aa == '\\')
{
Write(aa);
aa = Get();
}
}
}
goto case 3;
case 3:
bb = Next();
if (bb == '/' && (aa == '(' || aa == ',' || aa == '='))
{
Write(aa);
Write(bb);
for (;; )
{
aa = Get();
if (aa == '/')
{
break;
}
else if (aa == '\\')
{
Write(aa);
aa = Get();
}
else if (aa <= '\n')
{
throw new Exception("Unterminated Regular Expression literal.");
}
Write(aa);
}
bb = Next();
}
break;
}
}
private void Write(int ch)
{
writer.Write((char) ch);
}
/* IsAlphanum -- return true if the character is a letter, digit, underscore,
dollar sign, or non-ASCII character.
*/
private static bool IsAlphanum(int ch)
{
return ((ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') ||
(ch >= 'A' && ch <= 'Z') ||
ch == '_' || ch == '$' || ch == '\\' || ch > 126);
}
public class Exception : System.Exception
{
public Exception() {}
public Exception(string message) :
base(message) {}
public Exception(string message, Exception innerException) :
base(message, innerException) {}
}
}
}