Skip to content

Commit b496704

Browse files
committed
fix: isDom fn in iframe node
1 parent 2880228 commit b496704

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Dom/findDOMNode.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
22

33
export function isDOM(node: any): node is HTMLElement | SVGElement {
4+
const win = (node?.ownerDocument?.defaultView || window) as typeof win;
5+
46
// https://developer.mozilla.org/en-US/docs/Web/API/Element
57
// Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement
6-
return node instanceof HTMLElement || node instanceof SVGElement;
8+
return node instanceof win.HTMLElement || node instanceof win.SVGElement;
79
}
810

911
/**

tests/findDOMNode.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ describe('findDOMNode', () => {
7676
expect(true).toBeFalsy();
7777
}
7878
});
79+
80+
it('isDOM type in iframe', () => {
81+
const iframe = document.createElement('iframe');
82+
document.body.appendChild(iframe);
83+
const svg: any = iframe.contentWindow.document.createElementNS(
84+
'http://www.w3.org/2000/svg',
85+
'svg',
86+
);
87+
88+
// debugger;
89+
// This `getBoundingClientRect` is used for ts type check
90+
if (isDOM(svg) && svg.getBoundingClientRect()) {
91+
expect(true).toBeTruthy();
92+
} else {
93+
expect(true).toBeFalsy();
94+
}
95+
});
96+
7997
it('should return DOM node from ref.current', () => {
8098
const TestComponent = React.forwardRef<HTMLDivElement>((_, ref) => {
8199
return <div ref={ref}>test</div>;

0 commit comments

Comments
 (0)