forked from DmitryBaranovskiy/rappar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elemental.js
10 lines (10 loc) · 4.77 KB
/
elemental.js
1
2
3
4
5
6
7
8
9
10
/*
* Elemental 0.2.1 - Simple JavaScript Tag Parser
*
* Copyright (c) 2010 Dmitry Baranovskiy (http://dmitry.baranovskiy.com/)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
if (typeof require == "function") {
var eve = require("./eve.js");
}
(function(){function j(b){var f=function(a){f.parse(a)};f.mode="text",f.type=String(b||"html").toLowerCase(),f.textchunk="",f.raw="",f.parse=a,f.on=c,f.event=d,f.end=e;return f}function i(a,b,c){a=="\n"&&this.event("newline"),h[this.mode].call(this,a,b,c)}function e(){i.call(this,"eof"),this.event("eof")}function d(a,b,c){typeof eve=="function"&&eve("elemental."+a+"."+b,null,b,c||"",this.raw);var d=this.events&&this.events[a],e=d&&d.length;while(e--)try{this.events[a][e](b,c||"",this.raw)}catch(f){}this.raw=""}function c(a,b){this.events=this.events||{},this.events[a]=this.events[a]||[],this.events[a].push(b)}function b(a,b){return a&&(a.charAt?a.charAt(b):a[b])}function a(a){a=a||Object(a);var c=1,d=a.length+1,e,f,g=b(a,0);for(;c<d;c++)e=f,f=g,g=b(a,c),this.raw+=f,i.call(this,f,g,e);this._beforeEnd=function(){i.call(this,"","",f)}}var f=/[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]/,g=function(){for(var a in this.attr)this.attr.hasOwnProperty(a)&&this.event("attr",a,{value:this.attr[a],tagname:this.tagname,attr:this.attr})},h={text:function(a,b,c){switch(a){case"<":case"eof":this.nodename="",this.attr={},this.mode="tag name start",this.raw=this.raw.slice(0,-1),this.textchunk&&this.event("text",this.textchunk),this.raw+=a,this.textchunk="";break;default:this.textchunk+=a}},special:function(a,b,c){if(c=="!"&&a=="-"&&b=="-")this.mode="comment start";else{if(this.textchunk=="[CDATA"&&a=="["){this.mode="cdata",this.textchunk="";return}if(a==">"||a=="eof"){this.event("special",this.textchunk),this.mode="text",this.textchunk="";return}this.textchunk+=a}},cdata:function(a,b,c){c=="]"&&a=="]"&&b==">"?(this.mode="cdata end",this.textchunk=this.textchunk.slice(0,-1)):(a=="eof"&&h["cdata end"].call(this),this.textchunk+=a)},"cdata end":function(a,b,c){this.event("cdata",this.textchunk),this.textchunk="",this.mode="text"},"comment start":function(a,b,c){b==">"||a=="eof"?(this.event("comment",""),this.mode="comment instant end"):this.mode="comment"},"comment instant end":function(a,b,c){this.mode="text"},comment:function(a,b,c){a=="-"&&c=="-"&&b==">"?(this.mode="comment end",this.textchunk=this.textchunk.slice(0,-1)):a=="eof"?this.event("comment",this.textchunk):this.textchunk+=a},"comment end":function(a,b,c){this.event("comment",this.textchunk),this.textchunk="",this.mode="text"},declaration:function(a,b,c){a=="?"&&b==">"?this.mode="declaration end":(a=="eof"&&this.event("comment",this.textchunk),this.textchunk+=a)},"declaration end":function(a,b,c){this.event("comment",this.textchunk),this.textchunk="",this.mode="text"},"tag name start":function(a,b,c){if(a=="eof")this.event("text","<");else if(!f.test(a)){this.mode="tag name";if(a=="/"){this.mode="close tag name start";return}if(a=="!"){this.mode="special",this.textchunk="";return}if(a=="?"){this.mode="declaration";return}h[this.mode].call(this,a,b,c)}},"close tag name start":function(a,b,c){f.test(a)||(this.mode="close tag name",this.tagname="",this.nodename="",h[this.mode].call(this,a,b,c))},"close tag name":function(a,b,c){if(f.test(a))this.tagname=this.nodename;else switch(a){case">":this.event("/tag",this.tagname||this.nodename),this.mode="text";break;default:!this.tagname&&(this.nodename+=a)}},"tag name":function(a,b,c){if(f.test(a))this.tagname=this.nodename,this.nodename="",this.mode="attr start";else switch(a){case">":this.event("tag",this.nodename),this.mode="text";break;default:this.nodename+=a}},"attr start":function(a,b,c){f.test(a)||(this.mode="attr",this.nodename="",h[this.mode].call(this,a,b,c))},attr:function(a,b,c){if(f.test(a)||a=="=")this.attr[this.nodename]="",this.mode="attr value start";else switch(a){case">":this.nodename=="/"?(delete this.attr["/"],this.event("tag",this.tagname,this.attr),g.call(this),this.event("/tag",this.tagname,!0)):(this.nodename&&(this.attr[this.nodename]=""),this.event("tag",this.tagname,this.attr),g.call(this)),this.mode="text";break;default:this.nodename+=a}},"attr value start":function(a,b,c){if(!f.test(a)){this.mode="attr value",this.quote=!1;if(a=="'"||a=='"'){this.quote=a;return}h[this.mode].call(this,a,b,c)}},"attr value":function(a,b,c){if(f.test(a)&&!this.quote)this.mode="attr start";else if(a==">"&&!this.quote)this.event("tag",this.tagname,this.attr),this.mode="text";else switch(a){case'"':case"'":this.quote==a&&c!="\\"&&(this.mode="attr start");break;default:this.attr[this.nodename]+=a}}};j.version="0.2.1",(typeof exports=="undefined"?this:exports).elemental=j;(typeof exports=="undefined"?this:exports).eve=eve})()