Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

html-dom-parser

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-dom-parser - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

22

dist/html-dom-parser.js

@@ -14,4 +14,5 @@ (function (global, factory) {

var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
var HEAD_TAG_REGEX = /<head.*>/i;
var BODY_TAG_REGEX = /<body.*>/i;
// match-all-characters in case of newlines (DOTALL)
var HEAD_TAG_REGEX = /<head[^]*>/i;
var BODY_TAG_REGEX = /<body[^]*>/i;

@@ -75,3 +76,4 @@ // falls back to `parseFromString` if `createHTMLDocument` cannot be used

if (tagName) {
doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html;
var element = doc.documentElement.querySelector(tagName);
element.innerHTML = html;
return doc;

@@ -131,3 +133,3 @@ }

if (!HEAD_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(HEAD)[0];
element = doc.querySelector(HEAD);
if (element) {

@@ -139,3 +141,3 @@ element.parentNode.removeChild(element);

if (!BODY_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(BODY)[0];
element = doc.querySelector(BODY);
if (element) {

@@ -146,7 +148,8 @@ element.parentNode.removeChild(element);

return doc.getElementsByTagName(HTML);
return doc.querySelectorAll(HTML);
case HEAD:
case BODY:
elements = parseFromDocument(html).getElementsByTagName(firstTagName);
doc = parseFromDocument(html);
elements = doc.querySelectorAll(firstTagName);

@@ -164,5 +167,4 @@ // if there's a sibling element, then return both elements

}
return parseFromDocument(html, BODY).getElementsByTagName(BODY)[0]
.childNodes;
element = parseFromDocument(html, BODY).querySelector(BODY);
return element.childNodes;
}

@@ -169,0 +171,0 @@ }

@@ -1,2 +0,2 @@

!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="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},t="html",n="head",r="body",o=/<([a-zA-Z]+[0-9]?)/,i=/<head.*>/i,a=/<body.*>/i,s=function(){throw new Error("This browser does not support `document.implementation.createHTMLDocument`")},l=function(){throw new Error("This browser does not support `DOMParser.prototype.parseFromString`")};if("function"==typeof window.DOMParser){var u=new window.DOMParser;l=function(e,t){return t&&(e="<"+t+">"+e+"</"+t+">"),u.parseFromString(e,"text/html")},s=l}if(document.implementation){var c=document.implementation.createHTMLDocument();s=function(e,t){return t?(c.documentElement.getElementsByTagName(t)[0].innerHTML=e,c):(c.documentElement.innerHTML=e,c)}}var p,d=document.createElement("template");d.content&&(p=function(e){return d.innerHTML=e,d.content.childNodes});var f=function(e){var u,c,d,f,h=e.match(o);switch(h&&h[1]&&(u=h[1].toLowerCase()),u){case t:return c=l(e),i.test(e)||(d=c.getElementsByTagName(n)[0])&&d.parentNode.removeChild(d),a.test(e)||(d=c.getElementsByTagName(r)[0])&&d.parentNode.removeChild(d),c.getElementsByTagName(t);case n:case r:return f=s(e).getElementsByTagName(u),a.test(e)&&i.test(e)?f[0].parentNode.childNodes:f;default:return p?p(e):s(e,r).getElementsByTagName(r)[0].childNodes}},h={},m={},y={};!function(e){var t;Object.defineProperty(e,"__esModule",{value:!0}),e.Doctype=e.CDATA=e.Tag=e.Style=e.Script=e.Comment=e.Directive=e.Text=e.Root=e.isTag=e.ElementType=void 0,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"}(t=e.ElementType||(e.ElementType={})),e.isTag=function(e){return e.type===t.Tag||e.type===t.Script||e.type===t.Style},e.Root=t.Root,e.Text=t.Text,e.Directive=t.Directive,e.Comment=t.Comment,e.Script=t.Script,e.Style=t.Style,e.Tag=t.Tag,e.CDATA=t.CDATA,e.Doctype=t.Doctype}(y);var g,b={},v=e&&e.__extends||(g=function(e,t){return g=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},g(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}g(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),T=e&&e.__assign||function(){return T=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},T.apply(this,arguments)};Object.defineProperty(b,"__esModule",{value:!0}),b.cloneNode=b.hasChildren=b.isDocument=b.isDirective=b.isComment=b.isText=b.isCDATA=b.isTag=b.Element=b.Document=b.CDATA=b.NodeWithChildren=b.ProcessingInstruction=b.Comment=b.Text=b.DataNode=b.Node=void 0;var x=y,w=function(){function e(){this.parent=null,this.prev=null,this.next=null,this.startIndex=null,this.endIndex=null}return Object.defineProperty(e.prototype,"parentNode",{get:function(){return this.parent},set:function(e){this.parent=e},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"previousSibling",{get:function(){return this.prev},set:function(e){this.prev=e},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"nextSibling",{get:function(){return this.next},set:function(e){this.next=e},enumerable:!1,configurable:!0}),e.prototype.cloneNode=function(e){return void 0===e&&(e=!1),B(this,e)},e}();b.Node=w;var N=function(e){function t(t){var n=e.call(this)||this;return n.data=t,n}return v(t,e),Object.defineProperty(t.prototype,"nodeValue",{get:function(){return this.data},set:function(e){this.data=e},enumerable:!1,configurable:!0}),t}(w);b.DataNode=N;var C=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.Text,t}return v(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 3},enumerable:!1,configurable:!0}),t}(N);b.Text=C;var E=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.Comment,t}return v(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 8},enumerable:!1,configurable:!0}),t}(N);b.Comment=E;var D=function(e){function t(t,n){var r=e.call(this,n)||this;return r.name=t,r.type=x.ElementType.Directive,r}return v(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 1},enumerable:!1,configurable:!0}),t}(N);b.ProcessingInstruction=D;var O=function(e){function t(t){var n=e.call(this)||this;return n.children=t,n}return v(t,e),Object.defineProperty(t.prototype,"firstChild",{get:function(){var e;return null!==(e=this.children[0])&&void 0!==e?e:null},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"lastChild",{get:function(){return this.children.length>0?this.children[this.children.length-1]:null},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"childNodes",{get:function(){return this.children},set:function(e){this.children=e},enumerable:!1,configurable:!0}),t}(w);b.NodeWithChildren=O;var P=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.CDATA,t}return v(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 4},enumerable:!1,configurable:!0}),t}(O);b.CDATA=P;var S=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.Root,t}return v(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 9},enumerable:!1,configurable:!0}),t}(O);b.Document=S;var A=function(e){function t(t,n,r,o){void 0===r&&(r=[]),void 0===o&&(o="script"===t?x.ElementType.Script:"style"===t?x.ElementType.Style:x.ElementType.Tag);var i=e.call(this,r)||this;return i.name=t,i.attribs=n,i.type=o,i}return v(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 1},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"tagName",{get:function(){return this.name},set:function(e){this.name=e},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"attributes",{get:function(){var e=this;return Object.keys(this.attribs).map((function(t){var n,r;return{name:t,value:e.attribs[t],namespace:null===(n=e["x-attribsNamespace"])||void 0===n?void 0:n[t],prefix:null===(r=e["x-attribsPrefix"])||void 0===r?void 0:r[t]}}))},enumerable:!1,configurable:!0}),t}(O);function I(e){return(0,x.isTag)(e)}function j(e){return e.type===x.ElementType.CDATA}function _(e){return e.type===x.ElementType.Text}function M(e){return e.type===x.ElementType.Comment}function L(e){return e.type===x.ElementType.Directive}function k(e){return e.type===x.ElementType.Root}function B(e,t){var n;if(void 0===t&&(t=!1),_(e))n=new C(e.data);else if(M(e))n=new E(e.data);else if(I(e)){var r=t?F(e.children):[],o=new A(e.name,T({},e.attribs),r);r.forEach((function(e){return e.parent=o})),null!=e.namespace&&(o.namespace=e.namespace),e["x-attribsNamespace"]&&(o["x-attribsNamespace"]=T({},e["x-attribsNamespace"])),e["x-attribsPrefix"]&&(o["x-attribsPrefix"]=T({},e["x-attribsPrefix"])),n=o}else if(j(e)){r=t?F(e.children):[];var i=new P(r);r.forEach((function(e){return e.parent=i})),n=i}else if(k(e)){r=t?F(e.children):[];var a=new S(r);r.forEach((function(e){return e.parent=a})),e["x-mode"]&&(a["x-mode"]=e["x-mode"]),n=a}else{if(!L(e))throw new Error("Not implemented yet: ".concat(e.type));var s=new D(e.name,e.data);null!=e["x-name"]&&(s["x-name"]=e["x-name"],s["x-publicId"]=e["x-publicId"],s["x-systemId"]=e["x-systemId"]),n=s}return n.startIndex=e.startIndex,n.endIndex=e.endIndex,null!=e.sourceCodeLocation&&(n.sourceCodeLocation=e.sourceCodeLocation),n}function F(e){for(var t=e.map((function(e){return B(e,!0)})),n=1;n<t.length;n++)t[n].prev=t[n-1],t[n-1].next=t[n];return t}b.Element=A,b.isTag=I,b.isCDATA=j,b.isText=_,b.isComment=M,b.isDirective=L,b.isDocument=k,b.hasChildren=function(e){return Object.prototype.hasOwnProperty.call(e,"children")},b.cloneNode=B,function(t){var n=e&&e.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),r=e&&e.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),t.DomHandler=void 0;var o=y,i=b;r(b,t);var a={withStartIndices:!1,withEndIndices:!1,xmlMode:!1},s=function(){function e(e,t,n){this.dom=[],this.root=new i.Document(this.dom),this.done=!1,this.tagStack=[this.root],this.lastNode=null,this.parser=null,"function"==typeof t&&(n=t,t=a),"object"==typeof e&&(t=e,e=void 0),this.callback=null!=e?e:null,this.options=null!=t?t:a,this.elementCB=null!=n?n:null}return e.prototype.onparserinit=function(e){this.parser=e},e.prototype.onreset=function(){this.dom=[],this.root=new i.Document(this.dom),this.done=!1,this.tagStack=[this.root],this.lastNode=null,this.parser=null},e.prototype.onend=function(){this.done||(this.done=!0,this.parser=null,this.handleCallback(null))},e.prototype.onerror=function(e){this.handleCallback(e)},e.prototype.onclosetag=function(){this.lastNode=null;var e=this.tagStack.pop();this.options.withEndIndices&&(e.endIndex=this.parser.endIndex),this.elementCB&&this.elementCB(e)},e.prototype.onopentag=function(e,t){var n=this.options.xmlMode?o.ElementType.Tag:void 0,r=new i.Element(e,t,void 0,n);this.addNode(r),this.tagStack.push(r)},e.prototype.ontext=function(e){var t=this.lastNode;if(t&&t.type===o.ElementType.Text)t.data+=e,this.options.withEndIndices&&(t.endIndex=this.parser.endIndex);else{var n=new i.Text(e);this.addNode(n),this.lastNode=n}},e.prototype.oncomment=function(e){if(this.lastNode&&this.lastNode.type===o.ElementType.Comment)this.lastNode.data+=e;else{var t=new i.Comment(e);this.addNode(t),this.lastNode=t}},e.prototype.oncommentend=function(){this.lastNode=null},e.prototype.oncdatastart=function(){var e=new i.Text(""),t=new i.CDATA([e]);this.addNode(t),e.parent=t,this.lastNode=e},e.prototype.oncdataend=function(){this.lastNode=null},e.prototype.onprocessinginstruction=function(e,t){var n=new i.ProcessingInstruction(e,t);this.addNode(n)},e.prototype.handleCallback=function(e){if("function"==typeof this.callback)this.callback(e,this.dom);else if(e)throw e},e.prototype.addNode=function(e){var t=this.tagStack[this.tagStack.length-1],n=t.children[t.children.length-1];this.options.withStartIndices&&(e.startIndex=this.parser.startIndex),this.options.withEndIndices&&(e.endIndex=this.parser.endIndex),t.children.push(e),n&&(e.prev=n,n.next=e),e.parent=t,this.lastNode=null},e}();t.DomHandler=s,t.default=s}(m);for(var H,R={CASE_SENSITIVE_TAG_NAMES:["animateMotion","animateTransform","clipPath","feBlend","feColorMatrix","feComponentTransfer","feComposite","feConvolveMatrix","feDiffuseLighting","feDisplacementMap","feDropShadow","feFlood","feFuncA","feFuncB","feFuncG","feFuncR","feGaussainBlur","feImage","feMerge","feMergeNode","feMorphology","feOffset","fePointLight","feSpecularLighting","feSpotLight","feTile","feTurbulence","foreignObject","linearGradient","radialGradient","textPath"]},G=m,V=R.CASE_SENSITIVE_TAG_NAMES,z=G.Comment,W=G.Element,Z=G.ProcessingInstruction,q=G.Text,J={},K=0,Q=V.length;K<Q;K++)H=V[K],J[H.toLowerCase()]=H;function U(e){for(var t,n={},r=0,o=e.length;r<o;r++)n[(t=e[r]).name]=t.value;return n}function X(e){var t=function(e){return J[e]}(e=e.toLowerCase());return t||e}h.formatAttributes=U,h.formatDOM=function e(t,n,r){n=n||null;for(var o=[],i=0,a=t.length;i<a;i++){var s,l=t[i];switch(l.nodeType){case 1:(s=new W(X(l.nodeName),U(l.attributes))).children=e(l.childNodes,s);break;case 3:s=new q(l.nodeValue);break;case 8:s=new z(l.nodeValue);break;default:continue}var u=o[i-1]||null;u&&(u.next=s),s.parent=n,s.prev=u,s.next=null,o.push(s)}return r&&((s=new Z(r.substring(0,r.indexOf(" ")).toLowerCase(),r)).next=o[0]||null,s.parent=n,o.unshift(s),o[1]&&(o[1].prev=o[0])),o};var Y=f,$=h.formatDOM,ee=/<(![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,n=e.match(ee);return n&&n[1]&&(t=n[1]),$(Y(e),null,t)}}));
!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="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},t="html",n="head",r="body",o=/<([a-zA-Z]+[0-9]?)/,i=/<head[^]*>/i,a=/<body[^]*>/i,u=function(){throw new Error("This browser does not support `document.implementation.createHTMLDocument`")},l=function(){throw new Error("This browser does not support `DOMParser.prototype.parseFromString`")};if("function"==typeof window.DOMParser){var c=new window.DOMParser;l=function(e,t){return t&&(e="<"+t+">"+e+"</"+t+">"),c.parseFromString(e,"text/html")},u=l}if(document.implementation){var s=document.implementation.createHTMLDocument();u=function(e,t){return t?(s.documentElement.querySelector(t).innerHTML=e,s):(s.documentElement.innerHTML=e,s)}}var p,d=document.createElement("template");d.content&&(p=function(e){return d.innerHTML=e,d.content.childNodes});var f=function(e){var c,s,d,f,h=e.match(o);switch(h&&h[1]&&(c=h[1].toLowerCase()),c){case t:return s=l(e),i.test(e)||(d=s.querySelector(n))&&d.parentNode.removeChild(d),a.test(e)||(d=s.querySelector(r))&&d.parentNode.removeChild(d),s.querySelectorAll(t);case n:case r:return f=(s=u(e)).querySelectorAll(c),a.test(e)&&i.test(e)?f[0].parentNode.childNodes:f;default:return p?p(e):(d=u(e,r).querySelector(r)).childNodes}},h={},m={},y={};!function(e){var t;Object.defineProperty(e,"__esModule",{value:!0}),e.Doctype=e.CDATA=e.Tag=e.Style=e.Script=e.Comment=e.Directive=e.Text=e.Root=e.isTag=e.ElementType=void 0,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"}(t=e.ElementType||(e.ElementType={})),e.isTag=function(e){return e.type===t.Tag||e.type===t.Script||e.type===t.Style},e.Root=t.Root,e.Text=t.Text,e.Directive=t.Directive,e.Comment=t.Comment,e.Script=t.Script,e.Style=t.Style,e.Tag=t.Tag,e.CDATA=t.CDATA,e.Doctype=t.Doctype}(y);var b,v={},g=e&&e.__extends||(b=function(e,t){return b=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},b(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}b(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),T=e&&e.__assign||function(){return T=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},T.apply(this,arguments)};Object.defineProperty(v,"__esModule",{value:!0}),v.cloneNode=v.hasChildren=v.isDocument=v.isDirective=v.isComment=v.isText=v.isCDATA=v.isTag=v.Element=v.Document=v.CDATA=v.NodeWithChildren=v.ProcessingInstruction=v.Comment=v.Text=v.DataNode=v.Node=void 0;var x=y,w=function(){function e(){this.parent=null,this.prev=null,this.next=null,this.startIndex=null,this.endIndex=null}return Object.defineProperty(e.prototype,"parentNode",{get:function(){return this.parent},set:function(e){this.parent=e},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"previousSibling",{get:function(){return this.prev},set:function(e){this.prev=e},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"nextSibling",{get:function(){return this.next},set:function(e){this.next=e},enumerable:!1,configurable:!0}),e.prototype.cloneNode=function(e){return void 0===e&&(e=!1),F(this,e)},e}();v.Node=w;var C=function(e){function t(t){var n=e.call(this)||this;return n.data=t,n}return g(t,e),Object.defineProperty(t.prototype,"nodeValue",{get:function(){return this.data},set:function(e){this.data=e},enumerable:!1,configurable:!0}),t}(w);v.DataNode=C;var N=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.Text,t}return g(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 3},enumerable:!1,configurable:!0}),t}(C);v.Text=N;var E=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.Comment,t}return g(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 8},enumerable:!1,configurable:!0}),t}(C);v.Comment=E;var D=function(e){function t(t,n){var r=e.call(this,n)||this;return r.name=t,r.type=x.ElementType.Directive,r}return g(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 1},enumerable:!1,configurable:!0}),t}(C);v.ProcessingInstruction=D;var O=function(e){function t(t){var n=e.call(this)||this;return n.children=t,n}return g(t,e),Object.defineProperty(t.prototype,"firstChild",{get:function(){var e;return null!==(e=this.children[0])&&void 0!==e?e:null},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"lastChild",{get:function(){return this.children.length>0?this.children[this.children.length-1]:null},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"childNodes",{get:function(){return this.children},set:function(e){this.children=e},enumerable:!1,configurable:!0}),t}(w);v.NodeWithChildren=O;var S=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.CDATA,t}return g(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 4},enumerable:!1,configurable:!0}),t}(O);v.CDATA=S;var P=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.type=x.ElementType.Root,t}return g(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 9},enumerable:!1,configurable:!0}),t}(O);v.Document=P;var A=function(e){function t(t,n,r,o){void 0===r&&(r=[]),void 0===o&&(o="script"===t?x.ElementType.Script:"style"===t?x.ElementType.Style:x.ElementType.Tag);var i=e.call(this,r)||this;return i.name=t,i.attribs=n,i.type=o,i}return g(t,e),Object.defineProperty(t.prototype,"nodeType",{get:function(){return 1},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"tagName",{get:function(){return this.name},set:function(e){this.name=e},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"attributes",{get:function(){var e=this;return Object.keys(this.attribs).map((function(t){var n,r;return{name:t,value:e.attribs[t],namespace:null===(n=e["x-attribsNamespace"])||void 0===n?void 0:n[t],prefix:null===(r=e["x-attribsPrefix"])||void 0===r?void 0:r[t]}}))},enumerable:!1,configurable:!0}),t}(O);function I(e){return(0,x.isTag)(e)}function j(e){return e.type===x.ElementType.CDATA}function _(e){return e.type===x.ElementType.Text}function M(e){return e.type===x.ElementType.Comment}function L(e){return e.type===x.ElementType.Directive}function k(e){return e.type===x.ElementType.Root}function F(e,t){var n;if(void 0===t&&(t=!1),_(e))n=new N(e.data);else if(M(e))n=new E(e.data);else if(I(e)){var r=t?H(e.children):[],o=new A(e.name,T({},e.attribs),r);r.forEach((function(e){return e.parent=o})),null!=e.namespace&&(o.namespace=e.namespace),e["x-attribsNamespace"]&&(o["x-attribsNamespace"]=T({},e["x-attribsNamespace"])),e["x-attribsPrefix"]&&(o["x-attribsPrefix"]=T({},e["x-attribsPrefix"])),n=o}else if(j(e)){r=t?H(e.children):[];var i=new S(r);r.forEach((function(e){return e.parent=i})),n=i}else if(k(e)){r=t?H(e.children):[];var a=new P(r);r.forEach((function(e){return e.parent=a})),e["x-mode"]&&(a["x-mode"]=e["x-mode"]),n=a}else{if(!L(e))throw new Error("Not implemented yet: ".concat(e.type));var u=new D(e.name,e.data);null!=e["x-name"]&&(u["x-name"]=e["x-name"],u["x-publicId"]=e["x-publicId"],u["x-systemId"]=e["x-systemId"]),n=u}return n.startIndex=e.startIndex,n.endIndex=e.endIndex,null!=e.sourceCodeLocation&&(n.sourceCodeLocation=e.sourceCodeLocation),n}function H(e){for(var t=e.map((function(e){return F(e,!0)})),n=1;n<t.length;n++)t[n].prev=t[n-1],t[n-1].next=t[n];return t}v.Element=A,v.isTag=I,v.isCDATA=j,v.isText=_,v.isComment=M,v.isDirective=L,v.isDocument=k,v.hasChildren=function(e){return Object.prototype.hasOwnProperty.call(e,"children")},v.cloneNode=F,function(t){var n=e&&e.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),r=e&&e.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),t.DomHandler=void 0;var o=y,i=v;r(v,t);var a={withStartIndices:!1,withEndIndices:!1,xmlMode:!1},u=function(){function e(e,t,n){this.dom=[],this.root=new i.Document(this.dom),this.done=!1,this.tagStack=[this.root],this.lastNode=null,this.parser=null,"function"==typeof t&&(n=t,t=a),"object"==typeof e&&(t=e,e=void 0),this.callback=null!=e?e:null,this.options=null!=t?t:a,this.elementCB=null!=n?n:null}return e.prototype.onparserinit=function(e){this.parser=e},e.prototype.onreset=function(){this.dom=[],this.root=new i.Document(this.dom),this.done=!1,this.tagStack=[this.root],this.lastNode=null,this.parser=null},e.prototype.onend=function(){this.done||(this.done=!0,this.parser=null,this.handleCallback(null))},e.prototype.onerror=function(e){this.handleCallback(e)},e.prototype.onclosetag=function(){this.lastNode=null;var e=this.tagStack.pop();this.options.withEndIndices&&(e.endIndex=this.parser.endIndex),this.elementCB&&this.elementCB(e)},e.prototype.onopentag=function(e,t){var n=this.options.xmlMode?o.ElementType.Tag:void 0,r=new i.Element(e,t,void 0,n);this.addNode(r),this.tagStack.push(r)},e.prototype.ontext=function(e){var t=this.lastNode;if(t&&t.type===o.ElementType.Text)t.data+=e,this.options.withEndIndices&&(t.endIndex=this.parser.endIndex);else{var n=new i.Text(e);this.addNode(n),this.lastNode=n}},e.prototype.oncomment=function(e){if(this.lastNode&&this.lastNode.type===o.ElementType.Comment)this.lastNode.data+=e;else{var t=new i.Comment(e);this.addNode(t),this.lastNode=t}},e.prototype.oncommentend=function(){this.lastNode=null},e.prototype.oncdatastart=function(){var e=new i.Text(""),t=new i.CDATA([e]);this.addNode(t),e.parent=t,this.lastNode=e},e.prototype.oncdataend=function(){this.lastNode=null},e.prototype.onprocessinginstruction=function(e,t){var n=new i.ProcessingInstruction(e,t);this.addNode(n)},e.prototype.handleCallback=function(e){if("function"==typeof this.callback)this.callback(e,this.dom);else if(e)throw e},e.prototype.addNode=function(e){var t=this.tagStack[this.tagStack.length-1],n=t.children[t.children.length-1];this.options.withStartIndices&&(e.startIndex=this.parser.startIndex),this.options.withEndIndices&&(e.endIndex=this.parser.endIndex),t.children.push(e),n&&(e.prev=n,n.next=e),e.parent=t,this.lastNode=null},e}();t.DomHandler=u,t.default=u}(m);for(var B,R={CASE_SENSITIVE_TAG_NAMES:["animateMotion","animateTransform","clipPath","feBlend","feColorMatrix","feComponentTransfer","feComposite","feConvolveMatrix","feDiffuseLighting","feDisplacementMap","feDropShadow","feFlood","feFuncA","feFuncB","feFuncG","feFuncR","feGaussainBlur","feImage","feMerge","feMergeNode","feMorphology","feOffset","fePointLight","feSpecularLighting","feSpotLight","feTile","feTurbulence","foreignObject","linearGradient","radialGradient","textPath"]},q=m,G=R.CASE_SENSITIVE_TAG_NAMES,V=q.Comment,z=q.Element,W=q.ProcessingInstruction,Z=q.Text,J={},K=0,Q=G.length;K<Q;K++)B=G[K],J[B.toLowerCase()]=B;function U(e){for(var t,n={},r=0,o=e.length;r<o;r++)n[(t=e[r]).name]=t.value;return n}function X(e){var t=function(e){return J[e]}(e=e.toLowerCase());return t||e}h.formatAttributes=U,h.formatDOM=function e(t,n,r){n=n||null;for(var o=[],i=0,a=t.length;i<a;i++){var u,l=t[i];switch(l.nodeType){case 1:(u=new z(X(l.nodeName),U(l.attributes))).children=e(l.childNodes,u);break;case 3:u=new Z(l.nodeValue);break;case 8:u=new V(l.nodeValue);break;default:continue}var c=o[i-1]||null;c&&(c.next=u),u.parent=n,u.prev=c,u.next=null,o.push(u)}return r&&((u=new W(r.substring(0,r.indexOf(" ")).toLowerCase(),r)).next=o[0]||null,u.parent=n,o.unshift(u),o[1]&&(o[1].prev=o[0])),o};var Y=f,$=h.formatDOM,ee=/<(![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,n=e.match(ee);return n&&n[1]&&(t=n[1]),$(Y(e),null,t)}}));
//# sourceMappingURL=html-dom-parser.min.js.map

@@ -6,4 +6,5 @@ // constants

var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
var HEAD_TAG_REGEX = /<head.*>/i;
var BODY_TAG_REGEX = /<body.*>/i;
// match-all-characters in case of newlines (DOTALL)
var HEAD_TAG_REGEX = /<head[^]*>/i;
var BODY_TAG_REGEX = /<body[^]*>/i;

@@ -67,3 +68,4 @@ // falls back to `parseFromString` if `createHTMLDocument` cannot be used

if (tagName) {
doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html;
var element = doc.documentElement.querySelector(tagName);
element.innerHTML = html;
return doc;

@@ -123,3 +125,3 @@ }

if (!HEAD_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(HEAD)[0];
element = doc.querySelector(HEAD);
if (element) {

@@ -131,3 +133,3 @@ element.parentNode.removeChild(element);

if (!BODY_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(BODY)[0];
element = doc.querySelector(BODY);
if (element) {

@@ -138,7 +140,8 @@ element.parentNode.removeChild(element);

return doc.getElementsByTagName(HTML);
return doc.querySelectorAll(HTML);
case HEAD:
case BODY:
elements = parseFromDocument(html).getElementsByTagName(firstTagName);
doc = parseFromDocument(html);
elements = doc.querySelectorAll(firstTagName);

@@ -156,5 +159,4 @@ // if there's a sibling element, then return both elements

}
return parseFromDocument(html, BODY).getElementsByTagName(BODY)[0]
.childNodes;
element = parseFromDocument(html, BODY).querySelector(BODY);
return element.childNodes;
}

@@ -161,0 +163,0 @@ }

{
"name": "html-dom-parser",
"version": "3.0.0",
"version": "3.0.1",
"description": "HTML to DOM parser.",

@@ -75,3 +75,3 @@ "author": "Mark <mark@remarkablemark.org>",

"prettier": "2.7.1",
"rollup": "2.75.7",
"rollup": "2.76.0",
"rollup-plugin-terser": "7.0.2",

@@ -78,0 +78,0 @@ "sinon": "14.0.0",

@@ -98,4 +98,5 @@ # html-dom-parser

<!-- prettier-ignore -->
```js
[];
[]
```

@@ -102,0 +103,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc