All files / Scripts findTabStops.ts

100% Statements 15/15
100% Branches 5/5
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                    10x   2x 1x 1x     10x 13x 1x   12x 2x   10x 3x   7x 1x   6x  
/**
 * Finds all tab stops within a given HTML element.
 *
 * This function searches for all focusable elements within the specified element
 * and returns an array of those elements that are not disabled, have a non-negative
 * tabIndex, and are visible (i.e., their offsetParent is not null).
 *
 * @param element - The root HTML element within which to search for tab stops.
 * @returns An array of HTMLElements that are focusable and meet the criteria.
 */
export function findTabStops(element: HTMLElement | null): HTMLElement[] {
    // If the element is null, return empty array
    if (element === null) return [];
    const focusableElements = element.querySelectorAll("a, button, input, textarea, select, [tabindex]");
    return Array.from(focusableElements).filter(isFocusableElement);
}
 
export function isFocusableElement(el: Element): el is HTMLElement {
    if (!(el instanceof HTMLElement)) {
        return false;
    }
    if (el.hasAttribute("disabled")) {
        return false;
    }
    if (el.tabIndex < 0) {
        return false;
    }
    if (el.offsetParent === null) {
        return false;
    }
    return true;
}