查看效果:格式化代碼 從比較緊密地javascript轉(zhuǎn)換成標(biāo)準(zhǔn)地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;
//字符串轉(zhuǎn)換為哈希表
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;
}
//獲的需要轉(zhuǎn)換地代碼
this._codetxt = code;
if(typeof(syntax)==undefined){
syntax = ;
}
this._deletecomment = false;
//是否大小寫敏感
this._casesensitive = true;
//可以后面加塊語句地關(guān)鍵字
this._blockelement = this.str2hashtable(switch,if,while,try,finally);
//是函數(shù)申明
this._function = this.str2hashtable(function);
//本行括號內(nèi)分號不做換行
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 = //;
//轉(zhuǎn)義字符
this._escape = \\;
//多行引用開始
this._commenton = /*;
//多行引用結(jié)束
this._commentoff = */;
//行結(jié)束詞
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;
}
//的到分割字符數(shù)組(分詞)
for (var i = 0; i < this._codetxt.length; i++) {
if (this._worddelimiters.indexof(this._codetxt.charat(i)) == -1) { //找不到關(guān)鍵字
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; //引用標(biāo)記
var slash_star_comment_opened = false; //多行注釋標(biāo)記
var slash_slash_comment_opened = false; //單行注釋標(biāo)記
var line_num = 1; //行號
var quote_char = ; //引用標(biāo)記類型
var function_opened = false;
var bracket_open = false;
var for_open = false;
//按分割字,分塊顯示
for (var i=0; i <=word_index; i++){
//處理空行(由于轉(zhuǎn)義帶來)
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里地參數(shù)標(biāo)記
} 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];
}
}
//處理關(guān)鍵字
} else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isblockelement(codearr[i])){
htmltxt[htmltxt.length] = codearr[i];
//處理內(nè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] + ;
//處理雙引號(引號前不能為轉(zhuǎn)義字符)
} else if (!slash_star_comment_opened&&!slash_slash_comment_opened&&this._quotation.contains(codearr[i])){
if (quote_opened){
//是相應(yīng)地引號
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];
}
//處理轉(zhuǎn)義字符
} 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);
//處理多行注釋結(jié)束
} 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>經(jīng)典</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技術(shù)專欄