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,108 @@
1
+ import { version } from 'react';
2
+ import StyleToJS from 'style-to-js';
3
+ import type { Element } from 'html-dom-parser';
4
+
5
+ import type { Props } from './attributes-to-props';
6
+
7
+ const RESERVED_SVG_MATHML_ELEMENTS = new Set([
8
+ 'annotation-xml',
9
+ 'color-profile',
10
+ 'font-face',
11
+ 'font-face-src',
12
+ 'font-face-uri',
13
+ 'font-face-format',
14
+ 'font-face-name',
15
+ 'missing-glyph',
16
+ ]);
17
+
18
+ /**
19
+ * Check if a tag is a custom component.
20
+ *
21
+ * @see {@link https://github.com/facebook/react/blob/v16.6.3/packages/react-dom/src/shared/isCustomComponent.js}
22
+ *
23
+ * @param tagName - Tag name.
24
+ * @param props - Props passed to the element.
25
+ * @returns - Whether the tag is custom component.
26
+ */
27
+ export function isCustomComponent(
28
+ tagName: string,
29
+ props?: Record<string, any>,
30
+ ): boolean {
31
+ if (tagName.indexOf('-') === -1) {
32
+ return Boolean(props && typeof props.is === 'string');
33
+ }
34
+
35
+ // These are reserved SVG and MathML elements.
36
+ // We don't mind this whitelist too much because we expect it to never grow.
37
+ // The alternative is to track the namespace in a few places which is convoluted.
38
+ // https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
39
+ if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName)) {
40
+ return false;
41
+ }
42
+
43
+ return true;
44
+ }
45
+
46
+ const styleOptions = {
47
+ reactCompat: true,
48
+ } as const;
49
+
50
+ /**
51
+ * Sets style prop.
52
+ *
53
+ * @param style - Inline style.
54
+ * @param props - Props object.
55
+ */
56
+ export function setStyleProp(style: string, props: Props): void {
57
+ if (typeof style !== 'string') {
58
+ return;
59
+ }
60
+
61
+ if (!style.trim()) {
62
+ props.style = {};
63
+ return;
64
+ }
65
+
66
+ try {
67
+ props.style = StyleToJS(style, styleOptions);
68
+ } catch (error) {
69
+ props.style = {};
70
+ }
71
+ }
72
+
73
+ /**
74
+ * @see https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html
75
+ */
76
+ export const PRESERVE_CUSTOM_ATTRIBUTES = Number(version.split('.')[0]) >= 16;
77
+
78
+ /**
79
+ * @see https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213
80
+ */
81
+ export const ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([
82
+ 'tr',
83
+ 'tbody',
84
+ 'thead',
85
+ 'tfoot',
86
+ 'colgroup',
87
+ 'table',
88
+ 'head',
89
+ 'html',
90
+ 'frameset',
91
+ ]);
92
+
93
+ /**
94
+ * Checks if the given node can contain text nodes
95
+ *
96
+ * @param node - Element node.
97
+ * @returns - Whether the node can contain text nodes.
98
+ */
99
+ export const canTextBeChildOfNode = (node: Element) =>
100
+ !ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name);
101
+
102
+ /**
103
+ * Returns the first argument as is.
104
+ *
105
+ * @param arg - The argument to be returned.
106
+ * @returns - The input argument `arg`.
107
+ */
108
+ export const returnFirstArg = (arg: any) => arg;
package/index.d.ts DELETED
@@ -1,60 +0,0 @@
1
- // TypeScript Version: 5.3
2
- /* eslint-disable no-undef, no-unused-vars */
3
-
4
- import {
5
- Comment,
6
- Element,
7
- Node,
8
- ProcessingInstruction,
9
- Text
10
- } from 'domhandler';
11
- import type { DomHandlerOptions } from 'domhandler';
12
- import htmlToDOM from 'html-dom-parser';
13
- import { ParserOptions } from 'htmlparser2';
14
-
15
- import attributesToProps from './lib/attributes-to-props';
16
- import domToReact from './lib/dom-to-react';
17
-
18
- export { attributesToProps, domToReact, htmlToDOM };
19
- export type HTMLParser2Options = ParserOptions & DomHandlerOptions;
20
- export { Comment, Element, ProcessingInstruction, Text };
21
- export type DOMNode = Comment | Element | Node | ProcessingInstruction | Text;
22
-
23
- export interface HTMLReactParserOptions {
24
- htmlparser2?: HTMLParser2Options;
25
-
26
- library?: {
27
- cloneElement: (
28
- element: JSX.Element,
29
- props?: object,
30
- ...children: any
31
- ) => JSX.Element;
32
- createElement: (type: any, props?: object, ...children: any) => JSX.Element;
33
- isValidElement: (element: any) => boolean;
34
- [key: string]: any;
35
- };
36
-
37
- replace?: (
38
- domNode: DOMNode
39
- ) => JSX.Element | object | void | undefined | null | false;
40
-
41
- transform?: (
42
- reactNode: JSX.Element | string,
43
- domNode: DOMNode,
44
- index: number
45
- ) => JSX.Element | string | null;
46
-
47
- trim?: boolean;
48
- }
49
-
50
- /**
51
- * Converts HTML string to JSX element(s).
52
- *
53
- * @param html - HTML string.
54
- * @param options - Parser options.
55
- * @returns - JSX element(s), empty array, or string.
56
- */
57
- export default function HTMLReactParser(
58
- html: string,
59
- options?: HTMLReactParserOptions
60
- ): ReturnType<typeof domToReact>;
package/index.js DELETED
@@ -1,50 +0,0 @@
1
- var domhandler = require('domhandler');
2
- var htmlToDOM = require('html-dom-parser').default;
3
-
4
- var attributesToProps = require('./lib/attributes-to-props');
5
- var domToReact = require('./lib/dom-to-react');
6
-
7
- // support backwards compatibility for ES Module
8
- htmlToDOM =
9
- /* istanbul ignore next */
10
- typeof htmlToDOM.default === 'function' ? htmlToDOM.default : htmlToDOM;
11
-
12
- var domParserOptions = { lowerCaseAttributeNames: false };
13
-
14
- /**
15
- * Converts HTML string to React elements.
16
- *
17
- * @param {string} html - HTML string.
18
- * @param {object} [options] - Parser options.
19
- * @param {object} [options.htmlparser2] - htmlparser2 options.
20
- * @param {object} [options.library] - Library for React, Preact, etc.
21
- * @param {Function} [options.replace] - Replace method.
22
- * @returns {JSX.Element|JSX.Element[]|string} - React element(s), empty array, or string.
23
- */
24
- function HTMLReactParser(html, options) {
25
- if (typeof html !== 'string') {
26
- throw new TypeError('First argument must be a string');
27
- }
28
- if (html === '') {
29
- return [];
30
- }
31
- options = options || {};
32
- return domToReact(
33
- htmlToDOM(html, options.htmlparser2 || domParserOptions),
34
- options
35
- );
36
- }
37
-
38
- HTMLReactParser.domToReact = domToReact;
39
- HTMLReactParser.htmlToDOM = htmlToDOM;
40
- HTMLReactParser.attributesToProps = attributesToProps;
41
-
42
- // domhandler
43
- HTMLReactParser.Comment = domhandler.Comment;
44
- HTMLReactParser.Element = domhandler.Element;
45
- HTMLReactParser.ProcessingInstruction = domhandler.ProcessingInstruction;
46
- HTMLReactParser.Text = domhandler.Text;
47
-
48
- // support CommonJS and ES Modules
49
- module.exports = HTMLReactParser;
50
- HTMLReactParser.default = HTMLReactParser;
package/index.mjs DELETED
@@ -1,13 +0,0 @@
1
- import HTMLReactParser from './index.js';
2
-
3
- export var domToReact = HTMLReactParser.domToReact;
4
- export var htmlToDOM = HTMLReactParser.htmlToDOM;
5
- export var attributesToProps = HTMLReactParser.attributesToProps;
6
-
7
- // domhandler
8
- export var Comment = HTMLReactParser.Comment;
9
- export var Element = HTMLReactParser.Element;
10
- export var ProcessingInstruction = HTMLReactParser.ProcessingInstruction;
11
- export var Text = HTMLReactParser.Text;
12
-
13
- export default HTMLReactParser;