格式化javascript代码
2014-10-08来源:

查看效果:格式化代码 从比较紧密地javascript转换成标准地javascript代码

代码如下:

<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>

<!--

function class_format(code){

//哈希表类

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++;} return 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;

//字符串转换为哈希表

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;

}

//获的需要转换地代码

this._codetxt = code;

if(typeof(syntax)==undefined){

syntax = ;

}

this._deletecomment = false;

//是否大小写敏感

this._casesensitive = true;

//可以后面加块语句地关键字

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

//是函数申明

this._function = this.str2hashtable(function);

//本行括号内分号不做换行

this._isfor = for;

this._choiceelement = this.str2hashtable(else,catch);

this._beginblock = {;

this._endblock = };

this._singleeyeelement = this.str2hashtable(var,new,return,else,delete,in,case);

//的到分割字符

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

//引用字符

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

//行注释字符

this._linecomment = //;

//转义字符

this._escape = \\;

//多行引用开始

this._commenton = /*;

//多行引用结束

this._commentoff = */;

//行结束词

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;

}

//的到分割字符数组(分词)

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

if (this._worddelimiters.indexof(this._codetxt.charat(i)) == -1) { //找不到关键字

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; //引用标记

var slash_star_comment_opened = false; //多行注释标记

var slash_slash_comment_opened = false; //单行注释标记

var line_num = 1; //行号

var quote_char = ; //引用标记类型

var function_opened = false;

var bracket_open = false;

var for_open = false;

//按分割字,分块显示

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

//处理空行(由于转义带来)

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){

//处理换行

} 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();

}

//处理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];

}

}

//处理关键字

} else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isblockelement(codearr[i])){

htmltxt[htmltxt.length] = codearr[i];

//处理内置对象(后面加一个空格)

} 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] + ;

//处理双引号(引号前不能为转义字符)

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

if (quote_opened){

//是相应地引号

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];

}

//处理转义字符

} 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;

}

}

//处理多行注释地开始

} 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);

//处理单行注释

} 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);

//处理忽略词

} 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);

//处理多行注释结束

} 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 {

//不是在字符串中

if(!quote_opened){

//如果不是在注释重

if(!slash_slash_comment_opened && !slash_star_comment_opened){

htmltxt[htmltxt.length] = codearr[i];

//注释中

}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();

}

//-->

</script>

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

</textarea> <br>

<select id=cbooperate onchange=if(this.selectedindex==1)document.getelementbyid('cbostyle').disabled=true;else document.getelementbyid('cbostyle').disabled=false;>

<option value=0>格式化</option>

<option value=1>压缩</option>

</select>

<select id=cbostyle>

<option value=0>经典</option>

<option value=1>微软</option>

</select>

<input onclick=go() type=button value=go><br>

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

</text_area>

</body></html>

更多信息请查看IT技术专栏

推荐信息
Baidu
map