-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
focusable.js
120 lines (112 loc) · 3.7 KB
/
focusable.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* References:
*
* Focusable:
* - https://www.w3.org/TR/html5/editing.html#focus-management
*
* Sequential focus navigation:
* - https://www.w3.org/TR/html5/editing.html#sequential-focus-navigation-and-the-tabindex-attribute
*
* Disabled elements:
* - https://www.w3.org/TR/html5/disabled-elements.html#disabled-elements
*
* getClientRects algorithm (requiring layout box):
* - https://www.w3.org/TR/cssom-view-1/#extension-to-the-element-interface
*
* AREA elements associated with an IMG:
* - https://w3c.github.io/html/editing.html#data-model
*/
/**
* Returns a CSS selector used to query for focusable elements.
*
* @param {boolean} sequential If set, only query elements that are sequentially
* focusable. Non-interactive elements with a
* negative `tabindex` are focusable but not
* sequentially focusable.
* https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute
*
* @return {string} CSS selector.
*/
function buildSelector( sequential ) {
return [
sequential ? '[tabindex]:not([tabindex^="-"])' : '[tabindex]',
'a[href]',
'button:not([disabled])',
'input:not([type="hidden"]):not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
'iframe:not([tabindex^="-"])',
'object',
'embed',
'area[href]',
'[contenteditable]:not([contenteditable=false])',
].join( ',' );
}
/**
* Returns true if the specified element is visible (i.e. neither display: none
* nor visibility: hidden).
*
* @param {HTMLElement} element DOM element to test.
*
* @return {boolean} Whether element is visible.
*/
function isVisible( element ) {
return (
element.offsetWidth > 0 ||
element.offsetHeight > 0 ||
element.getClientRects().length > 0
);
}
/**
* Returns true if the specified area element is a valid focusable element, or
* false otherwise. Area is only focusable if within a map where a named map
* referenced by an image somewhere in the document.
*
* @param {HTMLAreaElement} element DOM area element to test.
*
* @return {boolean} Whether area element is valid for focus.
*/
function isValidFocusableArea( element ) {
/** @type {HTMLMapElement | null} */
const map = element.closest( 'map[name]' );
if ( ! map ) {
return false;
}
/** @type {HTMLImageElement | null} */
const img = element.ownerDocument.querySelector(
'img[usemap="#' + map.name + '"]'
);
return !! img && isVisible( img );
}
/**
* Returns all focusable elements within a given context.
*
* @param {Element} context Element in which to search.
* @param {Object} options
* @param {boolean} [options.sequential] If set, only return elements that are
* sequentially focusable.
* Non-interactive elements with a
* negative `tabindex` are focusable but
* not sequentially focusable.
* https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute
*
* @return {HTMLElement[]} Focusable elements.
*/
export function find( context, { sequential = false } = {} ) {
/* eslint-disable jsdoc/no-undefined-types */
/** @type {NodeListOf<HTMLElement>} */
/* eslint-enable jsdoc/no-undefined-types */
const elements = context.querySelectorAll( buildSelector( sequential ) );
return Array.from( elements ).filter( ( element ) => {
if ( ! isVisible( element ) ) {
return false;
}
const { nodeName } = element;
if ( 'AREA' === nodeName ) {
return isValidFocusableArea(
/** @type {HTMLAreaElement} */ ( element )
);
}
return true;
} );
}