From b496704d563756a90f3fd0b139a0f64c74e65baa Mon Sep 17 00:00:00 2001 From: apades <36807043+apades@users.noreply.github.com> Date: Tue, 1 Apr 2025 20:57:14 +0800 Subject: [PATCH] fix: isDom fn in iframe node --- src/Dom/findDOMNode.ts | 4 +++- tests/findDOMNode.test.tsx | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Dom/findDOMNode.ts b/src/Dom/findDOMNode.ts index 288ec01a..c41393e2 100644 --- a/src/Dom/findDOMNode.ts +++ b/src/Dom/findDOMNode.ts @@ -1,9 +1,11 @@ import React from 'react'; export function isDOM(node: any): node is HTMLElement | SVGElement { + const win = (node?.ownerDocument?.defaultView || window) as typeof win; + // https://developer.mozilla.org/en-US/docs/Web/API/Element // Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement - return node instanceof HTMLElement || node instanceof SVGElement; + return node instanceof win.HTMLElement || node instanceof win.SVGElement; } /** diff --git a/tests/findDOMNode.test.tsx b/tests/findDOMNode.test.tsx index 0856b81a..6272f1c3 100644 --- a/tests/findDOMNode.test.tsx +++ b/tests/findDOMNode.test.tsx @@ -76,6 +76,24 @@ describe('findDOMNode', () => { expect(true).toBeFalsy(); } }); + + it('isDOM type in iframe', () => { + const iframe = document.createElement('iframe'); + document.body.appendChild(iframe); + const svg: any = iframe.contentWindow.document.createElementNS( + 'http://www.w3.org/2000/svg', + 'svg', + ); + + // debugger; + // This `getBoundingClientRect` is used for ts type check + if (isDOM(svg) && svg.getBoundingClientRect()) { + expect(true).toBeTruthy(); + } else { + expect(true).toBeFalsy(); + } + }); + it('should return DOM node from ref.current', () => { const TestComponent = React.forwardRef((_, ref) => { return
test
;