大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 网页设计 -> HTML/CSS -> 标记语言处理模型(演示版)

标记语言处理模型(演示版)

时间: 2021-07-31 作者:daque

这是一个抄袭html,xml谈话从文本变化为东西这一进程的模子,并贬低了少许如tagname不许窜改,掩盖标志只能是“<”、“>”等的控制,尽大概的夸大对文本的自在处置。经过这个模子也就不妨创造出如 html 和 ubb 的双向变换步调,internet explorer中表露xml文书档案一律有hightlight和折叠功效的视图等一系列的对于标志谈话的范例步调。标志谈话处置模子<textarea id="code" rows=15 cols=100>[b color=#ff0000]aaa<b>bbb</b><b>ccc</b>[u]eeee[/b]<b>ddd</b></textarea><br /><h3>属性</h3><button onclick="alert(firstchildren.tagname)">tagname</button><button onclick="alert(firstchildren.childnodes)">childnodes</button><button onclick="alert(firstchildren.attributes)">attributes</button><hr /><h3>本领</h3><button onclick="alert(firstchildren.getouterhtml())">getouterhtml</button><button onclick="alert(firstchildren.getinnerhtml())">getinnerhtml</button><button onclick="alert(firstchildren.getattributenode('color').value)">getattributenode</button><button onclick="alert(firstchildren.getchildren())">getchildren</button><button onclick="alert(firstchildren.all())">all</button><hr /><h3>范例</h3><script>function ubb2html(){var ubbdocument= markuplanguagedocument(code.value, "[", "]")var allobject= ubbdocument.all()for (var i=0; i<allobject.length; i++){if (allobject[i].tagname == "b"){allobject[i].lt= "<"allobject[i].gt= ">"}}alert(ubbdocument.getouterhtml())}</script><button onclick="ubb2html()">ubb2html</button><xmp style="background-color: #eeeeee; padding: 10px;">function ubb2html(){var ubbdocument= markuplanguagedocument(code.value, "[", "]")var allobject= ubbdocument.all()for (var i=0; i<allobject.length; i++){if (allobject[i].tagname == "b"){allobject[i].lt= "<"allobject[i].gt= ">"}}alert(ubbdocument.getouterhtml())}</xmp><script>string.prototype.getabsreg= function(s){var absstr= this.match(/(\\______rela:-?\d+______|\\.[^\\]*|[^\\]*)/g)while (absstr.length-1){if (/^\\______rela:-?\d+______$/.test(absstr[1]))absstr[1]= "\\" + (parseint(absstr[1].match(/-?\d+/g)) + absstr[0].match(/\(/g).length)absstr[0]= absstr.shift() + absstr[0]}return new regexp(absstr.join(""), s)}regexp.prototype.getrelastr= function(){var regstr= this.sourcevar relastr= regstr.match(/(\\\d+|\\.[^\\]*|[^\\]*)/g)while (relastr.length-1){if (/^\\\d+$/.test(relastr[1]))relastr[1]= "\\______rela:" + (parseint(relastr[1].match(/\d+/g)) - relastr[0].match(/\(/g).length) + "______"relastr[0]= relastr.shift() + relastr[0]}return relastr.join("")}</script><script>function markuplanguagedocument(html, lt, gt){function htmlelement(tagname){this.tagname= tagnamethis.childnodes= new array()this.attributes= new array()this.setinnerhtml= function(html, lt, gt){this.childnodes= parsehtml(html, lt, gt)returnthis.childnodes}this.getinnerhtml= function (){var childrenhtml= ""for (var i=0; i<this.childnodes.length; i++){childrenhtml += (this.constructor == this.childnodes[i].constructor) ? this.childnodes[i].getouterhtml() : this.childnodes[i]}return childrenhtml}this.getouterhtml= function(){var tagname= this.tagnamevar childrenhtml= this.getinnerhtml()var attrhtml= ""for (var i=0; i<this.attributes.length; i++){attrhtml+= (this.attributes[i].name + (this.attributes[i].value ? "=\"" + this.attributes[i].value + "\" " : " "))}return tagname ? this.lt + tagname + (attrhtml ? " "+attrhtml : "") + (childrenhtml ? this.gt + childrenhtml + this.lt + "/" + tagname + this.gt : "/" + this.gt) : childrenhtml}this.getattributenode= function(name){for (var i=0; i<this.attributes.length; i++){if (this.attributes[i].name == name)return this.attributes[i]}return null}this.getchildren= function(){var childrenarray= new array()for (var i=0; i<this.childnodes.length; i++){if (this.constructor == this.childnodes[i].constructor)childrenarray[childrenarray.length] = this.childnodes[i]}return childrenarray}this.all= function(){var allarray= new array()var children= this.getchildren()for (var i=0; i<children.length; i++){allarray[allarray.length]= children[i]if (children[i].getchildren()){allarray = allarray.concat(children[i].all())}}return allarray}}function attributeobj(name, value){this.name= namethis.value= value}function parsehtml(html, lt, gt){var lt= lt ? lt.charat(0) : "<"var gt= gt ? gt.charat(0) : ">"var lt1= lt.replace(/([\[\]\.])/, "\\$1")var gt1= gt.replace(/([\[\]\.])/, "\\$1")//字符串正则:如 "abc"var strre= /(["'])(\\["'tbnr]|[^\1])*?\1///属性正则:如 <span attribute="abc"> 中的 attribute="abc"var attrre= ("(\\w+)(\\s*=\\s*(" + strre.getrelastr() + "|[^\\s"+gt1+"]+)|.{0})").getabsreg()//标出面正则:如 <span attribute="abc"> 中的 spanvar tagre= /((\w+:)?\w+)///标识单target正则:如 <span /> 中的 /var stagre= new regexp("\\\/\\s*(?=" + gt1 + ")")//标识target封闭正则:如 </span> 中的 /var ctagre= /\/\s*///东西正则:如 <span attribute="abc" />var objre= (lt1 + "(" + ctagre.getrelastr() + ")?" + tagre.getrelastr() + "(" + attrre.getrelastr() + "|\\s)*?(" + stagre.getrelastr() + ")?" + gt1).getabsreg()//节点正则:如 this is a <img src="http://www.okasp.com/techinfo/simple.jpg" /> 中的 this is a 和 <img src="http://www.okasp.com/techinfo/simple.jpg" />var nodere= ("(" + objre.getrelastr() + "|[^" + lt1 + "]*)").getabsreg()var htmlarray= new array()var node= ""var tmpobj= nullfunction getfisrtnode(html){return new function(){this.nodevalue= nodere.test(html)? html.match(nodere)[0] : ""this.otherhtml= html.substr(this.nodevalue.length, html.length)this.tagname= tagre.test(this.nodevalue)? this.nodevalue.match(tagre)[0] : undefinedvar tmpattrstr= this.nodevalue.replace(tagre, "")var tmparrtre= new regexp(attrre.source, "g")this.attrs= tmparrtre.test(tmpattrstr)? tmpattrstr.match(tmparrtre) : new array()}}while(html){tmpnode= getfisrtnode(html)node= tmpnode.nodevaluehtml= tmpnode.otherhtmlif (objre.test(node)){var tagname= tmpnode.tagnamevar childrenhtml= ""var j= stagre.test(node) ? 0 : 1var attrsobj= new array()node= new htmlelement(tagname)for (var i=0; i<tmpnode.attrs.length; i++){var tmpar= tmpnode.attrs[i].match(attrre)attrsobj[i]= new attributeobj(tmpar[1], tmpar[3].replace(/^(["'])([.\s\s]*?)\1$/, "$2"))}node.attributes= attrsobjnode.lt= ltnode.gt= gtwhile (j && html){var tmpnode= getfisrtnode(html)html= tmpnode.otherhtmlif (tmpnode.tagname == tagname && !(stagre.test(tmpnode.nodevalue))){("^" + lt1 + ctagre.getrelastr()).getabsreg().test(tmpnode.nodevalue) ? j-- : j++}if (j)childrenhtml+=tmpnode.nodevalue}node.childnodes= parsehtml(childrenhtml, lt, gt)}htmlarray[htmlarray.length]= node}return htmlarray}var mldocument= new htmlelement()mldocument.childnodes= parsehtml(html, lt, gt)return mldocument}var parsedocument= markuplanguagedocument(code.value, "[", "]")var firstchildren= parsedocument.childnodes[1]</script> [ctrl+a 十足采用 提醒:你可先窜改局部代码,再按运转]这个步调的处置本领大概是经过正则表白式的拼接(getabsreg 和 getrelastr 这两个自设置因变量),从 字符串 → 属性 → 东西 → 节点 头等级的拼接出她们的正则表白式,而后再从节点发端反向的从文本流中索取对应的实质,再将她们树立到 htmlelement 类 的相映的属性上,也即是看到了 [object object]。结果范例步调经过树立和运用 htmlelement 中供给属性和本领,将文本的每一个详细窜改到最后所须要的状况,结果运用getoutethtml或是getinnerhtml本领获得处置之后的文本流。其余,用这种本领处置还不妨到达标志机动补全的功效。 (根源:蓝色理念)

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.