File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 2
2
* Guard function that checks if provided `input` is an Element.
3
3
*/
4
4
export function isElement ( input : unknown ) : input is Element {
5
- return input && input instanceof Element ;
5
+ return (
6
+ typeof input === "object" &&
7
+ input !== null &&
8
+ ( input as Element ) . nodeType === Node . ELEMENT_NODE
9
+ ) ;
6
10
}
Original file line number Diff line number Diff line change
1
+ import { assert } from "chai" ;
2
+ import { isElement } from "../src/utilities-iselement" ;
3
+
4
+ describe ( "utilities - isElement" , ( ) => {
5
+ it ( "should identify non-element" , ( ) => {
6
+ assert . isFalse ( isElement ( null ) ) ;
7
+ assert . isFalse ( isElement ( undefined ) ) ;
8
+ assert . isFalse ( isElement ( "" ) ) ;
9
+ assert . isFalse ( isElement ( 0 ) ) ;
10
+ assert . isFalse ( isElement ( { } ) ) ;
11
+ assert . isFalse ( isElement ( [ ] ) ) ;
12
+ assert . isFalse ( isElement ( ( ) => { } ) ) ;
13
+ } ) ;
14
+ it ( "should identify a valid element" , ( ) => {
15
+ const element = document . createElement ( "div" ) ;
16
+ assert . isTrue ( isElement ( element ) ) ;
17
+ } ) ;
18
+ it ( "should identify a valid element inside an iframe" , ( ) => {
19
+ const iframe = document . createElement ( "iframe" ) ;
20
+ document . body . appendChild ( iframe ) ;
21
+ const element = iframe . contentDocument . createElement ( "div" ) ;
22
+ assert . isTrue ( isElement ( element ) ) ;
23
+ document . body . removeChild ( iframe ) ;
24
+ } ) ;
25
+ } ) ;
You can’t perform that action at this time.
0 commit comments