Skip to content

Commit bc26e3e

Browse files
committed
fix: isElement works on elements in an iframe
fixes #667
1 parent caa91ae commit bc26e3e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/utilities-iselement.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
* Guard function that checks if provided `input` is an Element.
33
*/
44
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+
);
610
}

test/utilities-is-element.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
});

0 commit comments

Comments
 (0)