From b6185e166aa154d6c2008da96aa96a0b2ab79c6c Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Fri, 17 Jan 2025 11:38:51 +0000 Subject: [PATCH] Merged in fix/tabbable-errors (pull request #1187) Fix: Can only call Element.querySelectorAll on instances of Element * Fix: Can only call Element.querySelectorAll on instances of Element errors Approved-by: Erik Tiekstra --- utils/tabbable.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/utils/tabbable.ts b/utils/tabbable.ts index 37dafb131..93c49de4e 100644 --- a/utils/tabbable.ts +++ b/utils/tabbable.ts @@ -47,17 +47,21 @@ export function focusable(element: HTMLElement, isTabIndexNotNaN: boolean) { } export function tabbable(element: HTMLElement) { - let tabIndex = element.getAttribute("tabindex") - //@ts-ignore - if (tabIndex === null) tabIndex = undefined - //@ts-ignore - const isTabIndexNaN = isNaN(tabIndex) - //@ts-ignore + const tabIndexAttr = element.getAttribute("tabindex") + const tabIndex = tabIndexAttr !== null ? Number(tabIndexAttr) : undefined + const isTabIndexNaN = tabIndex === undefined || isNaN(tabIndex) + return (isTabIndexNaN || tabIndex >= 0) && focusable(element, !isTabIndexNaN) } export default function findTabbableDescendants( - element: HTMLElement + element: HTMLElement | null | undefined ): HTMLElement[] { - return [].slice.call(element.querySelectorAll("*"), 0).filter(tabbable) + if (!(element instanceof HTMLElement)) { + return [] + } + + return Array.from(element.querySelectorAll("*")) + .filter((el): el is HTMLElement => el instanceof HTMLElement) + .filter(tabbable) }