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.2.3 to 0.3.0

dist/html-dom-parser.js.map

19

CHANGELOG.md

@@ -1,8 +0,23 @@

# Changelog
# Change Log
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
### [0.2.3](https://github.com/remarkablemark/html-dom-parser/compare/v0.2.2...v0.2.3) (2019-11-04)
<a name="0.3.0"></a>
# [0.3.0](https://github.com/remarkablemark/html-dom-parser/compare/v0.2.3...v0.3.0) (2020-06-02)
### Features
* **lib:** throw error if browser does not support parsing methods ([de327af](https://github.com/remarkablemark/html-dom-parser/commit/de327af))
### Performance Improvements
* **lib:** return `[]` if empty string is passed to server parser ([9850d05](https://github.com/remarkablemark/html-dom-parser/commit/9850d05))
## [0.2.3](https://github.com/remarkablemark/html-dom-parser/compare/v0.2.2...v0.2.3) (2019-11-04)
### Bug Fixes

@@ -9,0 +24,0 @@

554

dist/html-dom-parser.js

@@ -1,144 +0,436 @@

(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["HTMLDOMParser"] = factory();
else
root["HTMLDOMParser"] = factory();
})(window, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./lib/html-to-dom-client.js");
/******/ })
/************************************************************************/
/******/ ({
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.HTMLDOMParser = factory());
}(this, (function () { 'use strict';
/***/ "./lib/constants.js":
/*!**************************!*\
!*** ./lib/constants.js ***!
\**************************/
/*! no static exports found */
/***/ (function(module, exports) {
/**
* SVG elements are case-sensitive.
*
* @see {@link https://developer.mozilla.org/docs/Web/SVG/Element#SVG_elements_A_to_Z}
*/
var 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'
];
eval("/**\n * SVG elements are case-sensitive.\n *\n * @see {@link https://developer.mozilla.org/docs/Web/SVG/Element#SVG_elements_A_to_Z}\n */\nvar CASE_SENSITIVE_TAG_NAMES = [\n 'animateMotion',\n 'animateTransform',\n 'clipPath',\n 'feBlend',\n 'feColorMatrix',\n 'feComponentTransfer',\n 'feComposite',\n 'feConvolveMatrix',\n 'feDiffuseLighting',\n 'feDisplacementMap',\n 'feDropShadow',\n 'feFlood',\n 'feFuncA',\n 'feFuncB',\n 'feFuncG',\n 'feFuncR',\n 'feGaussainBlur',\n 'feImage',\n 'feMerge',\n 'feMergeNode',\n 'feMorphology',\n 'feOffset',\n 'fePointLight',\n 'feSpecularLighting',\n 'feSpotLight',\n 'feTile',\n 'feTurbulence',\n 'foreignObject',\n 'linearGradient',\n 'radialGradient',\n 'textPath'\n];\n\nmodule.exports = {\n CASE_SENSITIVE_TAG_NAMES: CASE_SENSITIVE_TAG_NAMES\n};\n\n\n//# sourceURL=webpack://HTMLDOMParser/./lib/constants.js?");
var constants = {
CASE_SENSITIVE_TAG_NAMES: CASE_SENSITIVE_TAG_NAMES
};
/***/ }),
var CASE_SENSITIVE_TAG_NAMES$1 = constants.CASE_SENSITIVE_TAG_NAMES;
/***/ "./lib/domparser.js":
/*!**************************!*\
!*** ./lib/domparser.js ***!
\**************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
var caseSensitiveTagNamesMap = {};
var tagName;
for (var i = 0, len = CASE_SENSITIVE_TAG_NAMES$1.length; i < len; i++) {
tagName = CASE_SENSITIVE_TAG_NAMES$1[i];
caseSensitiveTagNamesMap[tagName.toLowerCase()] = tagName;
}
eval("var utilities = __webpack_require__(/*! ./utilities */ \"./lib/utilities.js\");\n\n// constants\nvar HTML = 'html';\nvar HEAD = 'head';\nvar BODY = 'body';\nvar FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>\nvar HEAD_TAG_REGEX = /<head.*>/i;\nvar BODY_TAG_REGEX = /<body.*>/i;\n// http://www.w3.org/TR/html/syntax.html#void-elements\nvar VOID_ELEMENTS_REGEX = /<(area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)(.*?)\\/?>/gi;\n\n// detect IE browser\nvar isIE9 = utilities.isIE(9);\nvar isIE = isIE9 || utilities.isIE();\n\n/**\n * DOMParser (performance: slow).\n *\n * @see https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document\n */\nvar parseFromString;\n\nif (typeof window.DOMParser === 'function') {\n var domParser = new window.DOMParser();\n\n // IE9 does not support 'text/html' MIME type\n // https://msdn.microsoft.com/en-us/library/ff975278(v=vs.85).aspx\n var mimeType = isIE9 ? 'text/xml' : 'text/html';\n\n /**\n * Creates an HTML document using `DOMParser.parseFromString`.\n *\n * @param {string} html - The HTML string.\n * @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).\n * @return {HTMLDocument}\n */\n parseFromString = function domStringParser(html, tagName) {\n if (tagName) {\n html = '<' + tagName + '>' + html + '</' + tagName + '>';\n }\n\n // because IE9 only supports MIME type 'text/xml', void elements need to be self-closed\n if (isIE9) {\n html = html.replace(VOID_ELEMENTS_REGEX, '<$1$2$3/>');\n }\n\n return domParser.parseFromString(html, mimeType);\n };\n}\n\n/**\n * DOMImplementation (performance: fair).\n *\n * @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument\n */\nvar parseFromDocument;\n\nif (typeof document.implementation === 'object') {\n // title parameter is required in IE\n // https://msdn.microsoft.com/en-us/library/ff975457(v=vs.85).aspx\n var doc = document.implementation.createHTMLDocument(\n isIE ? 'HTML_DOM_PARSER_TITLE' : undefined\n );\n\n /**\n * Use HTML document created by `document.implementation.createHTMLDocument`.\n *\n * @param {string} html - The HTML string.\n * @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).\n * @return {HTMLDocument}\n */\n parseFromDocument = function createHTMLDocument(html, tagName) {\n if (tagName) {\n doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html;\n return doc;\n }\n\n try {\n doc.documentElement.innerHTML = html;\n return doc;\n // fallback when certain elements in `documentElement` are read-only (IE9)\n } catch (err) {\n if (parseFromString) {\n return parseFromString(html);\n }\n }\n };\n}\n\n/**\n * Template (performance: fast).\n *\n * @see https://developer.mozilla.org/docs/Web/HTML/Element/template\n */\nvar parseFromTemplate;\nvar template = document.createElement('template');\n\nif (template.content) {\n /**\n * Uses a template element (content fragment) to parse HTML.\n *\n * @param {string} html - The HTML string.\n * @return {NodeList}\n */\n parseFromTemplate = function templateParser(html) {\n template.innerHTML = html;\n return template.content.childNodes;\n };\n}\n\n// fallback document parser\nvar parseWithFallback = parseFromDocument || parseFromString;\n\n/**\n * Parses HTML string to DOM nodes.\n *\n * @param {string} html - The HTML string.\n * @return {NodeList|Array}\n */\nfunction domparser(html) {\n var firstTagName;\n var match = html.match(FIRST_TAG_REGEX);\n\n if (match && match[1]) {\n firstTagName = match[1].toLowerCase();\n }\n\n var doc;\n var element;\n var elements;\n\n switch (firstTagName) {\n case HTML:\n if (parseFromString) {\n doc = parseFromString(html);\n\n // the created document may come with filler head/body elements,\n // so make sure to remove them if they don't actually exist\n if (!HEAD_TAG_REGEX.test(html)) {\n element = doc.getElementsByTagName(HEAD)[0];\n if (element) {\n element.parentNode.removeChild(element);\n }\n }\n\n if (!BODY_TAG_REGEX.test(html)) {\n element = doc.getElementsByTagName(BODY)[0];\n if (element) {\n element.parentNode.removeChild(element);\n }\n }\n\n return doc.getElementsByTagName(HTML);\n }\n break;\n\n case HEAD:\n case BODY:\n if (parseWithFallback) {\n elements = parseWithFallback(html).getElementsByTagName(firstTagName);\n\n // account for possibility of sibling\n if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {\n return elements[0].parentNode.childNodes;\n }\n\n return elements;\n }\n break;\n\n // low-level tag or text\n default:\n if (parseFromTemplate) {\n return parseFromTemplate(html);\n }\n\n if (parseWithFallback) {\n return parseWithFallback(html, BODY).getElementsByTagName(BODY)[0]\n .childNodes;\n }\n\n break;\n }\n\n return [];\n}\n\nmodule.exports = domparser;\n\n\n//# sourceURL=webpack://HTMLDOMParser/./lib/domparser.js?");
/**
* Gets case-sensitive tag name.
*
* @param {String} tagName - The lowercase tag name.
* @return {String|undefined}
*/
function getCaseSensitiveTagName(tagName) {
return caseSensitiveTagNamesMap[tagName];
}
/***/ }),
/**
* Formats DOM attributes to a hash map.
*
* @param {NamedNodeMap} attributes - The list of attributes.
* @return {Object} - A map of attribute name to value.
*/
function formatAttributes(attributes) {
var result = {};
var attribute;
// `NamedNodeMap` is array-like
for (var i = 0, len = attributes.length; i < len; i++) {
attribute = attributes[i];
result[attribute.name] = attribute.value;
}
return result;
}
/***/ "./lib/html-to-dom-client.js":
/*!***********************************!*\
!*** ./lib/html-to-dom-client.js ***!
\***********************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
/**
* Corrects the tag name if it is case-sensitive (SVG).
* Otherwise, returns the lowercase tag name (HTML).
*
* @param {String} tagName - The lowercase tag name.
* @return {String} - The formatted tag name.
*/
function formatTagName(tagName) {
tagName = tagName.toLowerCase();
var caseSensitiveTagName = getCaseSensitiveTagName(tagName);
if (caseSensitiveTagName) {
return caseSensitiveTagName;
}
return tagName;
}
eval("var domparser = __webpack_require__(/*! ./domparser */ \"./lib/domparser.js\");\nvar utilities = __webpack_require__(/*! ./utilities */ \"./lib/utilities.js\");\n\nvar formatDOM = utilities.formatDOM;\nvar isIE9 = utilities.isIE(9);\n\nvar DIRECTIVE_REGEX = /<(![a-zA-Z\\s]+)>/; // e.g., <!doctype html>\n\n/**\n * Parses HTML and reformats DOM nodes output.\n *\n * @param {String} html - The HTML string.\n * @return {Array} - The formatted DOM nodes.\n */\nfunction parseDOM(html) {\n if (typeof html !== 'string') {\n throw new TypeError('First argument must be a string');\n }\n\n if (!html) {\n return [];\n }\n\n // match directive\n var match = html.match(DIRECTIVE_REGEX);\n var directive;\n\n if (match && match[1]) {\n directive = match[1];\n\n // remove directive in IE9 because DOMParser uses\n // MIME type 'text/xml' instead of 'text/html'\n if (isIE9) {\n html = html.replace(match[0], '');\n }\n }\n\n return formatDOM(domparser(html), null, directive);\n}\n\nmodule.exports = parseDOM;\n\n\n//# sourceURL=webpack://HTMLDOMParser/./lib/html-to-dom-client.js?");
/**
* Formats the browser DOM nodes to mimic the output of `htmlparser2.parseDOM()`.
*
* @param {NodeList} nodes - The DOM nodes.
* @param {Object} [parentObj] - The formatted parent node.
* @param {String} [directive] - The directive.
* @return {Object[]} - The formatted DOM object.
*/
function formatDOM(nodes, parentObj, directive) {
parentObj = parentObj || null;
/***/ }),
var result = [];
var node;
var prevNode;
var nodeObj;
/***/ "./lib/utilities.js":
/*!**************************!*\
!*** ./lib/utilities.js ***!
\**************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
// `NodeList` is array-like
for (var i = 0, len = nodes.length; i < len; i++) {
node = nodes[i];
// reset
nodeObj = {
next: null,
prev: result[i - 1] || null,
parent: parentObj
};
eval("var CASE_SENSITIVE_TAG_NAMES = __webpack_require__(/*! ./constants */ \"./lib/constants.js\").CASE_SENSITIVE_TAG_NAMES;\n\nvar caseSensitiveTagNamesMap = {};\nvar tagName;\nfor (var i = 0, len = CASE_SENSITIVE_TAG_NAMES.length; i < len; i++) {\n tagName = CASE_SENSITIVE_TAG_NAMES[i];\n caseSensitiveTagNamesMap[tagName.toLowerCase()] = tagName;\n}\n\n/**\n * Gets case-sensitive tag name.\n *\n * @param {String} tagName - The lowercase tag name.\n * @return {String|undefined}\n */\nfunction getCaseSensitiveTagName(tagName) {\n return caseSensitiveTagNamesMap[tagName];\n}\n\n/**\n * Formats DOM attributes to a hash map.\n *\n * @param {NamedNodeMap} attributes - The list of attributes.\n * @return {Object} - A map of attribute name to value.\n */\nfunction formatAttributes(attributes) {\n var result = {};\n var attribute;\n // `NamedNodeMap` is array-like\n for (var i = 0, len = attributes.length; i < len; i++) {\n attribute = attributes[i];\n result[attribute.name] = attribute.value;\n }\n return result;\n}\n\n/**\n * Corrects the tag name if it is case-sensitive (SVG).\n * Otherwise, returns the lowercase tag name (HTML).\n *\n * @param {String} tagName - The lowercase tag name.\n * @return {String} - The formatted tag name.\n */\nfunction formatTagName(tagName) {\n tagName = tagName.toLowerCase();\n var caseSensitiveTagName = getCaseSensitiveTagName(tagName);\n if (caseSensitiveTagName) {\n return caseSensitiveTagName;\n }\n return tagName;\n}\n\n/**\n * Formats the browser DOM nodes to mimic the output of `htmlparser2.parseDOM()`.\n *\n * @param {NodeList} nodes - The DOM nodes.\n * @param {Object} [parentObj] - The formatted parent node.\n * @param {String} [directive] - The directive.\n * @return {Object[]} - The formatted DOM object.\n */\nfunction formatDOM(nodes, parentObj, directive) {\n parentObj = parentObj || null;\n\n var result = [];\n var node;\n var prevNode;\n var nodeObj;\n\n // `NodeList` is array-like\n for (var i = 0, len = nodes.length; i < len; i++) {\n node = nodes[i];\n // reset\n nodeObj = {\n next: null,\n prev: result[i - 1] || null,\n parent: parentObj\n };\n\n // set the next node for the previous node (if applicable)\n prevNode = result[i - 1];\n if (prevNode) {\n prevNode.next = nodeObj;\n }\n\n // set the node name if it's not \"#text\" or \"#comment\"\n // e.g., \"div\"\n if (node.nodeName[0] !== '#') {\n nodeObj.name = formatTagName(node.nodeName);\n // also, nodes of type \"tag\" have \"attribs\"\n nodeObj.attribs = {}; // default\n if (node.attributes && node.attributes.length) {\n nodeObj.attribs = formatAttributes(node.attributes);\n }\n }\n\n // set the node type\n // e.g., \"tag\"\n switch (node.nodeType) {\n // 1 = element\n case 1:\n if (nodeObj.name === 'script' || nodeObj.name === 'style') {\n nodeObj.type = nodeObj.name;\n } else {\n nodeObj.type = 'tag';\n }\n // recursively format the children\n nodeObj.children = formatDOM(node.childNodes, nodeObj);\n break;\n // 2 = attribute\n // 3 = text\n case 3:\n nodeObj.type = 'text';\n nodeObj.data = node.nodeValue;\n break;\n // 8 = comment\n case 8:\n nodeObj.type = 'comment';\n nodeObj.data = node.nodeValue;\n break;\n }\n\n result.push(nodeObj);\n }\n\n if (directive) {\n result.unshift({\n name: directive.substring(0, directive.indexOf(' ')).toLowerCase(),\n data: directive,\n type: 'directive',\n next: result[0] ? result[0] : null,\n prev: null,\n parent: parentObj\n });\n\n if (result[1]) {\n result[1].prev = result[0];\n }\n }\n\n return result;\n}\n\n/**\n * Detects IE with or without version.\n *\n * @param {Number} [version] - The IE version to detect.\n * @return {Boolean} - Whether IE or the version has been detected.\n */\nfunction isIE(version) {\n if (version) {\n return document.documentMode === version;\n }\n return /(MSIE |Trident\\/|Edge\\/)/.test(navigator.userAgent);\n}\n\nmodule.exports = {\n formatAttributes: formatAttributes,\n formatDOM: formatDOM,\n isIE: isIE\n};\n\n\n//# sourceURL=webpack://HTMLDOMParser/./lib/utilities.js?");
// set the next node for the previous node (if applicable)
prevNode = result[i - 1];
if (prevNode) {
prevNode.next = nodeObj;
}
/***/ })
// set the node name if it's not "#text" or "#comment"
// e.g., "div"
if (node.nodeName[0] !== '#') {
nodeObj.name = formatTagName(node.nodeName);
// also, nodes of type "tag" have "attribs"
nodeObj.attribs = {}; // default
if (node.attributes && node.attributes.length) {
nodeObj.attribs = formatAttributes(node.attributes);
}
}
/******/ });
});
// set the node type
// e.g., "tag"
switch (node.nodeType) {
// 1 = element
case 1:
if (nodeObj.name === 'script' || nodeObj.name === 'style') {
nodeObj.type = nodeObj.name;
} else {
nodeObj.type = 'tag';
}
// recursively format the children
nodeObj.children = formatDOM(node.childNodes, nodeObj);
break;
// 2 = attribute
// 3 = text
case 3:
nodeObj.type = 'text';
nodeObj.data = node.nodeValue;
break;
// 8 = comment
case 8:
nodeObj.type = 'comment';
nodeObj.data = node.nodeValue;
break;
}
result.push(nodeObj);
}
if (directive) {
result.unshift({
name: directive.substring(0, directive.indexOf(' ')).toLowerCase(),
data: directive,
type: 'directive',
next: result[0] ? result[0] : null,
prev: null,
parent: parentObj
});
if (result[1]) {
result[1].prev = result[0];
}
}
return result;
}
/**
* Detects IE with or without version.
*
* @param {Number} [version] - The IE version to detect.
* @return {Boolean} - Whether IE or the version has been detected.
*/
function isIE(version) {
if (version) {
return document.documentMode === version;
}
return /(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent);
}
var utilities = {
formatAttributes: formatAttributes,
formatDOM: formatDOM,
isIE: isIE
};
// constants
var HTML = 'html';
var HEAD = 'head';
var BODY = 'body';
var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
var HEAD_TAG_REGEX = /<head.*>/i;
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
var parseFromDocument = function () {
throw new Error(
'This browser does not support `document.implementation.createHTMLDocument`'
);
};
var parseFromString = function () {
throw new Error(
'This browser does not support `DOMParser.prototype.parseFromString`'
);
};
/**
* DOMParser (performance: slow).
*
* @see https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document
*/
if (typeof window.DOMParser === 'function') {
var domParser = new window.DOMParser();
// 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';
/**
* Creates an HTML document using `DOMParser.parseFromString`.
*
* @param {string} html - The HTML string.
* @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).
* @return {HTMLDocument}
*/
parseFromString = function (html, tagName) {
if (tagName) {
html = '<' + tagName + '>' + html + '</' + tagName + '>';
}
// 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);
};
parseFromDocument = parseFromString;
}
/**
* DOMImplementation (performance: fair).
*
* @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument
*/
if (document.implementation) {
// 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
);
/**
* Use HTML document created by `document.implementation.createHTMLDocument`.
*
* @param {string} html - The HTML string.
* @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).
* @return {HTMLDocument}
*/
parseFromDocument = function (html, tagName) {
if (tagName) {
doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html;
return doc;
}
try {
doc.documentElement.innerHTML = html;
return doc;
// fallback when certain elements in `documentElement` are read-only (IE9)
} catch (err) {
if (parseFromString) {
return parseFromString(html);
}
}
};
}
/**
* Template (performance: fast).
*
* @see https://developer.mozilla.org/docs/Web/HTML/Element/template
*/
var template = document.createElement('template');
var parseFromTemplate;
if (template.content) {
/**
* Uses a template element (content fragment) to parse HTML.
*
* @param {string} html - The HTML string.
* @return {NodeList}
*/
parseFromTemplate = function (html) {
template.innerHTML = html;
return template.content.childNodes;
};
}
/**
* Parses HTML string to DOM nodes.
*
* @param {string} html - The HTML string.
* @return {NodeList|Array}
*/
function domparser(html) {
var firstTagName;
var match = html.match(FIRST_TAG_REGEX);
if (match && match[1]) {
firstTagName = match[1].toLowerCase();
}
var doc;
var element;
var elements;
switch (firstTagName) {
case HTML:
doc = parseFromString(html);
// the created document may come with filler head/body elements,
// so make sure to remove them if they don't actually exist
if (!HEAD_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(HEAD)[0];
if (element) {
element.parentNode.removeChild(element);
}
}
if (!BODY_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(BODY)[0];
if (element) {
element.parentNode.removeChild(element);
}
}
return doc.getElementsByTagName(HTML);
case HEAD:
case BODY:
elements = parseFromDocument(html).getElementsByTagName(firstTagName);
// if there's a sibling element, then return both elements
if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
return elements[0].parentNode.childNodes;
}
return elements;
// low-level tag or text
default:
if (parseFromTemplate) {
return parseFromTemplate(html);
}
return parseFromDocument(html, BODY).getElementsByTagName(BODY)[0]
.childNodes;
}
}
var domparser_1 = domparser;
var formatDOM$1 = utilities.formatDOM;
var isIE9$1 = utilities.isIE(9);
var DIRECTIVE_REGEX = /<(![a-zA-Z\s]+)>/; // e.g., <!doctype html>
/**
* Parses HTML and reformats DOM nodes output.
*
* @param {String} html - The HTML string.
* @return {Array} - The formatted DOM nodes.
*/
function parseDOM(html) {
if (typeof html !== 'string') {
throw new TypeError('First argument must be a string');
}
if (!html) {
return [];
}
// match directive
var match = html.match(DIRECTIVE_REGEX);
var directive;
if (match && match[1]) {
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], '');
}
}
return formatDOM$1(domparser_1(html), null, directive);
}
var htmlToDomClient = parseDOM;
return htmlToDomClient;
})));
//# sourceMappingURL=html-dom-parser.js.map

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

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.HTMLDOMParser=t():e.HTMLDOMParser=t()}(window,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){for(var r,o=n(3).CASE_SENSITIVE_TAG_NAMES,a={},i=0,u=o.length;i<u;i++)r=o[i],a[r.toLowerCase()]=r;function f(e){for(var t,n={},r=0,o=e.length;r<o;r++)n[(t=e[r]).name]=t.value;return n}function c(e){var t=function(e){return a[e]}(e=e.toLowerCase());return t||e}e.exports={formatAttributes:f,formatDOM:function e(t,n,r){n=n||null;for(var o,a,i,u=[],l=0,s=t.length;l<s;l++){switch(o=t[l],i={next:null,prev:u[l-1]||null,parent:n},(a=u[l-1])&&(a.next=i),"#"!==o.nodeName[0]&&(i.name=c(o.nodeName),i.attribs={},o.attributes&&o.attributes.length&&(i.attribs=f(o.attributes))),o.nodeType){case 1:"script"===i.name||"style"===i.name?i.type=i.name:i.type="tag",i.children=e(o.childNodes,i);break;case 3:i.type="text",i.data=o.nodeValue;break;case 8:i.type="comment",i.data=o.nodeValue}u.push(i)}return r&&(u.unshift({name:r.substring(0,r.indexOf(" ")).toLowerCase(),data:r,type:"directive",next:u[0]?u[0]:null,prev:null,parent:n}),u[1]&&(u[1].prev=u[0])),u},isIE:function(e){return e?document.documentMode===e:/(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent)}}},function(e,t,n){var r=n(2),o=n(0),a=o.formatDOM,i=o.isIE(9),u=/<(![a-zA-Z\s]+)>/;e.exports=function(e){if("string"!=typeof e)throw new TypeError("First argument must be a string");if(!e)return[];var t,n=e.match(u);return n&&n[1]&&(t=n[1],i&&(e=e.replace(n[0],""))),a(r(e),null,t)}},function(e,t,n){var r,o,a,i=n(0),u="html",f="head",c="body",l=/<([a-zA-Z]+[0-9]?)/,s=/<head.*>/i,d=/<body.*>/i,m=/<(area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)(.*?)\/?>/gi,p=i.isIE(9),g=p||i.isIE();if("function"==typeof window.DOMParser){var y=new window.DOMParser,b=p?"text/xml":"text/html";r=function(e,t){return t&&(e="<"+t+">"+e+"</"+t+">"),p&&(e=e.replace(m,"<$1$2$3/>")),y.parseFromString(e,b)}}if("object"==typeof document.implementation){var h=document.implementation.createHTMLDocument(g?"HTML_DOM_PARSER_TITLE":void 0);o=function(e,t){if(t)return h.documentElement.getElementsByTagName(t)[0].innerHTML=e,h;try{return h.documentElement.innerHTML=e,h}catch(t){if(r)return r(e)}}}var v=document.createElement("template");v.content&&(a=function(e){return v.innerHTML=e,v.content.childNodes});var M=o||r;e.exports=function(e){var t,n,o,i,m=e.match(l);switch(m&&m[1]&&(t=m[1].toLowerCase()),t){case u:if(r)return n=r(e),s.test(e)||(o=n.getElementsByTagName(f)[0])&&o.parentNode.removeChild(o),d.test(e)||(o=n.getElementsByTagName(c)[0])&&o.parentNode.removeChild(o),n.getElementsByTagName(u);break;case f:case c:if(M)return i=M(e).getElementsByTagName(t),d.test(e)&&s.test(e)?i[0].parentNode.childNodes:i;break;default:if(a)return a(e);if(M)return M(e,c).getElementsByTagName(c)[0].childNodes}return[]}},function(e,t){e.exports={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"]}}])}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).HTMLDOMParser=t()}(this,function(){"use strict";for(var e,t=["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"],n={},r=0,a=t.length;r<a;r++)n[(e=t[r]).toLowerCase()]=e;function f(e){for(var t,n={},r=0,a=e.length;r<a;r++)n[(t=e[r]).name]=t.value;return n}function c(e){e=e.toLowerCase();var t=n[e];return t||e}var o,i,s,u={formatAttributes:f,formatDOM:function e(t,n,r){n=n||null;for(var a,o,i,s=[],u=0,m=t.length;u<m;u++){switch(a=t[u],i={next:null,prev:s[u-1]||null,parent:n},(o=s[u-1])&&(o.next=i),"#"!==a.nodeName[0]&&(i.name=c(a.nodeName),i.attribs={},a.attributes&&a.attributes.length&&(i.attribs=f(a.attributes))),a.nodeType){case 1:"script"===i.name||"style"===i.name?i.type=i.name:i.type="tag",i.children=e(a.childNodes,i);break;case 3:i.type="text",i.data=a.nodeValue;break;case 8:i.type="comment",i.data=a.nodeValue}s.push(i)}return r&&(s.unshift({name:r.substring(0,r.indexOf(" ")).toLowerCase(),data:r,type:"directive",next:s[0]?s[0]:null,prev:null,parent:n}),s[1]&&(s[1].prev=s[0])),s},isIE:function(e){return e?document.documentMode===e:/(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent)}},m="html",l="head",d="body",p=/<([a-zA-Z]+[0-9]?)/,g=/<head.*>/i,h=/<body.*>/i,y=/<(area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)(.*?)\/?>/gi,b=u.isIE(9),w=b||u.isIE(),M=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`")};"function"==typeof window.DOMParser&&(o=new window.DOMParser,i=b?"text/xml":"text/html",M=T=function(e,t){return t&&(e="<"+t+">"+e+"</"+t+">"),b&&(e=e.replace(y,"<$1$2$3/>")),o.parseFromString(e,i)}),document.implementation&&(s=document.implementation.createHTMLDocument(w?"html-dom-parser":void 0),M=function(t,e){if(e)return s.documentElement.getElementsByTagName(e)[0].innerHTML=t,s;try{return s.documentElement.innerHTML=t,s}catch(e){if(T)return T(t)}});var v,E=document.createElement("template");E.content&&(v=function(e){return E.innerHTML=e,E.content.childNodes});var N=function(e){var t,n,r,a,o=e.match(p);switch(o&&o[1]&&(t=o[1].toLowerCase()),t){case m:return n=T(e),g.test(e)||(r=n.getElementsByTagName(l)[0])&&r.parentNode.removeChild(r),h.test(e)||(r=n.getElementsByTagName(d)[0])&&r.parentNode.removeChild(r),n.getElementsByTagName(m);case l:case d:return a=M(e).getElementsByTagName(t),h.test(e)&&g.test(e)?a[0].parentNode.childNodes:a;default:return v?v(e):M(e,d).getElementsByTagName(d)[0].childNodes}},L=u.formatDOM,x=u.isIE(9),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],x&&(e=e.replace(n[0],""))),L(N(e),null,t)}});
//# sourceMappingURL=html-dom-parser.min.js.map

@@ -17,2 +17,15 @@ var utilities = require('./utilities');

// falls back to `parseFromString` if `createHTMLDocument` cannot be used
var parseFromDocument = function () {
throw new Error(
'This browser does not support `document.implementation.createHTMLDocument`'
);
};
var parseFromString = function () {
throw new Error(
'This browser does not support `DOMParser.prototype.parseFromString`'
);
};
/**

@@ -23,4 +36,2 @@ * DOMParser (performance: slow).

*/
var parseFromString;
if (typeof window.DOMParser === 'function') {

@@ -40,3 +51,3 @@ var domParser = new window.DOMParser();

*/
parseFromString = function domStringParser(html, tagName) {
parseFromString = function (html, tagName) {
if (tagName) {

@@ -53,2 +64,4 @@ html = '<' + tagName + '>' + html + '</' + tagName + '>';

};
parseFromDocument = parseFromString;
}

@@ -61,9 +74,7 @@

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

@@ -78,3 +89,3 @@

*/
parseFromDocument = function createHTMLDocument(html, tagName) {
parseFromDocument = function (html, tagName) {
if (tagName) {

@@ -102,4 +113,4 @@ doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html;

*/
var template = document.createElement('template');
var parseFromTemplate;
var template = document.createElement('template');

@@ -113,3 +124,3 @@ if (template.content) {

*/
parseFromTemplate = function templateParser(html) {
parseFromTemplate = function (html) {
template.innerHTML = html;

@@ -120,5 +131,2 @@ return template.content.childNodes;

// fallback document parser
var parseWithFallback = parseFromDocument || parseFromString;
/**

@@ -144,38 +152,31 @@ * Parses HTML string to DOM nodes.

case HTML:
if (parseFromString) {
doc = parseFromString(html);
doc = parseFromString(html);
// the created document may come with filler head/body elements,
// so make sure to remove them if they don't actually exist
if (!HEAD_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(HEAD)[0];
if (element) {
element.parentNode.removeChild(element);
}
// the created document may come with filler head/body elements,
// so make sure to remove them if they don't actually exist
if (!HEAD_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(HEAD)[0];
if (element) {
element.parentNode.removeChild(element);
}
}
if (!BODY_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(BODY)[0];
if (element) {
element.parentNode.removeChild(element);
}
if (!BODY_TAG_REGEX.test(html)) {
element = doc.getElementsByTagName(BODY)[0];
if (element) {
element.parentNode.removeChild(element);
}
return doc.getElementsByTagName(HTML);
}
break;
return doc.getElementsByTagName(HTML);
case HEAD:
case BODY:
if (parseWithFallback) {
elements = parseWithFallback(html).getElementsByTagName(firstTagName);
elements = parseFromDocument(html).getElementsByTagName(firstTagName);
// account for possibility of sibling
if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
return elements[0].parentNode.childNodes;
}
return elements;
// if there's a sibling element, then return both elements
if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
return elements[0].parentNode.childNodes;
}
break;
return elements;

@@ -188,13 +189,7 @@ // low-level tag or text

if (parseWithFallback) {
return parseWithFallback(html, BODY).getElementsByTagName(BODY)[0]
.childNodes;
}
break;
return parseFromDocument(html, BODY).getElementsByTagName(BODY)[0]
.childNodes;
}
return [];
}
module.exports = domparser;

@@ -18,2 +18,7 @@ var Parser = require('htmlparser2/lib/Parser');

}
if (!html) {
return [];
}
var handler = new DomHandler(options);

@@ -20,0 +25,0 @@ new Parser(handler, options).end(html);

{
"name": "html-dom-parser",
"version": "0.2.3",
"version": "0.3.0",
"description": "HTML to DOM parser.",

@@ -8,11 +8,8 @@ "author": "Mark <mark@remarkablemark.org>",

"scripts": {
"build": "run-s build:*",
"build:min": "webpack index.js -o dist/html-dom-parser.min.js --mode production --output-library HTMLDOMParser --output-library-target umd",
"build:unmin": "webpack index.js -o dist/html-dom-parser.js --mode development --output-library HTMLDOMParser --output-library-target umd",
"build": "rollup --config",
"clean": "rm -rf dist",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"dtslint": "dtslint .",
"lint": "eslint . --ignore-path .gitignore",
"lint:dts": "dtslint .",
"lint:fix": "npm run lint -- --fix",
"prepublishOnly": "run-s lint dtslint test clean build",
"prepublishOnly": "run-s lint lint:dts test clean build",
"release": "standard-version --no-verify",

@@ -51,25 +48,29 @@ "test": "run-s test:server test:client",

"@commitlint/config-conventional": "^8.2.0",
"@rollup/plugin-commonjs": "^12.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"chai": "^4.2.0",
"coveralls": "^3.0.7",
"dtslint": "^1.0.2",
"eslint": "^6.6.0",
"dtslint": "^3.6.9",
"eslint": "^7.1.0",
"eslint-plugin-prettier": "^3.1.1",
"html-minifier": "^4.0.0",
"husky": "^3.0.9",
"husky": "^4.2.5",
"jsdomify": "^3.1.1",
"karma": "^4.4.1",
"karma": "^5.0.9",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-commonjs": "^1.0.0",
"karma-mocha": "^1.3.0",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-phantomjs-launcher": "^1.0.4",
"lint-staged": "^9.4.2",
"mocha": "^6.2.2",
"lint-staged": "^10.2.7",
"mocha": "^7.2.0",
"mock-require": "^3.0.3",
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"sinon": "^7.5.0",
"standard-version": "^6",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"rollup": "^2.12.1",
"rollup-plugin-uglify": "^6.0.4",
"sinon": "^9.0.2",
"standard-version": "^5",
"typescript": "^3.9.3",
"webpack": "^4.41.2",

@@ -76,0 +77,0 @@ "webpack-cli": "^3.3.10"

@@ -140,3 +140,3 @@ # html-dom-parser

```sh
$ npm run dtslint
$ npm run lint:dts
```

@@ -143,0 +143,0 @@

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