支持IE和firefox的js代码美化加亮源码

自己在IE和FF试过了js代码加亮效果,用了一段别人的压缩后的代码作为例子,受限制,删了一部分。 发现错误的大虾一定要留下测试用的代码啊!

我们 www.jb51.net

#oTxt{
color:#333333;
background-color:#f0f0f0;
font-family:Courier New;
font-size:12px;
margin:0;
line-height:13px;
border-left:none;
overflow:scroll;
}
#oResult{
color:#333333;
background-color:#f0f0f0;
font-family: Courier New;
font-size:12px;
margin:0;
line-height:13px;
border-left:none;
overflow:scroll;
height:164px;
}

//=0;i--){if(this[i]==o)return i;}return-1;};Array.prototype.contains=function(o){return this.indexOf(o)!= -1;};Array.prototype.copy=function(o){return this.concat();};Array.prototype.insertAt=function(o,i){this.splice(i,0,o);};Array.prototype.insertBefore=function(o,o2){var i=this.indexOf(o2);if(i== -1)this.push(o);else this.splice(i,0,o);};Array.prototype.removeAt=function(i){this.splice(i,1);};Array.prototype.remove=function(o){var i=this.indexOf(o);if(i!= -1)this.splice(i,1);};String.prototype.trim=function(){return this.replace(/(^\s+)|\s+$/g,"");};Function.READ=1;Function.WRITE=2;Function.READ_WRITE=3;Function.prototype.addProperty=function(sName,nReadWrite){nReadWrite=nReadWrite||Function.READ_WRITE;var capitalized=sName.charAt(0).toUpperCase()+sName.substr(1);if(nReadWrite&Function.READ)
this.prototype["get"+capitalized]=new Function("","return this._"+sName+";");if(nReadWrite&Function.WRITE) this.prototype["set"+capitalized]=new Function(sName,"this._"+sName+" = "+sName+";");};function BiObject(){this._hashCode=BiObject._hashCodePrefix+Math.round(Math.random()*1000)+BiObject._hashCodePrefix+BiObject._hashCodeCounter++;}
_p=BiObject.prototype=new Object;_p._className="BiObject";_p._disposed=false;_p._id=null;BiObject._hashCodeCounter=1;BiObject._hashCodePrefix="hc";BiObject.toHashCode=function(o){if(o._hashCode!=null)
return o._hashCode;return o._hashCode=BiObject._hashCodePrefix+Math.round(Math.random()*1000)+BiObject._hashCodePrefix+BiObject._hashCodeCounter++;};BiObject.prototype.getDisposed=function(){return this._disposed;};BiObject.prototype.getId=function(){return this._id;};BiObject.prototype.setId=function(v){this._id=v;};BiObject.prototype.getUserData=function(){return this._userData;};BiObject.prototype.setUserData=function(v){this._userData=v;};_p.toHashCode=function(){return BiObject.toHashCode(this);};_p.dispose=function(){this._disposed=true;delete this._userData;};_p.toString=function(){if(this._className)
return"[object "+this._className+"]";return"[object Object]";};_p.getProperty=function(sPropertyName){var getterName="get"+sPropertyName.charAt(0).toUpperCase()+sPropertyName.substr(1);if(typeof this[getterName]=="function")
return this[getterName]();throw new Error("No such property, "+sPropertyName);};_p.setProperty=function(sPropertyName,oValue){var setterName="set"+sPropertyName.charAt(0).toUpperCase()+sPropertyName.substr(1);if(typeof this[setterName]=="function")
this[setterName](oValue);else throw new Error("No such property, "+sPropertyName);};_p.setAttribute=function(sName,sValue,oParser)
{var v;if(sValue=="true")
v=true;else if(sValue=="false")
v=false;else if(parseFloat(sValue)==sValue)
v=parseFloat(sValue);else v=sValue;this.setProperty(sName,v);};_p.getAttribute=function(sName)
{return String(this.getProperty(sName));};_p.addXmlNode=function(oNode,oParser)
{if(oNode.nodeType==1)
oParser.fromNode(oNode);};if(typeof BiObject=="undefined")
BiObject=new Function;function BiBrowserCheck(){if(BiBrowserCheck._singleton)
return BiBrowserCheck._singleton;var ua=navigator.userAgent;this._ie= /msie/i.test(ua);this._moz=navigator.product=="Gecko";this._platform=navigator.platform;if(this._moz)
{/rv\:([^\);]+)(\)|;)/.test(ua);this._version=RegExp.$1;this._ie55=false;this._ie6=false;}
else {/MSIE\s+([^\);]+)(\)|;)/.test(ua);this._version=RegExp.$1;this._ie55= /msie 5\.5/i.test(ua);this._ie6= /msie 6/i.test(ua);}
BiBrowserCheck._singleton=this;}
BiBrowserCheck.prototype=new BiObject;BiBrowserCheck.prototype.getIe=function()
{return this._ie;};BiBrowserCheck.prototype.getIe55=function()
{return this._ie55;};BiBrowserCheck.prototype.getIe6=function()
{return this._ie6;};BiBrowserCheck.prototype.getMoz=function()
{return this._moz;};BiBrowserCheck.prototype.getVersion=function()
{return this._version;};BiBrowserCheck.prototype.getPlatform=function()
{return this._platform;};var _br=new BiBrowserCheck;BiBrowserCheck.ie=_br.getIe();BiBrowserCheck.ie55=_br.getIe55();BiBrowserCheck.ie6=_br.getIe6();BiBrowserCheck.moz=_br.getMoz();BiBrowserCheck.version=_br.getVersion();BiBrowserCheck.platform=_br.getPlatform();_br=null;function BiUri(sBase,sRel)
{this._params={};if(sBase)
{this.setHref(sBase);if(sRel)
this._setRelative(sRel)}}
_p=BiUri.prototype=new BiObject;_p._className="BiUri";_p._scheme="";_p._userInfo="";_p._port="";_p._host="";_p._path="";_p._dirPath="";_p._fragment="";_p._query="";_p._hrefCache=null;BiUri.prototype.getScheme=function(){return this._scheme;};BiUri.prototype.getPath=function(){return this._path;};BiUri.prototype.getDirPath=function(){return this._dirPath;};BiUri.prototype.getHost=function(){return this._host;};BiUri.prototype.getPort=function(){return this._port;};BiUri.prototype.getFragment=function(){return this._fragment;};BiUri.prototype.getQuery=function(){return this._query;};BiUri.prototype.getUserInfo=function(){return this._userInfo;};BiUri.regExps={scheme:/^([^:]+)\:.+$/,user:/^([^@\/]+)@.+$/,host:/^([^:\/\?\#]+).*$/,port:/^:(\d+)/,path:/^([^\?#]*)/,dirPath:/^(.*\/)[^\/]*$/,fragment:/^[^#]*#(.*)$/,absUri:/^\w(\w|\d|\+|\-|\.)*:/i};_p.toString=function()
{return this.getHref();};_p.setHref=function(s)
{this._hrefCache=null;s=String(s);this._scheme="";this._userInfo="";this._host="";this._port=null;this._path="";this._dirPath="";this._query="";this._fragment="";this._params={};var err=new Error("Not a well formatted URI");var ok=BiUri.regExps.scheme.test(s);if(!ok)throw err;this._scheme=RegExp.$1;if(this._scheme=="mailto"||this._scheme=="news"||this._scheme=="view-source")
s=s.substring(this._scheme.length+1);else s=s.substring(this._scheme.length+3);ok=BiUri.regExps.user.test(s);if(ok)
{this._userInfo=RegExp.$1;s=s.substring(this._userInfo.length+1);}
if(this._scheme!="file"||s.charAt(0)!="/")
{ok=BiUri.regExps.host.test(s);if(!ok)
throw err;this._host=RegExp.$1;s=s.substring(this._host.length);}
ok=BiUri.regExps.port.test(s);if(ok)
{this._port=Number(RegExp.$1);s=s.substring(RegExp.$1.length+1);}
this._parsePathAndRest(s);};_p._parsePathAndRest=function(s)
{var err=new Error("Not a well formatted URI");var ok=BiUri.regExps.path.test(s);if(!ok)
throw err;this._path=RegExp.$1;s=s.substring(this._path.length);if(this._path==""&&(this._scheme=="file"||this._scheme=="http"||this._scheme=="https"||this._scheme=="ftp"))
{this._path="/";}
var segments=this._path.split("/");var sb=[];var j=0;for(var i=0;i0)
{ok=BiUri.regExps.dirPath.test(this._path);if(!ok)
throw err;this._dirPath=RegExp.$1;}
ok=BiUri.regExps.fragment.test(s);if(ok)
{this._fragment=RegExp.$1;s=s.substring(0,s.length-this._fragment.length-1);this._fragment="#"+this._fragment.replace("#","%23");}
this._query=s;s=s.substring(1);if(this._query!="")
{var pairs=s.split(/\;|\&/);var parts;for(var i=0;i0?"?"+sb.join("&"):"");};function BiEvent(sType)
{BiObject.call(this);this._type=sType;}
_p=BiEvent.prototype=new BiObject;_p._className="BiEvent";_p._bubbles=false;_p._propagationStopped=true;_p._defaultPrevented=false;BiEvent.prototype.getType=function(){return this._type;};BiEvent.prototype.getTarget=function(){return this._target;};BiEvent.prototype.getCurrentTarget=function(){return this._currentTarget;};BiEvent.prototype.getBubbles=function(){return this._bubbles;};_p.initDispatch=function(){};_p.stopPropagation=function()
{this._propagationStopped=true;};_p.preventDefault=function()
{this._defaultPrevented=true;};_p.dispose=function()
{this._target=null;this._currentTarget=null;};_p.getDefaultPrevented=function()
{return this._defaultPrevented;};function BiMouseEvent(){};function BiKeyboardEvent(){};application=new BiApplication;application._coreScriptFiles_ie=["js/coreclasses.js","js/xmlrpc.js","js/guicomponents.js","js/gauge.ie.js","js/menu.ie.js","js/window.js","js/grids.js","js/databinding.js","js/chartingbase.js","js/charting.js","js/charting2.js","js/datetime.js"];application._coreScriptFiles_moz=["js/coreclasses.js","js/xmlrpc.js","js/guicomponents.js","js/menu.moz.js","js/window.js","js/grids.js","js/databinding.js","js/chartingbase.js","js/charting2.js","js/datetime.js"];if(BiBrowserCheck.ie)
application._coreScriptFiles=application._coreScriptFiles_ie;else application._coreScriptFiles=application._coreScriptFiles_moz;

//-->

"+document.getElementById("oTxt").value.formatJS()+"");', 1000);
}
//String.prototype.div = document.createElement('div');
String.prototype.toHtml = function () {
return this
.replace(/\&/g, '&')
.replace(/\/g, '>')
.replace(/\x20/g, ' ')
.replace(/(\r\n|\r|\n)/g, '
');
};
String.prototype.formatJS = function ()
{
var s = this;
var index = 0, brch=0;
var keywords = ['abstract','boolean','break','byte','case','catch','char','class','const','continue','default','delete','do','double','else','extends','false','final','finally','float','for','function','goto','if','implements','import','in','instanceof','int','interface','long','native','new','null','package','private','protected','public','return','short','static','super','switch','synchronized','this','throw','throws','transient','true','try','typeof','var','void','while','with'];
var objects = ['Anchor','anchors','Applet','applets','Area','Array','arguments','Button','Checkbox','Date','document','FileUpload','Form','forms','Frame','frames','Function','Hidden','history','Image','images','Link','links','Area','location','Math','MimeType','mimeTypes','navigator','options','Password','Plugin','plugins','Radio','Reset','RegExp','Select','String','Submit','Text','Textarea','window'];
var specials = ['prototype','callee','caller','apply','call','encodeURIComponent','decodeURIComponent','parseInt','parseFloat','__defineGetter__','__defineSetter__'];
var blocks = [];
var indent = "", indent_fix="";
s=s
.replace(/\t/g, ' ')
.replace(/\r\n|\r/g, "\n")
.replace(/(\/\/[^\n]*(\n|$)|\/\*(\*(?!\/)|[^\*])*\*+\/|\"(\\\"|\\\\|[^\"\n])*\"|\'(\\\'|\\\\|[^\'\n])*\'|([=!\(\:\{\[\;]\s*)\/(\\.|[^\/\n])+\/[igm]*)/g,
function ($0)
{
var color = "#CCCCCC", ret = "'"; // comment
var kkk = $0;
if(kkk.charAt(0) == '"')
color = "#FF00FF"; // double quote
else if(kkk.charAt(0) == "'")
color = "#9900FF"; // single quote
else if(kkk.match(/^(?:[=!\(\:\{\[]\s*)(\/(\\.|[^\/\n])+\/[igm]*$)/))
{
ret = kkk.replace(/^([=!\(\:\{\[]\s*)(\/(\\.|[^\/\n])+\/[igm]*$)/, "$1'");
kkk = kkk.replace(/^([=!\(\:\{\[]\s*)(\/(\\.|[^\/\n])+\/[igm]*$)/, "$2");
color = "#6666CC"; // regexp
}
blocks[blocks.length] = ""+kkk.toHtml()+"";
return ret;
})
// alert(blocks);s=s
.replace(/(\}?[\n ]*;[\n ]*)/g, function ($0,$1){return $1.replace(/[\n ]/g, '')+"\n";})
.replace(/\b(else)\b/g, "$1\n")
.replace(/\b((case|default)[^:]*\:)/g, "$1\n")
.replace(/(\{[\n ]*)((([A-Za-z\$\_][0-9A-Za-z\$\_]*|`)[\n ]*\:[\n ]*[^,\}]+,?)+)([\n ]*\})/g, function ($0,$1,$2,$3,$4,$5)
{
return $1+$2.replace(/[\n ]+/g, ' ').replace(/(([A-Za-z\$\_][0-9A-Za-z\$\_]*|`)\:[^,\}]+,?)/g, "$1\n")+$5;
})
.replace(/[\n ]*(\{|\};?)[\n ]*/g, function ($0, $1)
{
if($1.charAt(0) == '}') brch--;
var ss = "\n";
for(var i=0;i/g, '>')
.replace(/\x20/g, ' ')
.replace(eval('/\\b('+keywords.join('|')+')\\b/g'),'$1') // keywords
.replace(eval('/\\b('+objects.join('|')+')\\b/g'),'$1') // objects
.replace(eval('/\\b('+specials.join('|')+')\\b/g'),'$1') // objects
.replace(/\n/g, '
')
.replace(/\'/g, function ($0){return blocks[index++];});
return s;
}
tm_highlight = setTimeout('document.getElementById("oResult").innerHTML = (""+document.getElementById("oTxt").value.formatJS()+"");', 1);
//-->


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

(0)

相关推荐

  • 支持IE和firefox的js代码美化加亮源码

    自己在IE和FF试过了js代码加亮效果,用了一段别人的压缩后的代码作为例子,受限制,删了一部分. 发现错误的大虾一定要留下测试用的代码啊! 我们 www.jb51.net #oTxt{ color:#333333; background-color:#f0f0f0; font-family:Courier New; font-size:12px; margin:0; line-height:13px; border-left:none; overflow:scroll; } #oResult{

  • HTML+JS实现“代码雨”效果源码(黑客帝国文字下落效果)

    我们先看看HTML+JS实现"代码雨"的最终效果 1.绿色: 2.彩色: 3.背景色: 4.绿色逐渐变浅: 源代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Code -by ZhenYu.Sha</title>

  • 非常实用的js验证框架实现源码 附原理方法

    本文为大家分享一个很实用的js验证框架实现源码,供大家参考,具体内容如下 关键方法和原理: function check(thisInput) 方法中的 if (!eval(scriptCode)) { return false; } 调用示例: 复制代码 代码如下: <input type="text" class="text_field percentCheck" name="progress_payment_two" id="

  • solid.js响应式createSignal 源码解析

    目录 正文 createSignal readSignal writeSignal 案例分析 总结 正文 www.solidjs.com/docs/latest… createSignal 用来创建响应式数据,它可以跟踪单个值的变化. solid.js 的响应式实现参考了 S.js,它是一个体积超小的 reactive 库,支持自动收集依赖和简单的响应式编程. createSignal createSignal 首先我们来看下 createSignal 的声明: // packages/soli

  • jQuery实现百叶窗焦点图动画效果代码分享(附源码下载)

    这是一款基于jQuery的百叶窗焦点图动画,和之前介绍的CSS3百叶窗焦点图动画不同的是,它的兼容性更好,实用性更强,因为它是基于纯jQuery的,基本上所有浏览器都能够支持.焦点图的图片切换动画是百叶窗的动画方式,但也有几种不同的百叶窗动画,因此也不会觉得单调. 在线演示     源码下载 HTML代码 <div id="slider"> <img src="images/1.jpg" alt="我们1" title=&quo

  • 分享JS表单验证源码(带错误提示及密码等级)

    先晒图 index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index</title> <link rel="stylesheet" href="css/style.css"> </head> <body> &l

  • JS前端操作 Cookie源码示例解析

    目录 引言 源码分析 使用 源码 分析 set get remove withAttributes & withConverter 总结 引言 前端操作Cookie的场景其实并不多见,Cookie也因为各种问题被逐渐淘汰,但是我们不用Cookie也可以学习一下它的思想,或者通过这次的源码来学习其他的一些知识. 今天带来的是:js-cookie 源码分析 使用 根据README,我们可以看到js-cookie的使用方式: // 设置 Cookies.set('name', 'value'); //

  • 11行JS代码制作二维码生成功能

    HTML代码: <img style="display: none" id="qrcode" data-width="100" data-height="100" data-url="https://www.baidu.com/"> 相关JS代码: /** * 生成二维码 * data-width={宽度} * data-height={高度} * data-url={链接} * @param

  • JS模拟简易滚动条效果代码(附demo源码)

    本文实例讲述了JS模拟简易滚动条效果的方法.分享给大家供大家参考,具体如下: 使用Js模拟滚动条.简易模式,类似手机上常见的滚动条. 效果如下: Js代码如下: var scrollMoveObj = null, scrollPageY = 0, scrollY = 0; var scrollDivList = new Array(); // obj需要添加滚动条的对象 w滚动条宽度 className滚动条样式名称 // obj元素 必须指定高度,并设置overflow:hidden; //

  • php+webSoket实现聊天室示例代码(附源码)

    最近在公司利用直播间搭建一个图文直播间时正好要用到chatsever,研究了一下html5的websocket 实现了双向通信,根据前人的经验折腾了几天弄了个聊天室,实现了发送图片,发送QQ表情,群聊私聊等功能,特地分享给各位新手参考学习,大牛可以忽略. 前端:client.html <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewp

随机推荐