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
This commit is contained in:
Linus Flood
2025-01-17 11:38:51 +00:00
parent d229b5c463
commit b6185e166a

View File

@@ -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)
}