@@ -44,95 +44,50 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44
44
45
45
namespace ClientDependency . Core . CompositeFiles
46
46
{
47
-
48
47
public class JSMin
49
-
50
48
{
51
-
52
49
private const int Eof = - 1 ;
53
-
54
50
private TextReader _sr ;
55
-
56
51
private TextWriter _sw ;
57
-
58
52
private int _theA ;
59
-
60
53
private int _theB ;
61
-
62
54
private int _theLookahead = Eof ;
63
-
64
55
private int _theX = Eof ;
65
-
66
56
private int _theY = Eof ;
67
-
68
57
private int _retStatement = - 1 ;
69
-
70
58
private bool _start = false ;
71
59
72
-
73
-
74
60
[ Obsolete ( "Use the overloads specifying a Stream instead" ) ]
75
-
76
61
public static string CompressJS ( string body )
77
-
78
62
{
79
-
80
63
return new JSMin ( ) . Minify ( body ) ;
81
-
82
64
}
83
65
84
-
85
-
86
66
public static string CompressJS ( Stream stream )
87
-
88
67
{
89
-
90
68
var jsMin = new JSMin ( ) ;
91
-
92
69
if ( ! stream . CanRead ) throw new InvalidOperationException ( "Cannot read input stream" ) ;
93
-
94
70
if ( stream . CanSeek )
95
-
96
71
{
97
-
98
72
stream . Position = 0 ;
99
-
100
73
}
101
-
102
74
return jsMin . Minify ( new StreamReader ( stream ) ) ;
103
-
104
75
}
105
76
106
-
107
-
108
77
[ Obsolete ( "Use the overloads specifying a TextReader instead" ) ]
109
-
110
78
public string Minify ( string src )
111
-
112
79
{
113
-
114
80
StringBuilder sb = new StringBuilder ( ) ;
115
-
116
81
using ( _sr = new StringReader ( src ) )
117
-
118
82
{
119
-
120
83
using ( _sw = new StringWriter ( sb ) )
121
-
122
84
{
123
-
124
85
ExecuteJsMin ( ) ;
125
-
126
86
}
127
-
128
87
}
129
-
130
88
return sb . ToString ( ) ;
131
-
132
89
}
133
90
134
-
135
-
136
91
public string Minify ( TextReader reader )
137
92
{
138
93
_sr = reader ;
@@ -172,7 +127,6 @@ private void ExecuteJsMin()
172
127
case '\n ' :
173
128
case '\u2028 ' :
174
129
case '\u2029 ' :
175
-
176
130
switch ( _theB )
177
131
{
178
132
case '{' :
@@ -193,10 +147,8 @@ private void ExecuteJsMin()
193
147
Action ( 1 ) ;
194
148
break ;
195
149
case ' ' :
196
-
197
150
Action ( 3 ) ;
198
151
break ;
199
-
200
152
default :
201
153
if ( ! _start )
202
154
{
@@ -212,7 +164,7 @@ private void ExecuteJsMin()
212
164
default :
213
165
switch ( _theB )
214
166
{
215
-
167
+
216
168
case ' ' :
217
169
Action ( IsAlphanum ( _theA ) ? 1 : 3 ) ;
218
170
break ;
@@ -271,8 +223,6 @@ void Action(int d)
271
223
if ( ! HandleStringLiteral ( ) )
272
224
HandleEndOfStatement ( ) ;
273
225
274
-
275
-
276
226
goto case 3 ;
277
227
case 3 :
278
228
_theB = NextCharExcludingComments ( ) ;
@@ -312,8 +262,6 @@ private bool TrackReturnStatement()
312
262
return true ;
313
263
}
314
264
315
-
316
-
317
265
if ( _retStatement >= ( r . Length - 1 ) )
318
266
{
319
267
//reset when there is a return statement and the next char is not whitespace
@@ -341,8 +289,6 @@ private bool HandleEndOfStatement()
341
289
{
342
290
if ( _theA != '}' ) return false ;
343
291
344
-
345
-
346
292
var peek = Peek ( ) ;
347
293
//NOTE: We don't skip over a new line, this is becase in some cases
348
294
// library managers don't put a semicolon after a } when they have defined a variable as a method,
@@ -364,19 +310,13 @@ private bool HandleStringLiteral()
364
310
if ( _theA != '\' ' && _theA != '"' && _theA != '`' )
365
311
return false ;
366
312
367
-
368
-
369
313
//only allowed with template strings
370
-
371
314
var allowLineFeed = _theA == '`' ;
372
315
373
-
374
-
375
316
//write the start quote
376
-
377
317
Put ( _theA ) ;
378
318
_theA = Get ( replaceCr : ! allowLineFeed ) ; //don't replace CR here, if we need to deal with that
379
-
319
+
380
320
for ( ; ; )
381
321
{
382
322
//If the A matches B it means the string literal is done
@@ -388,15 +328,13 @@ private bool HandleStringLiteral()
388
328
389
329
//reset, this essentially resets the process
390
330
_theA = ' ' ;
391
-
392
331
break ;
393
332
}
394
333
395
334
var skipRead = false ;
396
335
397
336
switch ( _theA )
398
337
{
399
-
400
338
case '\r ' :
401
339
case '\n ' :
402
340
if ( ! allowLineFeed )
@@ -422,7 +360,6 @@ private bool HandleStringLiteral()
422
360
_theA = Get ( ) ; //get the escaped char
423
361
if ( _theA == Eof )
424
362
throw new Exception ( $ "Error: JSMIN unterminated string literal: { _theA } \n ") ;
425
-
426
363
Put ( _theA ) ; //write the escaped char
427
364
_theA = Get ( ) ;
428
365
skipRead = true ; //go to beginning of loop
0 commit comments