html-react-parser 1.3.0 → 1.4.3

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.
package/README.md CHANGED
@@ -5,7 +5,6 @@
5
5
  [![NPM version](https://img.shields.io/npm/v/html-react-parser.svg)](https://www.npmjs.com/package/html-react-parser)
6
6
  [![Build Status](https://github.com/remarkablemark/html-react-parser/workflows/build/badge.svg?branch=master)](https://github.com/remarkablemark/html-react-parser/actions?query=workflow%3Abuild)
7
7
  [![codecov](https://codecov.io/gh/remarkablemark/html-react-parser/branch/master/graph/badge.svg?token=wosFd1DBIR)](https://codecov.io/gh/remarkablemark/html-react-parser)
8
- [![Dependency status](https://david-dm.org/remarkablemark/html-react-parser.svg)](https://david-dm.org/remarkablemark/html-react-parser)
9
8
  [![NPM downloads](https://img.shields.io/npm/dm/html-react-parser.svg?style=flat-square)](https://www.npmjs.com/package/html-react-parser)
10
9
  [![Discord](https://img.shields.io/discord/422421589582282752.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/njExwXdrRJ)
11
10
 
@@ -26,7 +25,7 @@ const parse = require('html-react-parser');
26
25
  parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
27
26
  ```
28
27
 
29
- [Repl.it](https://repl.it/@remarkablemark/html-react-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [CodeSandbox](https://codesandbox.io/s/940pov1l4w) | [TypeScript](https://codesandbox.io/s/html-react-parser-z0kp6) | [Examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples)
28
+ [Replit](https://replit.com/@remarkablemark/html-react-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [CodeSandbox](https://codesandbox.io/s/940pov1l4w) | [TypeScript](https://codesandbox.io/s/html-react-parser-z0kp6) | [Examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples)
30
29
 
31
30
  <details>
32
31
  <summary>Table of Contents</summary>
@@ -188,8 +187,7 @@ parse('<p id="replace">text</p>', {
188
187
  For TypeScript projects, you may need to check that `domNode` is an instance of domhandler's `Element`:
189
188
 
190
189
  ```tsx
191
- import { HTMLReactParserOptions } from 'html-react-parser';
192
- import { Element } from 'domhandler/lib/node';
190
+ import { HTMLReactParserOptions, Element } from 'html-react-parser';
193
191
 
194
192
  const options: HTMLReactParserOptions = {
195
193
  replace: domNode => {
@@ -200,11 +198,11 @@ const options: HTMLReactParserOptions = {
200
198
  };
201
199
  ```
202
200
 
203
- If you're having issues with `domNode instanceof Element`, try this [alternative solution](https://github.com/remarkablemark/html-react-parser/issues/221#issuecomment-771600574).
201
+ If you're having issues take a look at our [Create React App example](./examples/create-react-app-typescript/src/App.tsx).
204
202
 
205
203
  #### replace element and children
206
204
 
207
- Replace the element and its children (see [demo](https://repl.it/@remarkablemark/html-react-parser-replace-example)):
205
+ Replace the element and its children (see [demo](https://replit.com/@remarkablemark/html-react-parser-replace-example)):
208
206
 
209
207
  ```jsx
210
208
  import parse, { domToReact } from 'html-react-parser';
@@ -285,7 +283,7 @@ HTML output:
285
283
 
286
284
  #### replace and remove element
287
285
 
288
- [Exclude](https://repl.it/@remarkablemark/html-react-parser-56) an element from rendering by replacing it with `<React.Fragment>`:
286
+ [Exclude](https://replit.com/@remarkablemark/html-react-parser-56) an element from rendering by replacing it with `<React.Fragment>`:
289
287
 
290
288
  ```jsx
291
289
  parse('<p><br id="remove"></p>', {
@@ -417,7 +415,7 @@ If the parser throws an erorr, check if your arguments are valid. See ["Does inv
417
415
 
418
416
  ### Is SSR supported?
419
417
 
420
- Yes, server-side rendering on Node.js is supported by this library. See [demo](https://repl.it/@remarkablemark/html-react-parser-SSR).
418
+ Yes, server-side rendering on Node.js is supported by this library. See [demo](https://replit.com/@remarkablemark/html-react-parser-SSR).
421
419
 
422
420
  ### Elements aren't nested correctly
423
421
 
@@ -452,7 +450,7 @@ parse('<CustomElement>', options); // React.createElement('CustomElement')
452
450
  > Warning: <CustomElement> is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements.
453
451
  > ```
454
452
 
455
- See [#62](https://github.com/remarkablemark/html-react-parser/issues/62) and [example](https://repl.it/@remarkablemark/html-react-parser-62).
453
+ See [#62](https://github.com/remarkablemark/html-react-parser/issues/62) and [example](https://replit.com/@remarkablemark/html-react-parser-62).
456
454
 
457
455
  ### TS Error: Property 'attribs' does not exist on type 'DOMNode'
458
456
 
@@ -2,7 +2,7 @@
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) :
3
3
  typeof define === 'function' && define.amd ? define(['react'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.HTMLReactParser = factory(global.React));
5
- }(this, (function (require$$0) { 'use strict';
5
+ })(this, (function (require$$0) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -1181,7 +1181,7 @@
1181
1181
  exports["default"] = StyleToJS;
1182
1182
  }(cjs));
1183
1183
 
1184
- var React$1 = require$$0__default['default'];
1184
+ var React$1 = require$$0__default["default"];
1185
1185
  var styleToJS = cjs.default;
1186
1186
 
1187
1187
  /**
@@ -1316,11 +1316,19 @@
1316
1316
 
1317
1317
  // convert HTML/SVG attribute to React prop
1318
1318
  attributeNameLowerCased = attributeName.toLowerCase();
1319
- propName = reactProperty.possibleStandardNames[attributeNameLowerCased];
1319
+ propName = getPropName(attributeNameLowerCased);
1320
1320
 
1321
1321
  if (propName) {
1322
- props[propName] = attributeValue;
1323
1322
  propertyInfo = reactProperty.getPropertyInfo(propName);
1323
+
1324
+ // convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
1325
+ // https://reactjs.org/docs/uncontrolled-components.html
1326
+ if (propName === 'checked' || propName === 'value') {
1327
+ propName = getPropName('default' + attributeNameLowerCased);
1328
+ }
1329
+
1330
+ props[propName] = attributeValue;
1331
+
1324
1332
  switch (propertyInfo && propertyInfo.type) {
1325
1333
  case reactProperty.BOOLEAN:
1326
1334
  props[propName] = true;
@@ -1346,7 +1354,17 @@
1346
1354
  return props;
1347
1355
  };
1348
1356
 
1349
- var React = require$$0__default['default'];
1357
+ /**
1358
+ * Gets prop name from lowercased attribute name.
1359
+ *
1360
+ * @param {string} attributeName - Lowercased attribute name.
1361
+ * @return {string}
1362
+ */
1363
+ function getPropName(attributeName) {
1364
+ return reactProperty.possibleStandardNames[attributeName];
1365
+ }
1366
+
1367
+ var React = require$$0__default["default"];
1350
1368
  var attributesToProps$1 = attributesToProps$2;
1351
1369
  var utilities$1 = utilities$3;
1352
1370
 
@@ -1645,6 +1663,10 @@
1645
1663
  }
1646
1664
  Object.defineProperty(Node.prototype, "nodeType", {
1647
1665
  // Read-only aliases
1666
+ /**
1667
+ * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible
1668
+ * node {@link type}.
1669
+ */
1648
1670
  get: function () {
1649
1671
  var _a;
1650
1672
  return (_a = nodeTypes.get(this.type)) !== null && _a !== void 0 ? _a : 1;
@@ -1654,6 +1676,10 @@
1654
1676
  });
1655
1677
  Object.defineProperty(Node.prototype, "parentNode", {
1656
1678
  // Read-write aliases for properties
1679
+ /**
1680
+ * Same as {@link parent}.
1681
+ * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
1682
+ */
1657
1683
  get: function () {
1658
1684
  return this.parent;
1659
1685
  },
@@ -1664,6 +1690,10 @@
1664
1690
  configurable: true
1665
1691
  });
1666
1692
  Object.defineProperty(Node.prototype, "previousSibling", {
1693
+ /**
1694
+ * Same as {@link prev}.
1695
+ * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
1696
+ */
1667
1697
  get: function () {
1668
1698
  return this.prev;
1669
1699
  },
@@ -1674,6 +1704,10 @@
1674
1704
  configurable: true
1675
1705
  });
1676
1706
  Object.defineProperty(Node.prototype, "nextSibling", {
1707
+ /**
1708
+ * Same as {@link next}.
1709
+ * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
1710
+ */
1677
1711
  get: function () {
1678
1712
  return this.next;
1679
1713
  },
@@ -1711,6 +1745,10 @@
1711
1745
  return _this;
1712
1746
  }
1713
1747
  Object.defineProperty(DataNode.prototype, "nodeValue", {
1748
+ /**
1749
+ * Same as {@link data}.
1750
+ * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
1751
+ */
1714
1752
  get: function () {
1715
1753
  return this.data;
1716
1754
  },
@@ -1774,6 +1812,7 @@
1774
1812
  }
1775
1813
  Object.defineProperty(NodeWithChildren.prototype, "firstChild", {
1776
1814
  // Aliases
1815
+ /** First child of the node. */
1777
1816
  get: function () {
1778
1817
  var _a;
1779
1818
  return (_a = this.children[0]) !== null && _a !== void 0 ? _a : null;
@@ -1782,6 +1821,7 @@
1782
1821
  configurable: true
1783
1822
  });
1784
1823
  Object.defineProperty(NodeWithChildren.prototype, "lastChild", {
1824
+ /** Last child of the node. */
1785
1825
  get: function () {
1786
1826
  return this.children.length > 0
1787
1827
  ? this.children[this.children.length - 1]
@@ -1791,6 +1831,10 @@
1791
1831
  configurable: true
1792
1832
  });
1793
1833
  Object.defineProperty(NodeWithChildren.prototype, "childNodes", {
1834
+ /**
1835
+ * Same as {@link children}.
1836
+ * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
1837
+ */
1794
1838
  get: function () {
1795
1839
  return this.children;
1796
1840
  },
@@ -1838,6 +1882,10 @@
1838
1882
  }
1839
1883
  Object.defineProperty(Element.prototype, "tagName", {
1840
1884
  // DOM Level 1 aliases
1885
+ /**
1886
+ * Same as {@link name}.
1887
+ * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
1888
+ */
1841
1889
  get: function () {
1842
1890
  return this.name;
1843
1891
  },
@@ -1941,6 +1989,9 @@
1941
1989
  var children = recursive ? cloneChildren(node.children) : [];
1942
1990
  var clone_1 = new Element$1(node.name, __assign({}, node.attribs), children);
1943
1991
  children.forEach(function (child) { return (child.parent = clone_1); });
1992
+ if (node.namespace != null) {
1993
+ clone_1.namespace = node.namespace;
1994
+ }
1944
1995
  if (node["x-attribsNamespace"]) {
1945
1996
  clone_1["x-attribsNamespace"] = __assign({}, node["x-attribsNamespace"]);
1946
1997
  }
@@ -1974,10 +2025,13 @@
1974
2025
  result = instruction;
1975
2026
  }
1976
2027
  else {
1977
- throw new Error("Not implemented yet: " + node.type);
2028
+ throw new Error("Not implemented yet: ".concat(node.type));
1978
2029
  }
1979
2030
  result.startIndex = node.startIndex;
1980
2031
  result.endIndex = node.endIndex;
2032
+ if (node.sourceCodeLocation != null) {
2033
+ result.sourceCodeLocation = node.sourceCodeLocation;
2034
+ }
1981
2035
  return result;
1982
2036
  }
1983
2037
  node.cloneNode = cloneNode;
@@ -2366,6 +2420,7 @@
2366
2420
  HTMLReactParser.domToReact = domToReact;
2367
2421
  HTMLReactParser.htmlToDOM = htmlToDOM;
2368
2422
  HTMLReactParser.attributesToProps = attributesToProps;
2423
+ HTMLReactParser.Element = node.Element;
2369
2424
 
2370
2425
  // support CommonJS and ES Modules
2371
2426
  htmlReactParser$1.exports = HTMLReactParser;
@@ -2375,5 +2430,5 @@
2375
2430
 
2376
2431
  return htmlReactParser;
2377
2432
 
2378
- })));
2433
+ }));
2379
2434
  //# sourceMappingURL=html-react-parser.js.map