Skip to content

Commit

Permalink
fix: isElement works on elements in an iframe
Browse files Browse the repository at this point in the history
fixes #667
  • Loading branch information
fczbkk committed Jun 2, 2024
1 parent caa91ae commit bc26e3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utilities-iselement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
* Guard function that checks if provided `input` is an Element.
*/
export function isElement(input: unknown): input is Element {
return input && input instanceof Element;
return (
typeof input === "object" &&
input !== null &&
(input as Element).nodeType === Node.ELEMENT_NODE
);
}
25 changes: 25 additions & 0 deletions test/utilities-is-element.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assert } from "chai";
import { isElement } from "../src/utilities-iselement";

describe("utilities - isElement", () => {
it("should identify non-element", () => {
assert.isFalse(isElement(null));
assert.isFalse(isElement(undefined));
assert.isFalse(isElement(""));
assert.isFalse(isElement(0));
assert.isFalse(isElement({}));
assert.isFalse(isElement([]));
assert.isFalse(isElement(() => {}));
});
it("should identify a valid element", () => {
const element = document.createElement("div");
assert.isTrue(isElement(element));
});
it("should identify a valid element inside an iframe", () => {
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const element = iframe.contentDocument.createElement("div");
assert.isTrue(isElement(element));
document.body.removeChild(iframe);
});
});

0 comments on commit bc26e3e

Please sign in to comment.