直接转为高亮版,IE+FF+Cr 测试通过:
代码:
程序代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 过渡//EN"
“ http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ” >
<html xmlns=" http://www.w3.org/1999/xhtml ">
<头>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<标题>普雷(al)</标题>
<样式类型=“文本/css”>
pre.preh{font-family:Consolas,"Lucida Console",monospace;}
pre.preh ol{边框:1px 实心#ddd;边距:0;填充:0 0 0 4em;颜色:灰色;}
pre.preh ol li{行高:1.75em;边框底部:1px实心#ddd;边框左:1px实心#ddd;}
pre.preh ol li span{color:black;white-space:pre;}
pre.preh ol li span span.regexp{背景:绿色;颜色:白色}
pre.preh ol li span em{颜色:蓝色;字体样式:正常;}
pre.preh ol li span var.object{color:red}
pre.preh ol li span var.method_property{color:orange}
pre.preh ol li span.comments{color:#aaa}
pre.preh li var{颜色:蓝色;字体样式:正常;}
pre.preh li q{颜色:绿色;}
q:之前{内容:无;显示:无;}
</风格>
<!--[如果是IE]> <style>pre.preh{font-family:fixedsys,monospace}</style> <![endif]-->
<脚本>
//关键字
var 关键字 = {js:{},html:{},css:{}};
keywords.js.keys="abstract break byte case catch char class const continue default delete do double else extends false Finalfinally float for function goto if 实现了 ininstanceof int 接口中的导入 long native null package private protected public Reset return Short static super switchsynchronous这抛出瞬态 true try var void while with";
keywords.js.objects="锚点 Applet 区域参数数组布尔按钮复选框集合加密日期字典文档驱动器驱动器元素枚举器事件文件 FileObject FileSystemObject FileUpload 文件夹文件夹表单框架函数全局隐藏历史记录 HTMLElement 图像无限输入 JavaArray JavaClass JavaObject JavaPackage JSObject 层链接数学 MimeType导航器 编号 对象 选项包 密码插件 PrivilegeManager 随机正则表达式 屏幕选择字符串 提交文本 文本区域 URL VBArray 窗口 WScript";
keywords.js.properties =“上面abs acos操作activeElement警报alinkColor所有altKey锚点锚点appCodeName小程序应用appName appVersion参数arity asin分配atan atan2 atobvailHeightavailLeftavailTopavailWidthActiveXObject背面背景低于bgColor大闪烁模糊粗体边框borderWidths底部btoa按钮调用被调用者调用者 cancelBubble captureEvents ceil charAt charCodeAt 字符集已检查子类 classNameclearclearIntervalclearTimeoutclickclientInformationclientXclientYcloseclosecolorDepth编译完成concat确认构造函数包含上下文cookie coscryptoctrlKey当前数据defaultCharsetdefaultCheckeddefaultSelecteddefaultStatusdefaultValue描述disableExternalCapturedisablePrivilege文档域E Echo元素元素嵌入enabledPlugin enableExternalCapture enablePrivilege 编码转义 eval 事件 exec exp Expando FromPoint fgColor fileName 查找固定底部焦点 fontColor fontSize 表单表单转发帧 fromCharCode fromElement getAttribute get getClass getDate getDay getFullYear getHours getMember getMilliseconds getMinutes getMonth getSeconds getSelection getSlot getTime getTimezoneOffset getUTCDate getUTCDay getUTCFullYear getUTCHours getUTCMilliseconds getUTCMinutes getUTCMonth getUTCSeconds getWindow getYear global go HandleEvent Height hash 隐藏历史记录 home host hostName href hspace id idsignoreCase images index IndexOf inside insideHTML insideText insideWidth insertAdjacentHTML insertAdjacentText isFinite isNAN italics java javaEnabled join keyCode 链接 LN10 LN2 LOG10E LOG2E lang 语言 lastIndex lastIndexOf lastMatch lastModified lastParen 层layerX layerY left leftContext 长度链接linkColor 加载位置 locationBar log lowsrc MAX_VALUE MIN_VALUE 边距匹配最大菜单栏方法 mimeTypes min 修饰符 moveAbove moveBelow moveBy moveTo moveToAbsolute 多行 NaN NEGATIVE_INFINITY 名称导航 navigator netscape next number offscreenBuffering offsetOffsetHeight offsetLeft offsetParent offsetTop offsetWidth offsetX offsetY onabort onblur onchange onclick ondblclick ondragdrop onerror onfocus onHelp onkeydown按键onkeyup onload onmousedown onmousemove onmouseout onmouseover onmouseup onmove onreset onresize onsubmit onunload 打开开启程序选项outerHeight outerHTML outerText outerWidth POSITIVE_INFINITY PI paddings pageX pageXOffset pageY pageYOffsetparentparentElementparentLayerparentWindowparseFloatparseInt路径名personalbarpixelDepth平台插件pop端口pow首选项以前的打印提示协议原型推送随机readyState原因引用者刷新releaseEvents重新加载removeAttribute删除成员resizeBy resizeTo returnValue反转右右上下文SQRT1_2 SQRT2 screenX screenY滚动滚动条scrollBy rollIntoView滚动搜索选择选定的selectedIndex self setAttribute setDay setFullYear setHotkeys setHours setInterval setMember setMilliseconds setMinutes setMonth setRessized setSeconds setSlot setTime setTimeout setUTCDate setUTCFullYear setUTCHours setUTCMillseconds setUTCMinutes setUTCMonth setUTCSeconds setYear setZOptionsshiftshiftKeysiblingAbovesiblingBelowsignTextsin切片smallsort源sourceIndexsplicesplitsqrtsrcsrcElementsrcFilter状态statusbarstopstrike样式subsubsubstr子字符串后缀sunsupsystemLanguageTYPEtagName标签tainttaintEnabledtan目标测试文本标题toElementtoGMTStringtoLocaleStringtoLowerCase工具栏toptoStringtoUpperCase toUTCString 类型 typeOf UTC unescape unshift untaint unwatch userAgent userLanguage value valueOfvisibility vlinkColor vspace watch 哪个宽度窗口写入 writeln zIndex";
函数 htmlEncode(strS){
return strS.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
}
函数 htmlEncodef(c){
返回 c=="&"?'&':(c=="<"?'<':(c=='>'?'>':c));
}
函数 htmlEncodefl(c){
返回 c=="&"?5:(c=="<"||c=='>'?4:1);
}
函数 html2txt(strS){
return strS.replace(/</g,"<").replace(/>/g,">").replace(/ /g," ").replace(/<br/?>/g," rn").replace(/&/g,"&").replace(/<.+?>/g,"");
}
var 词法分析器 = {}
lexers.js = {
regKW:new RegExp("^("+keywords.js.keys.replace(/ /g,"|")+")$","g"),
regObj:new RegExp("^("+keywords.js.objects.replace(/ /g,"|")+")$","g"),
regMP:new RegExp("^("+keywords.js.properties.replace(/ /g,"|")+")$","g"),
clKW:'<em>$1</em>',
clObj:'<var class="object">$1</var>',
clMP:'<var class="method_property">$1</var>',
词法:函数{
var str=" ", c="",word='',lastState="", seq, intNextQuote, intTemp, intCount, intWordStart,glbStr=s,i;
glbStr=glbStr.replace(/r|n/g,'rn');
glbStr=glbStr.replace(/rnrn/g,'rn');
for(i=0;i<glbStr.length;i++){
c=glbStr.charAt(i);
开关(c){
案件 ”””:
str+=htmlEncodef(c);
intNextQuote=i;
while(intNextQuote!=-1&&intNextQuote<glbStr.length){
intNextQuote=glbStr.indexOf(""",intNextQuote+1);
if(intNextQuote==-1||glbStr.charAt(intNextQuote-1)!=" \")break ;
整数计数=0; intTemp=intNextQuote;
while(glbStr.charAt(--intTemp)==" \")intCount ++;
if(intCount%2==0)break;
}
if(intNextQuote==-1)break;
str+="<q>"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"</q>"";
我=intNextQuote;
最后状态=“”;
休息;
案件 ”'”:
str+=htmlEncodef(c);
intNextQuote=i;
while(intNextQuote!=-1&&intNextQuote<glbStr.length){
intNextQuote=glbStr.indexOf("'",intNextQuote+1);
if(intNextQuote==-1||glbStr.charAt(intNextQuote-1)!=" \")break ;
整数计数=0; intTemp=intNextQuote;
while(glbStr.charAt(--intTemp)==" \")intCount ++;
if(intCount%2==0)break;
}
if(intNextQuote==-1)break;
str+="<q>"+htmlEncode(glbStr.substring(i+1,intNextQuote))+"</q>'";
我=intNextQuote;
最后状态=“”;
休息;
案件 ”/”:
str+=htmlEncodef(c);
if(glbStr.charAt(i+1)=="/"){
intNextQuote=i;
intNextQuote=glbStr.indexOf('rn',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>";
我=intNextQuote;
}否则 if(glbStr.charAt(i+1)=="*"){
intNextQuote=i;
intNextQuote=glbStr.indexOf("*/",intNextQuote+1);
if(intNextQuote==-1)返回;
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(/(?:(?:[!=(]|</var>|[|,|n|:)[ t]*/$)|^ n?[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>/";
我=intNextQuote;
最后状态=“”;
}
最后状态=“”;
休息;
默认:
if(c.match(/w/))word+=c; //一句话
if(c.match(/W/)){
如果(字!='')
str+=word.replace(this.regKW,this.clKW).replace(this.regObj,this.clObj).replace(this.regMP,this.clMP) + htmlEncodef(c)
否则 str+=htmlEncodef(c);
字='';
}
最后状态=“”;
休息;
}
}
return str.replace(/(s*r?n)+$/,'');
}
};
函数 str2styled(x,语言){
var _A = 词法分析器[语言].lex(html2txt(x)).split(/n/);
for(i=0;i<_A.length;i++){_A[i] = '<li><span>'+_A[i].replace(/r/g,'').replace(/ t/g,' ').replace(/ /g,' ')+'</span></li>';}
return '<ol>'+_A.join('')+'</ol>';
}
var $ = function(e){return document.getElementById(e)};
窗口.onload = 函数(){
var pres = document.getElementsByTagName('pre');
for(k=0;k<pres.length;k++)if(pres[k].className.match(/bprehb/))
pres[k].innerHTML = str2styled(pres[k].innerHTML,'js')
}
</脚本>
</头>
<正文>
<pre id="测试" class="preh js">
var 点 = 函数(){
这个.x = 1;
这个.y = 2;
this.valueOf = function(){return '点:('+x+','+y+')'};
}
如果(c<2)f()
var P = 新点();
警报(P.valueOf());
var rex_factory = function(){return {x:/re/g,y:/rere/g,z:[/re/,/rere/,/rerere/]}};
</pre>
<pre id="test2" class="preh js">
another = '这是另一个例子'
</前>
</正文>
</html>