File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 11import React from 'react' ;
22
33export 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/**
Original file line number Diff line number Diff 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 > ;
You can’t perform that action at this time.
0 commit comments