UNPKG

html-dom-parser

Version:
3 lines (2 loc) 6.8 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).HTMLDOMParser=t()}(this,function(){"use strict";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||(e={})),e.Root,e.Text,e.Directive,e.Comment,e.Script,e.Style,e.Tag,e.CDATA,e.Doctype;class t{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 d(this,e)}}class n extends t{data;constructor(e){super(),this.data=e}get nodeValue(){return this.data}set nodeValue(e){this.data=e}}class r extends n{type=e.Text;get nodeType(){return 3}}class o extends n{type=e.Comment;get nodeType(){return 8}}class i extends n{type=e.Directive;name;constructor(e,t){super(t),this.name=e}get nodeType(){return 1}"x-name";"x-publicId";"x-systemId"}class a extends t{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}}class s extends a{type=e.CDATA;get nodeType(){return 4}}class c extends a{type=e.Root;get nodeType(){return 9}}class l extends a{name;attribs;type;constructor(t,n,r=[],o=("script"===t?e.Script:"style"===t?e.Style:e.Tag)){super(r),this.name=t,this.attribs=n,this.type=o}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 u(t){return(n=t).type===e.Tag||n.type===e.Script||n.type===e.Style;var n}function d(t,n=!1){let a;if(function(t){return t.type===e.Text}(t))a=new r(t.data);else if(function(t){return t.type===e.Comment}(t))a=new o(t.data);else if(u(t)){const e=n?p(t.children):[],r=new l(t.name,{...t.attribs},e);for(const t of e)t.parent=r;null!=t.namespace&&(r.namespace=t.namespace),t["x-attribsNamespace"]&&(r["x-attribsNamespace"]={...t["x-attribsNamespace"]}),t["x-attribsPrefix"]&&(r["x-attribsPrefix"]={...t["x-attribsPrefix"]}),a=r}else if(function(t){return t.type===e.CDATA}(t)){const e=n?p(t.children):[],r=new s(e);for(const t of e)t.parent=r;a=r}else if(function(t){return t.type===e.Root}(t)){const e=n?p(t.children):[],r=new c(e);for(const t of e)t.parent=r;t["x-mode"]&&(r["x-mode"]=t["x-mode"]),a=r}else{if(!function(t){return t.type===e.Directive}(t))throw new Error(`Not implemented yet: ${t.type}`);{const e=new i(t.name,t.data);null!=t["x-name"]&&(e["x-name"]=t["x-name"],e["x-publicId"]=t["x-publicId"],e["x-systemId"]=t["x-systemId"]),a=e}}return a.startIndex=t.startIndex,a.endIndex=t.endIndex,null!=t.sourceCodeLocation&&(a.sourceCodeLocation=t.sourceCodeLocation),a}function p(e){const t=e.map(e=>d(e,!0));for(let e=1;e<t.length;e++)t[e].prev=t[e-1],t[e-1].next=t[e];return t}var f=["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"].reduce(function(e,t){return e[t.toLowerCase()]=t,e},{}),m=new RegExp("\r","g"),h="__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_".concat(Date.now().toString(),"__"),x=new RegExp(h,"g");function v(e){for(var t={},n=0,r=e.length;n<r;n++){var o=e[n];t[o.name]=o.value}return t}function g(e){var t=function(e){return f[e]}(e=e.toLowerCase());return t||e}function y(e,t){var n="<"+t,r=e.toLowerCase().indexOf(n);if(-1===r)return!1;var o=e[r+n.length];return">"===o||" "===o||"\t"===o||"\n"===o||"\r"===o||"/"===o}function b(e){return e.replace(x,"\r")}function w(e,t,n){var a,s,c,u;void 0===t&&(t=null);for(var d,p=[],f=0,m=e.length;f<m;f++){var h=e[f];switch(h.nodeType){case 1:var x=g(h.nodeName);(d=new l(x,v(h.attributes))).children=w("template"===x?h.content.childNodes:h.childNodes,d);break;case 3:d=new r(b(null!==(a=h.nodeValue)&&void 0!==a?a:""));break;case 8:d=new o(null!==(s=h.nodeValue)&&void 0!==s?s:"");break;default:continue}var y=null!==(c=p[f-1])&&void 0!==c?c:null;y&&(y.next=d),d.parent=t,d.prev=y,d.next=null,p.push(d)}return n&&((d=new i(n.substring(0,n.indexOf(" ")).toLowerCase(),n)).next=null!==(u=p[0])&&void 0!==u?u:null,d.parent=t,p.unshift(d),p[1]&&(p[1].prev=p[0])),p}var T="html",C="head",N="body",S=/<([a-zA-Z]+[0-9]?)/,D=function(e,t){throw new Error("This browser does not support `document.implementation.createHTMLDocument`")},L=function(e,t){throw new Error("This browser does not support `DOMParser.prototype.parseFromString`")},M="object"==typeof window&&window.DOMParser;if("function"==typeof M){var A=new M;D=L=function(e,t){return t&&(e="<".concat(t,">").concat(e,"</").concat(t,">")),A.parseFromString(e,"text/html")}}if("object"==typeof document&&document.implementation){var E=document.implementation.createHTMLDocument();D=function(e,t){if(t){var n=E.documentElement.querySelector(t);return n&&(n.innerHTML=e),E}return E.documentElement.innerHTML=e,E}}var I,R="object"==typeof document&&document.createElement("template");R&&R.content&&(I=function(e){return R.innerHTML=e,R.content.childNodes});var P=function(){return document.createDocumentFragment().childNodes};function O(e){var t,n,r,o,i,a;e=function(e){return e.replace(m,h)}(e);var s=S.exec(e),c=null===(t=null==s?void 0:s[1])||void 0===t?void 0:t.toLowerCase();switch(c){case T:var l=L(e);if(!y(e,C))null===(n=null==(d=l.querySelector(C))?void 0:d.parentNode)||void 0===n||n.removeChild(d);if(!y(e,N))null===(r=null==(d=l.querySelector(N))?void 0:d.parentNode)||void 0===r||r.removeChild(d);return l.querySelectorAll(T);case C:case N:var u=D(e).querySelectorAll(c);return y(e,N)&&y(e,C)?null!==(i=null===(o=u[0].parentNode)||void 0===o?void 0:o.childNodes)&&void 0!==i?i:P():u;default:return I?I(e):null!==(a=null==(d=D(e,N).querySelector(N))?void 0:d.childNodes)&&void 0!==a?a:P();var d}}var _=/<(![a-zA-Z\s]+)>/;return function(e){if("string"!=typeof e)throw new TypeError("First argument must be a string");if(!e)return[];var t=_.exec(e),n=t?t[1]:void 0;return w(O(e),null,n)}}); //# sourceMappingURL=html-dom-parser.min.js.map