UNPKG

html-dom-parser

Version:
3 lines 6.39 kB
(function(e,t){typeof exports==`object`&&typeof module<`u`?module.exports=t():typeof define==`function`&&define.amd?define([],t):(e=typeof globalThis<`u`?globalThis:e||self,e.HTMLDOMParser=t())})(this,function(){var e;(function(e){e.Root=`root`,e.Text=`text`,e.Directive=`directive`,e.Comment=`comment`,e.Script=`script`,e.Style=`style`,e.Tag=`tag`,e.CDATA=`cdata`,e.Doctype=`doctype`})(e||={});function t(t){return t.type===e.Tag||t.type===e.Script||t.type===e.Style}e.Root,e.Text,e.Directive,e.Comment,e.Script,e.Style,e.Tag,e.CDATA,e.Doctype;var n=class{parent=null;prev=null;next=null;startIndex=null;endIndex=null;get parentNode(){return this.parent}set parentNode(e){this.parent=e}get previousSibling(){return this.prev}set previousSibling(e){this.prev=e}get nextSibling(){return this.next}set nextSibling(e){this.next=e}cloneNode(e=!1){return _(this,e)}},r=class extends n{data;constructor(e){super(),this.data=e}get nodeValue(){return this.data}set nodeValue(e){this.data=e}},i=class extends r{type=e.Text;get nodeType(){return 3}},a=class extends r{type=e.Comment;get nodeType(){return 8}},o=class extends r{type=e.Directive;name;constructor(e,t){super(t),this.name=e}get nodeType(){return 1}"x-name";"x-publicId";"x-systemId"},s=class extends n{children;constructor(e){super(),this.children=e}get firstChild(){return this.children[0]??null}get lastChild(){return this.children.length>0?this.children[this.children.length-1]:null}get childNodes(){return this.children}set childNodes(e){this.children=e}},c=class extends s{type=e.CDATA;get nodeType(){return 4}},l=class extends s{type=e.Root;get nodeType(){return 9}},u=class extends s{name;attribs;type;constructor(t,n,r=[],i=t===`script`?e.Script:t===`style`?e.Style:e.Tag){super(r),this.name=t,this.attribs=n,this.type=i}get nodeType(){return 1}get tagName(){return this.name}set tagName(e){this.name=e}get attributes(){return Object.keys(this.attribs).map(e=>({name:e,value:this.attribs[e],namespace:this[`x-attribsNamespace`]?.[e],prefix:this[`x-attribsPrefix`]?.[e]}))}namespace;"x-attribsNamespace";"x-attribsPrefix"};function d(e){return t(e)}function f(t){return t.type===e.CDATA}function p(t){return t.type===e.Text}function m(t){return t.type===e.Comment}function h(t){return t.type===e.Directive}function g(t){return t.type===e.Root}function _(e,t=!1){let n;if(p(e))n=new i(e.data);else if(m(e))n=new a(e.data);else if(d(e)){let r=t?v(e.children):[],i=new u(e.name,{...e.attribs},r);for(let e of r)e.parent=i;e.namespace!=null&&(i.namespace=e.namespace),e[`x-attribsNamespace`]&&(i[`x-attribsNamespace`]={...e[`x-attribsNamespace`]}),e[`x-attribsPrefix`]&&(i[`x-attribsPrefix`]={...e[`x-attribsPrefix`]}),n=i}else if(f(e)){let r=t?v(e.children):[],i=new c(r);for(let e of r)e.parent=i;n=i}else if(g(e)){let r=t?v(e.children):[],i=new l(r);for(let e of r)e.parent=i;e[`x-mode`]&&(i[`x-mode`]=e[`x-mode`]),n=i}else if(h(e)){let t=new o(e.name,e.data);e[`x-name`]!=null&&(t[`x-name`]=e[`x-name`],t[`x-publicId`]=e[`x-publicId`],t[`x-systemId`]=e[`x-systemId`]),n=t}else throw Error(`Not implemented yet: ${e.type}`);return n.startIndex=e.startIndex,n.endIndex=e.endIndex,e.sourceCodeLocation!=null&&(n.sourceCodeLocation=e.sourceCodeLocation),n}function v(e){let t=e.map(e=>_(e,!0));for(let e=1;e<t.length;e++)t[e].prev=t[e-1],t[e-1].next=t[e];return t}let y=`animateMotion.animateTransform.clipPath.feBlend.feColorMatrix.feComponentTransfer.feComposite.feConvolveMatrix.feDiffuseLighting.feDisplacementMap.feDropShadow.feFlood.feFuncA.feFuncB.feFuncG.feFuncR.feGaussianBlur.feImage.feMerge.feMergeNode.feMorphology.feOffset.fePointLight.feSpecularLighting.feSpotLight.feTile.feTurbulence.foreignObject.linearGradient.radialGradient.textPath`.split(`.`).reduce((e,t)=>(e[t.toLowerCase()]=t,e),{}),b=RegExp(`\r`,`g`),x=`__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_${Date.now().toString()}__`,S=new RegExp(x,`g`);function C(e){return y[e]}function w(e){let t={},n=0,r=e.length;for(;n<r;n++){let r=e[n];t[r.name]=r.value}return t}function T(e){return e=e.toLowerCase(),C(e)||e}function E(e,t){let n=`<`+t,r=e.toLowerCase().indexOf(n);if(r===-1)return!1;let i=e[r+n.length];return i===`>`||i===` `||i===` `||i===` `||i===`\r`||i===`/`}function D(e){return e.replace(b,x)}function O(e){return e.replace(S,`\r`)}function k(e,t=null,n){let r=[],s,c=0,l=e.length;for(;c<l;c++){let n=e[c];switch(n.nodeType){case 1:{let e=T(n.nodeName);s=new u(e,w(n.attributes)),s.children=k(e===`template`?n.content.childNodes:n.childNodes,s);break}case 3:s=new i(O(n.nodeValue??``));break;case 8:s=new a(n.nodeValue??``);break;default:continue}let o=r[c-1]??null;o&&(o.next=s),s.parent=t,s.prev=o,s.next=null,r.push(s)}return n&&(s=new o(n.substring(0,n.indexOf(` `)).toLowerCase(),n),s.next=r[0]??null,s.parent=t,r.unshift(s),r[1]&&(r[1].prev=r[0])),r}let A=`html`,j=`head`,M=`body`,N=/<([a-zA-Z]+[0-9]?)/;function P(e,t){return t?t.createHTML(e):e}let F=(e,t,n)=>{throw Error("This browser does not support `document.implementation.createHTMLDocument`")},I=(e,t,n)=>{throw Error("This browser does not support `DOMParser.prototype.parseFromString`")},L=typeof window==`object`&&window.DOMParser;if(typeof L==`function`){let e=new L;I=(t,n,r)=>(n&&(t=`<${n}>${t}</${n}>`),e.parseFromString(t,`text/html`)),F=I}if(typeof document==`object`&&document.implementation){let e=document.implementation.createHTMLDocument();F=function(t,n,r){if(n){let i=e.documentElement.querySelector(n);return i&&(i.innerHTML=P(t,r)),e}return e.documentElement.innerHTML=P(t,r),e}}let R=typeof document==`object`&&document.createElement(`template`),z;R&&R.content&&(z=(e,t)=>(R.innerHTML=P(e,t),R.content.childNodes));let B=()=>document.createDocumentFragment().childNodes;function V(e,t){e=D(e);let n=N.exec(e)?.[1]?.toLowerCase();switch(n){case A:{let t=I(e);if(!E(e,j)){let e=t.querySelector(j);e?.parentNode?.removeChild(e)}if(!E(e,M)){let e=t.querySelector(M);e?.parentNode?.removeChild(e)}return t.querySelectorAll(A)}case j:case M:{let r=F(e,void 0,t).querySelectorAll(n);return E(e,M)&&E(e,j)?r[0].parentNode?.childNodes??B():r}default:return z?z(e,t):F(e,M,t).querySelector(M)?.childNodes??B()}}let H=/<(![a-zA-Z\s]+)>/;function U(e,t){if(typeof e!=`string`)throw TypeError(`First argument must be a string`);if(!e)return[];let n=H.exec(e),r=n?n[1]:void 0;return k(V(e,t?.trustedTypePolicy),null,r)}return U}); //# sourceMappingURL=html-dom-parser.min.js.map