Skip to content

Commit 5568ed7

Browse files
build(package): upgrade dependency html-dom-parser to 0.5.0
BREAKING CHANGE: upgrade dependency `html-dom-parser` to 0.5.0
1 parent 7f7695b commit 5568ed7

File tree

7 files changed

+77
-64
lines changed

7 files changed

+77
-64
lines changed

index.d.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
// TypeScript Version: 3.3
1+
// TypeScript Version: 4.1
2+
3+
import { ParserOptions } from 'htmlparser2';
4+
import {
5+
Comment,
6+
DomHandlerOptions,
7+
Element,
8+
ProcessingInstruction,
9+
Text
10+
} from 'domhandler';
11+
import htmlToDOM from 'html-dom-parser';
212

3-
import { DomElement, ParserOptions } from 'htmlparser2';
4-
import domToReact from './lib/dom-to-react';
513
import attributesToProps from './lib/attributes-to-props';
6-
import htmlToDOM from 'html-dom-parser';
14+
import domToReact from './lib/dom-to-react';
15+
16+
export { attributesToProps, domToReact, htmlToDOM };
17+
export type HTMLParser2Options = ParserOptions & DomHandlerOptions;
18+
export type DOMNode = Comment | Element | ProcessingInstruction | Text;
719

820
export interface HTMLReactParserOptions {
9-
htmlparser2?: ParserOptions;
21+
htmlparser2?: HTMLParser2Options;
1022

1123
library?: {
1224
cloneElement: (
@@ -20,7 +32,7 @@ export interface HTMLReactParserOptions {
2032
};
2133

2234
replace?: (
23-
domNode: DomElement
35+
domNode: DOMNode
2436
) => JSX.Element | object | void | undefined | null | false;
2537

2638
trim?: boolean;
@@ -33,11 +45,7 @@ export interface HTMLReactParserOptions {
3345
* @param options - Parser options.
3446
* @return - JSX element(s), empty array, or string.
3547
*/
36-
declare function HTMLReactParser(
48+
export default function HTMLReactParser(
3749
html: string,
3850
options?: HTMLReactParserOptions
3951
): ReturnType<typeof domToReact>;
40-
41-
export { DomElement, ParserOptions, domToReact, htmlToDOM, attributesToProps };
42-
43-
export default HTMLReactParser;

lib/attributes-to-props.d.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// TypeScript Version: 3.3
1+
// TypeScript Version: 4.1
2+
3+
export type Attributes = Record<string, string>;
4+
export type Props = Attributes;
25

36
/**
47
* Converts HTML/SVG DOM attributes to React props.
58
*
6-
* @param attributes - The HTML/SVG DOM attributes.
7-
* @returns - The React props.
9+
* @param attributes - HTML/SVG DOM attributes.
10+
* @return - React props.
811
*/
9-
export default function attributesToProps(
10-
attributes: { [key: string]: string }
11-
): { [key: string]: string };
12+
export default function attributesToProps(attributes: Attributes): Props;

lib/dom-to-react.d.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
// TypeScript Version: 3.3
1+
// TypeScript Version: 4.1
22

3-
import { HTMLReactParserOptions } from '..';
4-
import { DomElement } from 'domhandler';
3+
import { DOMNode, HTMLReactParserOptions } from '..';
4+
5+
export { DOMNode, HTMLReactParserOptions };
56

67
/**
78
* Converts DOM nodes to JSX element(s).
89
*
9-
* @param nodes - DOM nodes.
10-
* @param options - Parser options.
11-
* @returns - JSX element(s).
10+
* @param nodes - DOM nodes.
11+
* @param options - Parser options.
12+
* @return - JSX element(s).
1213
*/
1314
export default function domToReact(
14-
nodes: DomElement[],
15+
nodes: DOMNode[],
1516
options?: HTMLReactParserOptions
16-
): JSX.Element | JSX.Element[];
17+
): string | JSX.Element | JSX.Element[];

lib/dom-to-react.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var utilities = require('./utilities');
55
var setStyleProp = utilities.setStyleProp;
66

77
/**
8-
* Converts DOM nodes to React elements.
8+
* Converts DOM nodes to JSX element(s).
99
*
1010
* @param {DomElement[]} nodes - DOM nodes.
1111
* @param {object} [options={}] - Options.
1212
* @param {Function} [options.replace] - Replacer.
1313
* @param {object} [options.library] - Library (React/Preact/etc.).
14-
* @return {string|ReactElement|ReactElement[]}
14+
* @return {string|JSX.Element|JSX.Element[]}
1515
*/
1616
function domToReact(nodes, options) {
1717
options = options || {};

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
"dom"
3333
],
3434
"dependencies": {
35-
"@types/htmlparser2": "3.10.2",
36-
"html-dom-parser": "0.3.0",
35+
"html-dom-parser": "0.5.0",
3736
"react-property": "1.0.1",
3837
"style-to-js": "1.1.0"
3938
},

test/types/index.tsx

+21-20
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,54 @@ import parse, {
33
domToReact,
44
htmlToDOM
55
} from 'html-react-parser';
6+
import { Element } from 'domhandler';
67
import * as React from 'react';
78

89
// $ExpectError
910
parse();
1011

11-
// $ExpectType Element | Element[]
12+
// $ExpectType string | Element | Element[]
1213
parse('');
1314

14-
// $ExpectType Element | Element[]
15+
// $ExpectType string | Element | Element[]
1516
parse('string');
1617

17-
// $ExpectType Element | Element[]
18+
// $ExpectType string | Element | Element[]
1819
parse('<p>text</p>');
1920

20-
// $ExpectType Element | Element[]
21+
// $ExpectType string | Element | Element[]
2122
parse('<li>1</li><li>2</li>');
2223

23-
// $ExpectType Element | Element[]
24+
// $ExpectType string | Element | Element[]
2425
parse('<br id="replace">', {
2526
replace: domNode => {
26-
if (domNode.attribs && domNode.attribs.id === 'replace') {
27+
if (domNode instanceof Element && domNode.attribs.id === 'replace') {
2728
return <span>replaced</span>;
2829
}
2930
}
3031
});
3132

32-
// $ExpectType Element | Element[]
33+
// $ExpectType string | Element | Element[]
3334
parse('<br id="remove">', {
34-
replace({ attribs }) {
35-
return attribs && attribs.id === 'remove' && <></>;
35+
replace: domNode => {
36+
if (domNode instanceof Element && domNode.attribs.id === 'remove') {
37+
return <></>;
38+
}
3639
}
3740
});
3841

39-
let options: HTMLReactParserOptions;
40-
41-
options = {
42-
replace: node => {
43-
if (node.attribs && node.attribs.id === 'header') {
42+
const options: HTMLReactParserOptions = {
43+
replace: domNode => {
44+
if (domNode instanceof Element && domNode.attribs.id === 'header') {
4445
return;
4546
}
4647
}
4748
};
4849

49-
// $ExpectType Element | Element[]
50+
// $ExpectType string | Element | Element[]
5051
parse('<a id="header" href="#">Heading</a>', options);
5152

52-
// $ExpectType Element | Element[]
53+
// $ExpectType string | Element | Element[]
5354
parse('<hr>', {
5455
library: {
5556
cloneElement: (element, props, children) =>
@@ -60,7 +61,7 @@ parse('<hr>', {
6061
}
6162
});
6263

63-
// $ExpectType Element | Element[]
64+
// $ExpectType string | Element | Element[]
6465
parse('<p/><p/>', {
6566
htmlparser2: {
6667
xmlMode: true,
@@ -72,11 +73,11 @@ parse('<p/><p/>', {
7273
}
7374
});
7475

75-
// $ExpectType Element | Element[]
76+
// $ExpectType string | Element | Element[]
7677
parse('\t<p>text \r</p>\n', { trim: true });
7778

78-
// $ExpectType DomElement[]
79+
// $ExpectType DOMNode[]
7980
const domNodes = htmlToDOM('<div>text</div>');
8081

81-
// $ExpectType Element | Element[]
82+
// $ExpectType string | Element | Element[]
8283
domToReact(domNodes);

test/types/lib/dom-to-react.tsx

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
1-
import { DomElement } from 'domhandler';
1+
import { Element } from 'domhandler';
22
import { HTMLReactParserOptions } from 'html-react-parser';
33
import domToReact from 'html-react-parser/lib/dom-to-react';
44
import * as React from 'react';
55
import htmlToDOM from 'html-dom-parser';
66

7-
// $ExpectType DomElement[]
7+
// $ExpectType DOMNode[]
88
htmlToDOM('<div>text</div>');
99

10-
// $ExpectType Element | Element[]
10+
// $ExpectType string | Element | Element[]
11+
domToReact(htmlToDOM('string'));
12+
13+
// $ExpectType string | Element | Element[]
1114
domToReact(htmlToDOM('<div>text</div>'));
1215

13-
// $ExpectType Element | Element[]
16+
// $ExpectType string | Element | Element[]
1417
domToReact(htmlToDOM('<p id="replace">text</p>'), {
1518
replace: domNode => {
16-
if (domNode.attribs && domNode.attribs.id === 'replace') {
19+
if (domNode instanceof Element && domNode.attribs.id === 'replace') {
1720
return <span>replaced</span>;
1821
}
1922
}
2023
});
2124

22-
let options: HTMLReactParserOptions;
23-
24-
options = {
25-
replace({ attribs }) {
26-
return attribs && attribs.id === 'remove' && <></>;
25+
const options: HTMLReactParserOptions = {
26+
replace: domNode => {
27+
if (domNode instanceof Element && domNode.attribs.id === 'remove') {
28+
return <></>;
29+
}
2730
}
2831
};
2932

30-
// $ExpectType Element | Element[]
33+
// $ExpectType string | Element | Element[]
3134
domToReact(htmlToDOM('<p><br id="remove"></p>'), options);
3235

33-
// $ExpectType Element | Element[]
36+
// $ExpectType string | Element | Element[]
3437
domToReact(htmlToDOM('<a id="header" href="#">Heading</a>'), {
35-
replace: node => {
36-
if (node.attribs && node.attribs.id === 'header') {
38+
replace: domNode => {
39+
if (domNode instanceof Element && domNode.attribs.id === 'header') {
3740
return;
3841
}
3942
}
4043
});
4144

42-
// $ExpectType Element | Element[]
45+
// $ExpectType string | Element | Element[]
4346
domToReact(htmlToDOM('\t<p>text \r</p>\n'), { trim: true });

0 commit comments

Comments
 (0)