/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; /** * Accessible states used to check node's state from the accessiblity API * perspective. * * Note: if gecko is built with --disable-accessibility, the interfaces * are not defined. This is why we use getters instead to be able to use * these statically. */ this.AccessibilityUtils = (function () { const FORCE_DISABLE_ACCESSIBILITY_PREF = "accessibility.force_disabled"; // Accessible states. const { STATE_FOCUSABLE, STATE_INVISIBLE, STATE_LINKED, STATE_UNAVAILABLE } = Ci.nsIAccessibleStates; // Accessible action for showing long description. const CLICK_ACTION = "click"; // Roles that are considered focusable with the keyboard. const KEYBOARD_FOCUSABLE_ROLES = new Set([ Ci.nsIAccessibleRole.ROLE_BUTTONMENU, Ci.nsIAccessibleRole.ROLE_CHECKBUTTON, Ci.nsIAccessibleRole.ROLE_COMBOBOX, Ci.nsIAccessibleRole.ROLE_EDITCOMBOBOX, Ci.nsIAccessibleRole.ROLE_ENTRY, Ci.nsIAccessibleRole.ROLE_LINK, Ci.nsIAccessibleRole.ROLE_LISTBOX, Ci.nsIAccessibleRole.ROLE_PASSWORD_TEXT, Ci.nsIAccessibleRole.ROLE_PUSHBUTTON, Ci.nsIAccessibleRole.ROLE_RADIOBUTTON, Ci.nsIAccessibleRole.ROLE_SEARCHBOX, Ci.nsIAccessibleRole.ROLE_SLIDER, Ci.nsIAccessibleRole.ROLE_SPINBUTTON, Ci.nsIAccessibleRole.ROLE_SUMMARY, Ci.nsIAccessibleRole.ROLE_SWITCH, Ci.nsIAccessibleRole.ROLE_TOGGLE_BUTTON, ]); // Roles that are user interactive. const INTERACTIVE_ROLES = new Set([ ...KEYBOARD_FOCUSABLE_ROLES, Ci.nsIAccessibleRole.ROLE_CHECK_MENU_ITEM, Ci.nsIAccessibleRole.ROLE_CHECK_RICH_OPTION, Ci.nsIAccessibleRole.ROLE_COMBOBOX_OPTION, Ci.nsIAccessibleRole.ROLE_MENUITEM, Ci.nsIAccessibleRole.ROLE_OPTION, Ci.nsIAccessibleRole.ROLE_OUTLINE, Ci.nsIAccessibleRole.ROLE_OUTLINEITEM, Ci.nsIAccessibleRole.ROLE_PAGETAB, Ci.nsIAccessibleRole.ROLE_PARENT_MENUITEM, Ci.nsIAccessibleRole.ROLE_RADIO_MENU_ITEM, Ci.nsIAccessibleRole.ROLE_RICH_OPTION, ]); // Roles that are considered interactive when they are focusable. const INTERACTIVE_IF_FOCUSABLE_ROLES = new Set([ // If article is focusable, we can assume it is inside a feed. Ci.nsIAccessibleRole.ROLE_ARTICLE, // Column header can be focusable. Ci.nsIAccessibleRole.ROLE_COLUMNHEADER, Ci.nsIAccessibleRole.ROLE_GRID_CELL, Ci.nsIAccessibleRole.ROLE_MENUBAR, Ci.nsIAccessibleRole.ROLE_MENUPOPUP, Ci.nsIAccessibleRole.ROLE_PAGETABLIST, // Row header can be focusable. Ci.nsIAccessibleRole.ROLE_ROWHEADER, Ci.nsIAccessibleRole.ROLE_SCROLLBAR, Ci.nsIAccessibleRole.ROLE_SEPARATOR, Ci.nsIAccessibleRole.ROLE_TOOLBAR, ]); // Roles that are considered form controls. const FORM_ROLES = new Set([ Ci.nsIAccessibleRole.ROLE_CHECKBUTTON, Ci.nsIAccessibleRole.ROLE_CHECK_RICH_OPTION, Ci.nsIAccessibleRole.ROLE_COMBOBOX, Ci.nsIAccessibleRole.ROLE_EDITCOMBOBOX, Ci.nsIAccessibleRole.ROLE_ENTRY, Ci.nsIAccessibleRole.ROLE_LISTBOX, Ci.nsIAccessibleRole.ROLE_PASSWORD_TEXT, Ci.nsIAccessibleRole.ROLE_PROGRESSBAR, Ci.nsIAccessibleRole.ROLE_RADIOBUTTON, Ci.nsIAccessibleRole.ROLE_SLIDER, Ci.nsIAccessibleRole.ROLE_SPINBUTTON, Ci.nsIAccessibleRole.ROLE_SWITCH, ]); const DEFAULT_ENV = Object.freeze({ // Checks that accessible object has at least one accessible action. actionCountRule: true, // Checks that accessible object (and its corresponding node) is focusable // (has focusable state and its node's tabindex is not set to -1). focusableRule: true, // Checks that clickable accessible object (and its corresponding node) has // appropriate interactive semantics. ifClickableThenInteractiveRule: true, // Checks that accessible object has a role that is considered to be // interactive. interactiveRule: true, // Checks that accessible object has a non-empty label. labelRule: true, // Checks that a node is enabled and is expected to be enabled via // the accessibility API. mustBeEnabled: true, // Checks that a node has a corresponding accessible object. mustHaveAccessibleRule: true, // Checks that accessible object (and its corresponding node) have a non- // negative tabindex. Platform accessibility API still sets focusable state // on tabindex=-1 nodes. nonNegativeTabIndexRule: true, }); let gA11YChecks = false; let gEnv = { ...DEFAULT_ENV, }; // This is set by AccessibilityUtils.init so that we always have a reference // to SimpleTest regardless of changes to the global scope. let SimpleTest = null; /** * Get role attribute for an accessible object if specified for its * corresponding ``DOMNode``. * * @param {nsIAccessible} accessible * Accessible for which to determine its role attribute value. * * @returns {string} * Role attribute value if specified. */ function getAriaRoles(accessible) { try { return accessible.attributes.getStringProperty("xml-roles"); } catch (e) { // No xml-roles. nsPersistentProperties throws if the attribute for a key // is not found. } return ""; } /** * Get related accessible objects that are targets of labelled by relation e.g. * labels. * * @param {nsIAccessible} accessible * Accessible objects to get labels for. * * @returns {Array} * A list of accessible objects that are labels for a given accessible. */ function getLabels(accessible) { const relation = accessible.getRelationByType( Ci.nsIAccessibleRelation.RELATION_LABELLED_BY ); return [...relation.getTargets().enumerate(Ci.nsIAccessible)]; } /** * Test if an accessible has a ``hidden`` attribute. * * @param {nsIAccessible} accessible * Accessible object. * * @return {boolean} * True if the accessible object has a ``hidden`` attribute, false * otherwise. */ function hasHiddenAttribute(accessible) { let hidden = false; try { hidden = accessible.attributes.getStringProperty("hidden"); } catch (e) {} // If the property is missing, error will be thrown return hidden && hidden === "true"; } /** * Test if an accessible is hidden from the user. * * @param {nsIAccessible} accessible * Accessible object. * * @return {boolean} * True if accessible is hidden from user, false otherwise. */ function isHidden(accessible) { if (!accessible) { return true; } while (accessible) { if (hasHiddenAttribute(accessible)) { return true; } accessible = accessible.parent; } return false; } /** * Check if an accessible has a given state. * * @param {nsIAccessible} accessible * Accessible object to test. * @param {number} stateToMatch * State to match. * * @return {boolean} * True if |accessible| has |stateToMatch|, false otherwise. */ function matchState(accessible, stateToMatch) { const state = {}; accessible.getState(state, {}); return !!(state.value & stateToMatch); } /** * Determine if an accessible is a button that is purposefully non-focusable. * * The Go button in the Url Bar is an example of a purposefully * non-focusable image toolbar button that provides an mouse/touch-only * control for the search query submission, while a keyboard user could * press `Enter` to do it. Similarly, two scroll buttons that appear when * toolbar is overflowing, and keyboard-only users would actually scroll * tabs in the toolbar while trying to navigate to these controls. When * toolbarbuttons are redundant for keyboard users, we do not want to * create an extra tab stop for such controls, thus we are expecting the * button markup to include `keyNav="false"` attribute to flag it. */ function isNoKeyNavButton(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal || node.getAttribute("keyNav") != "false") { return false; } const ariaRoles = getAriaRoles(accessible); return ( ariaRoles.includes("button") || accessible.role == Ci.nsIAccessibleRole.ROLE_PUSHBUTTON ); } /** * Determine if an accessible is a keyboard focusable browser toolbar button. * Browser toolbar buttons aren't keyboard focusable in the usual way. * Instead, focus is managed by JS code which sets tabindex on a single * button at a time. Thus, we need to special case the focusable check for * these buttons. */ function isKeyboardFocusableBrowserToolbarButton(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } const toolbar = node.closest("toolbar") || node.flattenedTreeParentNode.closest("toolbar"); if (!toolbar || toolbar.getAttribute("keyNav") != "true") { return false; } return node.ownerGlobal.ToolbarKeyboardNavigator._isButton(node); } /** * Determine if an accessible is a keyboard focusable control within a Firefox * View list. The main landmark of the Firefox View has role="application" for * users to expect a custom keyboard navigation pattern. Controls within this * area aren't keyboard focusable in the usual way. Instead, focus is managed * by JS code which sets tabindex on a single control within each list at a * time. Thus, we need to special case the focusable check for these controls. */ function isKeyboardFocusableFxviewControlInApplication(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } // Firefox View application rows currently include only buttons and links: if ( !node.className.includes("fxview-tab-row-") || (accessible.role != Ci.nsIAccessibleRole.ROLE_PUSHBUTTON && accessible.role != Ci.nsIAccessibleRole.ROLE_LINK) ) { return false; // Not a button or a link in a Firefox View app. } // ToDo: We may eventually need to support intervening generics between // a list and its listitem here and/or aria-owns lists. const listitemAcc = accessible.parent; const listAcc = listitemAcc.parent; if ( (!listAcc || listAcc.role != Ci.nsIAccessibleRole.ROLE_LIST) && (!listitemAcc || listitemAcc.role != Ci.nsIAccessibleRole.ROLE_LISTITEM) ) { return false; // This button/link isn't inside a listitem within a list. } // All listitems should be not focusable while both a button and a link // within each list item might have tabindex="-1". if ( node.tabIndex && matchState(accessible, STATE_FOCUSABLE) && !matchState(listitemAcc, STATE_FOCUSABLE) ) { // ToDo: We may eventually need to support lists which use aria-owns here. // Check that there is only one keyboard reachable control within the list. const childCount = listAcc.childCount; let foundFocusable = false; for (let c = 0; c < childCount; c++) { const listitem = listAcc.getChildAt(c); const listitemChildCount = listitem.childCount; for (let i = 0; i < listitemChildCount; i++) { const listitemControl = listitem.getChildAt(i); // Use tabIndex rather than a11y focusable state because all controls // within the listitem might have tabindex="-1". if (listitemControl.DOMNode.tabIndex == 0) { if (foundFocusable) { // Only one control within a list should be focusable. // ToDo: Fine-tune the a11y-check error message generated in this case. // Strictly speaking, it's not ideal that we're performing an action // from an is function, which normally only queries something without // any externally observable behaviour. That said, fixing that would // involve different return values for different cases (not a list, // too many focusable listitem controls, etc) so we could move the // a11yFail call to the caller. a11yFail( "Only one control should be focusable in a list", accessible ); return false; } foundFocusable = true; } } } return foundFocusable; } return false; } /** * Determine if an accessible is a keyboard focusable option within a listbox. * We use it in the Url bar results - these controls are't keyboard focusable * in the usual way. Instead, focus is managed by JS code which sets tabindex * on a single option at a time. Thus, we need to special case the focusable * check for these option items. */ function isKeyboardFocusableOption(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } const urlbarListbox = node.closest(".urlbarView-results"); if (!urlbarListbox || urlbarListbox.getAttribute("role") != "listbox") { return false; } return node.getAttribute("role") == "option"; } /** * Determine if an accessible is a keyboard focusable PanelMultiView control. * These controls aren't keyboard focusable in the usual way. Instead, focus * is managed by JS code which sets tabindex dynamically. Thus, we need to * special case the focusable check for these controls. */ function isKeyboardFocusablePanelMultiViewControl(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } const panelview = node.closest("panelview"); if (!panelview || panelview.hasAttribute("disablekeynav")) { return false; } return ( node.ownerGlobal.PanelView.forNode(panelview)._tabNavigableWalker.filter( node ) == NodeFilter.FILTER_ACCEPT ); } /** * Determine if an accessible is a button that is excluded from a focus * order, because its adjacent sibling is a focusable spinner. Controls with * role="spinbutton" are often placed between two buttons that could * increase ("^") or decrease ("v") the value of this spinner. Those buttons * are not expected to be focusable, because their functionality for keyboard * users is redundant to the spinner. But they are exposed to assistive * technology for touch, mouse, switch, and speech-to-text users. Thus, we * need to special case the focusable check for these buttons adjacent to * a spinner. */ function isKeyboardFocusableSpinbuttonSibling(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } // The control itself is a button: if (accessible.role != Ci.nsIAccessibleRole.ROLE_PUSHBUTTON) { return false; } // At least one sibling is a keyboard-focusable spinbutton: for (const sibling of [ node.previousElementSibling, node.nextElementSibling, ]) { if (sibling && sibling.tabIndex >= 0 && sibling.role == "spinbutton") { return true; } } return false; } /** * Determine if an accessible is a keyboard focusable tab within a tablist. * Per the ARIA design pattern, these controls aren't keyboard focusable in * the usual way. Instead, focus is managed by JS code which sets tabindex on * a single tab at a time. Thus, we need to special case the focusable check * for these tab controls. */ function isKeyboardFocusableTabInTablist(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } if (accessible.role != Ci.nsIAccessibleRole.ROLE_PAGETAB) { return false; // Not a tab. } const tablist = findNonGenericParentAccessible(accessible); if (!tablist || tablist.role != Ci.nsIAccessibleRole.ROLE_PAGETABLIST) { return false; // The tab isn't inside a tablist. } // ToDo: We may eventually need to support tablists which use // aria-activedescendant here. // Check that there is only one keyboard reachable tab. let foundFocusable = false; for (const tab of findNonGenericChildrenAccessible(tablist)) { // Allow whitespaces to be included in the tablist for styling purposes const isWhitespace = tab.role == Ci.nsIAccessibleRole.ROLE_TEXT_LEAF && tab.DOMNode.textContent.trim().length === 0; if (tab.role != Ci.nsIAccessibleRole.ROLE_PAGETAB && !isWhitespace) { // The tablist includes children other than tabs or whitespaces a11yFail("Only tabs should be included in a tablist", accessible); } // Use tabIndex rather than a11y focusable state because all tabs might // have tabindex="-1". if (tab.DOMNode.tabIndex == 0) { if (foundFocusable) { // Only one tab within a tablist should be focusable. // ToDo: Fine-tune the a11y-check error message generated in this case. // Strictly speaking, it's not ideal that we're performing an action // from an is function, which normally only queries something without // any externally observable behaviour. That said, fixing that would // involve different return values for different cases (not a tab, // too many focusable tabs, etc) so we could move the a11yFail call // to the caller. a11yFail("Only one tab should be focusable in a tablist", accessible); return false; } foundFocusable = true; } } return foundFocusable; } /** * Determine if an accessible is a keyboard focusable button in the url bar. * Url bar buttons aren't keyboard focusable in the usual way. Instead, * focus is managed by JS code which sets tabindex on a single button at a * time. Thus, we need to special case the focusable check for these buttons. * This also applies to the search bar buttons that reuse the same pattern. */ function isKeyboardFocusableUrlbarButton(accessible) { const node = accessible.DOMNode; if (!node || !node.ownerGlobal) { return false; } const isUrlBar = node .closest(".urlbarView > .search-one-offs") ?.getAttribute("disabletab") == "true"; const isSearchBar = node .closest("#PopupSearchAutoComplete > .search-one-offs") ?.getAttribute("is_searchbar") == "true"; return ( (isUrlBar || isSearchBar) && node.getAttribute("tabindex") == "-1" && node.tagName == "button" && node.classList.contains("searchbar-engine-one-off-item") ); } /** * Determine if an accessible is a keyboard focusable XUL tab. * Only one tab is focusable at a time, but after focusing it, you can use * the keyboard to focus other tabs. */ function isKeyboardFocusableXULTab(accessible) { const node = accessible.DOMNode; return node && XULElement.isInstance(node) && node.tagName == "tab"; } /** * The gridcells are not expected to be interactive and focusable * individually, but it is allowed to manually manage focus within the grid * per ARIA Grid pattern (https://www.w3.org/WAI/ARIA/apg/patterns/grid/). * Example of such grid would be a datepicker where one gridcell can be * selected and the focus is moved with arrow keys once the user tabbed into * the grid. In grids like a calendar, only one element would be included in * the focus order and the rest of grid cells may not have an interactive * accessible created. We need to special case the check for these gridcells. */ function isAccessibleGridcell(node) { if (!node || !node.ownerGlobal) { return false; } const accessible = getAccessible(node); if (!accessible || accessible.role != Ci.nsIAccessibleRole.ROLE_GRID_CELL) { return false; // Not a grid cell. } // ToDo: We may eventually need to support intervening generics between // a grid cell and its grid container here. const gridRow = accessible.parent; if (!gridRow || gridRow.role != Ci.nsIAccessibleRole.ROLE_ROW) { return false; // The grid cell isn't inside a row. } let grid = gridRow.parent; if (!grid) { return false; // The grid cell isn't inside a grid. } if (grid.role == Ci.nsIAccessibleRole.ROLE_GROUPING) { // Grid built on the HTML table may include
wrapper: grid = grid.parent; if (!grid || grid.role != Ci.nsIAccessibleRole.ROLE_GRID) { return false; // The grid cell isn't inside a grid. } } // Check that there is only one keyboard reachable grid cell. let foundFocusable = false; for (const gridCell of grid.DOMNode.querySelectorAll( "td, [role=gridcell]" )) { // Grid cells are not expected to have a "tabindex" attribute and to be // included in the focus order, with the exception of the only one cell // that is included in the page tab sequence to provide access to the grid. if (gridCell.tabIndex == 0) { if (foundFocusable) { // Only one grid cell within a grid should be focusable. // ToDo: Fine-tune the a11y-check error message generated in this case. // Strictly speaking, it's not ideal that we're performing an action // from an is function, which normally only queries something without // any externally observable behaviour. That said, fixing that would // involve different return values for different cases (not a grid // cell, too many focusable grid cells, etc) so we could move the // a11yFail call to the caller. a11yFail( "Only one grid cell should be focusable in a grid", accessible ); return false; } foundFocusable = true; } } return foundFocusable; } /** * XUL treecol elements currently aren't focusable, making them inaccessible. * For now, we don't flag these as a failure to avoid breaking multiple tests. * ToDo: We should remove this exception after this is fixed in bug 1848397. */ function isInaccessibleXulTreecol(node) { if (!node || !node.ownerGlobal) { return false; } const listheader = node.flattenedTreeParentNode; if (listheader.tagName !== "listheader" || node.tagName !== "treecol") { return false; } return true; } /** * Determine if a DOM node is a combobox container of the url bar. We * intentionally leave this element unlabeled, because its child is a search * input that is the target and main control of this component. In general, we * want to avoid duplication in the label announcement when a user focuses the * input. Both NVDA and VO ignore the label on at least one of these controls * if both have a label. But the bigger concern here is that it's very * difficult to keep the accessible name synchronized between the combobox and * the input. Thus, we need to special case the label check for this control. */ function isUnlabeledUrlBarCombobox(node) { if (!node || !node.ownerGlobal) { return false; } let ariaRole = node.getAttribute("role"); // There are only two cases of this pattern: