Skip to content

Commit

Permalink
fix(refactor): Use nodeType and tagName for element type checks (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed May 16, 2020
1 parent ee9cc54 commit f1b2bd0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-pumas-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dom-accessibility-api": patch
---

Use nodeType and tagName for element type checks
35 changes: 6 additions & 29 deletions sources/util.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
export function isElement(node: Node | null): node is Element {
return (
// @ts-ignore
node !== null && node instanceof node.ownerDocument.defaultView.Element
);
return node !== null && node.nodeType === node.ELEMENT_NODE;
}

export function isHTMLInputElement(
node: Node | null
): node is HTMLInputElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLInputElement
);
return isElement(node) && node.tagName === "INPUT";
}

export function isHTMLSelectElement(
node: Node | null
): node is HTMLSelectElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLSelectElement
);
return isElement(node) && node.tagName === "SELECT";
}

export function isHTMLTextAreaElement(
node: Node | null
): node is HTMLTextAreaElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLTextAreaElement
);
return isElement(node) && node.tagName === "TEXTAREA";
}

export function safeWindow(node: Node): Window {
Expand All @@ -48,19 +33,11 @@ export function safeWindow(node: Node): Window {
export function isHTMLFieldSetElement(
node: Node | null
): node is HTMLFieldSetElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLFieldSetElement
);
return isElement(node) && node.tagName === "FIELDSET";
}

export function isHTMLLegendElement(
node: Node | null
): node is HTMLLegendElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLLegendElement
);
return isElement(node) && node.tagName === "LEGEND";
}

0 comments on commit f1b2bd0

Please sign in to comment.