html-react-parser 1.4.14 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1543,58 +1543,176 @@
1543
1543
 
1544
1544
  var domToReact_1 = domToReact$1;
1545
1545
 
1546
+ // constants
1547
+ var HTML = 'html';
1548
+ var HEAD = 'head';
1549
+ var BODY = 'body';
1550
+ var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
1551
+ // match-all-characters in case of newlines (DOTALL)
1552
+ var HEAD_TAG_REGEX = /<head[^]*>/i;
1553
+ var BODY_TAG_REGEX = /<body[^]*>/i;
1554
+
1555
+ // falls back to `parseFromString` if `createHTMLDocument` cannot be used
1556
+ var parseFromDocument = function () {
1557
+ throw new Error(
1558
+ 'This browser does not support `document.implementation.createHTMLDocument`'
1559
+ );
1560
+ };
1561
+
1562
+ var parseFromString = function () {
1563
+ throw new Error(
1564
+ 'This browser does not support `DOMParser.prototype.parseFromString`'
1565
+ );
1566
+ };
1567
+
1546
1568
  /**
1547
- * SVG elements are case-sensitive.
1569
+ * DOMParser (performance: slow).
1548
1570
  *
1549
- * @see {@link https://developer.mozilla.org/docs/Web/SVG/Element#SVG_elements_A_to_Z}
1571
+ * @see https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document
1550
1572
  */
1573
+ if (typeof window.DOMParser === 'function') {
1574
+ var domParser = new window.DOMParser();
1575
+ var mimeType = 'text/html';
1551
1576
 
1552
- var CASE_SENSITIVE_TAG_NAMES$1 = [
1553
- 'animateMotion',
1554
- 'animateTransform',
1555
- 'clipPath',
1556
- 'feBlend',
1557
- 'feColorMatrix',
1558
- 'feComponentTransfer',
1559
- 'feComposite',
1560
- 'feConvolveMatrix',
1561
- 'feDiffuseLighting',
1562
- 'feDisplacementMap',
1563
- 'feDropShadow',
1564
- 'feFlood',
1565
- 'feFuncA',
1566
- 'feFuncB',
1567
- 'feFuncG',
1568
- 'feFuncR',
1569
- 'feGaussainBlur',
1570
- 'feImage',
1571
- 'feMerge',
1572
- 'feMergeNode',
1573
- 'feMorphology',
1574
- 'feOffset',
1575
- 'fePointLight',
1576
- 'feSpecularLighting',
1577
- 'feSpotLight',
1578
- 'feTile',
1579
- 'feTurbulence',
1580
- 'foreignObject',
1581
- 'linearGradient',
1582
- 'radialGradient',
1583
- 'textPath'
1584
- ];
1577
+ /**
1578
+ * Creates an HTML document using `DOMParser.parseFromString`.
1579
+ *
1580
+ * @param {string} html - The HTML string.
1581
+ * @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).
1582
+ * @return {HTMLDocument}
1583
+ */
1584
+ parseFromString = function (html, tagName) {
1585
+ if (tagName) {
1586
+ html = '<' + tagName + '>' + html + '</' + tagName + '>';
1587
+ }
1585
1588
 
1586
- var constants$1 = {
1587
- CASE_SENSITIVE_TAG_NAMES: CASE_SENSITIVE_TAG_NAMES$1
1588
- };
1589
+ return domParser.parseFromString(html, mimeType);
1590
+ };
1589
1591
 
1590
- var node = {};
1592
+ parseFromDocument = parseFromString;
1593
+ }
1594
+
1595
+ /**
1596
+ * DOMImplementation (performance: fair).
1597
+ *
1598
+ * @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument
1599
+ */
1600
+ if (document.implementation) {
1601
+ var doc = document.implementation.createHTMLDocument();
1602
+
1603
+ /**
1604
+ * Use HTML document created by `document.implementation.createHTMLDocument`.
1605
+ *
1606
+ * @param {string} html - The HTML string.
1607
+ * @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).
1608
+ * @return {HTMLDocument}
1609
+ */
1610
+ parseFromDocument = function (html, tagName) {
1611
+ if (tagName) {
1612
+ var element = doc.documentElement.querySelector(tagName);
1613
+ element.innerHTML = html;
1614
+ return doc;
1615
+ }
1616
+
1617
+ doc.documentElement.innerHTML = html;
1618
+ return doc;
1619
+ };
1620
+ }
1621
+
1622
+ /**
1623
+ * Template (performance: fast).
1624
+ *
1625
+ * @see https://developer.mozilla.org/docs/Web/HTML/Element/template
1626
+ */
1627
+ var template = document.createElement('template');
1628
+ var parseFromTemplate;
1629
+
1630
+ if (template.content) {
1631
+ /**
1632
+ * Uses a template element (content fragment) to parse HTML.
1633
+ *
1634
+ * @param {string} html - The HTML string.
1635
+ * @return {NodeList}
1636
+ */
1637
+ parseFromTemplate = function (html) {
1638
+ template.innerHTML = html;
1639
+ return template.content.childNodes;
1640
+ };
1641
+ }
1642
+
1643
+ /**
1644
+ * Parses HTML string to DOM nodes.
1645
+ *
1646
+ * @param {string} html - HTML markup.
1647
+ * @return {NodeList}
1648
+ */
1649
+ function domparser$1(html) {
1650
+ var firstTagName;
1651
+ var match = html.match(FIRST_TAG_REGEX);
1652
+
1653
+ if (match && match[1]) {
1654
+ firstTagName = match[1].toLowerCase();
1655
+ }
1656
+
1657
+ var doc;
1658
+ var element;
1659
+ var elements;
1660
+
1661
+ switch (firstTagName) {
1662
+ case HTML:
1663
+ doc = parseFromString(html);
1664
+
1665
+ // the created document may come with filler head/body elements,
1666
+ // so make sure to remove them if they don't actually exist
1667
+ if (!HEAD_TAG_REGEX.test(html)) {
1668
+ element = doc.querySelector(HEAD);
1669
+ if (element) {
1670
+ element.parentNode.removeChild(element);
1671
+ }
1672
+ }
1673
+
1674
+ if (!BODY_TAG_REGEX.test(html)) {
1675
+ element = doc.querySelector(BODY);
1676
+ if (element) {
1677
+ element.parentNode.removeChild(element);
1678
+ }
1679
+ }
1680
+
1681
+ return doc.querySelectorAll(HTML);
1682
+
1683
+ case HEAD:
1684
+ case BODY:
1685
+ doc = parseFromDocument(html);
1686
+ elements = doc.querySelectorAll(firstTagName);
1687
+
1688
+ // if there's a sibling element, then return both elements
1689
+ if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
1690
+ return elements[0].parentNode.childNodes;
1691
+ }
1692
+ return elements;
1693
+
1694
+ // low-level tag or text
1695
+ default:
1696
+ if (parseFromTemplate) {
1697
+ return parseFromTemplate(html);
1698
+ }
1699
+ element = parseFromDocument(html, BODY).querySelector(BODY);
1700
+ return element.childNodes;
1701
+ }
1702
+ }
1703
+
1704
+ var domparser_1 = domparser$1;
1705
+
1706
+ var utilities = {};
1591
1707
 
1592
1708
  var lib$1 = {};
1593
1709
 
1710
+ var lib = {};
1711
+
1594
1712
  var hasRequiredLib$1;
1595
1713
 
1596
1714
  function requireLib$1 () {
1597
- if (hasRequiredLib$1) return lib$1;
1715
+ if (hasRequiredLib$1) return lib;
1598
1716
  hasRequiredLib$1 = 1;
1599
1717
  (function (exports) {
1600
1718
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1651,10 +1769,12 @@
1651
1769
  exports.CDATA = ElementType.CDATA;
1652
1770
  /** Type for <!doctype ...> */
1653
1771
  exports.Doctype = ElementType.Doctype;
1654
- } (lib$1));
1655
- return lib$1;
1772
+ } (lib));
1773
+ return lib;
1656
1774
  }
1657
1775
 
1776
+ var node = {};
1777
+
1658
1778
  var hasRequiredNode;
1659
1779
 
1660
1780
  function requireNode () {
@@ -1687,29 +1807,14 @@
1687
1807
  return __assign.apply(this, arguments);
1688
1808
  };
1689
1809
  Object.defineProperty(node, "__esModule", { value: true });
1690
- node.cloneNode = node.hasChildren = node.isDocument = node.isDirective = node.isComment = node.isText = node.isCDATA = node.isTag = node.Element = node.Document = node.NodeWithChildren = node.ProcessingInstruction = node.Comment = node.Text = node.DataNode = node.Node = void 0;
1810
+ node.cloneNode = node.hasChildren = node.isDocument = node.isDirective = node.isComment = node.isText = node.isCDATA = node.isTag = node.Element = node.Document = node.CDATA = node.NodeWithChildren = node.ProcessingInstruction = node.Comment = node.Text = node.DataNode = node.Node = void 0;
1691
1811
  var domelementtype_1 = requireLib$1();
1692
- var nodeTypes = new Map([
1693
- [domelementtype_1.ElementType.Tag, 1],
1694
- [domelementtype_1.ElementType.Script, 1],
1695
- [domelementtype_1.ElementType.Style, 1],
1696
- [domelementtype_1.ElementType.Directive, 1],
1697
- [domelementtype_1.ElementType.Text, 3],
1698
- [domelementtype_1.ElementType.CDATA, 4],
1699
- [domelementtype_1.ElementType.Comment, 8],
1700
- [domelementtype_1.ElementType.Root, 9],
1701
- ]);
1702
1812
  /**
1703
1813
  * This object will be used as the prototype for Nodes when creating a
1704
1814
  * DOM-Level-1-compliant structure.
1705
1815
  */
1706
1816
  var Node = /** @class */ (function () {
1707
- /**
1708
- *
1709
- * @param type The type of the node.
1710
- */
1711
- function Node(type) {
1712
- this.type = type;
1817
+ function Node() {
1713
1818
  /** Parent of the node */
1714
1819
  this.parent = null;
1715
1820
  /** Previous sibling */
@@ -1721,19 +1826,6 @@
1721
1826
  /** The end index of the node. Requires `withEndIndices` on the handler to be `true. */
1722
1827
  this.endIndex = null;
1723
1828
  }
1724
- Object.defineProperty(Node.prototype, "nodeType", {
1725
- // Read-only aliases
1726
- /**
1727
- * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible
1728
- * node {@link type}.
1729
- */
1730
- get: function () {
1731
- var _a;
1732
- return (_a = nodeTypes.get(this.type)) !== null && _a !== void 0 ? _a : 1;
1733
- },
1734
- enumerable: false,
1735
- configurable: true
1736
- });
1737
1829
  Object.defineProperty(Node.prototype, "parentNode", {
1738
1830
  // Read-write aliases for properties
1739
1831
  /**
@@ -1796,11 +1888,10 @@
1796
1888
  var DataNode = /** @class */ (function (_super) {
1797
1889
  __extends(DataNode, _super);
1798
1890
  /**
1799
- * @param type The type of the node
1800
1891
  * @param data The content of the data node
1801
1892
  */
1802
- function DataNode(type, data) {
1803
- var _this = _super.call(this, type) || this;
1893
+ function DataNode(data) {
1894
+ var _this = _super.call(this) || this;
1804
1895
  _this.data = data;
1805
1896
  return _this;
1806
1897
  }
@@ -1826,9 +1917,18 @@
1826
1917
  */
1827
1918
  var Text = /** @class */ (function (_super) {
1828
1919
  __extends(Text, _super);
1829
- function Text(data) {
1830
- return _super.call(this, domelementtype_1.ElementType.Text, data) || this;
1920
+ function Text() {
1921
+ var _this = _super !== null && _super.apply(this, arguments) || this;
1922
+ _this.type = domelementtype_1.ElementType.Text;
1923
+ return _this;
1831
1924
  }
1925
+ Object.defineProperty(Text.prototype, "nodeType", {
1926
+ get: function () {
1927
+ return 3;
1928
+ },
1929
+ enumerable: false,
1930
+ configurable: true
1931
+ });
1832
1932
  return Text;
1833
1933
  }(DataNode));
1834
1934
  node.Text = Text;
@@ -1837,9 +1937,18 @@
1837
1937
  */
1838
1938
  var Comment = /** @class */ (function (_super) {
1839
1939
  __extends(Comment, _super);
1840
- function Comment(data) {
1841
- return _super.call(this, domelementtype_1.ElementType.Comment, data) || this;
1940
+ function Comment() {
1941
+ var _this = _super !== null && _super.apply(this, arguments) || this;
1942
+ _this.type = domelementtype_1.ElementType.Comment;
1943
+ return _this;
1842
1944
  }
1945
+ Object.defineProperty(Comment.prototype, "nodeType", {
1946
+ get: function () {
1947
+ return 8;
1948
+ },
1949
+ enumerable: false,
1950
+ configurable: true
1951
+ });
1843
1952
  return Comment;
1844
1953
  }(DataNode));
1845
1954
  node.Comment = Comment;
@@ -1849,10 +1958,18 @@
1849
1958
  var ProcessingInstruction = /** @class */ (function (_super) {
1850
1959
  __extends(ProcessingInstruction, _super);
1851
1960
  function ProcessingInstruction(name, data) {
1852
- var _this = _super.call(this, domelementtype_1.ElementType.Directive, data) || this;
1961
+ var _this = _super.call(this, data) || this;
1853
1962
  _this.name = name;
1963
+ _this.type = domelementtype_1.ElementType.Directive;
1854
1964
  return _this;
1855
1965
  }
1966
+ Object.defineProperty(ProcessingInstruction.prototype, "nodeType", {
1967
+ get: function () {
1968
+ return 1;
1969
+ },
1970
+ enumerable: false,
1971
+ configurable: true
1972
+ });
1856
1973
  return ProcessingInstruction;
1857
1974
  }(DataNode));
1858
1975
  node.ProcessingInstruction = ProcessingInstruction;
@@ -1862,11 +1979,10 @@
1862
1979
  var NodeWithChildren = /** @class */ (function (_super) {
1863
1980
  __extends(NodeWithChildren, _super);
1864
1981
  /**
1865
- * @param type Type of the node.
1866
1982
  * @param children Children of the node. Only certain node types can have children.
1867
1983
  */
1868
- function NodeWithChildren(type, children) {
1869
- var _this = _super.call(this, type) || this;
1984
+ function NodeWithChildren(children) {
1985
+ var _this = _super.call(this) || this;
1870
1986
  _this.children = children;
1871
1987
  return _this;
1872
1988
  }
@@ -1907,14 +2023,40 @@
1907
2023
  return NodeWithChildren;
1908
2024
  }(Node));
1909
2025
  node.NodeWithChildren = NodeWithChildren;
2026
+ var CDATA = /** @class */ (function (_super) {
2027
+ __extends(CDATA, _super);
2028
+ function CDATA() {
2029
+ var _this = _super !== null && _super.apply(this, arguments) || this;
2030
+ _this.type = domelementtype_1.ElementType.CDATA;
2031
+ return _this;
2032
+ }
2033
+ Object.defineProperty(CDATA.prototype, "nodeType", {
2034
+ get: function () {
2035
+ return 4;
2036
+ },
2037
+ enumerable: false,
2038
+ configurable: true
2039
+ });
2040
+ return CDATA;
2041
+ }(NodeWithChildren));
2042
+ node.CDATA = CDATA;
1910
2043
  /**
1911
2044
  * The root node of the document.
1912
2045
  */
1913
2046
  var Document = /** @class */ (function (_super) {
1914
2047
  __extends(Document, _super);
1915
- function Document(children) {
1916
- return _super.call(this, domelementtype_1.ElementType.Root, children) || this;
2048
+ function Document() {
2049
+ var _this = _super !== null && _super.apply(this, arguments) || this;
2050
+ _this.type = domelementtype_1.ElementType.Root;
2051
+ return _this;
1917
2052
  }
2053
+ Object.defineProperty(Document.prototype, "nodeType", {
2054
+ get: function () {
2055
+ return 9;
2056
+ },
2057
+ enumerable: false,
2058
+ configurable: true
2059
+ });
1918
2060
  return Document;
1919
2061
  }(NodeWithChildren));
1920
2062
  node.Document = Document;
@@ -1935,11 +2077,19 @@
1935
2077
  : name === "style"
1936
2078
  ? domelementtype_1.ElementType.Style
1937
2079
  : domelementtype_1.ElementType.Tag; }
1938
- var _this = _super.call(this, type, children) || this;
2080
+ var _this = _super.call(this, children) || this;
1939
2081
  _this.name = name;
1940
2082
  _this.attribs = attribs;
2083
+ _this.type = type;
1941
2084
  return _this;
1942
2085
  }
2086
+ Object.defineProperty(Element.prototype, "nodeType", {
2087
+ get: function () {
2088
+ return 1;
2089
+ },
2090
+ enumerable: false,
2091
+ configurable: true
2092
+ });
1943
2093
  Object.defineProperty(Element.prototype, "tagName", {
1944
2094
  // DOM Level 1 aliases
1945
2095
  /**
@@ -2024,7 +2174,7 @@
2024
2174
  node.isDocument = isDocument;
2025
2175
  /**
2026
2176
  * @param node Node to check.
2027
- * @returns `true` if the node is a `NodeWithChildren` (has children), `false` otherwise.
2177
+ * @returns `true` if the node has children, `false` otherwise.
2028
2178
  */
2029
2179
  function hasChildren(node) {
2030
2180
  return Object.prototype.hasOwnProperty.call(node, "children");
@@ -2062,7 +2212,7 @@
2062
2212
  }
2063
2213
  else if (isCDATA(node)) {
2064
2214
  var children = recursive ? cloneChildren(node.children) : [];
2065
- var clone_2 = new NodeWithChildren(domelementtype_1.ElementType.CDATA, children);
2215
+ var clone_2 = new CDATA(children);
2066
2216
  children.forEach(function (child) { return (child.parent = clone_2); });
2067
2217
  result = clone_2;
2068
2218
  }
@@ -2106,355 +2256,10 @@
2106
2256
  return node;
2107
2257
  }
2108
2258
 
2109
- var constants = constants$1;
2110
- var domhandler = requireNode();
2111
-
2112
- var CASE_SENSITIVE_TAG_NAMES = constants.CASE_SENSITIVE_TAG_NAMES;
2113
-
2114
- var Comment = domhandler.Comment;
2115
- var Element = domhandler.Element;
2116
- var ProcessingInstruction = domhandler.ProcessingInstruction;
2117
- var Text = domhandler.Text;
2118
-
2119
- var caseSensitiveTagNamesMap = {};
2120
- var tagName;
2121
-
2122
- for (var i = 0, len = CASE_SENSITIVE_TAG_NAMES.length; i < len; i++) {
2123
- tagName = CASE_SENSITIVE_TAG_NAMES[i];
2124
- caseSensitiveTagNamesMap[tagName.toLowerCase()] = tagName;
2125
- }
2126
-
2127
- /**
2128
- * Gets case-sensitive tag name.
2129
- *
2130
- * @param {string} tagName - Tag name in lowercase.
2131
- * @return {string|undefined} - Case-sensitive tag name.
2132
- */
2133
- function getCaseSensitiveTagName(tagName) {
2134
- return caseSensitiveTagNamesMap[tagName];
2135
- }
2136
-
2137
- /**
2138
- * Formats DOM attributes to a hash map.
2139
- *
2140
- * @param {NamedNodeMap} attributes - List of attributes.
2141
- * @return {object} - Map of attribute name to value.
2142
- */
2143
- function formatAttributes(attributes) {
2144
- var result = {};
2145
- var attribute;
2146
- // `NamedNodeMap` is array-like
2147
- for (var i = 0, len = attributes.length; i < len; i++) {
2148
- attribute = attributes[i];
2149
- result[attribute.name] = attribute.value;
2150
- }
2151
- return result;
2152
- }
2153
-
2154
- /**
2155
- * Corrects the tag name if it is case-sensitive (SVG).
2156
- * Otherwise, returns the lowercase tag name (HTML).
2157
- *
2158
- * @param {string} tagName - Lowercase tag name.
2159
- * @return {string} - Formatted tag name.
2160
- */
2161
- function formatTagName(tagName) {
2162
- tagName = tagName.toLowerCase();
2163
- var caseSensitiveTagName = getCaseSensitiveTagName(tagName);
2164
- if (caseSensitiveTagName) {
2165
- return caseSensitiveTagName;
2166
- }
2167
- return tagName;
2168
- }
2169
-
2170
- /**
2171
- * Transforms DOM nodes to `domhandler` nodes.
2172
- *
2173
- * @param {NodeList} nodes - DOM nodes.
2174
- * @param {Element|null} [parent=null] - Parent node.
2175
- * @param {string} [directive] - Directive.
2176
- * @return {Array<Comment|Element|ProcessingInstruction|Text>}
2177
- */
2178
- function formatDOM$1(nodes, parent, directive) {
2179
- parent = parent || null;
2180
- var result = [];
2181
-
2182
- for (var index = 0, len = nodes.length; index < len; index++) {
2183
- var node = nodes[index];
2184
- var current;
2185
-
2186
- // set the node data given the type
2187
- switch (node.nodeType) {
2188
- case 1:
2189
- // script, style, or tag
2190
- current = new Element(
2191
- formatTagName(node.nodeName),
2192
- formatAttributes(node.attributes)
2193
- );
2194
- current.children = formatDOM$1(node.childNodes, current);
2195
- break;
2196
-
2197
- case 3:
2198
- current = new Text(node.nodeValue);
2199
- break;
2200
-
2201
- case 8:
2202
- current = new Comment(node.nodeValue);
2203
- break;
2204
-
2205
- default:
2206
- continue;
2207
- }
2208
-
2209
- // set previous node next
2210
- var prev = result[index - 1] || null;
2211
- if (prev) {
2212
- prev.next = current;
2213
- }
2214
-
2215
- // set properties for current node
2216
- current.parent = parent;
2217
- current.prev = prev;
2218
- current.next = null;
2219
-
2220
- result.push(current);
2221
- }
2222
-
2223
- if (directive) {
2224
- current = new ProcessingInstruction(
2225
- directive.substring(0, directive.indexOf(' ')).toLowerCase(),
2226
- directive
2227
- );
2228
- current.next = result[0] || null;
2229
- current.parent = parent;
2230
- result.unshift(current);
2231
-
2232
- if (result[1]) {
2233
- result[1].prev = result[0];
2234
- }
2235
- }
2236
-
2237
- return result;
2238
- }
2239
-
2240
- /**
2241
- * Detects if browser is Internet Explorer.
2242
- *
2243
- * @return {boolean} - Whether IE is detected.
2244
- */
2245
- function isIE$1() {
2246
- return /(MSIE |Trident\/|Edge\/)/.test(navigator.userAgent);
2247
- }
2248
-
2249
- var utilities = {
2250
- formatAttributes: formatAttributes,
2251
- formatDOM: formatDOM$1,
2252
- isIE: isIE$1
2253
- };
2254
-
2255
- // constants
2256
- var HTML = 'html';
2257
- var HEAD = 'head';
2258
- var BODY = 'body';
2259
- var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1>
2260
- var HEAD_TAG_REGEX = /<head.*>/i;
2261
- var BODY_TAG_REGEX = /<body.*>/i;
2262
-
2263
- // falls back to `parseFromString` if `createHTMLDocument` cannot be used
2264
- var parseFromDocument = function () {
2265
- throw new Error(
2266
- 'This browser does not support `document.implementation.createHTMLDocument`'
2267
- );
2268
- };
2269
-
2270
- var parseFromString = function () {
2271
- throw new Error(
2272
- 'This browser does not support `DOMParser.prototype.parseFromString`'
2273
- );
2274
- };
2275
-
2276
- /**
2277
- * DOMParser (performance: slow).
2278
- *
2279
- * @see https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document
2280
- */
2281
- if (typeof window.DOMParser === 'function') {
2282
- var domParser = new window.DOMParser();
2283
- var mimeType = 'text/html';
2284
-
2285
- /**
2286
- * Creates an HTML document using `DOMParser.parseFromString`.
2287
- *
2288
- * @param {string} html - The HTML string.
2289
- * @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).
2290
- * @return {HTMLDocument}
2291
- */
2292
- parseFromString = function (html, tagName) {
2293
- if (tagName) {
2294
- html = '<' + tagName + '>' + html + '</' + tagName + '>';
2295
- }
2296
-
2297
- return domParser.parseFromString(html, mimeType);
2298
- };
2299
-
2300
- parseFromDocument = parseFromString;
2301
- }
2302
-
2303
- /**
2304
- * DOMImplementation (performance: fair).
2305
- *
2306
- * @see https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument
2307
- */
2308
- if (document.implementation) {
2309
- var isIE = utilities.isIE;
2310
-
2311
- // title parameter is required in IE
2312
- // https://msdn.microsoft.com/en-us/library/ff975457(v=vs.85).aspx
2313
- var doc = document.implementation.createHTMLDocument(
2314
- isIE() ? 'html-dom-parser' : undefined
2315
- );
2316
-
2317
- /**
2318
- * Use HTML document created by `document.implementation.createHTMLDocument`.
2319
- *
2320
- * @param {string} html - The HTML string.
2321
- * @param {string} [tagName] - The element to render the HTML (with 'body' as fallback).
2322
- * @return {HTMLDocument}
2323
- */
2324
- parseFromDocument = function (html, tagName) {
2325
- if (tagName) {
2326
- doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html;
2327
- return doc;
2328
- }
2329
-
2330
- doc.documentElement.innerHTML = html;
2331
- return doc;
2332
- };
2333
- }
2334
-
2335
- /**
2336
- * Template (performance: fast).
2337
- *
2338
- * @see https://developer.mozilla.org/docs/Web/HTML/Element/template
2339
- */
2340
- var template = document.createElement('template');
2341
- var parseFromTemplate;
2342
-
2343
- if (template.content) {
2344
- /**
2345
- * Uses a template element (content fragment) to parse HTML.
2346
- *
2347
- * @param {string} html - The HTML string.
2348
- * @return {NodeList}
2349
- */
2350
- parseFromTemplate = function (html) {
2351
- template.innerHTML = html;
2352
- return template.content.childNodes;
2353
- };
2354
- }
2355
-
2356
- /**
2357
- * Parses HTML string to DOM nodes.
2358
- *
2359
- * @param {string} html - HTML markup.
2360
- * @return {NodeList}
2361
- */
2362
- function domparser$1(html) {
2363
- var firstTagName;
2364
- var match = html.match(FIRST_TAG_REGEX);
2365
-
2366
- if (match && match[1]) {
2367
- firstTagName = match[1].toLowerCase();
2368
- }
2369
-
2370
- var doc;
2371
- var element;
2372
- var elements;
2373
-
2374
- switch (firstTagName) {
2375
- case HTML:
2376
- doc = parseFromString(html);
2377
-
2378
- // the created document may come with filler head/body elements,
2379
- // so make sure to remove them if they don't actually exist
2380
- if (!HEAD_TAG_REGEX.test(html)) {
2381
- element = doc.getElementsByTagName(HEAD)[0];
2382
- if (element) {
2383
- element.parentNode.removeChild(element);
2384
- }
2385
- }
2386
-
2387
- if (!BODY_TAG_REGEX.test(html)) {
2388
- element = doc.getElementsByTagName(BODY)[0];
2389
- if (element) {
2390
- element.parentNode.removeChild(element);
2391
- }
2392
- }
2393
-
2394
- return doc.getElementsByTagName(HTML);
2395
-
2396
- case HEAD:
2397
- case BODY:
2398
- elements = parseFromDocument(html).getElementsByTagName(firstTagName);
2399
-
2400
- // if there's a sibling element, then return both elements
2401
- if (BODY_TAG_REGEX.test(html) && HEAD_TAG_REGEX.test(html)) {
2402
- return elements[0].parentNode.childNodes;
2403
- }
2404
- return elements;
2405
-
2406
- // low-level tag or text
2407
- default:
2408
- if (parseFromTemplate) {
2409
- return parseFromTemplate(html);
2410
- }
2411
-
2412
- return parseFromDocument(html, BODY).getElementsByTagName(BODY)[0]
2413
- .childNodes;
2414
- }
2415
- }
2416
-
2417
- var domparser_1 = domparser$1;
2418
-
2419
- var domparser = domparser_1;
2420
- var formatDOM = utilities.formatDOM;
2421
-
2422
- var DIRECTIVE_REGEX = /<(![a-zA-Z\s]+)>/; // e.g., <!doctype html>
2423
-
2424
- /**
2425
- * Parses HTML string to DOM nodes in browser.
2426
- *
2427
- * @param {string} html - HTML markup.
2428
- * @return {DomElement[]} - DOM elements.
2429
- */
2430
- function HTMLDOMParser(html) {
2431
- if (typeof html !== 'string') {
2432
- throw new TypeError('First argument must be a string');
2433
- }
2434
-
2435
- if (html === '') {
2436
- return [];
2437
- }
2438
-
2439
- // match directive
2440
- var match = html.match(DIRECTIVE_REGEX);
2441
- var directive;
2442
-
2443
- if (match && match[1]) {
2444
- directive = match[1];
2445
- }
2446
-
2447
- return formatDOM(domparser(html), null, directive);
2448
- }
2449
-
2450
- var htmlToDom = HTMLDOMParser;
2451
-
2452
- var lib = {};
2453
-
2454
- var hasRequiredLib;
2259
+ var hasRequiredLib;
2455
2260
 
2456
2261
  function requireLib () {
2457
- if (hasRequiredLib) return lib;
2262
+ if (hasRequiredLib) return lib$1;
2458
2263
  hasRequiredLib = 1;
2459
2264
  (function (exports) {
2460
2265
  var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
@@ -2474,12 +2279,10 @@
2474
2279
  Object.defineProperty(exports, "__esModule", { value: true });
2475
2280
  exports.DomHandler = void 0;
2476
2281
  var domelementtype_1 = requireLib$1();
2477
- var node_1 = requireNode();
2282
+ var node_js_1 = requireNode();
2478
2283
  __exportStar(requireNode(), exports);
2479
- var reWhitespace = /\s+/g;
2480
2284
  // Default options
2481
2285
  var defaultOpts = {
2482
- normalizeWhitespace: false,
2483
2286
  withStartIndices: false,
2484
2287
  withEndIndices: false,
2485
2288
  xmlMode: false,
@@ -2494,7 +2297,7 @@
2494
2297
  /** The elements of the DOM */
2495
2298
  this.dom = [];
2496
2299
  /** The root element for the DOM */
2497
- this.root = new node_1.Document(this.dom);
2300
+ this.root = new node_js_1.Document(this.dom);
2498
2301
  /** Indicated whether parsing has been completed. */
2499
2302
  this.done = false;
2500
2303
  /** Stack of open tags. */
@@ -2522,7 +2325,7 @@
2522
2325
  // Resets the handler back to starting state
2523
2326
  DomHandler.prototype.onreset = function () {
2524
2327
  this.dom = [];
2525
- this.root = new node_1.Document(this.dom);
2328
+ this.root = new node_js_1.Document(this.dom);
2526
2329
  this.done = false;
2527
2330
  this.tagStack = [this.root];
2528
2331
  this.lastNode = null;
@@ -2550,29 +2353,20 @@
2550
2353
  };
2551
2354
  DomHandler.prototype.onopentag = function (name, attribs) {
2552
2355
  var type = this.options.xmlMode ? domelementtype_1.ElementType.Tag : undefined;
2553
- var element = new node_1.Element(name, attribs, undefined, type);
2356
+ var element = new node_js_1.Element(name, attribs, undefined, type);
2554
2357
  this.addNode(element);
2555
2358
  this.tagStack.push(element);
2556
2359
  };
2557
2360
  DomHandler.prototype.ontext = function (data) {
2558
- var normalizeWhitespace = this.options.normalizeWhitespace;
2559
2361
  var lastNode = this.lastNode;
2560
2362
  if (lastNode && lastNode.type === domelementtype_1.ElementType.Text) {
2561
- if (normalizeWhitespace) {
2562
- lastNode.data = (lastNode.data + data).replace(reWhitespace, " ");
2563
- }
2564
- else {
2565
- lastNode.data += data;
2566
- }
2363
+ lastNode.data += data;
2567
2364
  if (this.options.withEndIndices) {
2568
2365
  lastNode.endIndex = this.parser.endIndex;
2569
2366
  }
2570
2367
  }
2571
2368
  else {
2572
- if (normalizeWhitespace) {
2573
- data = data.replace(reWhitespace, " ");
2574
- }
2575
- var node = new node_1.Text(data);
2369
+ var node = new node_js_1.Text(data);
2576
2370
  this.addNode(node);
2577
2371
  this.lastNode = node;
2578
2372
  }
@@ -2582,7 +2376,7 @@
2582
2376
  this.lastNode.data += data;
2583
2377
  return;
2584
2378
  }
2585
- var node = new node_1.Comment(data);
2379
+ var node = new node_js_1.Comment(data);
2586
2380
  this.addNode(node);
2587
2381
  this.lastNode = node;
2588
2382
  };
@@ -2590,8 +2384,8 @@
2590
2384
  this.lastNode = null;
2591
2385
  };
2592
2386
  DomHandler.prototype.oncdatastart = function () {
2593
- var text = new node_1.Text("");
2594
- var node = new node_1.NodeWithChildren(domelementtype_1.ElementType.CDATA, [text]);
2387
+ var text = new node_js_1.Text("");
2388
+ var node = new node_js_1.CDATA([text]);
2595
2389
  this.addNode(node);
2596
2390
  text.parent = node;
2597
2391
  this.lastNode = text;
@@ -2600,7 +2394,7 @@
2600
2394
  this.lastNode = null;
2601
2395
  };
2602
2396
  DomHandler.prototype.onprocessinginstruction = function (name, data) {
2603
- var node = new node_1.ProcessingInstruction(name, data);
2397
+ var node = new node_js_1.ProcessingInstruction(name, data);
2604
2398
  this.addNode(node);
2605
2399
  };
2606
2400
  DomHandler.prototype.handleCallback = function (error) {
@@ -2632,10 +2426,219 @@
2632
2426
  }());
2633
2427
  exports.DomHandler = DomHandler;
2634
2428
  exports.default = DomHandler;
2635
- } (lib));
2636
- return lib;
2429
+ } (lib$1));
2430
+ return lib$1;
2431
+ }
2432
+
2433
+ var constants$1 = {};
2434
+
2435
+ /**
2436
+ * SVG elements are case-sensitive.
2437
+ *
2438
+ * @see {@link https://developer.mozilla.org/docs/Web/SVG/Element#svg_elements_a_to_z}
2439
+ */
2440
+
2441
+ constants$1.CASE_SENSITIVE_TAG_NAMES = [
2442
+ 'animateMotion',
2443
+ 'animateTransform',
2444
+ 'clipPath',
2445
+ 'feBlend',
2446
+ 'feColorMatrix',
2447
+ 'feComponentTransfer',
2448
+ 'feComposite',
2449
+ 'feConvolveMatrix',
2450
+ 'feDiffuseLighting',
2451
+ 'feDisplacementMap',
2452
+ 'feDropShadow',
2453
+ 'feFlood',
2454
+ 'feFuncA',
2455
+ 'feFuncB',
2456
+ 'feFuncG',
2457
+ 'feFuncR',
2458
+ 'feGaussainBlur',
2459
+ 'feImage',
2460
+ 'feMerge',
2461
+ 'feMergeNode',
2462
+ 'feMorphology',
2463
+ 'feOffset',
2464
+ 'fePointLight',
2465
+ 'feSpecularLighting',
2466
+ 'feSpotLight',
2467
+ 'feTile',
2468
+ 'feTurbulence',
2469
+ 'foreignObject',
2470
+ 'linearGradient',
2471
+ 'radialGradient',
2472
+ 'textPath'
2473
+ ];
2474
+
2475
+ var domhandler = requireLib();
2476
+ var constants = constants$1;
2477
+
2478
+ var CASE_SENSITIVE_TAG_NAMES = constants.CASE_SENSITIVE_TAG_NAMES;
2479
+
2480
+ var Comment = domhandler.Comment;
2481
+ var Element = domhandler.Element;
2482
+ var ProcessingInstruction = domhandler.ProcessingInstruction;
2483
+ var Text = domhandler.Text;
2484
+
2485
+ var caseSensitiveTagNamesMap = {};
2486
+ var tagName;
2487
+
2488
+ for (var i = 0, len = CASE_SENSITIVE_TAG_NAMES.length; i < len; i++) {
2489
+ tagName = CASE_SENSITIVE_TAG_NAMES[i];
2490
+ caseSensitiveTagNamesMap[tagName.toLowerCase()] = tagName;
2491
+ }
2492
+
2493
+ /**
2494
+ * Gets case-sensitive tag name.
2495
+ *
2496
+ * @param {string} tagName - Tag name in lowercase.
2497
+ * @returns {string|undefined} - Case-sensitive tag name.
2498
+ */
2499
+ function getCaseSensitiveTagName(tagName) {
2500
+ return caseSensitiveTagNamesMap[tagName];
2501
+ }
2502
+
2503
+ /**
2504
+ * Formats DOM attributes to a hash map.
2505
+ *
2506
+ * @param {NamedNodeMap} attributes - List of attributes.
2507
+ * @returns {object} - Map of attribute name to value.
2508
+ */
2509
+ function formatAttributes(attributes) {
2510
+ var result = {};
2511
+ var attribute;
2512
+ // `NamedNodeMap` is array-like
2513
+ for (var i = 0, len = attributes.length; i < len; i++) {
2514
+ attribute = attributes[i];
2515
+ result[attribute.name] = attribute.value;
2516
+ }
2517
+ return result;
2518
+ }
2519
+
2520
+ /**
2521
+ * Corrects the tag name if it is case-sensitive (SVG).
2522
+ * Otherwise, returns the lowercase tag name (HTML).
2523
+ *
2524
+ * @param {string} tagName - Lowercase tag name.
2525
+ * @returns {string} - Formatted tag name.
2526
+ */
2527
+ function formatTagName(tagName) {
2528
+ tagName = tagName.toLowerCase();
2529
+ var caseSensitiveTagName = getCaseSensitiveTagName(tagName);
2530
+ if (caseSensitiveTagName) {
2531
+ return caseSensitiveTagName;
2532
+ }
2533
+ return tagName;
2534
+ }
2535
+
2536
+ /**
2537
+ * Transforms DOM nodes to `domhandler` nodes.
2538
+ *
2539
+ * @param {NodeList} nodes - DOM nodes.
2540
+ * @param {Element|null} [parent=null] - Parent node.
2541
+ * @param {string} [directive] - Directive.
2542
+ * @returns {Array<Comment|Element|ProcessingInstruction|Text>}
2543
+ */
2544
+ function formatDOM$1(nodes, parent, directive) {
2545
+ parent = parent || null;
2546
+ var result = [];
2547
+
2548
+ for (var index = 0, len = nodes.length; index < len; index++) {
2549
+ var node = nodes[index];
2550
+ var current;
2551
+
2552
+ // set the node data given the type
2553
+ switch (node.nodeType) {
2554
+ case 1:
2555
+ // script, style, or tag
2556
+ current = new Element(
2557
+ formatTagName(node.nodeName),
2558
+ formatAttributes(node.attributes)
2559
+ );
2560
+ current.children = formatDOM$1(node.childNodes, current);
2561
+ break;
2562
+
2563
+ case 3:
2564
+ current = new Text(node.nodeValue);
2565
+ break;
2566
+
2567
+ case 8:
2568
+ current = new Comment(node.nodeValue);
2569
+ break;
2570
+
2571
+ default:
2572
+ continue;
2573
+ }
2574
+
2575
+ // set previous node next
2576
+ var prev = result[index - 1] || null;
2577
+ if (prev) {
2578
+ prev.next = current;
2579
+ }
2580
+
2581
+ // set properties for current node
2582
+ current.parent = parent;
2583
+ current.prev = prev;
2584
+ current.next = null;
2585
+
2586
+ result.push(current);
2587
+ }
2588
+
2589
+ if (directive) {
2590
+ current = new ProcessingInstruction(
2591
+ directive.substring(0, directive.indexOf(' ')).toLowerCase(),
2592
+ directive
2593
+ );
2594
+ current.next = result[0] || null;
2595
+ current.parent = parent;
2596
+ result.unshift(current);
2597
+
2598
+ if (result[1]) {
2599
+ result[1].prev = result[0];
2600
+ }
2601
+ }
2602
+
2603
+ return result;
2637
2604
  }
2638
2605
 
2606
+ utilities.formatAttributes = formatAttributes;
2607
+ utilities.formatDOM = formatDOM$1;
2608
+
2609
+ var domparser = domparser_1;
2610
+ var formatDOM = utilities.formatDOM;
2611
+
2612
+ var DIRECTIVE_REGEX = /<(![a-zA-Z\s]+)>/; // e.g., <!doctype html>
2613
+
2614
+ /**
2615
+ * Parses HTML string to DOM nodes in browser.
2616
+ *
2617
+ * @param {string} html - HTML markup.
2618
+ * @return {DomElement[]} - DOM elements.
2619
+ */
2620
+ function HTMLDOMParser(html) {
2621
+ if (typeof html !== 'string') {
2622
+ throw new TypeError('First argument must be a string');
2623
+ }
2624
+
2625
+ if (html === '') {
2626
+ return [];
2627
+ }
2628
+
2629
+ // match directive
2630
+ var match = html.match(DIRECTIVE_REGEX);
2631
+ var directive;
2632
+
2633
+ if (match && match[1]) {
2634
+ directive = match[1];
2635
+ }
2636
+
2637
+ return formatDOM(domparser(html), null, directive);
2638
+ }
2639
+
2640
+ var htmlToDom = HTMLDOMParser;
2641
+
2639
2642
  var domToReact = domToReact_1;
2640
2643
  var attributesToProps = attributesToProps$2;
2641
2644
  var htmlToDOM = htmlToDom;