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
48
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 0.5.0 to 1.0.0

lib/client/constants.d.ts

37

CHANGELOG.md

@@ -5,2 +5,39 @@ # Change Log

# [1.0.0](https://github.com/remarkablemark/html-dom-parser/compare/v0.5.0...v1.0.0) (2020-12-25)
### Build System
* **package:** upgrade `domhandler` to v4 and `htmlparser2` to v6 ([ec5673e](https://github.com/remarkablemark/html-dom-parser/commit/ec5673ef38050f808ce49e2e4ee165d30492b190))
### Performance Improvements
* **client:** deprecate Internet Explorer 9 (IE9) ([d42ea4e](https://github.com/remarkablemark/html-dom-parser/commit/d42ea4ed5df9f44abf16528e36b3cfbb4c7fdd08))
* **utilities:** continue if nodeType is not element, text, comment ([793ff0c](https://github.com/remarkablemark/html-dom-parser/commit/793ff0c7a84ea05ff061e24984595f6e143362e6))
### BREAKING CHANGES
* **package:** upgrade `domhandler` to v4 and `htmlparser2` to v6
domhandler 3.3.0 → 4.0.0
htmlparser2 4.1.0 → 6.0.0
domhandler:
* https://github.com/fb55/domhandler/releases/tag/v4.0.0
htmlparser2:
* https://github.com/fb55/htmlparser2/releases/tag/v5.0.0
* https://github.com/fb55/htmlparser2/releases/tag/v5.0.1
* https://github.com/fb55/htmlparser2/releases/tag/v6.0.0
`decodeEntities` option now defaults to true. `<title>` is
parsed correctly. Remove root parent node to keep parser
backwards compatible.
# [0.5.0](https://github.com/remarkablemark/html-dom-parser/compare/v0.4.0...v0.5.0) (2020-12-13)

@@ -7,0 +44,0 @@

195

dist/html-dom-parser.js

@@ -83,3 +83,3 @@ (function (global, factory) {

Object.defineProperty(exports, "__esModule", { value: true });
exports.cloneNode = exports.Element = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0;
exports.cloneNode = exports.Element = exports.Document = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0;
var nodeTypes = new Map([

@@ -93,2 +93,3 @@ ["tag" /* Tag */, 1],

["comment" /* Comment */, 8],
["root" /* Root */, 9],
]);

@@ -220,6 +221,8 @@ /**

exports.ProcessingInstruction = ProcessingInstruction;
/**
* A `Node` that can have children.
*/
var NodeWithChildren = /** @class */ (function (_super) {
__extends(NodeWithChildren, _super);
/**
*
* @param type Type of the node.

@@ -264,2 +267,10 @@ * @param children Children of the node. Only certain node types can have children.

exports.NodeWithChildren = NodeWithChildren;
var Document = /** @class */ (function (_super) {
__extends(Document, _super);
function Document(children) {
return _super.call(this, "root" /* Root */, children) || this;
}
return Document;
}(NodeWithChildren));
exports.Document = Document;
var Element = /** @class */ (function (_super) {

@@ -298,6 +309,11 @@ __extends(Element, _super);

var _this = this;
return Object.keys(this.attribs).map(function (name) { return ({
name: name,
value: _this.attribs[name],
}); });
return Object.keys(this.attribs).map(function (name) {
var _a, _b;
return ({
name: name,
value: _this.attribs[name],
namespace: (_a = _this["x-attribsNamespace"]) === null || _a === void 0 ? void 0 : _a[name],
prefix: (_b = _this["x-attribsPrefix"]) === null || _b === void 0 ? void 0 : _b[name],
});
});
},

@@ -318,11 +334,20 @@ enumerable: false,

if (recursive === void 0) { recursive = false; }
var result;
switch (node.type) {
case "text" /* Text */:
return new Text(node.data);
result = new Text(node.data);
break;
case "directive" /* Directive */: {
var instr = node;
return new ProcessingInstruction(instr.name, instr.data);
result = new ProcessingInstruction(instr.name, instr.data);
if (instr["x-name"] != null) {
result["x-name"] = instr["x-name"];
result["x-publicId"] = instr["x-publicId"];
result["x-systemId"] = instr["x-systemId"];
}
break;
}
case "comment" /* Comment */:
return new Comment(node.data);
result = new Comment(node.data);
break;
case "tag" /* Tag */:

@@ -335,3 +360,10 @@ case "script" /* Script */:

children.forEach(function (child) { return (child.parent = clone_1); });
return clone_1;
if (elem["x-attribsNamespace"]) {
clone_1["x-attribsNamespace"] = __assign({}, elem["x-attribsNamespace"]);
}
if (elem["x-attribsPrefix"]) {
clone_1["x-attribsPrefix"] = __assign({}, elem["x-attribsPrefix"]);
}
result = clone_1;
break;
}

@@ -341,6 +373,18 @@ case "cdata" /* CDATA */: {

var children = recursive ? cloneChildren(cdata.children) : [];
var clone_2 = new NodeWithChildren("cdata" /* CDATA */, children);
var clone_2 = new NodeWithChildren(node.type, children);
children.forEach(function (child) { return (child.parent = clone_2); });
return clone_2;
result = clone_2;
break;
}
case "root" /* Root */: {
var doc = node;
var children = recursive ? cloneChildren(doc.children) : [];
var clone_3 = new Document(children);
children.forEach(function (child) { return (child.parent = clone_3); });
if (doc["x-mode"]) {
clone_3["x-mode"] = doc["x-mode"];
}
result = clone_3;
break;
}
case "doctype" /* Doctype */: {

@@ -351,2 +395,5 @@ // This type isn't used yet.

}
result.startIndex = node.startIndex;
result.endIndex = node.endIndex;
return result;
}

@@ -423,69 +470,69 @@ exports.cloneNode = cloneNode;

/**
* Formats the browser DOM nodes to mimic the output of `htmlparser2.parseDOM()`.
* Transforms DOM nodes to `domhandler` nodes.
*
* @param {NodeList} nodes - DOM nodes.
* @param {Element} [parentNode] - Formatted parent node.
* @param {string} [directive] - Directive.
* @param {NodeList} nodes - DOM nodes.
* @param {Element|null} [parent=null] - Parent node.
* @param {string} [directive] - Directive.
* @return {Array<Comment|Element|ProcessingInstruction|Text>}
*/
function formatDOM(domNodes, parentNode, directive) {
parentNode = parentNode || null;
function formatDOM(nodes, parent, directive) {
parent = parent || null;
var result = [];
var domNode;
var node;
var prevNode;
var output = [];
for (var index = 0, len = nodes.length; index < len; index++) {
var node = nodes[index];
var current;
for (var i = 0, len = domNodes.length; i < len; i++) {
domNode = domNodes[i];
// set the node data given the type
switch (domNode.nodeType) {
switch (node.nodeType) {
case 1:
// script, style, or tag
node = new Element(
formatTagName(domNode.nodeName),
formatAttributes(domNode.attributes)
current = new Element(
formatTagName(node.nodeName),
formatAttributes(node.attributes)
);
node.children = formatDOM(domNode.childNodes, node);
current.children = formatDOM(node.childNodes, current);
break;
case 3:
node = new Text(domNode.nodeValue);
current = new Text(node.nodeValue);
break;
case 8:
node = new Comment(domNode.nodeValue);
current = new Comment(node.nodeValue);
break;
default:
continue;
}
// set next for previous node
prevNode = output[i - 1] || null;
if (prevNode) {
prevNode.next = node;
// set previous node next
var prev = result[index - 1] || null;
if (prev) {
prev.next = current;
}
// set properties for current node
node.parent = parentNode;
node.prev = prevNode;
node.next = null;
current.parent = parent;
current.prev = prev;
current.next = null;
output.push(node);
result.push(current);
}
if (directive) {
node = new ProcessingInstruction(
current = new ProcessingInstruction(
directive.substring(0, directive.indexOf(' ')).toLowerCase(),
directive
);
node.next = output[0] || null;
node.parent = parentNode;
output.unshift(node);
current.next = result[0] || null;
current.parent = parent;
result.unshift(current);
if (output[1]) {
output[1].prev = output[0];
if (result[1]) {
result[1].prev = result[0];
}
}
return output;
return result;
}

@@ -496,9 +543,5 @@

*
* @param {number} [version] - IE version to detect.
* @return {boolean} - Whether IE or the version is detected.
* @return {boolean} - Whether IE is detected.
*/
function isIE(version) {
if (version) {
return document.documentMode === version;
}
function isIE() {
return /(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent);

@@ -520,9 +563,3 @@ }

var BODY_TAG_REGEX = /<body.*>/i;
// http://www.w3.org/TR/html/syntax.html#void-elements
var VOID_ELEMENTS_REGEX = /<(area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)(.*?)\/?>/gi;
// detect IE browser
var isIE9 = utilities.isIE(9);
var isIE$1 = isIE9 || utilities.isIE();
// falls back to `parseFromString` if `createHTMLDocument` cannot be used

@@ -548,7 +585,4 @@ var parseFromDocument = function () {

var domParser = new window.DOMParser();
var mimeType = 'text/html';
// IE9 does not support 'text/html' MIME type
// https://msdn.microsoft.com/en-us/library/ff975278(v=vs.85).aspx
var mimeType = isIE9 ? 'text/xml' : 'text/html';
/**

@@ -566,7 +600,2 @@ * Creates an HTML document using `DOMParser.parseFromString`.

// because IE9 only supports MIME type 'text/xml', void elements need to be self-closed
if (isIE9) {
html = html.replace(VOID_ELEMENTS_REGEX, '<$1$2$3/>');
}
return domParser.parseFromString(html, mimeType);

@@ -584,6 +613,8 @@ };

if (document.implementation) {
var isIE$1 = utilities.isIE;
// title parameter is required in IE
// https://msdn.microsoft.com/en-us/library/ff975457(v=vs.85).aspx
var doc = document.implementation.createHTMLDocument(
isIE$1 ? 'html-dom-parser' : undefined
isIE$1() ? 'html-dom-parser' : undefined
);

@@ -604,11 +635,4 @@

try {
doc.documentElement.innerHTML = html;
return doc;
// fallback when certain elements in `documentElement` are read-only (IE9)
} catch (err) {
if (parseFromString) {
return parseFromString(html);
}
}
doc.documentElement.innerHTML = html;
return doc;
};

@@ -702,3 +726,2 @@ }

var formatDOM$1 = utilities.formatDOM;
var isIE9$1 = utilities.isIE(9);

@@ -710,3 +733,3 @@ var DIRECTIVE_REGEX = /<(![a-zA-Z\s]+)>/; // e.g., <!doctype html>

*
* @param {String} html - HTML markup.
* @param {string} html - HTML markup.
* @return {DomElement[]} - DOM elements.

@@ -719,3 +742,3 @@ */

if (!html) {
if (html === '') {
return [];

@@ -730,8 +753,2 @@ }

directive = match[1];
// remove directive in IE9 because DOMParser uses
// MIME type 'text/xml' instead of 'text/html'
if (isIE9$1) {
html = html.replace(match[0], '');
}
}

@@ -742,7 +759,7 @@

var htmlToDomClient = HTMLDOMParser;
var htmlToDom = HTMLDOMParser;
return htmlToDomClient;
return htmlToDom;
})));
//# sourceMappingURL=html-dom-parser.js.map

@@ -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=["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"],t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};for(var n,r=function(e){var t={exports:{}};return e(t,t.exports),t.exports}((function(e,n){var r,o=t&&t.__extends||(r=function(e,t){return(r=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])})(e,t)},function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),i=t&&t.__assign||function(){return(i=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}).apply(this,arguments)};Object.defineProperty(n,"__esModule",{value:!0}),n.cloneNode=n.Element=n.NodeWithChildren=n.ProcessingInstruction=n.Comment=n.Text=n.DataNode=n.Node=void 0;var a=new Map([["tag",1],["script",1],["style",1],["directive",1],["text",3],["cdata",4],["comment",8]]),u=function(){function e(e){this.type=e,this.parent=null,this.prev=null,this.next=null,this.startIndex=null,this.endIndex=null}return Object.defineProperty(e.prototype,"nodeType",{get:function(){var e;return null!==(e=a.get(this.type))&&void 0!==e?e:1},enumerable:!1,configurable:!0}),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),h(this,e)},e}();n.Node=u;var c=function(e){function t(t,n){var r=e.call(this,t)||this;return r.data=n,r}return o(t,e),Object.defineProperty(t.prototype,"nodeValue",{get:function(){return this.data},set:function(e){this.data=e},enumerable:!1,configurable:!0}),t}(u);n.DataNode=c;var s=function(e){function t(t){return e.call(this,"text",t)||this}return o(t,e),t}(c);n.Text=s;var l=function(e){function t(t){return e.call(this,"comment",t)||this}return o(t,e),t}(c);n.Comment=l;var f=function(e){function t(t,n){var r=e.call(this,"directive",n)||this;return r.name=t,r}return o(t,e),t}(c);n.ProcessingInstruction=f;var d=function(e){function t(t,n){var r=e.call(this,t)||this;return r.children=n,r}return o(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}(u);n.NodeWithChildren=d;var p=function(e){function t(t,n,r){void 0===r&&(r=[]);var o=e.call(this,"script"===t?"script":"style"===t?"style":"tag",r)||this;return o.name=t,o.attribs=n,o.attribs=n,o}return o(t,e),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){return{name:t,value:e.attribs[t]}}))},enumerable:!1,configurable:!0}),t}(d);function h(e,t){switch(void 0===t&&(t=!1),e.type){case"text":return new s(e.data);case"directive":var n=e;return new f(n.name,n.data);case"comment":return new l(e.data);case"tag":case"script":case"style":var r=e,o=t?m(r.children):[],a=new p(r.name,i({},r.attribs),o);return o.forEach((function(e){return e.parent=a})),a;case"cdata":o=t?m(e.children):[];var u=new d("cdata",o);return o.forEach((function(e){return e.parent=u})),u;case"doctype":throw new Error("Not implemented yet: ElementType.Doctype case")}}function m(e){for(var t=e.map((function(e){return h(e,!0)})),n=1;n<t.length;n++)t[n].prev=t[n-1],t[n-1].next=t[n];return t}n.Element=p,n.cloneNode=h})),o=e,i=r.Comment,a=r.Element,u=r.ProcessingInstruction,c=r.Text,s={},l=0,f=o.length;l<f;l++)n=o[l],s[n.toLowerCase()]=n;function d(e){for(var t,n={},r=0,o=e.length;r<o;r++)n[(t=e[r]).name]=t.value;return n}function p(e){var t=function(e){return s[e]}(e=e.toLowerCase());return t||e}var h={formatAttributes:d,formatDOM:function e(t,n,r){var o,s,l;n=n||null;for(var f=[],h=0,m=t.length;h<m;h++){switch((o=t[h]).nodeType){case 1:(s=new a(p(o.nodeName),d(o.attributes))).children=e(o.childNodes,s);break;case 3:s=new c(o.nodeValue);break;case 8:s=new i(o.nodeValue)}(l=f[h-1]||null)&&(l.next=s),s.parent=n,s.prev=l,s.next=null,f.push(s)}return r&&((s=new u(r.substring(0,r.indexOf(" ")).toLowerCase(),r)).next=f[0]||null,s.parent=n,f.unshift(s),f[1]&&(f[1].prev=f[0])),f},isIE:function(e){return e?document.documentMode===e:/(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent)}},m="html",g="head",y="body",b=/<([a-zA-Z]+[0-9]?)/,v=/<head.*>/i,w=/<body.*>/i,T=/<(area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)(.*?)\/?>/gi,x=h.isIE(9),O=x||h.isIE(),N=function(){throw new Error("This browser does not support `document.implementation.createHTMLDocument`")},E=function(){throw new Error("This browser does not support `DOMParser.prototype.parseFromString`")};if("function"==typeof window.DOMParser){var P=new window.DOMParser,M=x?"text/xml":"text/html";N=E=function(e,t){return t&&(e="<"+t+">"+e+"</"+t+">"),x&&(e=e.replace(T,"<$1$2$3/>")),P.parseFromString(e,M)}}if(document.implementation){var j=document.implementation.createHTMLDocument(O?"html-dom-parser":void 0);N=function(e,t){if(t)return j.documentElement.getElementsByTagName(t)[0].innerHTML=e,j;try{return j.documentElement.innerHTML=e,j}catch(t){if(E)return E(e)}}}var C,D=document.createElement("template");D.content&&(C=function(e){return D.innerHTML=e,D.content.childNodes});var L=function(e){var t,n,r,o,i=e.match(b);switch(i&&i[1]&&(t=i[1].toLowerCase()),t){case m:return n=E(e),v.test(e)||(r=n.getElementsByTagName(g)[0])&&r.parentNode.removeChild(r),w.test(e)||(r=n.getElementsByTagName(y)[0])&&r.parentNode.removeChild(r),n.getElementsByTagName(m);case g:case y:return o=N(e).getElementsByTagName(t),w.test(e)&&v.test(e)?o[0].parentNode.childNodes:o;default:return C?C(e):N(e,y).getElementsByTagName(y)[0].childNodes}},_=h.formatDOM,I=h.isIE(9),B=/<(![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(B);return n&&n[1]&&(t=n[1],I&&(e=e.replace(n[0],""))),_(L(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=["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"],t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};for(var n,r=function(e){var t={exports:{}};return e(t,t.exports),t.exports}((function(e,n){var r,o=t&&t.__extends||(r=function(e,t){return(r=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])})(e,t)},function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),i=t&&t.__assign||function(){return(i=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}).apply(this,arguments)};Object.defineProperty(n,"__esModule",{value:!0}),n.cloneNode=n.Element=n.Document=n.NodeWithChildren=n.ProcessingInstruction=n.Comment=n.Text=n.DataNode=n.Node=void 0;var a=new Map([["tag",1],["script",1],["style",1],["directive",1],["text",3],["cdata",4],["comment",8],["root",9]]),u=function(){function e(e){this.type=e,this.parent=null,this.prev=null,this.next=null,this.startIndex=null,this.endIndex=null}return Object.defineProperty(e.prototype,"nodeType",{get:function(){var e;return null!==(e=a.get(this.type))&&void 0!==e?e:1},enumerable:!1,configurable:!0}),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),m(this,e)},e}();n.Node=u;var c=function(e){function t(t,n){var r=e.call(this,t)||this;return r.data=n,r}return o(t,e),Object.defineProperty(t.prototype,"nodeValue",{get:function(){return this.data},set:function(e){this.data=e},enumerable:!1,configurable:!0}),t}(u);n.DataNode=c;var s=function(e){function t(t){return e.call(this,"text",t)||this}return o(t,e),t}(c);n.Text=s;var l=function(e){function t(t){return e.call(this,"comment",t)||this}return o(t,e),t}(c);n.Comment=l;var f=function(e){function t(t,n){var r=e.call(this,"directive",n)||this;return r.name=t,r}return o(t,e),t}(c);n.ProcessingInstruction=f;var d=function(e){function t(t,n){var r=e.call(this,t)||this;return r.children=n,r}return o(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}(u);n.NodeWithChildren=d;var p=function(e){function t(t){return e.call(this,"root",t)||this}return o(t,e),t}(d);n.Document=p;var h=function(e){function t(t,n,r){void 0===r&&(r=[]);var o=e.call(this,"script"===t?"script":"style"===t?"style":"tag",r)||this;return o.name=t,o.attribs=n,o.attribs=n,o}return o(t,e),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}(d);function m(e,t){var n;switch(void 0===t&&(t=!1),e.type){case"text":n=new s(e.data);break;case"directive":var r=e;n=new f(r.name,r.data),null!=r["x-name"]&&(n["x-name"]=r["x-name"],n["x-publicId"]=r["x-publicId"],n["x-systemId"]=r["x-systemId"]);break;case"comment":n=new l(e.data);break;case"tag":case"script":case"style":var o=e,a=t?b(o.children):[],u=new h(o.name,i({},o.attribs),a);a.forEach((function(e){return e.parent=u})),o["x-attribsNamespace"]&&(u["x-attribsNamespace"]=i({},o["x-attribsNamespace"])),o["x-attribsPrefix"]&&(u["x-attribsPrefix"]=i({},o["x-attribsPrefix"])),n=u;break;case"cdata":a=t?b(e.children):[];var c=new d(e.type,a);a.forEach((function(e){return e.parent=c})),n=c;break;case"root":var m=e,g=(a=t?b(m.children):[],new p(a));a.forEach((function(e){return e.parent=g})),m["x-mode"]&&(g["x-mode"]=m["x-mode"]),n=g;break;case"doctype":throw new Error("Not implemented yet: ElementType.Doctype case")}return n.startIndex=e.startIndex,n.endIndex=e.endIndex,n}function b(e){for(var t=e.map((function(e){return m(e,!0)})),n=1;n<t.length;n++)t[n].prev=t[n-1],t[n-1].next=t[n];return t}n.Element=h,n.cloneNode=m})),o=e,i=r.Comment,a=r.Element,u=r.ProcessingInstruction,c=r.Text,s={},l=0,f=o.length;l<f;l++)n=o[l],s[n.toLowerCase()]=n;function d(e){for(var t,n={},r=0,o=e.length;r<o;r++)n[(t=e[r]).name]=t.value;return n}function p(e){var t=function(e){return s[e]}(e=e.toLowerCase());return t||e}var h=function e(t,n,r){n=n||null;for(var o=[],s=0,l=t.length;s<l;s++){var f,h=t[s];switch(h.nodeType){case 1:(f=new a(p(h.nodeName),d(h.attributes))).children=e(h.childNodes,f);break;case 3:f=new c(h.nodeValue);break;case 8:f=new i(h.nodeValue);break;default:continue}var m=o[s-1]||null;m&&(m.next=f),f.parent=n,f.prev=m,f.next=null,o.push(f)}return r&&((f=new u(r.substring(0,r.indexOf(" ")).toLowerCase(),r)).next=o[0]||null,f.parent=n,o.unshift(f),o[1]&&(o[1].prev=o[0])),o},m=function(){return/(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent)},b="html",g="head",v="body",y=/<([a-zA-Z]+[0-9]?)/,x=/<head.*>/i,w=/<body.*>/i,N=function(){throw new Error("This browser does not support `document.implementation.createHTMLDocument`")},T=function(){throw new Error("This browser does not support `DOMParser.prototype.parseFromString`")};if("function"==typeof window.DOMParser){var P=new window.DOMParser;N=T=function(e,t){return t&&(e="<"+t+">"+e+"</"+t+">"),P.parseFromString(e,"text/html")}}if(document.implementation){var O=m,E=document.implementation.createHTMLDocument(O()?"html-dom-parser":void 0);N=function(e,t){return t?(E.documentElement.getElementsByTagName(t)[0].innerHTML=e,E):(E.documentElement.innerHTML=e,E)}}var M,j=document.createElement("template");j.content&&(M=function(e){return j.innerHTML=e,j.content.childNodes});var C=function(e){var t,n,r,o,i=e.match(y);switch(i&&i[1]&&(t=i[1].toLowerCase()),t){case b:return n=T(e),x.test(e)||(r=n.getElementsByTagName(g)[0])&&r.parentNode.removeChild(r),w.test(e)||(r=n.getElementsByTagName(v)[0])&&r.parentNode.removeChild(r),n.getElementsByTagName(b);case g:case v:return o=N(e).getElementsByTagName(t),w.test(e)&&x.test(e)?o[0].parentNode.childNodes:o;default:return M?M(e):N(e,v).getElementsByTagName(v)[0].childNodes}},I=h,D=/<(![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(D);return n&&n[1]&&(t=n[1]),I(C(e),null,t)}}));
//# sourceMappingURL=html-dom-parser.min.js.map
// TypeScript Version: 4.1
export { default } from './lib/html-to-dom-server';
export { default } from './lib/server/html-to-dom';

@@ -7,2 +7,2 @@ /**

*/
module.exports = require('./lib/html-to-dom-server');
module.exports = require('./lib/server/html-to-dom');
{
"name": "html-dom-parser",
"version": "0.5.0",
"version": "1.0.0",
"description": "HTML to DOM parser.",

@@ -37,4 +37,4 @@ "author": "Mark <mark@remarkablemark.org>",

"dependencies": {
"domhandler": "3.3.0",
"htmlparser2": "4.1.0"
"domhandler": "4.0.0",
"htmlparser2": "6.0.0"
},

@@ -45,9 +45,10 @@ "devDependencies": {

"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"@size-limit/preset-big-lib": "^4.9.1",
"chai": "^4.2.0",
"dtslint": "^4.0.6",
"eslint": "^7.15.0",
"eslint-plugin-prettier": "^3.2.0",
"eslint": "^7.16.0",
"eslint-plugin-prettier": "^3.3.0",
"html-minifier": "^4.0.0",
"husky": "^4.3.5",
"husky": "^4.3.6",
"jsdomify": "^3.1.1",

@@ -66,5 +67,6 @@ "karma": "^5.2.3",

"prettier": "^2.2.1",
"rollup": "^2.34.2",
"rollup": "^2.35.1",
"rollup-plugin-terser": "^7.0.2",
"sinon": "^9.2.2",
"size-limit": "^4.9.1",
"standard-version": "^5",

@@ -81,5 +83,5 @@ "typescript": "^4.1.3",

"browser": {
"./index.js": "./lib/html-to-dom-client.js"
"./index.js": "./lib/client/html-to-dom.js"
},
"license": "MIT"
}

@@ -17,9 +17,9 @@ # html-dom-parser

It converts an HTML string to a JavaScript object that describes the DOM tree.
The parser converts an HTML string to a JavaScript object that describes the DOM tree.
#### Example:
#### Example
```js
var parse = require('html-dom-parser');
parse('<div>text</div>');
const parse = require('html-dom-parser');
parse('<p>Hello, World!</p>');
```

@@ -29,15 +29,26 @@

```
[ { type: 'tag',
name: 'div',
attribs: {},
children:
[ { data: 'text',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
```js
[
Element {
type: 'tag',
parent: null,
prev: null,
next: null,
prev: null,
parent: null } ]
startIndex: null,
endIndex: null,
children: [
Text {
type: 'text',
parent: [Circular],
prev: null,
next: null,
startIndex: null,
endIndex: null,
data: 'Hello, World!'
}
],
name: 'p',
attribs: {}
}
]
```

@@ -47,3 +58,3 @@

## Installation
## Install

@@ -65,3 +76,3 @@ [NPM](https://www.npmjs.com/package/html-dom-parser):

```html
<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.js"></script>
<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.min.js"></script>
<script>

@@ -74,16 +85,16 @@ window.HTMLDOMParser(/* string */);

Import the module:
Import or require the module:
```js
// CommonJS
var parse = require('html-dom-parser');
// ES Modules
import parse from 'html-dom-parser';
// CommonJS
const parse = require('html-dom-parser');
```
Parse markup:
Parse empty string:
```js
parse('<p class="primary" style="color: skyblue;">Hello world</p>');
parse('');
```

@@ -93,19 +104,54 @@

```js
[];
```
[ { type: 'tag',
name: 'p',
attribs: { class: 'primary', style: 'color: skyblue;' },
children:
[ { data: 'Hello world',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
Parse string:
```js
parse('Hello, World!');
```
```js
[
Text {
type: 'text',
parent: null,
prev: null,
next: null,
startIndex: null,
endIndex: null,
data: 'Hello, World!'
}
]
```
Parse element with attributes:
```js
parse('<p class="foo" style="color: #bada55">Hello, <em>world</em>!</p>');
```
Output:
```js
[
Element {
type: 'tag',
parent: null,
prev: null,
parent: null } ]
next: null,
startIndex: null,
endIndex: null,
children: [ [Text], [Element], [Text] ],
name: 'p',
attribs: { class: 'foo', style: 'color: #bada55' }
}
]
```
The _server parser_ is a wrapper of [htmlparser2](https://github.com/fb55/htmlparser2)'s `parseDOM`; the _client parser_ mimics the server parser by using the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) API.
The server parser is a wrapper of [htmlparser2](https://github.com/fb55/htmlparser2) `parseDOM` but with the root parent node excluded.
The client parser mimics the server parser by using the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) API to parse the HTML string.
## Testing

@@ -129,5 +175,3 @@

$ npm run lint
# fix lint errors
$ npm run lint:fix
# npm run lint:fix
```

@@ -134,0 +178,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