Skip to content

Commit ac4aff3

Browse files
fix(index): include domhandler Node in DOMNode type
Fixes #207
1 parent a4f945b commit ac4aff3

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

index.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Comment,
66
DomHandlerOptions,
77
Element,
8+
Node,
89
ProcessingInstruction,
910
Text
1011
} from 'domhandler';
@@ -15,8 +16,8 @@ import domToReact from './lib/dom-to-react';
1516

1617
export { attributesToProps, domToReact, htmlToDOM };
1718
export type HTMLParser2Options = ParserOptions & DomHandlerOptions;
18-
export { Comment, Element, ProcessingInstruction, Text };
19-
export type DOMNode = Comment | Element | ProcessingInstruction | Text;
19+
export { Comment, Element, Node, ProcessingInstruction, Text };
20+
export type DOMNode = Comment | Element | Node | ProcessingInstruction | Text;
2021

2122
export interface HTMLReactParserOptions {
2223
htmlparser2?: HTMLParser2Options;

test/types/index.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,24 @@ parse('<br id="remove">', {
3939
}
4040
});
4141

42-
const options: HTMLReactParserOptions = {
42+
let options: HTMLReactParserOptions;
43+
44+
options = {
4345
replace: domNode => {
4446
if (domNode instanceof Element && domNode.attribs.id === 'header') {
4547
return;
4648
}
4749
}
4850
};
4951

52+
options = {
53+
replace: domNode => {
54+
if (domNode instanceof Element) {
55+
return <>{domToReact(domNode.children)}</>;
56+
}
57+
}
58+
};
59+
5060
// $ExpectType string | Element | Element[]
5161
parse('<a id="header" href="#">Heading</a>', options);
5262

@@ -76,7 +86,7 @@ parse('<p/><p/>', {
7686
// $ExpectType string | Element | Element[]
7787
parse('\t<p>text \r</p>\n', { trim: true });
7888

79-
// $ExpectType DOMNode[]
89+
// $ExpectType (Comment | Element | ProcessingInstruction | Text)[]
8090
const domNodes = htmlToDOM('<div>text</div>');
8191

8292
// $ExpectType string | Element | Element[]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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 DOMNode[]
7+
// $ExpectType (Comment | Element | ProcessingInstruction | Text)[]
88
htmlToDOM('<div>text</div>');
99

1010
// $ExpectType string | Element | Element[]

0 commit comments

Comments
 (0)