html-react-parser 4.2.10 → 5.0.0

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.
@@ -0,0 +1,3 @@
1
+ import attributesToProps from '../lib/attributes-to-props.js';
2
+
3
+ export default attributesToProps.default;
@@ -0,0 +1,3 @@
1
+ import domToReact from '../lib/dom-to-react.js';
2
+
3
+ export default domToReact.default;
package/esm/index.mjs ADDED
@@ -0,0 +1,13 @@
1
+ import HTMLReactParser from '../lib/index.js';
2
+
3
+ export {
4
+ Comment,
5
+ Element,
6
+ ProcessingInstruction,
7
+ Text,
8
+ attributesToProps,
9
+ domToReact,
10
+ htmlToDOM,
11
+ } from '../lib/index.js';
12
+
13
+ export default HTMLReactParser.default;
@@ -0,0 +1,8 @@
1
+ export {
2
+ isCustomComponent,
3
+ setStyleProp,
4
+ PRESERVE_CUSTOM_ATTRIBUTES,
5
+ ELEMENTS_WITH_NO_TEXT_CHILDREN,
6
+ canTextBeChildOfNode,
7
+ returnFirstArg,
8
+ } from '../lib/utilities.js';
@@ -1,12 +1,11 @@
1
- // TypeScript Version: 5.3
2
- /* eslint-disable no-unused-vars */
3
-
4
1
  export type Attributes = Record<string, string>;
5
-
6
- export type Props = Record<string, string> & {
7
- style: Record<string, string>;
2
+ export type Props = Record<string, string | boolean> & {
3
+ dangerouslySetInnerHTML?: {
4
+ __html: string;
5
+ };
6
+ key?: string | number;
7
+ style?: Record<string, string>;
8
8
  };
9
-
10
9
  /**
11
10
  * Converts HTML/SVG DOM attributes to React props.
12
11
  *
@@ -14,7 +13,5 @@ export type Props = Record<string, string> & {
14
13
  * @param nodeName - DOM node name.
15
14
  * @returns - React props.
16
15
  */
17
- export default function attributesToProps(
18
- attributes: Attributes,
19
- nodeName?: string
20
- ): Props;
16
+ export default function attributesToProps(attributes?: Attributes, nodeName?: string): Props;
17
+ //# sourceMappingURL=attributes-to-props.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributes-to-props.d.ts","sourceRoot":"","sources":["../src/attributes-to-props.ts"],"names":[],"mappings":"AAmBA,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEhD,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG;IACrD,uBAAuB,CAAC,EAAE;QACxB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,iBAAiB,CACvC,UAAU,GAAE,UAAe,EAC3B,QAAQ,CAAC,EAAE,MAAM,GAChB,KAAK,CA0DP"}
@@ -1,92 +1,75 @@
1
- var reactProperty = require('react-property');
2
- var utilities = require('./utilities');
3
-
4
- // https://reactjs.org/docs/uncontrolled-components.html
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var react_property_1 = require("react-property");
4
+ var utilities_1 = require("./utilities");
5
+ // https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components
5
6
  // https://developer.mozilla.org/docs/Web/HTML/Attributes
6
7
  var UNCONTROLLED_COMPONENT_ATTRIBUTES = ['checked', 'value'];
7
8
  var UNCONTROLLED_COMPONENT_NAMES = ['input', 'select', 'textarea'];
8
-
9
- var VALUE_ONLY_INPUTS = {
10
- reset: true,
11
- submit: true
9
+ var valueOnlyInputs = {
10
+ reset: true,
11
+ submit: true,
12
12
  };
13
-
14
13
  /**
15
14
  * Converts HTML/SVG DOM attributes to React props.
16
15
  *
17
- * @param {object} [attributes={}] - HTML/SVG DOM attributes.
18
- * @param {string} [nodeName] - DOM node name.
16
+ * @param attributes - HTML/SVG DOM attributes.
17
+ * @param nodeName - DOM node name.
19
18
  * @returns - React props.
20
19
  */
21
- module.exports = function attributesToProps(attributes, nodeName) {
22
- attributes = attributes || {};
23
-
24
- var attributeName;
25
- var attributeNameLowerCased;
26
- var attributeValue;
27
- var propName;
28
- var propertyInfo;
29
- var props = {};
30
- var inputIsValueOnly = attributes.type && VALUE_ONLY_INPUTS[attributes.type];
31
-
32
- for (attributeName in attributes) {
33
- attributeValue = attributes[attributeName];
34
-
35
- // ARIA (aria-*) or custom data (data-*) attribute
36
- if (reactProperty.isCustomAttribute(attributeName)) {
37
- props[attributeName] = attributeValue;
38
- continue;
20
+ function attributesToProps(attributes, nodeName) {
21
+ if (attributes === void 0) { attributes = {}; }
22
+ var props = {};
23
+ var isInputValueOnly = Boolean(attributes.type &&
24
+ valueOnlyInputs[attributes.type]);
25
+ for (var attributeName in attributes) {
26
+ var attributeValue = attributes[attributeName];
27
+ // ARIA (aria-*) or custom data (data-*) attribute
28
+ if ((0, react_property_1.isCustomAttribute)(attributeName)) {
29
+ props[attributeName] = attributeValue;
30
+ continue;
31
+ }
32
+ // convert HTML/SVG attribute to React prop
33
+ var attributeNameLowerCased = attributeName.toLowerCase();
34
+ var propName = getPropName(attributeNameLowerCased);
35
+ if (propName) {
36
+ var propertyInfo = (0, react_property_1.getPropertyInfo)(propName);
37
+ // convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
38
+ if (UNCONTROLLED_COMPONENT_ATTRIBUTES.indexOf(propName) !== -1 &&
39
+ UNCONTROLLED_COMPONENT_NAMES.indexOf(nodeName) !== -1 &&
40
+ !isInputValueOnly) {
41
+ propName = getPropName('default' + attributeNameLowerCased);
42
+ }
43
+ props[propName] = attributeValue;
44
+ switch (propertyInfo && propertyInfo.type) {
45
+ case react_property_1.BOOLEAN:
46
+ props[propName] = true;
47
+ break;
48
+ case react_property_1.OVERLOADED_BOOLEAN:
49
+ if (attributeValue === '') {
50
+ props[propName] = true;
51
+ }
52
+ break;
53
+ }
54
+ continue;
55
+ }
56
+ // preserve custom attribute if React >=16
57
+ if (utilities_1.PRESERVE_CUSTOM_ATTRIBUTES) {
58
+ props[attributeName] = attributeValue;
59
+ }
39
60
  }
40
-
41
- // convert HTML/SVG attribute to React prop
42
- attributeNameLowerCased = attributeName.toLowerCase();
43
- propName = getPropName(attributeNameLowerCased);
44
-
45
- if (propName) {
46
- propertyInfo = reactProperty.getPropertyInfo(propName);
47
-
48
- // convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
49
- if (
50
- UNCONTROLLED_COMPONENT_ATTRIBUTES.indexOf(propName) !== -1 &&
51
- UNCONTROLLED_COMPONENT_NAMES.indexOf(nodeName) !== -1 &&
52
- !inputIsValueOnly
53
- ) {
54
- propName = getPropName('default' + attributeNameLowerCased);
55
- }
56
-
57
- props[propName] = attributeValue;
58
-
59
- switch (propertyInfo && propertyInfo.type) {
60
- case reactProperty.BOOLEAN:
61
- props[propName] = true;
62
- break;
63
- case reactProperty.OVERLOADED_BOOLEAN:
64
- if (attributeValue === '') {
65
- props[propName] = true;
66
- }
67
- break;
68
- }
69
- continue;
70
- }
71
-
72
- // preserve custom attribute if React >=16
73
- if (utilities.PRESERVE_CUSTOM_ATTRIBUTES) {
74
- props[attributeName] = attributeValue;
75
- }
76
- }
77
-
78
- // transform inline style to object
79
- utilities.setStyleProp(attributes.style, props);
80
-
81
- return props;
82
- };
83
-
61
+ // transform inline style to object
62
+ (0, utilities_1.setStyleProp)(attributes.style, props);
63
+ return props;
64
+ }
65
+ exports.default = attributesToProps;
84
66
  /**
85
67
  * Gets prop name from lowercased attribute name.
86
68
  *
87
- * @param {string} attributeName - Lowercased attribute name.
69
+ * @param attributeName - Lowercased attribute name.
88
70
  * @returns - Prop name.
89
71
  */
90
72
  function getPropName(attributeName) {
91
- return reactProperty.possibleStandardNames[attributeName];
73
+ return react_property_1.possibleStandardNames[attributeName];
92
74
  }
75
+ //# sourceMappingURL=attributes-to-props.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributes-to-props.js","sourceRoot":"","sources":["../src/attributes-to-props.ts"],"names":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,KAAK,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC;YAEjC,QAAQ,YAAY,IAAI,YAAY,CAAC,IAAI,EAAE;gBACzC,KAAK,wBAAO;oBACV,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;oBACvB,MAAM;gBACR,KAAK,mCAAkB;oBACrB,IAAI,cAAc,KAAK,EAAE,EAAE;wBACzB,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;qBACxB;oBACD,MAAM;aACT;YACD,SAAS;SACV;QAED,0CAA0C;QAC1C,IAAI,sCAA0B,EAAE;YAC9B,KAAK,CAAC,aAAa,CAAC,GAAG,cAAc,CAAC;SACvC;KACF;IAED,mCAAmC;IACnC,IAAA,wBAAY,EAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAEtC,OAAO,KAAK,CAAC;AACf,CAAC;AA7DD,oCA6DC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,aAAqB;IACxC,OAAO,sCAAqB,CAAC,aAAa,CAAC,CAAC;AAC9C,CAAC"}
@@ -1,18 +1,12 @@
1
- // TypeScript Version: 5.3
2
- /* eslint-disable no-undef, no-unused-vars */
3
-
4
- import { DOMNode, HTMLReactParserOptions } from '..';
5
-
6
- export { DOMNode, HTMLReactParserOptions };
7
-
1
+ /// <reference types="react" />
2
+ import type { DOMNode } from 'html-dom-parser';
3
+ import type { HTMLReactParserOptions } from './types';
8
4
  /**
9
5
  * Converts DOM nodes to JSX element(s).
10
6
  *
11
7
  * @param nodes - DOM nodes.
12
- * @param options - Parser options.
13
- * @returns - JSX element(s).
8
+ * @param options - Options.
9
+ * @returns - String or JSX element(s).
14
10
  */
15
- export default function domToReact(
16
- nodes: DOMNode[],
17
- options?: HTMLReactParserOptions
18
- ): string | JSX.Element | JSX.Element[];
11
+ export default function domToReact(nodes: DOMNode[], options?: HTMLReactParserOptions): string | JSX.Element | JSX.Element[];
12
+ //# sourceMappingURL=dom-to-react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom-to-react.d.ts","sourceRoot":"","sources":["../src/dom-to-react.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAW,OAAO,EAAQ,MAAM,iBAAiB,CAAC;AAY9D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAQtD;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAChC,KAAK,EAAE,OAAO,EAAE,EAChB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,MAAM,GAAG,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,CA6GtC"}
@@ -1,141 +1,122 @@
1
- var React = require('react');
2
- var attributesToProps = require('./attributes-to-props');
3
- var utilities = require('./utilities');
4
-
5
- var setStyleProp = utilities.setStyleProp;
6
- var canTextBeChildOfNode = utilities.canTextBeChildOfNode;
7
-
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var react_1 = require("react");
7
+ var attributes_to_props_1 = __importDefault(require("./attributes-to-props"));
8
+ var utilities_1 = require("./utilities");
9
+ var React = {
10
+ cloneElement: react_1.cloneElement,
11
+ createElement: react_1.createElement,
12
+ isValidElement: react_1.isValidElement,
13
+ };
8
14
  /**
9
15
  * Converts DOM nodes to JSX element(s).
10
16
  *
11
- * @param {DomElement[]} nodes - DOM nodes.
12
- * @param {object} [options={}] - Options.
13
- * @param {Function} [options.replace] - Replacer.
14
- * @param {Function} [options.transform] - Transform.
15
- * @param {object} [options.library] - Library (React, Preact, etc.).
17
+ * @param nodes - DOM nodes.
18
+ * @param options - Options.
16
19
  * @returns - String or JSX element(s).
17
20
  */
18
21
  function domToReact(nodes, options) {
19
- options = options || {};
20
-
21
- var library = options.library || React;
22
- var cloneElement = library.cloneElement;
23
- var createElement = library.createElement;
24
- var isValidElement = library.isValidElement;
25
-
26
- var result = [];
27
- var node;
28
- var isWhitespace;
29
- var hasReplace = typeof options.replace === 'function';
30
- var transform = options.transform || utilities.returnFirstArg;
31
- var replaceElement;
32
- var props;
33
- var children;
34
- var trim = options.trim;
35
-
36
- for (var i = 0, len = nodes.length; i < len; i++) {
37
- node = nodes[i];
38
-
39
- // replace with custom React element (if present)
40
- if (hasReplace) {
41
- replaceElement = options.replace(node);
42
-
43
- if (isValidElement(replaceElement)) {
44
- // set "key" prop for sibling elements
45
- // https://fb.me/react-warning-keys
46
- if (len > 1) {
47
- replaceElement = cloneElement(replaceElement, {
48
- key: replaceElement.key || i
49
- });
22
+ var reactElements = [];
23
+ var hasReplace = typeof (options === null || options === void 0 ? void 0 : options.replace) === 'function';
24
+ var transform = (options === null || options === void 0 ? void 0 : options.transform) || utilities_1.returnFirstArg;
25
+ var _a = (options === null || options === void 0 ? void 0 : options.library) || React, cloneElement = _a.cloneElement, createElement = _a.createElement, isValidElement = _a.isValidElement;
26
+ var index = 0;
27
+ var nodesLength = nodes.length;
28
+ for (; index < nodesLength; index++) {
29
+ var node = nodes[index];
30
+ // replace with custom React element (if present)
31
+ if (hasReplace) {
32
+ var replaceElement = options.replace(node);
33
+ if (isValidElement(replaceElement)) {
34
+ // set "key" prop for sibling elements
35
+ // https://react.dev/learn/rendering-lists#rules-of-keys
36
+ if (nodesLength > 1) {
37
+ replaceElement = cloneElement(replaceElement, {
38
+ key: replaceElement.key || index,
39
+ });
40
+ }
41
+ reactElements.push(transform(replaceElement, node, index));
42
+ continue;
43
+ }
50
44
  }
51
- result.push(transform(replaceElement, node, i));
52
- continue;
53
- }
54
- }
55
-
56
- if (node.type === 'text') {
57
- isWhitespace = !node.data.trim().length;
58
-
59
- if (isWhitespace && node.parent && !canTextBeChildOfNode(node.parent)) {
60
- // We have a whitespace node that can't be nested in its parent
61
- // so skip it
62
- continue;
63
- }
64
-
65
- if (trim && isWhitespace) {
66
- // Trim is enabled and we have a whitespace node
67
- // so skip it
68
- continue;
69
- }
70
-
71
- // We have a text node that's not whitespace and it can be nested
72
- // in its parent so add it to the results
73
- result.push(transform(node.data, node, i));
74
- continue;
75
- }
76
-
77
- props = node.attribs;
78
- if (skipAttributesToProps(node)) {
79
- setStyleProp(props.style, props);
80
- } else if (props) {
81
- props = attributesToProps(props, node.name);
82
- }
83
-
84
- children = null;
85
-
86
- switch (node.type) {
87
- case 'script':
88
- case 'style':
89
- // prevent text in <script> or <style> from being escaped
90
- // https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
91
- if (node.children[0]) {
92
- props.dangerouslySetInnerHTML = {
93
- __html: node.children[0].data
94
- };
45
+ if (node.type === 'text') {
46
+ var isWhitespace = !node.data.trim().length;
47
+ // We have a whitespace node that can't be nested in its parent
48
+ // so skip it
49
+ if (isWhitespace &&
50
+ node.parent &&
51
+ !(0, utilities_1.canTextBeChildOfNode)(node.parent)) {
52
+ continue;
53
+ }
54
+ // Trim is enabled and we have a whitespace node
55
+ // so skip it
56
+ if ((options === null || options === void 0 ? void 0 : options.trim) && isWhitespace) {
57
+ continue;
58
+ }
59
+ // We have a text node that's not whitespace and it can be nested
60
+ // in its parent so add it to the results
61
+ reactElements.push(transform(node.data, node, index));
62
+ continue;
95
63
  }
96
- break;
97
-
98
- case 'tag':
99
- // setting textarea value in children is an antipattern in React
100
- // https://reactjs.org/docs/forms.html#the-textarea-tag
101
- if (node.name === 'textarea' && node.children[0]) {
102
- props.defaultValue = node.children[0].data;
103
- } else if (node.children && node.children.length) {
104
- // continue recursion of creating React elements (if applicable)
105
- children = domToReact(node.children, options);
64
+ var element = node;
65
+ var props = {};
66
+ if (skipAttributesToProps(element)) {
67
+ (0, utilities_1.setStyleProp)(element.attribs.style, element.attribs);
68
+ props = element.attribs;
106
69
  }
107
- break;
108
-
109
- // skip all other cases (e.g., comment)
110
- default:
111
- continue;
112
- }
113
-
114
- // set "key" prop for sibling elements
115
- // https://fb.me/react-warning-keys
116
- if (len > 1) {
117
- props.key = i;
70
+ else if (element.attribs) {
71
+ props = (0, attributes_to_props_1.default)(element.attribs, element.name);
72
+ }
73
+ var children = null;
74
+ switch (node.type) {
75
+ case 'script':
76
+ case 'style':
77
+ // prevent text in <script> or <style> from being escaped
78
+ // https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html
79
+ if (node.children[0]) {
80
+ props.dangerouslySetInnerHTML = {
81
+ __html: node.children[0].data,
82
+ };
83
+ }
84
+ break;
85
+ case 'tag':
86
+ // setting textarea value in children is an antipattern in React
87
+ // https://react.dev/reference/react-dom/components/textarea#caveats
88
+ if (node.name === 'textarea' && node.children[0]) {
89
+ props.defaultValue = node.children[0].data;
90
+ }
91
+ else if (node.children && node.children.length) {
92
+ // continue recursion of creating React elements (if applicable)
93
+ children = domToReact(node.children, options);
94
+ }
95
+ break;
96
+ // skip all other cases (e.g., comment)
97
+ default:
98
+ continue;
99
+ }
100
+ // set "key" prop for sibling elements
101
+ // https://react.dev/learn/rendering-lists#rules-of-keys
102
+ if (nodesLength > 1) {
103
+ props.key = index;
104
+ }
105
+ reactElements.push(transform(createElement(node.name, props, children), node, index));
118
106
  }
119
-
120
- result.push(transform(createElement(node.name, props, children), node, i));
121
- }
122
-
123
- return result.length === 1 ? result[0] : result;
107
+ return reactElements.length === 1 ? reactElements[0] : reactElements;
124
108
  }
125
-
109
+ exports.default = domToReact;
126
110
  /**
127
111
  * Determines whether DOM element attributes should be transformed to props.
128
112
  * Web Components should not have their attributes transformed except for `style`.
129
113
  *
130
- * @param {DomElement} node
131
- * @returns - Whether node attributes should be converted to props.
114
+ * @param node - Element node.
115
+ * @returns - Whether the node attributes should be converted to props.
132
116
  */
133
117
  function skipAttributesToProps(node) {
134
- return (
135
- utilities.PRESERVE_CUSTOM_ATTRIBUTES &&
136
- node.type === 'tag' &&
137
- utilities.isCustomComponent(node.name, node.attribs)
138
- );
118
+ return (utilities_1.PRESERVE_CUSTOM_ATTRIBUTES &&
119
+ node.type === 'tag' &&
120
+ (0, utilities_1.isCustomComponent)(node.name, node.attribs));
139
121
  }
140
-
141
- module.exports = domToReact;
122
+ //# sourceMappingURL=dom-to-react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom-to-react.js","sourceRoot":"","sources":["../src/dom-to-react.ts"],"names":[],"mappings":";;;;;AAAA,+BAAoE;AAGpE,8EAAsD;AAEtD,yCAMqB;AAIrB,IAAM,KAAK,GAAG;IACZ,YAAY,sBAAA;IACZ,aAAa,uBAAA;IACb,cAAc,wBAAA;CACN,CAAC;AAEX;;;;;;GAMG;AACH,SAAwB,UAAU,CAChC,KAAgB,EAChB,OAAgC;IAEhC,IAAM,aAAa,GAAG,EAAE,CAAC;IAEzB,IAAM,UAAU,GAAG,OAAO,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAA,KAAK,UAAU,CAAC;IAC1D,IAAM,SAAS,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,KAAI,0BAAc,CAAC;IACjD,IAAA,KACJ,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,KAAI,KAAK,EADnB,YAAY,kBAAA,EAAE,aAAa,mBAAA,EAAE,cAAc,oBACxB,CAAC;IAE5B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC;IAEjC,OAAO,KAAK,GAAG,WAAW,EAAE,KAAK,EAAE,EAAE;QACnC,IAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QAE1B,iDAAiD;QACjD,IAAI,UAAU,EAAE;YACd,IAAI,cAAc,GAAG,OAAO,CAAC,OAAQ,CAAC,IAAI,CAAgB,CAAC;YAE3D,IAAI,cAAc,CAAC,cAAc,CAAC,EAAE;gBAClC,sCAAsC;gBACtC,wDAAwD;gBACxD,IAAI,WAAW,GAAG,CAAC,EAAE;oBACnB,cAAc,GAAG,YAAY,CAAC,cAAc,EAAE;wBAC5C,GAAG,EAAE,cA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AI,EAAE,IAAI,CAAC,OAAO,CAAC,CAC3C,CAAC;AACJ,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /// <reference types="react" />
2
+ import htmlToDOM from 'html-dom-parser';
3
+ import attributesToProps from './attributes-to-props';
4
+ import domToReact from './dom-to-react';
5
+ import type { HTMLReactParserOptions } from './types';
6
+ export { Comment, Element, ProcessingInstruction, Text } from 'domhandler';
7
+ export type { DOMNode } from 'html-dom-parser';
8
+ export { HTMLReactParserOptions, attributesToProps, domToReact, htmlToDOM };
9
+ /**
10
+ * Converts HTML string to React elements.
11
+ *
12
+ * @param html - HTML string.
13
+ * @param options - Parser options.
14
+ * @returns - React element(s), empty array, or string.
15
+ */
16
+ export default function HTMLReactParser(html: string, options?: HTMLReactParserOptions): JSX.Element | JSX.Element[] | string;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAExC,OAAO,iBAAiB,MAAM,uBAAuB,CAAC;AACtD,OAAO,UAAU,MAAM,gBAAgB,CAAC;AACxC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEtD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,qBAAqB,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAC3E,YAAY,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC;AAI5E;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,sBAAsB,GAC/B,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,GAAG,MAAM,CAatC"}
package/lib/index.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.htmlToDOM = exports.domToReact = exports.attributesToProps = exports.Text = exports.ProcessingInstruction = exports.Element = exports.Comment = void 0;
7
+ var html_dom_parser_1 = __importDefault(require("html-dom-parser"));
8
+ exports.htmlToDOM = html_dom_parser_1.default;
9
+ var attributes_to_props_1 = __importDefault(require("./attributes-to-props"));
10
+ exports.attributesToProps = attributes_to_props_1.default;
11
+ var dom_to_react_1 = __importDefault(require("./dom-to-react"));
12
+ exports.domToReact = dom_to_react_1.default;
13
+ var domhandler_1 = require("domhandler");
14
+ Object.defineProperty(exports, "Comment", { enumerable: true, get: function () { return domhandler_1.Comment; } });
15
+ Object.defineProperty(exports, "Element", { enumerable: true, get: function () { return domhandler_1.Element; } });
16
+ Object.defineProperty(exports, "ProcessingInstruction", { enumerable: true, get: function () { return domhandler_1.ProcessingInstruction; } });
17
+ Object.defineProperty(exports, "Text", { enumerable: true, get: function () { return domhandler_1.Text; } });
18
+ var domParserOptions = { lowerCaseAttributeNames: false };
19
+ /**
20
+ * Converts HTML string to React elements.
21
+ *
22
+ * @param html - HTML string.
23
+ * @param options - Parser options.
24
+ * @returns - React element(s), empty array, or string.
25
+ */
26
+ function HTMLReactParser(html, options) {
27
+ if (typeof html !== 'string') {
28
+ throw new TypeError('First argument must be a string');
29
+ }
30
+ if (!html) {
31
+ return [];
32
+ }
33
+ return (0, dom_to_react_1.default)((0, html_dom_parser_1.default)(html, (options === null || options === void 0 ? void 0 : options.htmlparser2) || domParserOptions), options);
34
+ }
35
+ exports.default = HTMLReactParser;
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,oEAAwC;AASwB,oBATzD,yBAAS,CASyD;AAPzE,8EAAsD;AAOrB,4BAP1B,6BAAiB,CAO0B;AANlD,gEAAwC;AAMY,qBAN7C,sBAAU,CAM6C;AAH9D,yCAA2E;AAAlE,qGAAA,OAAO,OAAA;AAAE,qGAAA,OAAO,OAAA;AAAE,mHAAA,qBAAqB,OAAA;AAAE,kGAAA,IAAI,OAAA;AAKtD,IAAM,gBAAgB,GAAG,EAAE,uBAAuB,EAAE,KAAK,EAAW,CAAC;AAErE;;;;;;GAMG;AACH,SAAwB,eAAe,CACrC,IAAY,EACZ,OAAgC;IAEhC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,MAAM,IAAI,SAAS,CAAC,iCAAiC,CAAC,CAAC;KACxD;IAED,IAAI,CAAC,IAAI,EAAE;QACT,OAAO,EAAE,CAAC;KACX;IAED,OAAO,IAAA,sBAAU,EACf,IAAA,yBAAS,EAAC,IAAI,EAAE,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,KAAI,gBAAgB,CAAC,EACzD,OAAO,CACR,CAAC;AACJ,CAAC;AAhBD,kCAgBC"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { DomHandlerOptions } from 'domhandler';
2
+ import type { DOMNode } from 'html-dom-parser';
3
+ import type { ParserOptions } from 'htmlparser2';
4
+ import type { ReactNode } from 'react';
5
+ export interface HTMLReactParserOptions {
6
+ htmlparser2?: ParserOptions & DomHandlerOptions;
7
+ library?: {
8
+ cloneElement: (element: JSX.Element, props?: object, ...children: any) => JSX.Element;
9
+ createElement: (type: any, props?: object, ...children: any) => JSX.Element;
10
+ isValidElement: (element: any) => boolean;
11
+ [key: string]: any;
12
+ };
13
+ replace?: (domNode: DOMNode) => JSX.Element | string | null | boolean | void;
14
+ transform?: (reactNode: ReactNode, domNode: DOMNode, index: number) => JSX.Element | string | null | void;
15
+ trim?: boolean;
16
+ }
17
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,MAAM,WAAW,sBAAsB;IACrC,WAAW,CAAC,EAAE,aAAa,GAAG,iBAAiB,CAAC;IAEhD,OAAO,CAAC,EAAE;QACR,YAAY,EAAE,CACZ,OAAO,EAAE,GAAG,CAAC,OAAO,EACpB,KAAK,CAAC,EAAE,MAAM,EACd,GAAG,QAAQ,EAAE,GAAG,KACb,GAAG,CAAC,OAAO,CAAC;QACjB,aAAa,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,QAAQ,EAAE,GAAG,KAAK,GAAG,CAAC,OAAO,CAAC;QAC5E,cAAc,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC;QAC1C,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB,CAAC;IAEF,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,GAAG,CAAC,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,GAAG,IAAI,CAAC;IAE7E,SAAS,CAAC,EAAE,CACV,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,KACV,GAAG,CAAC,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IAExC,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB"}
package/lib/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}