( root: Element, opts?: FocusManagerOptions, scope?: Element[] )
| 891 | * that matches all focusable/tabbable elements. |
| 892 | */ |
| 893 | export function getFocusableTreeWalker( |
| 894 | root: Element, |
| 895 | opts?: FocusManagerOptions, |
| 896 | scope?: Element[] |
| 897 | ): ShadowTreeWalker | TreeWalker { |
| 898 | let filter = opts?.tabbable ? isTabbable : isFocusable; |
| 899 | |
| 900 | // Ensure that root is an Element or fall back appropriately |
| 901 | let rootElement = root?.nodeType === Node.ELEMENT_NODE ? (root as Element) : null; |
| 902 | |
| 903 | // Determine the document to use |
| 904 | let doc = getOwnerDocument(rootElement); |
| 905 | |
| 906 | // Create a TreeWalker, ensuring the root is an Element or Document |
| 907 | let walker = createShadowTreeWalker(doc, root || doc, NodeFilter.SHOW_ELEMENT, { |
| 908 | acceptNode(node) { |
| 909 | // Skip nodes inside the starting node. |
| 910 | if (nodeContains(opts?.from, node)) { |
| 911 | return NodeFilter.FILTER_REJECT; |
| 912 | } |
| 913 | |
| 914 | if ( |
| 915 | opts?.tabbable && |
| 916 | (node as Element).tagName === 'INPUT' && |
| 917 | (node as HTMLInputElement).getAttribute('type') === 'radio' |
| 918 | ) { |
| 919 | // If the radio is in a form, we can get all the other radios by name |
| 920 | if (!isTabbableRadio(node as HTMLInputElement)) { |
| 921 | return NodeFilter.FILTER_REJECT; |
| 922 | } |
| 923 | // If the radio is in the same group as the current node and none are selected, we can skip it |
| 924 | if ( |
| 925 | (walker.currentNode as Element).tagName === 'INPUT' && |
| 926 | (walker.currentNode as HTMLInputElement).type === 'radio' && |
| 927 | (walker.currentNode as HTMLInputElement).name === (node as HTMLInputElement).name |
| 928 | ) { |
| 929 | return NodeFilter.FILTER_REJECT; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | if ( |
| 934 | filter(node as Element) && |
| 935 | (!scope || isElementInScope(node as Element, scope)) && |
| 936 | (!opts?.accept || opts.accept(node as Element)) |
| 937 | ) { |
| 938 | return NodeFilter.FILTER_ACCEPT; |
| 939 | } |
| 940 | |
| 941 | return NodeFilter.FILTER_SKIP; |
| 942 | } |
| 943 | }); |
| 944 | |
| 945 | if (opts?.from) { |
| 946 | walker.currentNode = opts.from; |
| 947 | } |
| 948 | |
| 949 | return walker; |
| 950 | } |
no test coverage detected