mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +01:00
## The basics - [x] I [validated my changes](https://developers.google.com/blockly/guides/contribute/core#making_and_verifying_a_change) ## The details ### Resolves Fixes part of #8207 Fixes part of #3370 ### Proposed Changes This introduces initial broad ARIA integration in order to enable at least basic screen reader support when using keyboard navigation. Largely this involves introducing ARIA roles and labels in a bunch of places, sometimes done in a way to override normal built-in behaviors of the accessibility node tree in order to get a richer first-class output for Blockly (such as for blocks and workspaces). ### Reason for Changes ARIA is the fundamental basis for configuring how focusable nodes in Blockly are represented to the user when using a screen reader. As such, all focusable nodes requires labels and roles in order to correctly communicate their contexts. The specific approach taken in this PR is to simply add labels and roles to all nodes where obvious with some extra work done for `WorkspaceSvg` and `BlockSvg` in order to represent blocks as a tree (since that seems to be the best fitting ARIA role per those available: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles). The custom work specifically for blocks includes: - Overriding the role description to be 'block' rather than 'tree item' (which is the default). - Overriding the position, level, and number of sibling counts since those are normally determined based on the DOM tree and blocks are not laid out in the tree the same way they are visually or logically (so these computations were incorrect). This is also the reason for a bunch of extra computation logic being introduced. One note on some of the labels being nonsensical (e.g. 'DoNotOverride?'): this was done intentionally to try and ensure _all_ focusable nodes (that can be focused) have labels, even when the specifics of what that label should be aren't yet clear. More components had these temporary labels until testing revealed how exactly they would behave from a screen reader perspective (at which point their roles and labels were updated as needed). The temporary labels act as an indicator when navigating through the UI, and some of the nodes can't easily be reached (for reasons) and thus may never actually need a label. More work is needed in understanding both what components need labels and what those labels should be, but that will be done beyond this PR. ### Test Coverage No tests are added to this as it's experimental and not a final implementation. The keyboard navigation tests are failing due to a visibility expansion of `connectionCandidate` in `BlockDragStrategy`. There's no way to avoid this breakage, unfortunately. Instead, this PR will be merged and then https://github.com/google/blockly-keyboard-experimentation/pull/684 will be finalized and merged to fix it. There's some additional work that will happen both in that branch and in a later PR in core Blockly to integrate the two experimentation branches as part of #9283 so that CI passes correctly for both branches. ### Documentation No documentation is needed at this time. ### Additional Information This work is experimental and is meant to serve two purposes: - Provide a foundation for testing and iterating the core screen reader experience in Blockly. - Provide a reference point for designing a long-term solution that accounts for all requirements collected during user testing. This code should never be merged into `develop` as it stands. Instead, it will be redesigned with maintainability, testing, and correctness in mind at a future date (see https://github.com/google/blockly-keyboard-experimentation/discussions/673).
208 lines
6.5 KiB
TypeScript
208 lines
6.5 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
// Former goog.module ID: Blockly.utils.aria
|
|
|
|
/** ARIA states/properties prefix. */
|
|
const ARIA_PREFIX = 'aria-';
|
|
|
|
/** ARIA role attribute. */
|
|
const ROLE_ATTRIBUTE = 'role';
|
|
|
|
/**
|
|
* ARIA role values.
|
|
* Copied from Closure's goog.a11y.aria.Role
|
|
*/
|
|
export enum Role {
|
|
// ARIA role for a group of related elements like tree item siblings.
|
|
GROUP = 'group',
|
|
|
|
// ARIA role for a listbox.
|
|
LISTBOX = 'listbox',
|
|
|
|
// ARIA role for a popup menu.
|
|
MENU = 'menu',
|
|
|
|
// ARIA role for menu item elements.
|
|
MENUITEM = 'menuitem',
|
|
// ARIA role for option items that are children of combobox, listbox, menu,
|
|
// radiogroup, or tree elements.
|
|
OPTION = 'option',
|
|
// ARIA role for ignorable cosmetic elements with no semantic significance.
|
|
PRESENTATION = 'presentation',
|
|
|
|
// ARIA role for a tree.
|
|
TREE = 'tree',
|
|
|
|
// ARIA role for a tree item that sometimes may be expanded or collapsed.
|
|
TREEITEM = 'treeitem',
|
|
|
|
// ARIA role for a visual separator in e.g. a menu.
|
|
SEPARATOR = 'separator',
|
|
|
|
// ARIA role for a live region providing information.
|
|
STATUS = 'status',
|
|
|
|
IMAGE = 'image',
|
|
FIGURE = 'figure',
|
|
BUTTON = 'button',
|
|
CHECKBOX = 'checkbox',
|
|
TEXTBOX = 'textbox',
|
|
}
|
|
|
|
/**
|
|
* ARIA states and properties.
|
|
* Copied from Closure's goog.a11y.aria.State
|
|
*/
|
|
export enum State {
|
|
// ARIA property for setting the currently active descendant of an element,
|
|
// for example the selected item in a list box. Value: ID of an element.
|
|
ACTIVEDESCENDANT = 'activedescendant',
|
|
// ARIA state for a disabled item. Value: one of {true, false}.
|
|
DISABLED = 'disabled',
|
|
|
|
// ARIA state for setting whether the element like a tree node is expanded.
|
|
// Value: one of {true, false, undefined}.
|
|
EXPANDED = 'expanded',
|
|
|
|
// ARIA state indicating that the entered value does not conform. Value:
|
|
// one of {false, true, 'grammar', 'spelling'}
|
|
INVALID = 'invalid',
|
|
|
|
// ARIA property that provides a label to override any other text, value, or
|
|
// contents used to describe this element. Value: string.
|
|
LABEL = 'label',
|
|
// ARIA property for setting the element which labels another element.
|
|
// Value: space-separated IDs of elements.
|
|
LABELLEDBY = 'labelledby',
|
|
|
|
// ARIA property for setting the level of an element in the hierarchy.
|
|
// Value: integer.
|
|
LEVEL = 'level',
|
|
|
|
// ARIA property that defines an element's number of position in a list.
|
|
// Value: integer.
|
|
POSINSET = 'posinset',
|
|
|
|
// ARIA state for setting the currently selected item in the list.
|
|
// Value: one of {true, false, undefined}.
|
|
SELECTED = 'selected',
|
|
// ARIA property defining the number of items in a list. Value: integer.
|
|
SETSIZE = 'setsize',
|
|
|
|
// ARIA property for slider maximum value. Value: number.
|
|
VALUEMAX = 'valuemax',
|
|
|
|
// ARIA property for slider minimum value. Value: number.
|
|
VALUEMIN = 'valuemin',
|
|
|
|
// ARIA property for live region chattiness.
|
|
// Value: one of {polite, assertive, off}.
|
|
LIVE = 'live',
|
|
|
|
// ARIA property for removing elements from the accessibility tree.
|
|
// Value: one of {true, false, undefined}.
|
|
HIDDEN = 'hidden',
|
|
|
|
ROLEDESCRIPTION = 'roledescription',
|
|
OWNS = 'owns',
|
|
}
|
|
|
|
/**
|
|
* Updates the specific role for the specified element.
|
|
*
|
|
* @param element The element whose ARIA role should be changed.
|
|
* @param roleName The new role for the specified element, or null if its role
|
|
* should be cleared.
|
|
*/
|
|
export function setRole(element: Element, roleName: Role | null) {
|
|
if (roleName) {
|
|
element.setAttribute(ROLE_ATTRIBUTE, roleName);
|
|
} else element.removeAttribute(ROLE_ATTRIBUTE);
|
|
}
|
|
|
|
/**
|
|
* Returns the ARIA role of the specified element, or null if it either doesn't
|
|
* have a designated role or if that role is unknown.
|
|
*
|
|
* @param element The element from which to retrieve its ARIA role.
|
|
* @returns The ARIA role of the element, or null if undefined or unknown.
|
|
*/
|
|
export function getRole(element: Element): Role | null {
|
|
// This is an unsafe cast which is why it needs to be checked to ensure that
|
|
// it references a valid role.
|
|
const currentRoleName = element.getAttribute(ROLE_ATTRIBUTE) as Role;
|
|
if (Object.values(Role).includes(currentRoleName)) {
|
|
return currentRoleName;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Sets the specified ARIA state by its name and value for the specified
|
|
* element.
|
|
*
|
|
* Note that the type of value is not validated against the specific type of
|
|
* state being changed, so it's up to callers to ensure the correct value is
|
|
* used for the given state.
|
|
*
|
|
* @param element The element whose ARIA state may be changed.
|
|
* @param stateName The state to change.
|
|
* @param value The new value to specify for the provided state.
|
|
*/
|
|
export function setState(
|
|
element: Element,
|
|
stateName: State,
|
|
value: string | boolean | number | string[],
|
|
) {
|
|
if (Array.isArray(value)) {
|
|
value = value.join(' ');
|
|
}
|
|
const attrStateName = ARIA_PREFIX + stateName;
|
|
element.setAttribute(attrStateName, `${value}`);
|
|
}
|
|
|
|
/**
|
|
* Returns a string representation of the specified state for the specified
|
|
* element, or null if it's not defined or specified.
|
|
*
|
|
* Note that an explicit set state of 'null' will return the 'null' string, not
|
|
* the value null.
|
|
*
|
|
* @param element The element whose state is being retrieved.
|
|
* @param stateName The state to retrieve.
|
|
* @returns The string representation of the requested state for the specified
|
|
* element, or null if not defined.
|
|
*/
|
|
export function getState(element: Element, stateName: State): string | null {
|
|
const attrStateName = ARIA_PREFIX + stateName;
|
|
return element.getAttribute(attrStateName);
|
|
}
|
|
|
|
/**
|
|
* Softly requests that the specified text be read to the user if a screen
|
|
* reader is currently active.
|
|
*
|
|
* This relies on a centrally managed ARIA live region that should not interrupt
|
|
* existing announcements (that is, this is what's considered a polite
|
|
* announcement).
|
|
*
|
|
* Callers should use this judiciously. It's often considered bad practice to
|
|
* over announce information that can be inferred from other sources on the
|
|
* page, so this ought to only be used when certain context cannot be easily
|
|
* determined (such as dynamic states that may not have perfect ARIA
|
|
* representations or indications).
|
|
*
|
|
* @param text The text to politely read to the user.
|
|
*/
|
|
export function announceDynamicAriaState(text: string) {
|
|
const ariaAnnouncementSpan = document.getElementById('blocklyAriaAnnounce');
|
|
if (!ariaAnnouncementSpan) {
|
|
throw new Error('Expected element with id blocklyAriaAnnounce to exist.');
|
|
}
|
|
ariaAnnouncementSpan.innerHTML = text;
|
|
}
|