You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
3.5 KiB
1 line
3.5 KiB
1 year ago
|
function HTMLParser(e,t){function r(e,r,n,s){if(r=r.toLowerCase(),block[r])for(;l.last()&&inline[l.last()];)a("",l.last());if(closeSelf[r]&&l.last()==r&&a("",r),s=empty[r]||!!s,s||l.push(r),t.start){var i=[];n.replace(attr,function(e,t){var r=arguments[2]?arguments[2]:arguments[3]?arguments[3]:arguments[4]?arguments[4]:fillAttrs[t]?t:"";i.push({name:t,value:r,escaped:r.replace(/(^|[^\\])"/g,'$1\\"')})}),t.start&&t.start(r,i,s)}}function a(e,r){if(r)for(var a=l.length-1;a>=0&&l[a]!=r;a--);else var a=0;if(a>=0){for(var n=l.length-1;n>=a;n--)t.end&&t.end(l[n]);l.length=a}}var n,s,i,l=[],c=e;for(l.last=function(){return this[this.length-1]};e;){if(s=!0,l.last()&&special[l.last()])e=e.replace(new RegExp("([\\s\\S]*?)</"+l.last()+"[^>]*>"),function(e,r){return r=r.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g,"$1$2"),t.chars&&t.chars(r),""}),a("",l.last());else if(0==e.indexOf("\x3c!--")?(n=e.indexOf("--\x3e"))>=0&&(t.comment&&t.comment(e.substring(4,n)),e=e.substring(n+3),s=!1):0==e.indexOf("</")?(i=e.match(endTag))&&(e=e.substring(i[0].length),i[0].replace(endTag,a),s=!1):0==e.indexOf("<")&&(i=e.match(startTag))&&(e=e.substring(i[0].length),i[0].replace(startTag,r),s=!1),s){n=e.indexOf("<");var o=n<0?e:e.substring(0,n);e=n<0?"":e.substring(n),t.chars&&t.chars(o)}if(e==c)throw"Parse Error: "+e;c=e}a()}function makeMap(e){for(var t={},r=e.split(","),a=0;a<r.length;a++)t[r[a]]=!0;return t}function removeDOCTYPE(e){return e.replace(/<\?xml.*\?>\n/,"").replace(/<!doctype.*>\n/,"").replace(/<!DOCTYPE.*>\n/,"")}function parseAttrs(e){return e.reduce(function(e,t){var r=t.value,a=t.name;return e[a]?e[a]=e[a]+" "+r:e[a]=r,e},{})}function parseHtml(e){e=removeDOCTYPE(e);var t=[],r={node:"root",children:[]};return HTMLParser(e,{start:function(e,a,n){var s={name:e};if(0!==a.length&&(s.attrs=parseAttrs(a)),n){var i=t[0]||r;i.children||(i.children=[]),i.children.push(s)}else t.unshift(s)},end:function(e){var a=t.shift();if(a.name!==e&&console.error("invalid state: mismatch end tag"),0===t.length)r.children.push(a);else{var n=t[0];n.children||(n.children=[]),n.children.push(a)}},chars:function(e){var a={type:"text",text:e};if(0===t.length)r.children.push(a);else{var n=t[0];n.children||(n.children=[]),n.children.push(a)}},comment:function(e){var r={node:"comment",text:e},a=t[0];a.children||(a.children=[]),a.children.push(r)}}),r.children}Object.defineProperty(exports,"__esModule",{value:!0});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,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,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,code,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("script,style");exports.default=parseHtml;
|