From 88eea66dfe8f1a57aeff7ea631eaf64541e0131f Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 29 Oct 2023 15:41:48 -0400 Subject: [PATCH] fix(types): add back `object` to `replace` option return type Fixes #1126 Maintains backward compatibility for invalid return type --- __tests__/dom-to-react.test.tsx | 21 +++++++++++++-------- src/types.ts | 4 +++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/__tests__/dom-to-react.test.tsx b/__tests__/dom-to-react.test.tsx index e99509df..92cafb54 100644 --- a/__tests__/dom-to-react.test.tsx +++ b/__tests__/dom-to-react.test.tsx @@ -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('
'), { + replace: () => value, + }) as JSX.Element; + expect(reactElement).toEqual(
); + }, + ); + it("does not set key if there's a single node", () => { const reactElement = domToReact(htmlToDOM(html.single), { replace: () =>
, @@ -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('
test
'), options); + expect(reactElement).toEqual(
test
); }); }); diff --git a/src/types.ts b/src/types.ts index c7f0d9f3..d032410f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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,