Skip to content

Commit

Permalink
fix(index): include domhandler Node in DOMNode type
Browse files Browse the repository at this point in the history
Fixes #207
  • Loading branch information
remarkablemark committed Jan 12, 2021
1 parent a4f945b commit ac4aff3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Comment,
DomHandlerOptions,
Element,
Node,
ProcessingInstruction,
Text
} from 'domhandler';
Expand All @@ -15,8 +16,8 @@ import domToReact from './lib/dom-to-react';

export { attributesToProps, domToReact, htmlToDOM };
export type HTMLParser2Options = ParserOptions & DomHandlerOptions;
export { Comment, Element, ProcessingInstruction, Text };
export type DOMNode = Comment | Element | ProcessingInstruction | Text;
export { Comment, Element, Node, ProcessingInstruction, Text };
export type DOMNode = Comment | Element | Node | ProcessingInstruction | Text;

export interface HTMLReactParserOptions {
htmlparser2?: HTMLParser2Options;
Expand Down
14 changes: 12 additions & 2 deletions test/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,24 @@ parse('<br id="remove">', {
}
});

const options: HTMLReactParserOptions = {
let options: HTMLReactParserOptions;

options = {
replace: domNode => {
if (domNode instanceof Element && domNode.attribs.id === 'header') {
return;
}
}
};

options = {
replace: domNode => {
if (domNode instanceof Element) {
return <>{domToReact(domNode.children)}</>;
}
}
};

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

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

// $ExpectType DOMNode[]
// $ExpectType (Comment | Element | ProcessingInstruction | Text)[]
const domNodes = htmlToDOM('<div>text</div>');

// $ExpectType string | Element | Element[]
Expand Down
2 changes: 1 addition & 1 deletion test/types/lib/dom-to-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import domToReact from 'html-react-parser/lib/dom-to-react';
import * as React from 'react';
import htmlToDOM from 'html-dom-parser';

// $ExpectType DOMNode[]
// $ExpectType (Comment | Element | ProcessingInstruction | Text)[]
htmlToDOM('<div>text</div>');

// $ExpectType string | Element | Element[]
Expand Down

0 comments on commit ac4aff3

Please sign in to comment.