11import {
22 isArray ,
33 isFunction ,
4- isHTMLButtonElement ,
54 isHTMLElement ,
65 isNull ,
76 isObject ,
@@ -11,9 +10,6 @@ import {
1110
1211
1312describe ( 'Type methods' , ( ) => {
14- const div = document . createElement ( 'div' ) ;
15- const text = document . createTextNode ( 'test' ) ;
16-
1713 describe ( 'isObject' , ( ) => {
1814 test ( 'can return `true` if a subject is an object.' , ( ) => {
1915 [ { } , { a : 1 } , new Date ( ) ] . forEach ( subject => {
@@ -93,29 +89,46 @@ describe( 'Type methods', () => {
9389 } ) ;
9490 } ) ;
9591 } ) ;
92+ } ) ;
9693
97- describe ( 'isHTMLElement' , ( ) => {
98- test ( 'can return `true` if a subject is an HTMLElement.' , ( ) => {
99- expect ( isHTMLElement ( div ) ) . toBe ( true ) ;
100- } ) ;
94+ describe ( 'isHTMLElement' , ( ) => {
95+ const div = document . createElement ( 'div' ) ;
96+ const text = document . createTextNode ( 'test' ) ;
10197
102- test ( 'should return `false` for other subjects.' , ( ) => {
103- [ document , window , text , 1 , true , undefined , '1' , null , [ 1 ] , { a : 1 } , NaN ] . forEach ( subject => {
104- expect ( isHTMLElement ( subject ) ) . toBe ( false ) ;
105- } ) ;
106- } ) ;
98+ document . body . innerHTML = '<iframe></iframe>' ;
99+
100+ test ( 'can return `true` if a subject is an HTMLElement.' , ( ) => {
101+ expect ( isHTMLElement ( div ) ) . toBe ( true ) ;
107102 } ) ;
108103
109- describe ( 'isHTMLButtonElement' , ( ) => {
110- test ( 'can return `true` if a subject is an HTMLElement.' , ( ) => {
111- const button = document . createElement ( 'button' ) ;
112- expect ( isHTMLButtonElement ( button ) ) . toBe ( true ) ;
104+ test ( 'should return `false` for other subjects.' , ( ) => {
105+ [ document , window , text , 1 , true , undefined , '1' , null , [ 1 ] , { a : 1 } , NaN ] . forEach ( subject => {
106+ expect ( isHTMLElement ( subject ) ) . toBe ( false ) ;
113107 } ) ;
108+ } ) ;
114109
115- test ( 'should return `false` for other subjects.' , ( ) => {
116- [ document , window , div , text , 1 , true , undefined , '1' , null , [ 1 ] , { a : 1 } , NaN ] . forEach ( subject => {
117- expect ( isHTMLButtonElement ( subject ) ) . toBe ( false ) ;
118- } ) ;
119- } ) ;
110+ test ( 'should work for nodes coming from other realms.' , ( ) => {
111+ const iframe = document . querySelector ( 'iframe' ) ;
112+
113+ expect ( iframe ) . toBeTruthy ( ) ;
114+
115+ const { contentDocument, contentWindow } = iframe ;
116+ const iDiv = contentDocument . createElement ( 'div' ) ;
117+
118+ expect ( contentWindow ) . not . toBe ( window ) ;
119+
120+ // This should fail since `HTMLElement` is different with the iframe's.
121+ expect ( iDiv instanceof HTMLElement ) . toBe ( false ) ;
122+
123+ // But this method will work since checking owners.
124+ expect ( isHTMLElement ( iDiv ) ) . toBe ( true ) ;
120125 } ) ;
121- } ) ;
126+
127+ test ( 'should work for nodes that do not belong to a specific window.' , ( ) => {
128+ const div = new DOMParser ( ) . parseFromString ( '<div>' , 'text/html' ) . body . firstElementChild ;
129+
130+ expect ( div ) . toBeTruthy ( ) ;
131+ expect ( div . ownerDocument . defaultView ) . toBeNull ( ) ;
132+ expect ( isHTMLElement ( div ) ) . toBe ( true ) ;
133+ } ) ;
134+ } ) ;
0 commit comments