@octanejs/html-react-parser 0.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.
- package/LICENSE +22 -0
- package/README.md +11 -0
- package/UPSTREAM.md +57 -0
- package/package.json +52 -0
- package/src/attributes-to-props.ts +111 -0
- package/src/dom-to-react.ts +165 -0
- package/src/index.ts +40 -0
- package/src/types.ts +38 -0
- package/src/utilities.ts +113 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Menglin "Mark" Xu <mark@remarkablemark.org>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @octanejs/html-react-parser
|
|
2
|
+
|
|
3
|
+
Octane binding for [`html-react-parser@6.1.7`](https://github.com/remarkablemark/html-react-parser).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import parse from '@octanejs/html-react-parser';
|
|
7
|
+
|
|
8
|
+
const tree = parse('<p>hello <em>world</em></p>');
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The default `library` is Octane `createElement` / `cloneElement` / `isValidElement`. See [UPSTREAM.md](./UPSTREAM.md).
|
package/UPSTREAM.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Upstream provenance
|
|
2
|
+
|
|
3
|
+
`@octanejs/html-react-parser` is pinned to `html-react-parser@6.1.7`.
|
|
4
|
+
|
|
5
|
+
- repository: https://github.com/remarkablemark/html-react-parser
|
|
6
|
+
- tag: `v6.1.7`
|
|
7
|
+
- tag commit: `7c930d10b0b112132a80f782930c3597b13927dc`
|
|
8
|
+
- advertised range: `6.1.x`
|
|
9
|
+
- license: MIT
|
|
10
|
+
- npm integrity: `sha512-TU3KHM2bDXHetJ7mKddkwdMiMJZdgpq2ZmbzGwVo7O4VECTbvczXKwjeA7+LNvy1iAr2/av351VvIis1OPEGfg==`
|
|
11
|
+
- npm shasum: `fdf1e5e0686e4233389ecd226aa3fc3ab3dcc251`
|
|
12
|
+
|
|
13
|
+
## Source boundary
|
|
14
|
+
|
|
15
|
+
- `upstream/` contains the canonical `src/`, LICENSE, package metadata, and
|
|
16
|
+
configs from the tag. The canonical `__tests__/` tree at the same commit was
|
|
17
|
+
inspected case-for-case for the adapted files listed below.
|
|
18
|
+
- `upstream/npm/` is the published tarball.
|
|
19
|
+
- Octane `src/` mirrors `upstream/src/`.
|
|
20
|
+
|
|
21
|
+
Vendored evidence is development-only and excluded from package `files`.
|
|
22
|
+
|
|
23
|
+
## Export crosswalk
|
|
24
|
+
|
|
25
|
+
| Upstream export | Octane status | Evidence / divergence |
|
|
26
|
+
| --- | --- | --- |
|
|
27
|
+
| default `HTMLReactParser` / `parse` | Ported | `src/index.ts`; `tests/index.test.ts` |
|
|
28
|
+
| `attributesToProps` | Ported | `tests/attributes-to-props/index.test.ts` |
|
|
29
|
+
| `domToReact` | Ported | `src/dom-to-react.ts`; `tests/index.test.ts` |
|
|
30
|
+
| `htmlToDOM` | Reused (`html-dom-parser@8.0.2`) | `src/index.ts` |
|
|
31
|
+
| `Comment` / `Element` / `ProcessingInstruction` / `Text` | Reused (`domhandler@6.0.1`) | `tests/index.test.ts` |
|
|
32
|
+
| `HTMLReactParserOptions` | Ported | `src/types.ts`; ElementDescriptor/OctaneNode |
|
|
33
|
+
| `library` option | Ported | defaults to Octane element APIs |
|
|
34
|
+
|
|
35
|
+
## Upstream test disposition
|
|
36
|
+
|
|
37
|
+
| Upstream artifact | Disposition |
|
|
38
|
+
| --- | --- |
|
|
39
|
+
| `__tests__/index.test.tsx` | Adapted case-for-case in `tests/index.test.ts`; element snapshots use Octane `renderToStaticMarkup` output or equivalent public descriptor fields. |
|
|
40
|
+
| `__tests__/dom-to-react/index.test.tsx` | Adapted case-for-case in `tests/dom-to-react/index.test.ts`; JSX/snapshots use `createElement`, descriptor assertions, and static HTML. |
|
|
41
|
+
| `__tests__/dom-to-react/custom-attributes.test.ts` | Adapted in `tests/dom-to-react/custom-attributes.test.ts`; the React-15 version gate is an explicit Octane divergence and custom attributes remain preserved. |
|
|
42
|
+
| `__tests__/attributes-to-props/index.test.ts` | Ported as-is to `tests/attributes-to-props/index.test.ts` |
|
|
43
|
+
| `__tests__/attributes-to-props/preserve-custom-attributes.test.ts` | Ported as-is (vi.mock) |
|
|
44
|
+
| `__tests__/utilities.test.ts` | Adapted case-for-case in `tests/utilities.test.ts`; `PRESERVE_CUSTOM_ATTRIBUTES` asserts Octane's always-true contract. |
|
|
45
|
+
| `__tests__/options/trim.test.ts` | Adapted case-for-case in `tests/options/trim.test.ts`; HTML assertions are retained. |
|
|
46
|
+
| `__tests__/options/trusted-type-policy.test.ts` | Adapted case-for-case in `tests/options/trusted-type-policy.test.ts`. |
|
|
47
|
+
| `__tests__/esm/*` | Out of scope: published CJS/ESM node:test after upstream build |
|
|
48
|
+
| `__tests__/integration/` | Out of scope: webpack/build integration |
|
|
49
|
+
| vitest browser config | Out of scope: browser runner |
|
|
50
|
+
|
|
51
|
+
## Intentional divergences
|
|
52
|
+
|
|
53
|
+
- Default `library` is Octane, not React.
|
|
54
|
+
- `PRESERVE_CUSTOM_ATTRIBUTES` is `true` regardless of `octane.version`.
|
|
55
|
+
- Octane serializes style objects through CSSOM. Custom-element markup may
|
|
56
|
+
differ in vendor-prefix spelling and spacing from React snapshots; the
|
|
57
|
+
parsed `style` object is the parser contract.
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/html-react-parser",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Octane binding for html-react-parser 6.1.7.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.22.2"
|
|
9
|
+
},
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Dominic Gannaway",
|
|
12
|
+
"email": "dg@domgan.com"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
20
|
+
"directory": "packages/html-react-parser"
|
|
21
|
+
},
|
|
22
|
+
"main": "./src/index.ts",
|
|
23
|
+
"module": "./src/index.ts",
|
|
24
|
+
"types": "./src/index.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"README.md",
|
|
28
|
+
"UPSTREAM.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"exports": {
|
|
32
|
+
".": "./src/index.ts",
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"domhandler": "6.0.1",
|
|
37
|
+
"html-dom-parser": "8.0.2",
|
|
38
|
+
"react-property": "2.0.2",
|
|
39
|
+
"style-to-js": "2.0.2"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"octane": "0.1.45"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"vitest": "^4.1.10",
|
|
46
|
+
"octane": "0.1.45"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"test": "vitest run --root ../.. --config vitest.config.js --project html-react-parser",
|
|
50
|
+
"typecheck": "tsrx-tsc --noEmit -p tsconfig.json && tsrx-tsc --noEmit -p tsconfig.tests.json"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BOOLEAN,
|
|
3
|
+
getPropertyInfo,
|
|
4
|
+
isCustomAttribute,
|
|
5
|
+
OVERLOADED_BOOLEAN,
|
|
6
|
+
possibleStandardNames,
|
|
7
|
+
} from 'react-property';
|
|
8
|
+
|
|
9
|
+
import { PRESERVE_CUSTOM_ATTRIBUTES, setStyleProp } from './utilities';
|
|
10
|
+
|
|
11
|
+
// https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components
|
|
12
|
+
// https://developer.mozilla.org/docs/Web/HTML/Attributes
|
|
13
|
+
const UNCONTROLLED_COMPONENT_ATTRIBUTES = ['checked', 'value'] as const;
|
|
14
|
+
const UNCONTROLLED_COMPONENT_NAMES = ['input', 'select', 'textarea'] as const;
|
|
15
|
+
|
|
16
|
+
type UncontrolledComponentAttributes = (typeof UNCONTROLLED_COMPONENT_ATTRIBUTES)[number];
|
|
17
|
+
|
|
18
|
+
type UncontrolledComponentNames = (typeof UNCONTROLLED_COMPONENT_NAMES)[number];
|
|
19
|
+
|
|
20
|
+
const valueOnlyInputs = {
|
|
21
|
+
reset: true,
|
|
22
|
+
submit: true,
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
export type ValueOnlyInputsKeys = keyof typeof valueOnlyInputs;
|
|
26
|
+
|
|
27
|
+
export type Attributes = Record<PropertyKey, string>;
|
|
28
|
+
|
|
29
|
+
export type Props = Record<PropertyKey, string | boolean> & {
|
|
30
|
+
dangerouslySetInnerHTML?: {
|
|
31
|
+
__html: string;
|
|
32
|
+
};
|
|
33
|
+
key?: string | number;
|
|
34
|
+
style?: Record<PropertyKey, string>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Converts HTML/SVG DOM attributes to React props.
|
|
39
|
+
*
|
|
40
|
+
* @param attributes - HTML/SVG DOM attributes.
|
|
41
|
+
* @param nodeName - DOM node name.
|
|
42
|
+
* @returns - React props.
|
|
43
|
+
*/
|
|
44
|
+
export default function attributesToProps(attributes: Attributes = {}, nodeName?: string): Props {
|
|
45
|
+
const props: Props = {};
|
|
46
|
+
|
|
47
|
+
const isInputValueOnly = Boolean(
|
|
48
|
+
attributes.type && valueOnlyInputs[attributes.type as ValueOnlyInputsKeys],
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
for (const attributeName in attributes) {
|
|
52
|
+
const attributeValue = attributes[attributeName];
|
|
53
|
+
|
|
54
|
+
// ARIA (aria-*) or custom data (data-*) attribute
|
|
55
|
+
if (isCustomAttribute(attributeName)) {
|
|
56
|
+
props[attributeName] = attributeValue;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// convert HTML/SVG attribute to React prop
|
|
61
|
+
const attributeNameLowerCased = attributeName.toLowerCase();
|
|
62
|
+
let propName = getPropName(attributeNameLowerCased);
|
|
63
|
+
|
|
64
|
+
if (propName) {
|
|
65
|
+
const propertyInfo = getPropertyInfo(propName) as { type: number } | undefined;
|
|
66
|
+
|
|
67
|
+
// convert attribute to uncontrolled component prop (e.g., `value` to `defaultValue`)
|
|
68
|
+
if (
|
|
69
|
+
UNCONTROLLED_COMPONENT_ATTRIBUTES.includes(propName as UncontrolledComponentAttributes) &&
|
|
70
|
+
UNCONTROLLED_COMPONENT_NAMES.includes(nodeName as UncontrolledComponentNames) &&
|
|
71
|
+
!isInputValueOnly
|
|
72
|
+
) {
|
|
73
|
+
propName = getPropName('default' + attributeNameLowerCased);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
props[propName] = attributeValue;
|
|
77
|
+
|
|
78
|
+
switch (propertyInfo?.type) {
|
|
79
|
+
case BOOLEAN:
|
|
80
|
+
props[propName] = true;
|
|
81
|
+
break;
|
|
82
|
+
case OVERLOADED_BOOLEAN:
|
|
83
|
+
if (attributeValue === '') {
|
|
84
|
+
props[propName] = true;
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// preserve custom attribute if React >=16
|
|
92
|
+
if (PRESERVE_CUSTOM_ATTRIBUTES) {
|
|
93
|
+
props[attributeName] = attributeValue;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// transform inline style to object
|
|
98
|
+
setStyleProp(attributes.style, props);
|
|
99
|
+
|
|
100
|
+
return props;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Gets prop name from lowercased attribute name.
|
|
105
|
+
*
|
|
106
|
+
* @param attributeName - Lowercased attribute name.
|
|
107
|
+
* @returns - Prop name.
|
|
108
|
+
*/
|
|
109
|
+
function getPropName(attributeName: string): string {
|
|
110
|
+
return possibleStandardNames[attributeName];
|
|
111
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
|
2
|
+
|
|
3
|
+
import { Element as DomHandlerElement } from 'domhandler';
|
|
4
|
+
import type { DOMNode, Element, Text } from 'html-dom-parser';
|
|
5
|
+
import type { ElementDescriptor } from 'octane';
|
|
6
|
+
import { cloneElement, createElement, isValidElement } from 'octane';
|
|
7
|
+
|
|
8
|
+
import type { Props } from './attributes-to-props';
|
|
9
|
+
import attributesToProps from './attributes-to-props';
|
|
10
|
+
import type { HTMLReactParserOptions } from './types';
|
|
11
|
+
import {
|
|
12
|
+
canTextBeChildOfNode,
|
|
13
|
+
isCustomComponent,
|
|
14
|
+
PRESERVE_CUSTOM_ATTRIBUTES,
|
|
15
|
+
returnFirstArg,
|
|
16
|
+
setStyleProp,
|
|
17
|
+
} from './utilities';
|
|
18
|
+
|
|
19
|
+
const React = {
|
|
20
|
+
cloneElement,
|
|
21
|
+
createElement,
|
|
22
|
+
isValidElement,
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Converts DOM nodes to JSX element(s).
|
|
27
|
+
*
|
|
28
|
+
* @param nodes - DOM nodes.
|
|
29
|
+
* @param options - Options.
|
|
30
|
+
* @returns - String or JSX element(s).
|
|
31
|
+
*/
|
|
32
|
+
export default function domToReact(
|
|
33
|
+
nodes: DOMNode[],
|
|
34
|
+
options: HTMLReactParserOptions = {},
|
|
35
|
+
): string | ElementDescriptor | ElementDescriptor[] {
|
|
36
|
+
const reactElements: ElementDescriptor[] = [];
|
|
37
|
+
|
|
38
|
+
const hasReplace = typeof options.replace === 'function';
|
|
39
|
+
const transform = options.transform ?? returnFirstArg;
|
|
40
|
+
const { cloneElement, createElement, isValidElement } = options.library ?? React;
|
|
41
|
+
|
|
42
|
+
const nodesLength = nodes.length;
|
|
43
|
+
|
|
44
|
+
normalizeDOMNodes(nodes);
|
|
45
|
+
|
|
46
|
+
for (let index = 0; index < nodesLength; index++) {
|
|
47
|
+
const node = nodes[index];
|
|
48
|
+
|
|
49
|
+
// replace with custom React element (if present)
|
|
50
|
+
if (hasReplace) {
|
|
51
|
+
let replaceElement = options.replace?.(node, index) as ElementDescriptor;
|
|
52
|
+
|
|
53
|
+
if (isValidElement(replaceElement)) {
|
|
54
|
+
// set "key" prop for sibling elements
|
|
55
|
+
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
56
|
+
if (nodesLength > 1) {
|
|
57
|
+
replaceElement = cloneElement(replaceElement, {
|
|
58
|
+
key: replaceElement.key ?? index,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
reactElements.push(transform(replaceElement, node, index) as ElementDescriptor);
|
|
63
|
+
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (node.type === 'text') {
|
|
69
|
+
const isWhitespace = !node.data.trim().length;
|
|
70
|
+
|
|
71
|
+
// We have a whitespace node that can't be nested in its parent
|
|
72
|
+
// so skip it
|
|
73
|
+
if (isWhitespace && node.parent && !canTextBeChildOfNode(node.parent as Element)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Trim is enabled and we have a whitespace node
|
|
78
|
+
// so skip it
|
|
79
|
+
if (options.trim && isWhitespace) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// We have a text node that's not whitespace and it can be nested
|
|
84
|
+
// in its parent so add it to the results
|
|
85
|
+
reactElements.push(transform(node.data, node, index) as ElementDescriptor);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const element = node as Element;
|
|
90
|
+
let props: Props = {};
|
|
91
|
+
|
|
92
|
+
if (skipAttributesToProps(element)) {
|
|
93
|
+
setStyleProp(element.attribs.style, element.attribs);
|
|
94
|
+
props = element.attribs;
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
96
|
+
} else if (element.attribs) {
|
|
97
|
+
props = attributesToProps(element.attribs, element.name);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let children: ReturnType<typeof domToReact> | undefined;
|
|
101
|
+
|
|
102
|
+
switch (node.type) {
|
|
103
|
+
case 'script':
|
|
104
|
+
case 'style':
|
|
105
|
+
// prevent text in <script> or <style> from being escaped
|
|
106
|
+
// https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html
|
|
107
|
+
if (node.children[0]) {
|
|
108
|
+
props.dangerouslySetInnerHTML = {
|
|
109
|
+
__html: (node.children[0] as Text).data,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
|
|
114
|
+
case 'tag':
|
|
115
|
+
// setting textarea value in children is an antipattern in React
|
|
116
|
+
// https://react.dev/reference/react-dom/components/textarea#caveats
|
|
117
|
+
if (node.name === 'textarea' && node.children[0]) {
|
|
118
|
+
props.defaultValue = (node.children[0] as Text).data;
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
120
|
+
} else if (node.children?.length) {
|
|
121
|
+
// continue recursion of creating React elements (if applicable)
|
|
122
|
+
children = domToReact(node.children as Text[], options);
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
|
|
126
|
+
// skip all other cases (e.g., comment)
|
|
127
|
+
default:
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// set "key" prop for sibling elements
|
|
132
|
+
// https://react.dev/learn/rendering-lists#rules-of-keys
|
|
133
|
+
if (nodesLength > 1) {
|
|
134
|
+
props.key = index;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
reactElements.push(
|
|
138
|
+
transform(createElement(node.name, props, children), node, index) as ElementDescriptor,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return reactElements.length === 1 ? reactElements[0] : reactElements;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function normalizeDOMNodes(nodes: DOMNode[]): void {
|
|
146
|
+
for (const node of nodes) {
|
|
147
|
+
if (node.type === 'tag' || node.type === 'script' || node.type === 'style') {
|
|
148
|
+
Object.setPrototypeOf(node, DomHandlerElement.prototype);
|
|
149
|
+
normalizeDOMNodes(node.children as DOMNode[]);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Determines whether DOM element attributes should be transformed to props.
|
|
156
|
+
* Web Components should not have their attributes transformed except for `style`.
|
|
157
|
+
*
|
|
158
|
+
* @param node - Element node.
|
|
159
|
+
* @returns - Whether the node attributes should be converted to props.
|
|
160
|
+
*/
|
|
161
|
+
function skipAttributesToProps(node: Element): boolean {
|
|
162
|
+
return (
|
|
163
|
+
PRESERVE_CUSTOM_ATTRIBUTES && node.type === 'tag' && isCustomComponent(node.name, node.attribs)
|
|
164
|
+
);
|
|
165
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import htmlToDOM from 'html-dom-parser';
|
|
2
|
+
|
|
3
|
+
import attributesToProps from './attributes-to-props';
|
|
4
|
+
import domToReact from './dom-to-react';
|
|
5
|
+
import type { HTMLReactParserOptions } from './types';
|
|
6
|
+
|
|
7
|
+
export { Comment, Element, ProcessingInstruction, Text } from 'domhandler';
|
|
8
|
+
export type { DOMNode } from 'html-dom-parser';
|
|
9
|
+
|
|
10
|
+
export type { HTMLReactParserOptions };
|
|
11
|
+
export { attributesToProps, domToReact, htmlToDOM };
|
|
12
|
+
|
|
13
|
+
const domParserOptions = { lowerCaseAttributeNames: false } as const;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Converts HTML string to React elements.
|
|
17
|
+
*
|
|
18
|
+
* @param html - HTML string.
|
|
19
|
+
* @param options - Parser options.
|
|
20
|
+
* @returns - React element(s), empty array, or string.
|
|
21
|
+
*/
|
|
22
|
+
export default function HTMLReactParser(
|
|
23
|
+
html: string,
|
|
24
|
+
options?: HTMLReactParserOptions,
|
|
25
|
+
): ReturnType<typeof domToReact> {
|
|
26
|
+
if (typeof html !== 'string') {
|
|
27
|
+
throw new TypeError('First argument must be a string');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!html) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const htmlToDOMOptions = {
|
|
35
|
+
...(options?.htmlparser2 ?? domParserOptions),
|
|
36
|
+
trustedTypePolicy: options?.trustedTypePolicy,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return domToReact(htmlToDOM(html, htmlToDOMOptions), options);
|
|
40
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { DOMNode, HTMLDOMParserOptions, TrustedTypePolicy } from 'html-dom-parser';
|
|
2
|
+
import type { ElementDescriptor, OctaneNode } from 'octane';
|
|
3
|
+
|
|
4
|
+
export interface HTMLReactParserOptions {
|
|
5
|
+
htmlparser2?: Omit<HTMLDOMParserOptions, 'trustedTypePolicy'>;
|
|
6
|
+
trustedTypePolicy?: TrustedTypePolicy;
|
|
7
|
+
|
|
8
|
+
library?: {
|
|
9
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
10
|
+
cloneElement: (
|
|
11
|
+
element: ElementDescriptor,
|
|
12
|
+
props?: object,
|
|
13
|
+
...children: any[]
|
|
14
|
+
) => ElementDescriptor;
|
|
15
|
+
|
|
16
|
+
createElement: (type: any, props?: object, ...children: any[]) => ElementDescriptor;
|
|
17
|
+
|
|
18
|
+
isValidElement: (element: any) => boolean;
|
|
19
|
+
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
replace?: (
|
|
25
|
+
domNode: DOMNode,
|
|
26
|
+
index: number,
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
28
|
+
) => ElementDescriptor | string | null | boolean | object | void;
|
|
29
|
+
|
|
30
|
+
transform?: (
|
|
31
|
+
reactNode: OctaneNode,
|
|
32
|
+
domNode: DOMNode,
|
|
33
|
+
index: number,
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
35
|
+
) => ElementDescriptor | string | null | void;
|
|
36
|
+
|
|
37
|
+
trim?: boolean;
|
|
38
|
+
}
|
package/src/utilities.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { Element } from 'html-dom-parser';
|
|
2
|
+
import StyleToJS from 'style-to-js';
|
|
3
|
+
|
|
4
|
+
import type { Props } from './attributes-to-props';
|
|
5
|
+
|
|
6
|
+
const RESERVED_SVG_MATHML_ELEMENTS = new Set([
|
|
7
|
+
'annotation-xml',
|
|
8
|
+
'color-profile',
|
|
9
|
+
'font-face',
|
|
10
|
+
'font-face-src',
|
|
11
|
+
'font-face-uri',
|
|
12
|
+
'font-face-format',
|
|
13
|
+
'font-face-name',
|
|
14
|
+
'missing-glyph',
|
|
15
|
+
] as const);
|
|
16
|
+
|
|
17
|
+
type ReservedSvgMathmlElements =
|
|
18
|
+
typeof RESERVED_SVG_MATHML_ELEMENTS extends Set<infer T> ? T : never;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Check if a tag is a custom component.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://github.com/facebook/react/blob/v16.6.3/packages/react-dom/src/shared/isCustomComponent.js}
|
|
24
|
+
*
|
|
25
|
+
* @param tagName - Tag name.
|
|
26
|
+
* @param props - Props passed to the element.
|
|
27
|
+
* @returns - Whether the tag is custom component.
|
|
28
|
+
*/
|
|
29
|
+
export function isCustomComponent(tagName: string, props?: Record<PropertyKey, string>): boolean {
|
|
30
|
+
if (!tagName.includes('-')) {
|
|
31
|
+
return Boolean(props && typeof props.is === 'string');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// These are reserved SVG and MathML elements.
|
|
35
|
+
// We don't mind this whitelist too much because we expect it to never grow.
|
|
36
|
+
// The alternative is to track the namespace in a few places which is convoluted.
|
|
37
|
+
// https://w3c.github.io/webcomponents/spec/custom/#custom-elements-core-concepts
|
|
38
|
+
if (RESERVED_SVG_MATHML_ELEMENTS.has(tagName as ReservedSvgMathmlElements)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const styleOptions = {
|
|
46
|
+
reactCompat: true,
|
|
47
|
+
} as const;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Sets style prop.
|
|
51
|
+
*
|
|
52
|
+
* @param style - Inline style.
|
|
53
|
+
* @param props - Props object.
|
|
54
|
+
*/
|
|
55
|
+
export function setStyleProp(style: string, props: Props): void {
|
|
56
|
+
if (typeof style !== 'string') {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!style.trim()) {
|
|
61
|
+
props.style = {};
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
props.style = StyleToJS(style, styleOptions);
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
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
|
+
// OCTANE DIVERGENCE: octane.version is 0.x, but Octane matches React 16+ DOM
|
|
77
|
+
// attribute handling, so custom attributes are preserved.
|
|
78
|
+
export const PRESERVE_CUSTOM_ATTRIBUTES = true;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @see https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213
|
|
82
|
+
*/
|
|
83
|
+
export const ELEMENTS_WITH_NO_TEXT_CHILDREN = new Set([
|
|
84
|
+
'tr',
|
|
85
|
+
'tbody',
|
|
86
|
+
'thead',
|
|
87
|
+
'tfoot',
|
|
88
|
+
'colgroup',
|
|
89
|
+
'table',
|
|
90
|
+
'head',
|
|
91
|
+
'html',
|
|
92
|
+
'frameset',
|
|
93
|
+
] as const);
|
|
94
|
+
|
|
95
|
+
type ElementsWithNoTextChildren =
|
|
96
|
+
typeof ELEMENTS_WITH_NO_TEXT_CHILDREN extends Set<infer T> ? T : never;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Checks if the given node can contain text nodes
|
|
100
|
+
*
|
|
101
|
+
* @param node - Element node.
|
|
102
|
+
* @returns - Whether the node can contain text nodes.
|
|
103
|
+
*/
|
|
104
|
+
export const canTextBeChildOfNode = (node: Element) =>
|
|
105
|
+
!ELEMENTS_WITH_NO_TEXT_CHILDREN.has(node.name as ElementsWithNoTextChildren);
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns the first argument as is.
|
|
109
|
+
*
|
|
110
|
+
* @param arg - The argument to be returned.
|
|
111
|
+
* @returns - The input argument `arg`.
|
|
112
|
+
*/
|
|
113
|
+
export const returnFirstArg = <T>(arg: T): T => arg;
|