@@ -43,51 +43,51 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4343*/
4444
4545namespace 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+
9191 public string Minify ( TextReader reader )
9292 {
9393 _sr = reader ;
@@ -126,7 +126,7 @@ private void ExecuteJsMin()
126126 break ;
127127 case '\n ' :
128128 case '\u2028 ' :
129- case '\u2029 ' :
129+ case '\u2029 ' :
130130 switch ( _theB )
131131 {
132132 case '{' :
@@ -146,9 +146,9 @@ private void ExecuteJsMin()
146146 //Maintain the line break
147147 Action ( 1 ) ;
148148 break ;
149- case ' ' :
149+ case ' ' :
150150 Action ( 3 ) ;
151- break ;
151+ break ;
152152 default :
153153 if ( ! _start )
154154 {
@@ -164,7 +164,7 @@ private void ExecuteJsMin()
164164 default :
165165 switch ( _theB )
166166 {
167-
167+
168168 case ' ' :
169169 Action ( IsAlphanum ( _theA ) ? 1 : 3 ) ;
170170 break ;
@@ -221,8 +221,8 @@ void Action(int d)
221221
222222 //process string literals or end of statement and track return statement
223223 if ( ! HandleStringLiteral ( ) )
224- HandleEndOfStatement ( ) ;
225-
224+ HandleEndOfStatement ( ) ;
225+
226226 goto case 3 ;
227227 case 3 :
228228 _theB = NextCharExcludingComments ( ) ;
@@ -260,8 +260,8 @@ private bool TrackReturnStatement()
260260 {
261261 _retStatement = 0 ;
262262 return true ;
263- }
264-
263+ }
264+
265265 if ( _retStatement >= ( r . Length - 1 ) )
266266 {
267267 //reset when there is a return statement and the next char is not whitespace
@@ -287,8 +287,8 @@ private bool TrackReturnStatement()
287287 /// </summary>
288288 private bool HandleEndOfStatement ( )
289289 {
290- if ( _theA != '}' ) return false ;
291-
290+ if ( _theA != '}' ) return false ;
291+
292292 var peek = Peek ( ) ;
293293 //NOTE: We don't skip over a new line, this is becase in some cases
294294 // 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()
308308 private bool HandleStringLiteral ( )
309309 {
310310 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
317317 Put ( _theA ) ;
318318 _theA = Get ( replaceCr : ! allowLineFeed ) ; //don't replace CR here, if we need to deal with that
319-
319+
320320 for ( ; ; )
321321 {
322322 //If the A matches B it means the string literal is done
@@ -327,14 +327,14 @@ private bool HandleStringLiteral()
327327 Put ( _theA ) ;
328328
329329 //reset, this essentially resets the process
330- _theA = ' ' ;
330+ _theA = ' ' ;
331331 break ;
332332 }
333333
334334 var skipRead = false ;
335335
336336 switch ( _theA )
337- {
337+ {
338338 case '\r ' :
339339 case '\n ' :
340340 if ( ! allowLineFeed )
@@ -359,7 +359,7 @@ private bool HandleStringLiteral()
359359 Put ( _theA ) ; //write the backslash
360360 _theA = Get ( ) ; //get the escaped char
361361 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 ") ;
363363 Put ( _theA ) ; //write the escaped char
364364 _theA = Get ( ) ;
365365 skipRead = true ; //go to beginning of loop
@@ -590,6 +590,26 @@ private int NextCharExcludingComments()
590590 }
591591 }
592592
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+
593613 _theY = _theX ;
594614 _theX = c ;
595615 return c ;
@@ -615,6 +635,8 @@ private int Get(bool replaceCr = true)
615635 {
616636 int c = _theLookahead ;
617637 _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
618640 if ( c == Eof )
619641 {
620642 c = _sr . Read ( ) ;
@@ -662,4 +684,4 @@ private bool IsLineSeparator(int c)
662684 }
663685
664686 }
665- }
687+ }
0 commit comments