html-dom-parser
Version:
HTML to DOM parser.
3 lines (2 loc) • 6.5 kB
JavaScript
!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{constructor(){this.parent=null,this.prev=null,this.next=null,this.startIndex=null,this.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{constructor(e){super(),this.data=e}get nodeValue(){return this.data}set nodeValue(e){this.data=e}}class r extends n{constructor(){super(...arguments),this.type=e.Text}get nodeType(){return 3}}class o extends n{constructor(){super(...arguments),this.type=e.Comment}get nodeType(){return 8}}class i extends n{constructor(t,n){super(n),this.name=t,this.type=e.Directive}get nodeType(){return 1}}class s extends t{constructor(e){super(),this.children=e}get firstChild(){var e;return null!==(e=this.children[0])&&void 0!==e?e: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 a extends s{constructor(){super(...arguments),this.type=e.CDATA}get nodeType(){return 4}}class c extends s{constructor(){super(...arguments),this.type=e.Root}get nodeType(){return 9}}class u extends s{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=>{var t,n;return{name:e,value:this.attribs[e],namespace:null===(t=this["x-attribsNamespace"])||void 0===t?void 0:t[e],prefix:null===(n=this["x-attribsPrefix"])||void 0===n?void 0:n[e]}}))}}function l(t){return(n=t).type===e.Tag||n.type===e.Script||n.type===e.Style;var n}function d(t,n=!1){let s;if(function(t){return t.type===e.Text}(t))s=new r(t.data);else if(function(t){return t.type===e.Comment}(t))s=new o(t.data);else if(l(t)){const e=n?p(t.children):[],r=new u(t.name,{...t.attribs},e);e.forEach((e=>e.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"]}),s=r}else if(function(t){return t.type===e.CDATA}(t)){const e=n?p(t.children):[],r=new a(e);e.forEach((e=>e.parent=r)),s=r}else if(function(t){return t.type===e.Root}(t)){const e=n?p(t.children):[],r=new c(e);e.forEach((e=>e.parent=r)),t["x-mode"]&&(r["x-mode"]=t["x-mode"]),s=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"]),s=e}}return s.startIndex=t.startIndex,s.endIndex=t.endIndex,null!=t.sourceCodeLocation&&(s.sourceCodeLocation=t.sourceCodeLocation),s}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}),{}),h=new RegExp("\r","g"),m="__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_".concat(Date.now(),"__"),x=new RegExp(m,"g");function y(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 v(e,t,n){void 0===t&&(t=null);for(var s,a=[],c=0,l=e.length;c<l;c++){var d=e[c];switch(d.nodeType){case 1:var p=g(d.nodeName);(s=new u(p,y(d.attributes))).children=v("template"===p?d.content.childNodes:d.childNodes,s);break;case 3:s=new r(d.nodeValue.replace(x,"\r"));break;case 8:s=new o(d.nodeValue);break;default:continue}var f=a[c-1]||null;f&&(f.next=s),s.parent=t,s.prev=f,s.next=null,a.push(s)}return n&&((s=new i(n.substring(0,n.indexOf(" ")).toLowerCase(),n)).next=a[0]||null,s.parent=t,a.unshift(s),a[1]&&(a[1].prev=a[0])),a}var b="html",T="head",w="body",C=/<([a-zA-Z]+[0-9]?)/,S=/<head[^]*>/i,N=/<body[^]*>/i,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 R,I="object"==typeof document&&document.createElement("template");function P(e){var t,n,r=(e=function(e){return e.replace(h,m)}(e)).match(C),o=r&&r[1]?r[1].toLowerCase():"";switch(o){case b:var i=L(e);if(!S.test(e))null===(t=null==(a=i.querySelector(T))?void 0:a.parentNode)||void 0===t||t.removeChild(a);if(!N.test(e))null===(n=null==(a=i.querySelector(w))?void 0:a.parentNode)||void 0===n||n.removeChild(a);return i.querySelectorAll(b);case T:case w:var s=D(e).querySelectorAll(o);return N.test(e)&&S.test(e)?s[0].parentNode.childNodes:s;default:return R?R(e):(a=D(e,w).querySelector(w)).childNodes;var a}}I&&I.content&&(R=function(e){return I.innerHTML=e,I.content.childNodes});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=e.match(_),n=t?t[1]:void 0;return v(P(e),null,n)}}));
//# sourceMappingURL=html-dom-parser.min.js.map