Simple JS code compressor _JAVASCRIPT Skills

Source: Internet
Author: User
Tags comments hash
1,


Copy Code code as follows:



<!--


-From: <a href= "http://www.iecn.net/bbs/view/105499.html" target= "_blank" >http://www.iecn.net/bbs/view/ 105499.html</a>


-Iecn.net 2006-09-22


-->





<script language= "JavaScript"





Type= "Text/javascript" >


Get rid of all comments


String.prototype.DeleteComment = function ()


{


var str = this.replace (/([' "]) (. +?) (['])/g,function (S,S1,S2,S3) {return s1+s2.replace (/[\/\*]/g, "\\$&") +s3});


str = str.replace (/\/\/[^\r]+|\/\*[\s\s]+?\*\//g, "");


str = str.replace (/([' "]) (. +?) (['])/g,function (S,S1,S2,S3) {return s1+s2.replace (/\\ ([^\\])/g, "$") +s3});


return str;


}


Format code


String.prototype.FormatCode = function ()


{


Return This.replace (/\\$/MG, ""). replace (/[^\s>;] $/MG, "$&;");


}


Remove extra space before and after a string


String.prototype.Trim = function (m)


{


Return This.replace (M./^\S*|\S*$/MG:/^\s*|\s*$/g, "");


}


Run code


function RunCode (obj)


{


window.open (', ' _blank '). document.write (Obj.value);


}





</script>





Test Code:<br/>


<textarea id= "Text" rows= "cols=" >


<script language= "JavaScript" >


/*


Test code


*/


var x=5;


document.write ("x =//" + x);


document.write ("bh/*" + x);


document.write ("x =jjhj */hhhhhh" + x);


document.write ("x =//" + x);





Alert (22)





Alert (22)





var a= "/fwe/////wefawef";


Alert (/^\/*[^\/]*$/.test (a));


Comments


</script>





</textArea> <br/><br/>


1th Step: <button onclick= "text.value = Text.value.DeleteComment ();" > Delete comments </button> <br/> <br/>


2nd step: <button onclick= "Text.value=text.value.formatcode ();" > Format Code </button> <br/> <br/>


3rd Step: <button onclick= "Text.value=text.value.trim (true);" > Remove both sides of the space </button> <br/> <br/>


4th step: <button onclick= "RunCode (Text);" > Run code </button> <br/> <br/>








2,


Copy Code code as follows:
<HTML><HEAD><TITLE>Format</TITLE>


<meta content= "MSHTML 6.00.2800.1528" name=generator>


<meta content= "" name=author>


<meta content= "" name=keywords>


<meta content= "" name=description></head>


<BODY>


<script language=javascript>


<!--


/**//**//**//**


**    ==================================================================================================


* * Class Name: Class_formater


* * Function: JS format


* * Example:


---------------------------------------------------------------------------------------------------





var xx = new Class_formater (code);





document.getElementById ("Display"). InnerHTML = Xx.format ();





---------------------------------------------------------------------------------------------------


* * Author: Ttyp


* * Mail: <a href= "mailto:ttyp@21cn.com" >ttyp@21cn.com</a>


* * Date: 2006-5-21


* * Version: 0.1


**    ==================================================================================================


**/





function Class_format (code) {


Hash Table class


function Hashtable () {


This._hash = new Object ();


This.add = function (key,value) {


if (typeof (key)!= "undefined") {


if (This.contains (key) ==false) {


This._hash[key]=typeof (value) = = "undefined"? Null:value;


return true;


} else {


return false;


}


} else {


return false;


}


}


This.remove = function (key) {delete This._hash[key];}


This.count = function () {var i=0;for (var k in this._hash) {i++;}


This.items = function (key) {return this._hash[key];}


This.contains = function (key) {return typeof (This._hash[key])!= "undefined";}


This.clear = function () {for (var k in this._hash) {delete this._hash[k];}





}





This._casesensitive = true;





string conversion to hash table


this.str2hashtable = function (Key,cs) {





var _key = Key.split (/,/g);


var _hash = new Hashtable ();


var _cs = true;








if (typeof (cs) = = "Undefined" | | Cs==null) {


_cs = this._casesensitive;


} else {


_cs = CS;


}





for (var i in _key) {


if (_cs) {


_hash.add (_key[i]);


} else {


_hash.add ((_key[i]+ ""). toLowerCase ());


}





}


return _hash;


}





Get the code that needs to be converted


This._codetxt = code;





if (typeof (syntax) = = "undefined") {


syntax = "";


}





This._deletecomment = false;


is case sensitive


This._casesensitive = true;


Keyword that can be followed by a block statement


This._blockelement = this.str2hashtable ("switch,if,while,try,finally");


It's a function statement.


This._function = this.str2hashtable ("function");


Semicolons in parentheses do not wrap lines


This._isfor = "for";





This._choiceelement = this.str2hashtable ("Else,catch");





This._beginblock = "{";


This._endblock = "}";





This._singleeyeelement = this.str2hashtable ("Var,new,return,else,delete,in,case");


Get split character


This._worddelimiters= ",.?!;: \ \/<> () {}[]\ "' \r\n\t=+-|*%@#$^&";


Reference characters


This._quotation = this.str2hashtable ("\", ' ");


Line comment characters


This._linecomment = "//";


Escape character


This._escape = "\ \";


MultiRow Reference Start


This._commenton = "/*";


End of MultiRow reference


This._commentoff = "* *";


Line End Word


This._rowend = ";";





this._in = "in";








This.iscompress = false;


This.style = 0;





This._tabnum = 0;








This.format = function () {


var Codearr = new Array ();


var word_index = 0;


var htmltxt = new Array ();





if (this.iscompress) {


This._deletecomment = true;


}








Get the split character array (participle)


for (var i = 0; i < this._codetxt.length; i++) {


if (This._worddelimiters.indexof (This._codetxt.charat (i)) = = 1) {//no keyword found


if (codearr[word_index] = = NULL | | typeof (Codearr[word_index]) = = ' undefined ') {


Codearr[word_index] = "";


}


Codearr[word_index] + = This._codetxt.charat (i);


} else {


if (typeof (Codearr[word_index])!= ' undefined ' && codearr[word_index].length > 0)


word_index++;


codearr[word_index++] = This._codetxt.charat (i);


}


}








var quote_opened = false; Reference mark


var slash_star_comment_opened = false; Multi-line Comment mark


var slash_slash_comment_opened = false; Single line comment mark


var line_num = 1; Line number


var Quote_char = ""; Reference tag Type





var function_opened = false;





var bracket_open = false;


var for_open = false;





By split word, block display


for (Var i=0 i <=word_index; i++) {


Handling empty lines (due to escape)


if (typeof (Codearr[i]) = = "Undefined" | | codearr[i].length==0) {


Continue


else if (codearr[i]== "" | | codearr[i]== "\ T") {


if (slash_slash_comment_opened| | slash_star_comment_opened) {


if (!this._deletecomment) {


Htmltxt[htmltxt.length] = Codearr[i];


}


}


if (quote_opened) {


Htmltxt[htmltxt.length] = Codearr[i];


}


else if (codearr[i]== "\ n") {


Handling Line Wrapping


else if (codearr[i] = = "\ r") {


slash_slash_comment_opened = false;


quote_opened = false;


line_num++;


if (!this.iscompress) {


Htmltxt[htmltxt.length] = "\ r \ n" + this.getident ();


}


Handling parameter Markers in function


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && This.isfunction (Codearr[i])) {


Htmltxt[htmltxt.length] = Codearr[i] + "";


Function_opened = true;


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && Codearr[i]==this._isfor) {


Htmltxt[htmltxt.length] = Codearr[i];


For_open = true;


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && codearr[i]== "(") {


Bracket_open = true;


Htmltxt[htmltxt.length] = Codearr[i];


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && codearr[i]== ")") {


Bracket_open = false;


Htmltxt[htmltxt.length] = Codearr[i];


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && Codearr[i]==this._rowend) {


if (!this.iscompress) {


if (!for_open) {


if (i<word_index&& (codearr[i+1]!= "\ r" &&codearr[i+1]!= "\ n")) {


Htmltxt[htmltxt.length] = codearr[i] + "\ n" + this.getident ();


}else{


Htmltxt[htmltxt.length] = Codearr[i] + this.getident ();


}


}else{


Htmltxt[htmltxt.length] = Codearr[i];


}


}else{


Htmltxt[htmltxt.length] = Codearr[i];


}


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && Codearr[i]==this._beginblock) {


For_open = false;


if (!this.iscompress) {


Switch (This.style) {


Case 0:


this._tabnum++;


Htmltxt[htmltxt.length] = codearr[i] + "\ n" + this.getident ();


Break


Case 1:


Htmltxt[htmltxt.length] = "\ n" + this.getident ();


this._tabnum++;


Htmltxt[htmltxt.length] = codearr[i] + "\ n" + this.getident ();


Break


Default


this._tabnum++;


Htmltxt[htmltxt.length] = Codearr[i];


Break





}


}else{


Htmltxt[htmltxt.length] = Codearr[i];


}





else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && Codearr[i]==this._endblock) {


if (!this.iscompress) {


this._tabnum--;


if (i<word_index&&codearr[i+1]!=this._rowend) {


Htmltxt[htmltxt.length] = "\ n" + this.getident () + codearr[i];


}else{


Htmltxt[htmltxt.length] = "\ n" + this.getident () + codearr[i];


}


}else{


if (i<word_index&&codearr[i+1]!=this._rowend) {


Htmltxt[htmltxt.length] = Codearr[i] + this._rowend;


}else{


Htmltxt[htmltxt.length] = Codearr[i];


}


}


Working with Keywords


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && This.isblockelement (Codearr[i])) {


Htmltxt[htmltxt.length] = Codearr[i];


Working with built-in objects (followed by a space)


else if (!slash_slash_comment_opened&&!slash_star_comment_opened &&!quote_opened && This.issingleeyeelement (Codearr[i])) {


if (codearr[i]==this._in) {


Htmltxt[htmltxt.length] = "";


}


Htmltxt[htmltxt.length] = Codearr[i] + "";


Handle double quotes (cannot be escaped characters before quotes)


else if (!slash_star_comment_opened&&!slash_slash_comment_opened&&this._quotation.contains ( Codearr[i])) {


if (quote_opened) {


is the corresponding quotation mark


if (Quote_char==codearr[i]) {


Htmltxt[htmltxt.length] = Codearr[i];


quote_opened = false;


Quote_char = "";


} else {


Htmltxt[htmltxt.length] = Codearr[i];


}


} else {


Htmltxt[htmltxt.length] = Codearr[i];


Quote_opened = true;


Quote_char = Codearr[i];


}


Handling Escape characters


else if (codearr[i] = = This._escape) {


Htmltxt[htmltxt.length] = Codearr[i];


if (i<word_index-1) {


if (codearr[i+1].charcodeat (0) >=32&&codearr[i+1].charcodeat (0) <=127) {


Htmltxt[htmltxt.length] = codearr[i+1].substr (0,1);


Htmltxt[htmltxt.length] = codearr[i+1].substr (1);


i=i+1;


}


}


To handle the start of multiple-line comments


else if (!slash_slash_comment_opened &&!slash_star_comment_opened&&!quote_opened&& This.isstartwith (this._commenton,codearr,i)) {


Slash_star_comment_opened = true;


if (!this._deletecomment) {


Htmltxt[htmltxt.length] = This._commenton;


}


i = i + this.getskiplength (This._commenton);


Working with Single-line annotations


else if (!slash_slash_comment_opened &&!slash_star_comment_opened&&!quote_opened&& This.isstartwith (this._linecomment,codearr,i)) {


Slash_slash_comment_opened = true;


if (!this._deletecomment) {


Htmltxt[htmltxt.length] = this._linecomment;


}


i = i + this.getskiplength (this._linecomment);


Handling ignored words


else if (!slash_slash_comment_opened &&!slash_star_comment_opened&&!quote_opened&& This.isstartwith (this._ignore,codearr,i)) {


Slash_slash_comment_opened = true;


Htmltxt[htmltxt.length] = This._ignore;


i = i + this.getskiplength (this._ignore);


Work with multiline comment end


else if (!quote_opened&&!slash_slash_comment_opened&&this.isstartwith (THIS._COMMENTOFF,CODEARR), i)) {


if (slash_star_comment_opened) {


slash_star_comment_opened = false;


if (!this._deletecomment) {


Htmltxt[htmltxt.length] = This._commentoff;


}


i = i + this.getskiplength (this._commentoff);


}


} else {


Not in a string.


if (!quote_opened) {


If the comment is not heavy


if (!slash_slash_comment_opened &&!slash_star_comment_opened) {


Htmltxt[htmltxt.length] = Codearr[i];


In the note


}else{


if (!this._deletecomment) {


Htmltxt[htmltxt.length] = Codearr[i];


}


}


}else{


Htmltxt[htmltxt.length] = Codearr[i];


}


}





}





Return Htmltxt.join ("");


}





This.isstartwith = function (Str,code,index) {





if (typeof (str)!= "undefined" &&str.length>0) {


var cc = new Array ();


for (Var i=index;i<index+str.length;i++) {


Cc[cc.length] = Code[i];


}


var c = cc.join ("");


if (this._casesensitive) {


if (Str.length>=code[index].length&&c.indexof (str) ==0) {


return true;


}


}else{


if (Str.length>=code[index].length&&c.tolowercase (). IndexOf (Str.tolowercase ()) ==0) {


return true;


}


}


return false;





} else {


return false;


}


}





This.isfunction = function (val) {


Return This._function.contains (This._casesensitive?val:val.tolowercase ());


}





This.isblockelement = function (val) {


Return This._blockelement.contains (This._casesensitive?val:val.tolowercase ());


}





This.ischoiceelement = function (val) {


Return This._choiceelement.contains (This._casesensitive?val:val.tolowercase ());


}





This.issingleeyeelement = function (val) {


Return This._singleeyeelement.contains (This._casesensitive?val:val.tolowercase ());


}





This.isnextelement = function (From,word) {


for (Var i=from;i<word.length;i++) {


if (word[i]!= "" &&word[i]!= "\ T" &&word[i]!= "\ r" &&word[i]!= "\ n") {


Return This.ischoiceelement (Word[i]);


}


}


return false;


}





This.getskiplength = function (val) {


var count = 0;


for (Var i=0;i<val.length;i++) {


if (This._worddelimiters.indexof (Val.charat (i)) >=0) {


count++;


}


}


if (count>0) {


count=count-1;


}


return count;


}





This.getident=function () {


var n = [];


for (Var i=0;i<this._tabnum;i++) {


N[n.length] = "T";


}


Return N.join ("");


}


}





function Doformat (o) {


var htmltxt = "";





if (o = = null) {


Alert ("Domnode is null!");


Return


}





var _codetxt = "";





if (typeof (o) = = "Object") {


Switch (o.tagname) {


Case "TEXTAREA":


Case "INPUT":


_codetxt = O.value;


Break


Case "DIV":


Case "SPAN":


_codetxt = O.innertext;


Break


Default


_codetxt = o.innerhtml;


Break


}


}else{


_codetxt = O;


}





var _syn = new Class_format (_codetxt);


Htmltxt = _syn.format ();


return htmltxt;


}








function Go ()


{


var code = document.getElementById ("code"). Value;


var xx = new Class_format (code);


var a = new Date ();





if (document.getElementById (' cbooperate '). Selectedindex==1) {


Xx.iscompress=true;


}else{


Xx.style = parseint (document.getElementById (' Cbostyle '). Value);


}


document.getElementById ("Display"). Value = Xx.format ();


Alert ("Total flowers:" + (New Date (). GetTime ()-a.gettime ()) + "MS");


}


-->


</SCRIPT>


<textarea Id=code rows=12 cols=100>


/*


This is a class


*/








function xx (NUM,STR) {//description


var a = Num;this.aa = A;


THIS.BB = function () {alert (str);}


this.cc = function () {for (Var i=0;i<10;i++) {document.title=i;}}};





xx.prototype.dd= function () {alert ("D-KD");}





var a = new XX (+, "Hello"), B=new xx (0, "Ttyp");


if (1>2) {alert ();


}


else {


Alert ("Hell");


}





A.BB ();


B.dd ();


alert (A.AA);





</TEXTAREA> <BR>


<select id= "Cbooperate" onchange= "if" (this.selectedindex==1) document.getElementById (' CboStyle '). Disabled=true; else document.getElementById (' Cbostyle '). Disabled=false; " >


<option value= "0" > Format </option>


<option value= "1" > Compression </option>


</select>


<select id= "Cbostyle" >


<option value= "0" > Classic </option>


<option value= "1" > Microsoft </option>


</select>


<input Onclick=go () Type=button value=go><br>


<textarea Id=display rows=12 cols=100>


</textarea>


</BODY></HTML>





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.