|
| 1 | +import { DomElement } from 'domhandler'; |
1 | 2 | import { HTMLReactParserOptions } from 'html-react-parser';
|
2 | 3 | import domToReact from 'html-react-parser/lib/dom-to-react';
|
3 | 4 | import * as React from 'react';
|
4 | 5 | import htmlToDOM from 'html-dom-parser';
|
5 | 6 |
|
6 |
| -/* $ExpectType ReactElement | ReactElement[] */ |
7 |
| -domToReact(htmlToDOM('<div>text</div>')); |
| 7 | +// $ExpectType DomElement[] |
| 8 | +htmlToDOM('<div>text</div>'); |
8 | 9 |
|
9 |
| -// `options.replace` |
| 10 | +// $ExpectType Element | Element[] |
| 11 | +domToReact(htmlToDOM('<div>text</div>')); |
10 | 12 |
|
11 |
| -// Return React.createElement from `replace` |
| 13 | +// $ExpectType Element | Element[] |
12 | 14 | domToReact(htmlToDOM('<p id="replace">text</p>'), {
|
13 | 15 | replace: domNode => {
|
14 | 16 | if (domNode.attribs && domNode.attribs.id === 'replace') {
|
15 |
| - return React.createElement('span', {}, 'replaced'); |
| 17 | + return <span>replaced</span>; |
16 | 18 | }
|
17 | 19 | }
|
18 | 20 | });
|
19 | 21 |
|
20 |
| -// Return ReactElement |
21 |
| -const options: HTMLReactParserOptions = { |
| 22 | +let options: HTMLReactParserOptions; |
| 23 | + |
| 24 | +options = { |
22 | 25 | replace({ attribs }) {
|
23 |
| - return attribs && attribs.id === 'remove' && <React.Fragment />; |
| 26 | + return attribs && attribs.id === 'remove' && <></>; |
24 | 27 | }
|
25 | 28 | };
|
26 | 29 |
|
| 30 | +// $ExpectType Element | Element[] |
27 | 31 | domToReact(htmlToDOM('<p><br id="remove"></p>'), options);
|
28 | 32 |
|
29 |
| -// Return domhandler node |
| 33 | +// $ExpectType Element | Element[] |
30 | 34 | domToReact(htmlToDOM('<a id="header" href="#">Heading</a>'), {
|
31 | 35 | replace: node => {
|
32 | 36 | if (node.attribs && node.attribs.id === 'header') {
|
33 |
| - return { |
34 |
| - type: 'h1', |
35 |
| - props: { children: 'Heading' } |
36 |
| - }; |
| 37 | + return; |
37 | 38 | }
|
38 | 39 | }
|
39 | 40 | });
|
0 commit comments