Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions __tests__/dom-to-react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ describe('library option', () => {
});

describe('replace option', () => {
it.each([undefined, null, 0, 1, true, false, {}])(
'does not replace for invalid return value %p',
(value) => {
const reactElement = domToReact(htmlToDOM('<br>'), {
replace: () => value,
}) as JSX.Element;
expect(reactElement).toEqual(<br />);
},
);

it("does not set key if there's a single node", () => {
const reactElement = domToReact(htmlToDOM(html.single), {
replace: () => <div />,
Expand Down Expand Up @@ -213,17 +223,12 @@ describe('replace option', () => {
const options: HTMLReactParserOptions = {
replace(domNode) {
if (domNode instanceof Element) {
return <>{domToReact(domNode.children as DOMNode[], options)}</>;
return domToReact(domNode.children as DOMNode[], options);
}
},
};

const reactElement = domToReact(
htmlToDOM(html.single),
options,
) as JSX.Element;

expect(reactElement).toBeInstanceOf(Object);
const reactElement = domToReact(htmlToDOM('<div>test</div>'), options);
expect(reactElement).toEqual(<div>test</div>);
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export interface HTMLReactParserOptions {
[key: string]: any;
};

replace?: (domNode: DOMNode) => JSX.Element | string | null | boolean | void;
replace?: (
domNode: DOMNode,
) => JSX.Element | string | null | boolean | object | void;

transform?: (
reactNode: ReactNode,
Expand Down