Using system;using system.collections.generic;using system.linq;using system.web;using System.Text;using System.IO;/ <SUMMARY>///provides the ability to compress JavaScript code. </summary>public sealed class javascriptminifier{#region private field-Const int EOF =-1; Private ReadOnly StringBuilder _jsbuilder; Private ReadOnly TextReader _jsreader; private int _thea = Convert.ToInt32 (' \ n '); private int _theb; private int _thelookahead = EOF; #endregion #region Constructors///<summary>//Initializes a new instance of the <see cref= "Javascriptminifier"/> class. </summary>//<param name= "Jsreader" > contains <see cref= "TextReader"/> to compress JavaScript code. </param> Private Javascriptminifier (TextReader jsreader) {if (Jsreader = = null) throw new Argumentnull Exception ("Jsreader"); This._jsreader = Jsreader; This._jsbuilder = new StringBuilder (); } #endregion #region static method///<summary>//compress the specified JavaScript code. ///</summary>//<param name= "JS" > contains <see cref= "StringBuilder"/> To compress the JavaScript code. </param>///<returns> returns the <see cref= "StringBuilder"/> that contains the compressed JavaScript code. </returns> public static StringBuilder minify (StringBuilder js) {return minify (new StringReader (JS. ToString ())); }//<summary>//compress the specified JavaScript code. </summary>//<param name= "Jscode" > JavaScript code to compress. </param>///<returns> returns the <see cref= "StringBuilder"/> that contains the compressed JavaScript code. </returns> public static StringBuilder minify (string jscode) {return minify (new StringReader (Jscode));} <summary>///compress the specified JavaScript code. </summary>//<param name= "Jsreader" > contains <see cref= "TextReader"/> to compress JavaScript code. </param>///<returns> returns the <see cref= "StringBuilder"/> that contains the compressed JavaScript code. </returns> public static StringBuilder minify (TextReader jsreader) { Javascriptminifier jsmin = new Javascriptminifier (Jsreader); Jsmin._jsmin (); return jsmin._jsbuilder; } #endregion The private method is #region privately void _jsmin () {this._action (3); while (This._thea! = EOF) {switch (Char) this._thea) {case ': if (_isalphanum (This._theb)) this._action (1); else This._action (2); Break Case ' \ n ': Switch ((Char) this._theb) {case ' {': Case ' [': Case ' (': Case ' + ': Case '-': This._action (1); Break Case ': this._action (3); Break Default:if (_isalphanum (This._theb)) this. _action (1); else This._action (2); Break } break; Default:switch ((Char) this._theb) {case ': if (_isalphanum (This._thea)) {this._action (1); Break } this._action (3); Break Case ' \ n ': Switch ((Char) This._thea) { Case '} ': Case '] ': Case ') ': Case ' + ': Case '-': Case ' "': Case ' \ ': this._action (1); Break Default:if (_isalphanum (This._thea)) this._action (1); else This._action (3); Break } break; Default:this._action (1); Break } break; }}} private void _action (int d) {if (d <= 1) this._put (This._thea); if (d <= 2) {This._thea = This._theb; if (This._thea = = ' \ ' | | this._thea = = ' "') {for (;;) {this._put (This._thea); This._thea = This._get (); if (This._thea = = This._theb) break; if (This._thea <= ' \ n ') throw the new Exception (string. Format ("Error:Jsmin unterminated string literal: {0} ", This._thea)); if (this._thea! = ' \ \ ') continue; This._put (This._thea); This._thea = This._get (); }}} if (D > 3) return; This._theb = This._next (); if (This._theb! = '/' | | mp;& This._thea! = '! ') && This._thea! = ': ') && This._thea! = ' & ') && this._thea! = ' | ') && This._thea! = '? ') && This._thea! = ' {') && This._thea! = '} ') && this._thea! = '; ') && This._thea! = ' \ n ')) return; This._put (This._thea); This._put (This._theb); for (;;) {This._thea = This._get (); if (This._thea = = '/') break; if (This._thea = = ' \ \ ') {this._put (This._thea); THis._thea = This._get (); } else if (This._thea <= ' \ n ') throw the new Exception (string. Format ("Error:jsmin unterminated Regular Expression literal: {0}.", This._thea)); This._put (This._thea); } This._theb = This._next (); } private int _next () {int c = this._get (); const int s = (int) ' * '; if (c = = '/') {switch (Char) This._peek ()) {case '/': for (; ; ) {c = This._get (); if (c <= ' \ n ') return C; } case ' * ': This._get (); for (;;) {switch (This._get ()) {case S: if (this._peek () = = '/') {this._get (); Return Convert.ToInt32 ("); } break; Case Eof:throw New Exception ("Error:jsmin unterminated comment."); }} Default:return C; }} return C; } private int _peek () {this._thelookahead = This._get (); return this._thelookahead; } private int _get () {int c = this._thelookahead; This._thelookahead = EOF; if (c = = EOF) c = This._jsreader.read (); return c >= ' | | c = = ' \ n ' | | c = = EOF? C: (c = = ' \ r ')? ' \ n ': '); } private void _put (int c) {this._jsbuilder.append ((char) c);} private static bool _isalphanum (int c) {return ((c >= ' a ' && c <= ' z ') | | (c >= ' 0 ' && C <= ' 9 ') | | (c >= ' A ' && C <= ' Z ') | | c = = ' _ ' | | c = = ' $ ' | | c = = ' \ \ ' | | C> 126); } #endregion}
Javascriptminifier
Javascriptminifier C # compressed JavaScript