@@ -43,51 +43,51 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
43
*/
44
44
45
45
namespace ClientDependency . Core . CompositeFiles
46
- {
47
- public class JSMin
48
- {
49
- private const int Eof = - 1 ;
50
- private TextReader _sr ;
51
- private TextWriter _sw ;
52
- private int _theA ;
53
- private int _theB ;
54
- private int _theLookahead = Eof ;
55
- private int _theX = Eof ;
56
- private int _theY = Eof ;
57
- private int _retStatement = - 1 ;
58
- private bool _start = false ;
59
-
60
- [ Obsolete ( "Use the overloads specifying a Stream instead" ) ]
61
- public static string CompressJS ( string body )
62
- {
63
- return new JSMin ( ) . Minify ( body ) ;
64
- }
65
-
66
- public static string CompressJS ( Stream stream )
67
- {
68
- var jsMin = new JSMin ( ) ;
69
- if ( ! stream . CanRead ) throw new InvalidOperationException ( "Cannot read input stream" ) ;
70
- if ( stream . CanSeek )
71
- {
72
- stream . Position = 0 ;
73
- }
74
- return jsMin . Minify ( new StreamReader ( stream ) ) ;
75
- }
76
-
77
- [ Obsolete ( "Use the overloads specifying a TextReader instead" ) ]
78
- public string Minify ( string src )
79
- {
80
- StringBuilder sb = new StringBuilder ( ) ;
81
- using ( _sr = new StringReader ( src ) )
82
- {
83
- using ( _sw = new StringWriter ( sb ) )
84
- {
85
- ExecuteJsMin ( ) ;
86
- }
87
- }
88
- return sb . ToString ( ) ;
89
- }
90
-
46
+ {
47
+ public class JSMin
48
+ {
49
+ private const int Eof = - 1 ;
50
+ private TextReader _sr ;
51
+ private TextWriter _sw ;
52
+ private int _theA ;
53
+ private int _theB ;
54
+ private int _theLookahead = Eof ;
55
+ private int _theX = Eof ;
56
+ private int _theY = Eof ;
57
+ private int _retStatement = - 1 ;
58
+ private bool _start = false ;
59
+
60
+ [ Obsolete ( "Use the overloads specifying a Stream instead" ) ]
61
+ public static string CompressJS ( string body )
62
+ {
63
+ return new JSMin ( ) . Minify ( body ) ;
64
+ }
65
+
66
+ public static string CompressJS ( Stream stream )
67
+ {
68
+ var jsMin = new JSMin ( ) ;
69
+ if ( ! stream . CanRead ) throw new InvalidOperationException ( "Cannot read input stream" ) ;
70
+ if ( stream . CanSeek )
71
+ {
72
+ stream . Position = 0 ;
73
+ }
74
+ return jsMin . Minify ( new StreamReader ( stream ) ) ;
75
+ }
76
+
77
+ [ Obsolete ( "Use the overloads specifying a TextReader instead" ) ]
78
+ public string Minify ( string src )
79
+ {
80
+ StringBuilder sb = new StringBuilder ( ) ;
81
+ using ( _sr = new StringReader ( src ) )
82
+ {
83
+ using ( _sw = new StringWriter ( sb ) )
84
+ {
85
+ ExecuteJsMin ( ) ;
86
+ }
87
+ }
88
+ return sb . ToString ( ) ;
89
+ }
90
+
91
91
public string Minify ( TextReader reader )
92
92
{
93
93
_sr = reader ;
@@ -126,7 +126,7 @@ private void ExecuteJsMin()
126
126
break ;
127
127
case '\n ' :
128
128
case '\u2028 ' :
129
- case '\u2029 ' :
129
+ case '\u2029 ' :
130
130
switch ( _theB )
131
131
{
132
132
case '{' :
@@ -146,9 +146,9 @@ private void ExecuteJsMin()
146
146
//Maintain the line break
147
147
Action ( 1 ) ;
148
148
break ;
149
- case ' ' :
149
+ case ' ' :
150
150
Action ( 3 ) ;
151
- break ;
151
+ break ;
152
152
default :
153
153
if ( ! _start )
154
154
{
@@ -164,7 +164,7 @@ private void ExecuteJsMin()
164
164
default :
165
165
switch ( _theB )
166
166
{
167
-
167
+
168
168
case ' ' :
169
169
Action ( IsAlphanum ( _theA ) ? 1 : 3 ) ;
170
170
break ;
@@ -221,8 +221,8 @@ void Action(int d)
221
221
222
222
//process string literals or end of statement and track return statement
223
223
if ( ! HandleStringLiteral ( ) )
224
- HandleEndOfStatement ( ) ;
225
-
224
+ HandleEndOfStatement ( ) ;
225
+
226
226
goto case 3 ;
227
227
case 3 :
228
228
_theB = NextCharExcludingComments ( ) ;
@@ -260,8 +260,8 @@ private bool TrackReturnStatement()
260
260
{
261
261
_retStatement = 0 ;
262
262
return true ;
263
- }
264
-
263
+ }
264
+
265
265
if ( _retStatement >= ( r . Length - 1 ) )
266
266
{
267
267
//reset when there is a return statement and the next char is not whitespace
@@ -287,8 +287,8 @@ private bool TrackReturnStatement()
287
287
/// </summary>
288
288
private bool HandleEndOfStatement ( )
289
289
{
290
- if ( _theA != '}' ) return false ;
291
-
290
+ if ( _theA != '}' ) return false ;
291
+
292
292
var peek = Peek ( ) ;
293
293
//NOTE: We don't skip over a new line, this is becase in some cases
294
294
// library managers don't put a semicolon after a } when they have defined a variable as a method,
@@ -308,15 +308,15 @@ private bool HandleEndOfStatement()
308
308
private bool HandleStringLiteral ( )
309
309
{
310
310
if ( _theA != '\' ' && _theA != '"' && _theA != '`' )
311
- return false ;
312
-
313
- //only allowed with template strings
314
- var allowLineFeed = _theA == '`' ;
315
-
316
- //write the start quote
311
+ return false ;
312
+
313
+ //only allowed with template strings
314
+ var allowLineFeed = _theA == '`' ;
315
+
316
+ //write the start quote
317
317
Put ( _theA ) ;
318
318
_theA = Get ( replaceCr : ! allowLineFeed ) ; //don't replace CR here, if we need to deal with that
319
-
319
+
320
320
for ( ; ; )
321
321
{
322
322
//If the A matches B it means the string literal is done
@@ -327,14 +327,14 @@ private bool HandleStringLiteral()
327
327
Put ( _theA ) ;
328
328
329
329
//reset, this essentially resets the process
330
- _theA = ' ' ;
330
+ _theA = ' ' ;
331
331
break ;
332
332
}
333
333
334
334
var skipRead = false ;
335
335
336
336
switch ( _theA )
337
- {
337
+ {
338
338
case '\r ' :
339
339
case '\n ' :
340
340
if ( ! allowLineFeed )
@@ -359,7 +359,7 @@ private bool HandleStringLiteral()
359
359
Put ( _theA ) ; //write the backslash
360
360
_theA = Get ( ) ; //get the escaped char
361
361
if ( _theA == Eof )
362
- throw new Exception ( $ "Error: JSMIN unterminated string literal: { _theA } \n ") ;
362
+ throw new Exception ( $ "Error: JSMIN unterminated string literal: { _theA } \n ") ;
363
363
Put ( _theA ) ; //write the escaped char
364
364
_theA = Get ( ) ;
365
365
skipRead = true ; //go to beginning of loop
@@ -590,6 +590,26 @@ private int NextCharExcludingComments()
590
590
}
591
591
}
592
592
593
+ //ECMA javascript standard comment format <!--singleLinecommentChars-->
594
+ else if ( c == '<' )
595
+ {
596
+ if ( Peek ( ) == '!' )
597
+ {
598
+ for ( ; ; )
599
+ {
600
+ c = Get ( ) ;
601
+ if ( c == '-' ) {
602
+ if ( Peek ( ) == '>' )
603
+ {
604
+ Get ( ) ;
605
+ break ;
606
+ }
607
+ }
608
+ }
609
+ c = Get ( ) ;
610
+ }
611
+ }
612
+
593
613
_theY = _theX ;
594
614
_theX = c ;
595
615
return c ;
@@ -615,6 +635,8 @@ private int Get(bool replaceCr = true)
615
635
{
616
636
int c = _theLookahead ;
617
637
_theLookahead = Eof ;
638
+ //c==Eof means Get() without Peek()
639
+ //if c!=Eof means the next char already in Peek(), no need to read again
618
640
if ( c == Eof )
619
641
{
620
642
c = _sr . Read ( ) ;
@@ -662,4 +684,4 @@ private bool IsLineSeparator(int c)
662
684
}
663
685
664
686
}
665
- }
687
+ }
0 commit comments