var startTag=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,endTag=/^<\/([-A-Za-z0-9_]+)[^>]*>/,attr=/([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,empty=makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"),block=makeMap("a,address,code,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video"),inline=makeMap("abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var"),closeSelf=makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"),fillAttrs=makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"),special=makeMap("wxxxcode-style,script,style,view,scroll-view,block");function HTMLParser(e,t){var a,r,s,n=[],i=e;for(n.last=function(){return this[this.length-1]};e;){if(r=!0,n.last()&&special[n.last()])e=e.replace(new RegExp("([\\s\\S]*?)</"+n.last()+"[^>]*>"),(function(e,a){return a=a.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g,"$1$2"),t.chars&&t.chars(a),""})),c("",n.last());else if(0==e.indexOf("\x3c!--")?(a=e.indexOf("--\x3e"))>=0&&(t.comment&&t.comment(e.substring(4,a)),e=e.substring(a+3),r=!1):0==e.indexOf("</")?(s=e.match(endTag))&&(e=e.substring(s[0].length),s[0].replace(endTag,c),r=!1):0==e.indexOf("<")&&(s=e.match(startTag))&&(e=e.substring(s[0].length),s[0].replace(startTag,o),r=!1),r){a=e.indexOf("<");for(var l="";0===a;)l+="<",a=(e=e.substring(1)).indexOf("<");l+=a<0?e:e.substring(0,a),e=a<0?"":e.substring(a),t.chars&&t.chars(l)}if(e==i)throw"Parse Error: "+e;i=e}function o(e,a,r,s){if(a=a.toLowerCase(),block[a])for(;n.last()&&inline[n.last()];)c("",n.last());if(closeSelf[a]&&n.last()==a&&c("",a),(s=empty[a]||!!s)||n.push(a),t.start){var i=[];r.replace(attr,(function(e,t){var a=arguments[2]?arguments[2]:arguments[3]?arguments[3]:arguments[4]?arguments[4]:fillAttrs[t]?t:"";i.push({name:t,value:a,escaped:a.replace(/(^|[^\\])"/g,'$1\\"')})})),t.start&&t.start(a,i,s)}}function c(e,a){if(a){a=a.toLowerCase();for(r=n.length-1;r>=0&&n[r]!=a;r--);}else var r=0;if(r>=0){for(var s=n.length-1;s>=r;s--)t.end&&t.end(n[s]);n.length=r}}c()}function makeMap(e){for(var t={},a=e.split(","),r=0;r<a.length;r++)t[a[r]]=!0;return t}module.exports=HTMLParser;