코드는 다음과 같습니다.
형식
<script> <BR><!-- <BR>/**//**//** <BR>** ======================================== == =============================================== == ======= <BR>** 클래스 이름: CLASS_FORMATER <BR>** 함수: JS 형식화 <BR>** 예: <BR> ------------- --- ---------------------------------- --- ---------------------- <br><br> var xx = new CLASS_FORMATER( 코드); <br><br> document.getElementById("display").innerHTML = xx.format(); <br><br> ------------------ -------- ---------------- -------- --------------- <BR>** 작성자: ttyp <BR>** 이메일 : <a href="mailto :ttyp@21cn.com">ttyp@21cn.com <BR>** 날짜: 2006-5-21 <BR>** 버전: 0.1 <BR>* * ======= ========================================= ======== ========================================<BR>**<🎝>* <br>** =================== ================================================= ============================ <br>** 특정명:CLASS_FORMATER <BR>** 功能:JS格式化 <BR>** 예문: <BR> ----------------------------- ------------------------------------- ------- <BR><BR> var xx = new CLASS_FORMATER(코드); <BR><BR> document.getElementById("display").innerHTML = xx.format(); <BR><BR> ------------------------------- ------------------------------------- ----- <BR>** 작곡자:ttyp <BR>** 邮件:<a href="mailto:ttyp@21cn.com">ttyp@21cn.com <BR>** 日期:2006-5-21 <BR>** 版本:0.1 <BR>** ======================== ================================================= ====================== <BR>**/ <BR><BR>함수 CLASS_FORMAT(코드){ <🎜> //哈希表类<🎜> 함수 Hashtable(){ <🎜> this._hash = new Object(); <🎜> this.add = 함수(키, 값){ <🎜> if(typeof(key)!="미정의"){ <🎜> if(this.contain s(key)==false){ <🎜> 이 ._hash[키]=typeof(값)=="정의되지 않음"?null:값; <🎜> true를 반환합니다. <🎜> } else { <🎜> false를 반환합니다. <🎜> } <🎜> } 그렇지 않으면 { <🎜> 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;}<BR> this.items = function(key){return this._hash[key];} <BR> this.contains = function(key){return typeof(this._hash[key])!="정의되지 않음";} <BR> this.clear = function(){for(var k in this._hash){delete this._hash[k];}} <br><br> } <br><br> this._caseSensitive = true; <br><br> //字符串转换为哈希表 <BR> this.str2hashtable = function(key,cs){ <br><br> var _key = key.split(/,/g); <BR> var _hash = new Hashtable(); <BR> var _cs = true; <br><br> <BR> if(typeof(cs)=="undefed"||cs==null){ <BR> _cs = this._caseSensitive; <BR> } else { <BR> _cs = cs; <BR> } <br><br> for(var i in _key){ <BR> if(_cs){ <BR> _hash.add(_key[i]); <BR> } else { <BR> _hash.add((_key[i] "").toLowerCase()); <BR> } <br><br> } <BR> return _hash; <BR> } <br><br> //获得需要转换的代码 <BR> this._codetxt = code; <br><br> if(typeof(syntax)=="정의되지 않음"){ <BR> 구문 = ""; <BR> } <br><br> this._deleteComment = false; <BR> //是否大写敏感 <BR> this._caseSensitive = true; <BR> //可以后face加块语句的关键字 <BR> this._blockElement = this.str2hashtable("switch,if,while,try,finally"); <BR> //是函数申明 <BR> this._function = this.str2hashtable("function"); <BR> //本行括号内分号不做换行 <BR> this._isFor = "for"; <br><br> this._choiceElement = this.str2hashtable("else,catch"); <br><br> this._beginBlock = "{"; <BR> this._endBlock = "}"; <br><br> this._singleEyeElement = this.str2hashtable("var,new,return,else,delete,in,case"); <BR> //得到分割字符 <BR> this._wordDelimiters= " ,.?!;:\/<>(){}[]"'rnt= -|*%@#$^&"; <BR> //인용 문자 <BR> this._quotation = this.str2hashtable("",'"); <BR> //줄 주석 문자 <BR> this._lineComment = "//"; <BR> //이스케이프 문자<BR> this._escape = "\" <BR> //여러줄 인용 시작<BR> this._commentOn = "/* “; _in 🎜><BR><BR> this.format = function() { <BR> var codeArr = new Array() <BR> var word_index = 0 <br> var htmlTxt = new Array(); <br><br> if(this.isCompress ){ <br> this._deleteComment = true; <BR> } <BR><br><br> //분할 문자 배열 가져오기(단어 분할) <br> for (var i = 0; i < this._codetxt.length; i ) { ~ || typeof(codeArr[word_index]) == '정의되지 않음') { <br> codeArr[word_index] = """ codeArr[ word_index] = this._codet xt.charAt(i); <BR> codeArr[word_index] = this._codetxt.charAt (i); <br><br><BR> var quote_opened = false; //인용 태그<BR> var slash_star_comment_opened = false; //여러 줄 주석 태그<BR> var slash_ slash_comment_opened = false; > var line_num = 1; 🎜> var Bracket_open = false; <BR> var for_open = false <<> </> // 분할을 누르고 <BR> 표시 for (var I = 0; i & lt; = word_index ; <br> if(typeof(codeArr[i])=="정의되지 않음"||codeArr[i].length==0){ | htmlTxt[htmlTxt .length] = codeArr[i]; <br> 도착[i]; "){ <br> else if(codeArr[i]=="r"){ slash_slash_comment_opened = 거짓; <BR> quote_opened = false; <BR> line_num ; <BR> if(!this.isCompress){ <BR> htmlTxt[htmlTxt.length] = "rn" this.getIdent(); <BR> } <BR> //처리 기능의 다양화 <BR> } 그렇지 않으면 (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isFunction(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] " "; <BR> function_opened = true; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._isFor){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> for_open = true; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]=="("){ <BR> bracket_open = true; htmlTxt[htmlTxt.length] = codeArr[i] <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==")"){ <BR> bracket_open = false; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._rowEnd){ <BR> if(!this.isCompress){ if(!for_open){ <BR> if( i<word_index&&(codeArr[i 1]!="r"&&codeArr[i 1]!="n")){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] "n" this.getIdent(); <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i] this.getIdent(); <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else if(!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._beginBlock){ <BR> _open = false; <BR> if(!this.isCompress){ <BR> 스위치(this.style){ <BR> 케이스 0 <BR> this._tabNum ; <BR> htmlTxt[htmlTxt.length] = codeArr[i] "n" this.getIdent(); <BR> 휴식; <BR> 사례 1: <BR> htmlTxt[htmlTxt.length] = "n" this.getIdent(); <BR> this._tabNum ; <BR> htmlTxt[htmlTxt.length] = codeArr[i] "n" this.getIdent(); <BR> 휴식; <BR> 기본값: <BR> this._tabNum ; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> 휴식; <br><br> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <br><br> } else if(!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._endBlock){ <BR> if(!this.isCompress){ <BR> this._tabNum-- ; <BR> if(i<word_index&&codeArr[i 1]!=this._rowEnd){ <BR> htmlTxt[htmlTxt.length] = "n" this.getIdent() codeArr[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = "n" this.getIdent() codeArr[i]; <BR> } <BR> }else{ <BR> if(i<word_index&&codeArr[i 1]!=this._rowEnd){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] this._rowEnd; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } <BR> //处理关键字 <BR> } 그렇지 않으면 (!slash_slash_comment_opened&&!slash_star_ comment_opened && !quote_opened && this.isBlockElement(codeArr[i])){ <BR> htmlTxt[htmlTxt.length ] = codeArr[i]; <BR> //处理内置对象(后面加一个空格) <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isSingleE yeElement(codeArr[i])){ <BR> if(codeArr[i]= =this._in){ <BR> htmlTxt[htmlTxt.length] = " "; <BR> } <BR> htmlTxt[htmlTxt.length] = codeArr[i] " "; <BR> //处理双引号(引号前不能为转义字符) <BR> } else if (!slash_star_comment_opened&&!slash_slash_comment_opened&&this._quotation.contains(codeArr[i])){ <BR> if (quote_opened){ <BR> //是상应의 引号 <BR> if(quote_char==codeArr[i]){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = false; <BR> quote_char = ""; <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = true; <BR> quote_char = codeArr[i]; <BR> } <BR> //处理转义字符 <BR> } else if(codeArr[i] == this._escape){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> if(i<word_index-1){ <BR> if(codeArr[i 1].charCodeAt(0)>=32&&codeArr[i 1].charCodeAt(0)<=127){ <BR> htmlTxt [htmlTxt.length] = codeArr[i 1].substr(0,1); <BR> htmlTxt[htmlTxt.length] = codeArr[i 1].substr(1); <BR> i=i 1; <BR> } <BR> } <BR> /////处理多行注释的开始 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._commentOn,codeArr,i)){ slash_star_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._commentOn; <BR> } <BR> i = i this.getSkipLength(this._commentOn); <BR> //处理单行注释 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._lineComment,codeArr,i)){ <BR> slash_slash_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._lineComment; <BR> } <BR> i = i this.getSkipLength(this._lineComment); <BR> //处理忽略词 <BR> <BR> slash_slash_comment_opened = true; <BR> htmlTxt[htmlTxt.length] = this._ignore; <BR> i = i this.getSkipLength(this._ignore); <BR> //처리다행注释结束 <BR> } else if (!quote_opened&&!slash_slash_comment_opened&&this.isStartWith(this._commentOff,codeArr,i)){ <BR> if (slash_star_comment_opened) { <BR> slash_star_comment_opened = false; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._commentOff; <BR> }<BR> //문자열에 없음 <BR> ~ 댓글에는 없음 <BR> if(!slash_slash_comment_opened && !slash _star_comment_opened){ if(! <BR> 🎜> }그밖에{ <BR> ~ 와 함께 = function(str,code,index ){ <BR><BR> if(typeof(str)!="undefine"&&str.length>0){ var cc = new Array() for(var i=index;i< ;색인 str.length;i ){ <BR> cc[cc.length] <BR> if(str.length>=code[index].length&&c.indexOf(str)==0){ <BR> if(str .length> ;=code[index].length&&c.toLowerCase().indexOf(str.toLowerCase())==0){ <BR> <BR> false를 반환합니다. <br><br> } else { <BR> false를 반환합니다. <BR> } <BR> } <br><br> this.isFunction = function(val){ <BR> return this._function.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isBlockElement = function(val) { <BR> return this._blockElement.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isChoiceElement = function(val) { <BR> return this._choiceElement.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isSingleEyeElement = function(val) { <BR> return this._singleEyeElement.contains(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.isNextElement = function(from,word){ <BR> for(var i=from;i<word.length;i ){ <BR> if(word[i]! =" "&&word[i]!="t"&&word[i]!="r"&&word[i]!="n"){ <BR> return this.isChoiceElement(word[i]); <BR> } <BR> } <BR> false를 반환; <BR> } <br><br> this.getSkipLength = function(val){ <BR> var count = 0; <BR> for(var i=0;i<val.length;i ){ <BR> if(this._wordDelimiters.indexOf(val.charAt(i))>=0){ <BR> 개수 ; <BR> } <BR> } <BR> if(count>0){ <BR> count=count-1; <BR> } <BR> 반품 횟수; <BR> } <br><br> this.getIdent=function(){ <BR> var n = []; <BR> for(var i=0;i<this._tabNum;i ){ <BR> n[n.length] = "t"; <BR> } <BR> return n.join(""); <BR> } <BR>} <br><br>함수 doformat(o){ <BR> var htmltxt = ""; <br><br> if (o == null){ <BR> alert("domNode가 null입니다!"); <BR> 반품; <BR> }<br><br> var _codetxt = ""; <br><br> if(typeof(o)=="object"){ <BR> switch(o.tagName){ <BR> case "TEXTAREA": <BR> case "INPUT" <BR> _code txt = o.값; <BR> 휴식; <BR> 케이스 'DIV': <BR> 케이스 'SPAN': <BR> _codetxt = o.innerText; <BR> 휴식; <BR> 기본값: <BR> _codetxt = o.innerHTML; <BR> 휴식; <BR> } <BR> }else{ <BR> _codetxt = o; <BR> } <br><br> var _syn = new CLASS_FORMAT(_codetxt); <BR> htmltxt = _syn.format(); <BR> return htmltxt; <BR>} <br><br><BR>function go() <BR>{ <BR> var code = document.getElementById("code").value; <BR> var xx = 새로운 CLASS_FORMAT(코드); <BR> var a = new Date(); <br><br> if(document.getElementById('cboOperate').selectedIndex==1){ <BR> xx.isCompress=true; <BR> }else{ <BR> xx.style = parseInt(document.getElementById('cboStyle').value); <BR> } <BR> document.getElementById("display").value = xx.format(); <BR> alert("共꽃:" (new Date().getTime()-a.getTime()) "ms"); <BR>} <BR>//--> <BR></script>