|
2#
楼主 |
发表于 2006-11-14 14:34:43
|
只看该作者
var regKW=new RegExp("(\\W"+KEYWORDS.replace(/ /g,"$)|(\\W")+"$)","g");
var regObj=new RegExp("(\\W"+OBJECTS.replace(/ /g,"$)|(\\W")+"$)","g");
var regMP=new RegExp("(\\W"+METHODS_PROPERTIES.replace(/ /g,"$)|(\\W")+"$)","g");
//var regOP=new RegExp("(\\W"+OPS.replace(/ /g,"$)|(\\W")+"$)","g");
var colorKW="blue";
var colorObj="red";
var colorMP="#FF8000";
var colorOP="#004000";
//------------------
// Global Variables
//------------------
var divJSInput, txtJSInput, divJSOutput, divWaiting, spnProcess;
var glbStr, glbP, glbRe, curRe, glbTimer;
function init(){
// init global variables
divJSInput=document.getElementById("divJSInput");
txtJSInput=document.getElementById("txtJSInput");
divJSOutput=document.getElementById("divJSOutput");
divWaiting=document.getElementById("divWaiting");
spnProcess=document.getElementById("spnProcess");
// init window state
maximizeWindow();
divJSOutput.style.width=document.body.clientWidth-2;
divJSOutput.style.height=document.body.clientHeight-26;
// init global events
divJSInput.onkeydown=divJSInput_keydown;
}
//------------------
// event scripts
//------------------
function divJSInput_keydown(e){
var e=window.event?window.event:e;
var srcEle=e.srcElement?e.srcElement:e.target;
var sel;
if(e.keyCode==27)hideJSInput();
if(e.keyCode==13&&e.ctrlKey)execJSInput();
if(e.keyCode==9&&srcEle==txtJSInput){
document.selection.createRange().text="\t";return(false); // not support FF
}
}
//------------------
// functional scripts
//------------------
function showJSInput(){
with(divJSInput.style){
display="block";
left=(document.body.clientWidth-divJSInput.offsetWidth)/2;
top=(document.body.clientHeight-divJSInput.offsetHeight)/2;
}
txtJSInput.focus();
return(false);
}
function hideJSInput(){
divJSInput.style.display="none";
}
function execJSInput(){
hideJSInput();
divJSOutput.innerHTML="";
glbStr=txtJSInput.value.replace(/\r\n[ \t]+/gi,"\r\n").replace(/(\r\n)+/gi,"\r\n");
glbP=0;
curRe=glbRe=document.createElement("div");
divJSOutput.appendChild(glbRe);
glbRe.className="codeRoot";
showWait();
core_analysis();
}
function showWait(){
document.body.style.cursor="wait";
with(divWaiting.style){
display="block";
left=(document.body.clientWidth-divWaiting.offsetWidth)/2;
top=(document.body.clientHeight-divWaiting.offsetHeight)/2;
}
spnProcess.innerHTML="0.00% ( 0 / 0 )"
return(false);
}
function stopExec(){
document.body.style.cursor="";
divWaiting.style.display="none";
try{clearTimeout(glbTimer);}catch(e){}
return(false);
}
function core_analysis(){
var str=" ", c="", lastState="", seq, intNextQuote, intTemp, intCount, intWordStart;
spnProcess.innerHTML=parseFloat(glbP/glbStr.length*100).toFixed(2)+"% ( "+glbP+" / "+glbStr.length+" )";
for(var i=glbP;i<glbStr.length;i++){
c=glbStr.charAt(i);
str+=htmlEncode(c);
switch(c){
case "\r": case " ": case "\t":
if(lastState=="\r\n"){str=" ";break;}
if(c.match(/\W/)&&glbStr.charAt(i-1).match(/\w/)){
str=str.substring(0,str.length-htmlEncode(c).length);
str=str.replace(regKW,clKW).replace(regObj,clObj).replace(regMP,clMP)+htmlEncode(c);
}
break;
case "\n": case ";":
if(lastState=="\r\n"){str=" ";break;}
outputLn(str);
str=" ";
lastState="\r\n";
if(i-glbP>200){
glbP=i+1;
glbTimer=setTimeout(core_analysis);
return;
}
break;
case "\"":
intNextQuote=i;
while(intNextQuote!=-1&&intNextQuote<glbStr.length){
intNextQuote=glbStr.indexOf("\"",intNextQuote+1);
if(intNextQuote==-1||glbStr.charAt(intNextQuote-1)!="\\")break;
intCount=0; intTemp=intNextQuote;
while(glbStr.charAt(--intTemp)=="\\")intCount++;
if(intCount%2==0)break;
}
if(intNextQuote==-1)break;
str+="<span class=\"quote\">"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"<\/span>\"";
i=intNextQuote;
lastState="";
break;
case "\'":
intNextQuote=i;
while(intNextQuote!=-1&&intNextQuote<glbStr.length){
intNextQuote=glbStr.indexOf("\'",intNextQuote+1);
if(intNextQuote==-1||glbStr.charAt(intNextQuote-1)!="\\")break;
intCount=0; intTemp=intNextQuote;
while(glbStr.charAt(--intTemp)=="\\")intCount++;
if(intCount%2==0)break;
}
if(intNextQuote==-1)break;
str+="<span class=\"quote\">"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"<\/span>\'";
i=intNextQuote;
lastState="";
break;
case "\/":
if(glbStr.charAt(i+1)=="\/"){
intNextQuote=i;
intNextQuote=glbStr.indexOf("\r\n",intNextQuote+1);
if(intNextQuote==-1)intNextQuote=glbStr.length;
str=str.substring(0,str.length-1);
str+="<span class=\"comments\">\/"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"<\/span>";
i=intNextQuote;
}else if(glbStr.charAt(i+1)=="*"){
intNextQuote=i;
intNextQuote=glbStr.indexOf("*\/",intNextQuote+1);
if(intNextQuote==-1)return;
str=str.substring(0,str.length-1);
str+="<span class=\"comments\">\/"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"*\/<\/span>";
i=intNextQuote+1;
}else if(str.match(/[=(][ \t]*\//)){
intNextQuote=i;
while(intNextQuote!=-1&&intNextQuote<glbStr.length){
intNextQuote=glbStr.indexOf("\/",intNextQuote+1);
if(intNextQuote==-1||glbStr.charAt(intNextQuote-1)!="\\")break;
intCount=0; intTemp=intNextQuote;
while(glbStr.charAt(--intTemp)=="\\")intCount++;
if(intCount%2==0)break;
}
if(intNextQuote==-1)break;
str+="<span class=\"regexp\">"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"<\/span>\/";
i=intNextQuote;
lastState="";
}
lastState="";
break;
case "{":
outputLn(str);
str=" ";
seq=document.createElement("div");
seq.className="indent";
curRe.appendChild(seq);
curRe=seq;
lastState="\r\n";
if(i-glbP>200){
glbP=i+1;
glbTimer=setTimeout(core_analysis);
return;
}
break;
case "}":
outputLn(str.substring(0,str.length-1));
str="} ";
lastState="";
curRe=curRe.parentNode;
break;
default:
if(c.match(/\w/)&&glbStr.charAt(i-1).match(/\W/)){intWordStart=i;}
if(c.match(/\W/)&&glbStr.charAt(i-1).match(/\w/)){
str=str.substring(0,str.length-htmlEncode(c).length);
str=str.replace(regKW,clKW).replace(regObj,clObj).replace(regMP,clMP)+htmlEncode(c);
}
lastState="";
break;
}
}
if(i==glbStr.length){
if(str!=""){ outputLn(str); str=" "; }
stopExec();
}
}
function outputLn(theStr){
var seq=document.createElement("p");
seq.innerHTML=theStr;
curRe.appendChild(seq);
}
function clKW(str){ return(str.charAt(0)+str.substring(1).fontcolor(colorKW)); }
function clObj(str){ return(str.charAt(0)+str.substring(1).fontcolor(colorObj)); }
function clMP(str){ return(str.charAt(0)+str.substring(1).fontcolor(colorMP)); }
function clOP(str){ return(str.charAt(0)+str.substring(1).fontcolor(colorOP)); }
//------------------
// global scripts
//------------------
function maximizeWindow(){
window.moveTo(0,0);
window.resizeTo(screen.availWidth,screen.availHeight);
}
function htmlEncode(strS){
return(strS.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/ /g," ").replace(/\r\n/g,"<br\/>"));
}
</script>
</head>
<body>
<div id="divTools">
<a href="#" onclick="return(showJSInput());">Input JS</a>
</div> |
|