@@ -61,24 +61,6 @@ function isElementHiddenDirectly(element) {
6161 return false ;
6262}
6363
64- /**
65- * Returns the normalized element tabindex. If not focusable, returns -1.
66- * It checks for the attribute "tabindex" instead of the element property
67- * `tabIndex` since browsers assign different values to it.
68- * e.g. in Firefox `<div contenteditable>` has `tabIndex = -1`
69- *
70- * @param {HTMLElement } element
71- * @return {number }
72- */
73- function normalizeTabIndex ( element ) {
74- if ( ! isElementFocusable ( element ) ) {
75- return - 1 ;
76- }
77-
78- const tabIndex = element . getAttribute ( 'tabindex' ) || 0 ;
79- return Number ( tabIndex ) ;
80- }
81-
8264/**
8365 * Returns if element `a` has lower tab order compared to element `b`
8466 * (both elements are assumed to be focusable and tabbable).
@@ -138,42 +120,6 @@ function sortElementsByTabIndex(elements) {
138120 return mergeSortByTabIndex ( left , right ) ;
139121}
140122
141- /**
142- * Searches for nodes that are tabbable and adds them to the `result` array.
143- * Returns if the `result` array needs to be sorted by tabindex.
144- *
145- * @param {Node } node The starting point for the search; added to `result` if tabbable.
146- * @param {HTMLElement[] } result
147- * @return {boolean }
148- * @private
149- */
150- function collectFocusableNodes ( node , result ) {
151- if ( node . nodeType !== Node . ELEMENT_NODE || isElementHiddenDirectly ( node ) ) {
152- // Don't traverse children if the node is not an HTML element or not visible.
153- return false ;
154- }
155-
156- const element = /** @type {HTMLElement } */ ( node ) ;
157- const tabIndex = normalizeTabIndex ( element ) ;
158- let needsSort = tabIndex > 0 ;
159- if ( tabIndex >= 0 ) {
160- result . push ( element ) ;
161- }
162-
163- let children = [ ] ;
164- if ( element . localName === 'slot' ) {
165- children = element . assignedNodes ( { flatten : true } ) ;
166- } else {
167- // Use shadow root if possible, will check for distributed nodes.
168- children = ( element . shadowRoot || element ) . children ;
169- }
170- [ ...children ] . forEach ( ( child ) => {
171- // Ensure method is always invoked to collect focusable children.
172- needsSort = collectFocusableNodes ( child , result ) || needsSort ;
173- } ) ;
174- return needsSort ;
175- }
176-
177123/**
178124 * Returns true if the element is hidden, false otherwise.
179125 *
@@ -239,6 +185,60 @@ export function isElementFocused(element) {
239185 return element . getRootNode ( ) . activeElement === element ;
240186}
241187
188+ /**
189+ * Returns the normalized element tabindex. If not focusable, returns -1.
190+ * It checks for the attribute "tabindex" instead of the element property
191+ * `tabIndex` since browsers assign different values to it.
192+ * e.g. in Firefox `<div contenteditable>` has `tabIndex = -1`
193+ *
194+ * @param {HTMLElement } element
195+ * @return {number }
196+ */
197+ function normalizeTabIndex ( element ) {
198+ if ( ! isElementFocusable ( element ) ) {
199+ return - 1 ;
200+ }
201+
202+ const tabIndex = element . getAttribute ( 'tabindex' ) || 0 ;
203+ return Number ( tabIndex ) ;
204+ }
205+
206+ /**
207+ * Searches for nodes that are tabbable and adds them to the `result` array.
208+ * Returns if the `result` array needs to be sorted by tabindex.
209+ *
210+ * @param {Node } node The starting point for the search; added to `result` if tabbable.
211+ * @param {HTMLElement[] } result
212+ * @return {boolean }
213+ * @private
214+ */
215+ function collectFocusableNodes ( node , result ) {
216+ if ( node . nodeType !== Node . ELEMENT_NODE || isElementHiddenDirectly ( node ) ) {
217+ // Don't traverse children if the node is not an HTML element or not visible.
218+ return false ;
219+ }
220+
221+ const element = /** @type {HTMLElement } */ ( node ) ;
222+ const tabIndex = normalizeTabIndex ( element ) ;
223+ let needsSort = tabIndex > 0 ;
224+ if ( tabIndex >= 0 ) {
225+ result . push ( element ) ;
226+ }
227+
228+ let children = [ ] ;
229+ if ( element . localName === 'slot' ) {
230+ children = element . assignedNodes ( { flatten : true } ) ;
231+ } else {
232+ // Use shadow root if possible, will check for distributed nodes.
233+ children = ( element . shadowRoot || element ) . children ;
234+ }
235+ [ ...children ] . forEach ( ( child ) => {
236+ // Ensure method is always invoked to collect focusable children.
237+ needsSort = collectFocusableNodes ( child , result ) || needsSort ;
238+ } ) ;
239+ return needsSort ;
240+ }
241+
242242/**
243243 * Returns a tab-ordered array of focusable elements for a root element.
244244 * The resulting array will include the root element if it is focusable.
0 commit comments