他の人が書いた乱雑なコードを見て頭が痛くなったことはありますか? BWindow コードを見て悩んだことはありますか? JS のサイズを小さくすることと JS の読みやすさとの間に矛盾があることを考えるのが最善の方法です。コードを持っています フォーマットとコード圧縮のためのプログラムが今日ついに利用可能になりました。これは純粋な JS で書かれており、もちろんオープンソースです。申し訳ありませんが、最初に宣伝させてください。長い間書きましたが、週末に書き終えました。まだ小さなバグがいくつかあります。頻繁に更新することに注意してください。
<HTML><HEAD><TITLE>形式</TITLE>
<META content="MSHTML 6.00.2800.1528" name=GENERATOR>
<META content="" name=著者>
<META content="" name=キーワード>
<META content="" name=説明></HEAD>
<本体>
<SCRIPT言語=JavaScript>
<!--
/**//**//**//**
** =============================================== =================================================
** クラス名: CLASS_FORMATER
** 機能: JS フォーマット
** 例:
-------------------------------------------------- --------------------------------------------------
var xx = 新しい CLASS_FORMATER(コード);
document.getElementById("display").innerHTML = xx.format();
-------------------------------------------------- --------------------------------------------------
** 著者: ttyp
** 電子メール: [email protected]
** 日付: 2006-5-21
** バージョン: 0.1
** =============================================== =================================================
**/
関数 CLASS_FORMAT(コード){
//ハッシュテーブルクラス
関数ハッシュテーブル(){
this._hash = 新しいオブジェクト();
this.add = function(key,value){
if(typeof(key)!="未定義"){
if(this.contains(key)==false){
this._hash[キー]=typeof(値)=="未定義"?null:値;
true を返します。
} それ以外 {
false を返します。
}
} それ以外 {
false を返します。
}
}
this.remove = function(key){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])!="未定義";}
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 = 新しいハッシュテーブル();
var _cs = true;
if(typeof(cs)=="未定義"||cs==null){
_cs = this._caseSensitive;
} それ以外 {
_cs = cs;
}
for(_key の変数 i){
if(_cs){
_hash.add(_key[i]);
} それ以外 {
_hash.add((_key[i]+"").toLowerCase());
}
}
_ハッシュを返します;
}
//変換する必要があるコードを取得します
this._codetxt = コード;
if(typeof(syntax)=="未定義"){
構文 = "";
this._deleteComment
= false;
//大文字と小文字を区別するかどうか
this._caseSensitive = true;
//ブロック文の後に追加できるキーワード
this._blockElement = this.str2hashtable("switch,if,while,try,finally");
//関数宣言です
this._function = this.str2hashtable("関数");
//この行の括弧内のセミコロンは改行を引き起こしません。
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= " ,.?!;:\/<>(){}[]"'rnt=+-|*%@#$^&";
//文字を引用符で囲む
this._quote = 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 = 新しい Array();
var word_index = 0;
var htmlTxt = 新しい配列();
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]) == '未定義') {
codeArr[word_index] = "";
}
codeArr[word_index] += this._codetxt.charAt(i);
} それ以外 {
if (typeof(codeArr[word_index]) != '未定義' && codeArr[word_index].length > 0)
word_index++;
codeArr[word_index++] = this._codetxt.charAt(i);
}
}
var quote_opened = false; //引用符;
var sh_star_comment_opened = false; //複数行のコメントマーク
var sh_slash_comment_opened = false; // 単一行のコメントマーク
var line_num = 1; //行番号
var quote_char = "" //引用符の種類
var function_opened = false
;
var for_open = false;
//分割された単語に応じてブロックで表示します。
for (var i=0; i <=word_index; i++){
// 空行の処理 (エスケープによる)
if(typeof(codeArr[i])=="未定義"||codeArr[i].length==0){
続く;
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"){
スラッシュ_スラッシュ_コメント_オープン = false;
quote_opened = false;
line_num++;
if(!this.isCompress){
htmlTxt[htmlTxt.length] = "rn"+ this.getIdent();
}
//関数内のパラメータマーカーを処理します
} 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]=="("){
ブラケットオープン = true;
htmlTxt[htmlTxt.length] = codeArr[i];
} else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==")"){
ブラケットオープン = 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();
}それ以外{
htmlTxt[htmlTxt.length] = codeArr[i] + this.getIdent();
}
}それ以外{
htmlTxt[htmlTxt.length] = codeArr[i];
}
}それ以外{
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){
スイッチ(this.style){
ケース0:
this._tabNum++;
htmlTxt[htmlTxt.length] = codeArr[i] + "n" + this.getIdent();
壊す;
ケース1:
htmlTxt[htmlTxt.length] = "n" + this.getIdent();
this._tabNum++;
htmlTxt[htmlTxt.length] = codeArr[i] + "n"+ this.getIdent();
壊す;
デフォルト:
this._tabNum++;
htmlTxt[htmlTxt.length] = codeArr[i];
壊す;
}
}それ以外{
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];
}それ以外{
htmlTxt[htmlTxt.length] = "n" + this.getIdent() + codeArr[i];
}
}それ以外{
if(i<word_index&&codeArr[i+1]!=this._rowEnd){
htmlTxt[htmlTxt.length] = codeArr[i] + this._rowEnd;
}それ以外{
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 = "";
} それ以外 {
htmlTxt[htmlTxt.length] = codeArr[i];
}
} それ以外 {
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)){
スラッシュ_スター_コメント_オープン = 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)){
スラッシュ_スラッシュ_コメント_オープン = 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)){
スラッシュ_スラッシュ_コメント_オープン = 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) {
スラッシュ_スター_コメント_オープン = false;
if(!this._deleteComment){
htmlTxt[htmlTxt.length] = this._commentOff;
}
i = i + this.getSkipLength(this._commentOff);
}
} それ以外 {
//文字列内にない
if(!quote_opened){
// コメントにない場合
if(!slash_slash_comment_opened && !slash_star_comment_opened){
htmlTxt[htmlTxt.length] = codeArr[i];
//コメントする
}それ以外{
if(!this._deleteComment){
htmlTxt[htmlTxt.length] = codeArr[i];
}
}
}それ以外{
htmlTxt[htmlTxt.length] = codeArr[i];
}
}
return
htmlTxt.join("");
this.isStartWith = function(str,code,index){
if(typeof(str)!="未定義"&&str.length>0)
{
var cc = 新しい配列();
for(var i=index;i<index+str.length;i++){
cc[cc.length] = コード[i];
}
var c = cc.join("");
if(this._caseSensitive){
if(str.length>=code[index].length&&c.indexOf(str)==0){
true を返します。
}
}それ以外{
if(str.length>=code[index].length&&c.toLowerCase().indexOf(str.toLowerCase())==0){
true を返します。
}
}
false を返します
} それ以外の場合は {
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"){
this.isChoiceElement(word[i]) を返します。
}
}
false を返します。
this.getSkipLength = function(val)
{
変数カウント = 0;
for(var i=0;i<val.length;i++){
if(this._wordDelimiters.indexOf(val.charAt(i))>=0){
カウント++;
}
}
if(カウント>0){
カウント=カウント-1;
}
戻り数;
this.getIdent=function()
{
var n = [];
for(var i=0;i<this._tabNum;i++){
n[n.length] = "t";
}
return n.join("");
}
関数 doformat(o)
{
var htmltxt = ""
if (o == null){
alert("domNode が null!");
戻る;
var
_codetxt = "";
if(typeof(o)=="オブジェクト"){
switch(o.tagName){
ケース「TEXTAREA」:
「入力」の場合:
_codetxt = o.value;
壊す;
ケース「DIV」:
「スパン」の場合:
_codetxt = o.innerText;
壊す;
デフォルト:
_codetxt = o.innerHTML;
壊す;
}
}それ以外{
_codetxt = o;
var _syn = 新しい CLASS_FORMAT(_codetxt)
;
htmltxt = _syn.format();
htmltxtを返します。
}
関数 go()
{
var code = document.getElementById("code").value;
var xx = 新しい CLASS_FORMAT(コード);
var a = new Date();
if(document.getElementById('cboOperate').selectedIndex==1){
xx.isCompress=true;
}それ以外{
xx.style = parseInt(document.getElementById('cboStyle').value);
}
document.getElementById("表示").value = xx.format();
alert("花の合計:" + (new Date().getTime()-a.getTime()) + "ms");
}
//-->
</スクリプト>
<TEXTAREA id=code rows=12cols=100>
/*
これはクラスです
*/
関数 xx (num,str){//説明
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 d kd"); var
a = 新しい xx(100,"hello"),b=新しい xx(0,"ttyp");
if(1>2) { アラート();
}
それ以外 {
アラート(「地獄」);
a.bb
();
b.dd();
アラート(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">形式</option>
<option value="1">圧縮</option>
</選択>
<select id="cboStyle">
<option value="0">クラシック</option>
<option value="1">マイクロソフト</option>
</選択>
<INPUT onclick=go() type=button value=go><br>
<textarea id=display rows=12cols=100>
</textarea>
</BODY></HTML>