2018-11-02 01:54:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 08:20:25 +00:00
|
|
|
// Used as a TypeDef
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-05-07 10:54:55 +00:00
|
|
|
import { CDPSession } from './Connection';
|
2020-04-21 11:11:06 +00:00
|
|
|
// Used as a TypeDef
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-05-07 10:54:55 +00:00
|
|
|
import { ElementHandle } from './JSHandle';
|
2020-04-23 14:35:03 +00:00
|
|
|
|
|
|
|
interface SerializedAXNode {
|
|
|
|
role: string;
|
|
|
|
name?: string;
|
2020-05-07 10:54:55 +00:00
|
|
|
value?: string | number;
|
2020-04-23 14:35:03 +00:00
|
|
|
description?: string;
|
|
|
|
keyshortcuts?: string;
|
|
|
|
roledescription?: string;
|
|
|
|
valuetext?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
expanded?: boolean;
|
|
|
|
focused?: boolean;
|
|
|
|
modal?: boolean;
|
|
|
|
multiline?: boolean;
|
|
|
|
multiselectable?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
required?: boolean;
|
|
|
|
selected?: boolean;
|
2020-05-07 10:54:55 +00:00
|
|
|
checked?: boolean | 'mixed';
|
|
|
|
pressed?: boolean | 'mixed';
|
2020-04-23 14:35:03 +00:00
|
|
|
level?: number;
|
|
|
|
valuemin?: number;
|
|
|
|
valuemax?: number;
|
|
|
|
autocomplete?: string;
|
|
|
|
haspopup?: string;
|
|
|
|
invalid?: string;
|
|
|
|
orientation?: string;
|
2020-04-28 14:06:43 +00:00
|
|
|
children?: SerializedAXNode[];
|
2020-04-23 14:35:03 +00:00
|
|
|
}
|
2020-04-21 08:20:25 +00:00
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
export class Accessibility {
|
|
|
|
_client: CDPSession;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
constructor(client: CDPSession) {
|
2018-11-02 01:54:51 +00:00
|
|
|
this._client = client;
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
async snapshot(
|
|
|
|
options: { interestingOnly?: boolean; root?: ElementHandle } = {}
|
|
|
|
): Promise<SerializedAXNode> {
|
|
|
|
const { interestingOnly = true, root = null } = options;
|
|
|
|
const { nodes } = await this._client.send('Accessibility.getFullAXTree');
|
2019-05-10 06:39:42 +00:00
|
|
|
let backendNodeId = null;
|
|
|
|
if (root) {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { node } = await this._client.send('DOM.describeNode', {
|
|
|
|
objectId: root._remoteObject.objectId,
|
|
|
|
});
|
2019-05-10 06:39:42 +00:00
|
|
|
backendNodeId = node.backendNodeId;
|
|
|
|
}
|
|
|
|
const defaultRoot = AXNode.createTree(nodes);
|
|
|
|
let needle = defaultRoot;
|
|
|
|
if (backendNodeId) {
|
2020-05-07 10:54:55 +00:00
|
|
|
needle = defaultRoot.find(
|
|
|
|
(node) => node._payload.backendDOMNodeId === backendNodeId
|
|
|
|
);
|
|
|
|
if (!needle) return null;
|
2019-05-10 06:39:42 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!interestingOnly) return serializeTree(needle)[0];
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
const interestingNodes = new Set<AXNode>();
|
2019-05-10 06:39:42 +00:00
|
|
|
collectInterestingNodes(interestingNodes, defaultRoot, false);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!interestingNodes.has(needle)) return null;
|
2019-05-10 06:39:42 +00:00
|
|
|
return serializeTree(needle, interestingNodes)[0];
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Set<!AXNode>} collection
|
|
|
|
* @param {!AXNode} node
|
|
|
|
* @param {boolean} insideControl
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
function collectInterestingNodes(
|
|
|
|
collection: Set<AXNode>,
|
|
|
|
node: AXNode,
|
|
|
|
insideControl: boolean
|
|
|
|
): void {
|
|
|
|
if (node.isInteresting(insideControl)) collection.add(node);
|
|
|
|
if (node.isLeafNode()) return;
|
2018-11-02 01:54:51 +00:00
|
|
|
insideControl = insideControl || node.isControl();
|
|
|
|
for (const child of node._children)
|
|
|
|
collectInterestingNodes(collection, child, insideControl);
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
function serializeTree(
|
|
|
|
node: AXNode,
|
|
|
|
whitelistedNodes?: Set<AXNode>
|
|
|
|
): SerializedAXNode[] {
|
2020-04-23 14:35:03 +00:00
|
|
|
const children: SerializedAXNode[] = [];
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const child of node._children)
|
|
|
|
children.push(...serializeTree(child, whitelistedNodes));
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
if (whitelistedNodes && !whitelistedNodes.has(node)) return children;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
const serializedNode = node.serialize();
|
2020-05-07 10:54:55 +00:00
|
|
|
if (children.length) serializedNode.children = children;
|
2018-11-02 01:54:51 +00:00
|
|
|
return [serializedNode];
|
|
|
|
}
|
|
|
|
|
|
|
|
class AXNode {
|
2020-04-23 14:35:03 +00:00
|
|
|
_payload: Protocol.Accessibility.AXNode;
|
|
|
|
_children: AXNode[] = [];
|
|
|
|
_richlyEditable = false;
|
|
|
|
_editable = false;
|
|
|
|
_focusable = false;
|
|
|
|
_expanded = false;
|
|
|
|
_hidden = false;
|
|
|
|
_name: string;
|
|
|
|
_role: string;
|
|
|
|
_cachedHasFocusableChild?: boolean;
|
|
|
|
|
|
|
|
constructor(payload: Protocol.Accessibility.AXNode) {
|
2018-11-02 01:54:51 +00:00
|
|
|
this._payload = payload;
|
|
|
|
this._name = this._payload.name ? this._payload.name.value : '';
|
|
|
|
this._role = this._payload.role ? this._payload.role.value : 'Unknown';
|
|
|
|
|
|
|
|
for (const property of this._payload.properties || []) {
|
|
|
|
if (property.name === 'editable') {
|
|
|
|
this._richlyEditable = property.value.value === 'richtext';
|
|
|
|
this._editable = true;
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
if (property.name === 'focusable') this._focusable = property.value.value;
|
|
|
|
if (property.name === 'expanded') this._expanded = property.value.value;
|
|
|
|
if (property.name === 'hidden') this._hidden = property.value.value;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
_isPlainTextField(): boolean {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (this._richlyEditable) return false;
|
|
|
|
if (this._editable) return true;
|
|
|
|
return (
|
|
|
|
this._role === 'textbox' ||
|
|
|
|
this._role === 'ComboBox' ||
|
|
|
|
this._role === 'searchbox'
|
|
|
|
);
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
_isTextOnlyObject(): boolean {
|
2018-11-02 01:54:51 +00:00
|
|
|
const role = this._role;
|
2020-05-07 10:54:55 +00:00
|
|
|
return role === 'LineBreak' || role === 'text' || role === 'InlineTextBox';
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
_hasFocusableChild(): boolean {
|
2018-11-02 01:54:51 +00:00
|
|
|
if (this._cachedHasFocusableChild === undefined) {
|
|
|
|
this._cachedHasFocusableChild = false;
|
|
|
|
for (const child of this._children) {
|
|
|
|
if (child._focusable || child._hasFocusableChild()) {
|
|
|
|
this._cachedHasFocusableChild = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._cachedHasFocusableChild;
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
find(predicate: (x: AXNode) => boolean): AXNode | null {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (predicate(this)) return this;
|
2019-05-10 06:39:42 +00:00
|
|
|
for (const child of this._children) {
|
|
|
|
const result = child.find(predicate);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (result) return result;
|
2019-05-10 06:39:42 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
isLeafNode(): boolean {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!this._children.length) return true;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
// These types of objects may have children that we use as internal
|
|
|
|
// implementation details, but we want to expose them as leaves to platform
|
|
|
|
// accessibility APIs because screen readers might be confused if they find
|
|
|
|
// any children.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (this._isPlainTextField() || this._isTextOnlyObject()) return true;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
// Roles whose children are only presentational according to the ARIA and
|
|
|
|
// HTML5 Specs should be hidden from screen readers.
|
|
|
|
// (Note that whilst ARIA buttons can have only presentational children, HTML5
|
|
|
|
// buttons are allowed to have content.)
|
|
|
|
switch (this._role) {
|
|
|
|
case 'doc-cover':
|
|
|
|
case 'graphics-symbol':
|
|
|
|
case 'img':
|
|
|
|
case 'Meter':
|
|
|
|
case 'scrollbar':
|
|
|
|
case 'slider':
|
|
|
|
case 'separator':
|
|
|
|
case 'progressbar':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here and below: Android heuristics
|
2020-05-07 10:54:55 +00:00
|
|
|
if (this._hasFocusableChild()) return false;
|
|
|
|
if (this._focusable && this._name) return true;
|
|
|
|
if (this._role === 'heading' && this._name) return true;
|
2018-11-02 01:54:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
isControl(): boolean {
|
2018-11-02 01:54:51 +00:00
|
|
|
switch (this._role) {
|
|
|
|
case 'button':
|
|
|
|
case 'checkbox':
|
|
|
|
case 'ColorWell':
|
|
|
|
case 'combobox':
|
|
|
|
case 'DisclosureTriangle':
|
|
|
|
case 'listbox':
|
|
|
|
case 'menu':
|
|
|
|
case 'menubar':
|
|
|
|
case 'menuitem':
|
|
|
|
case 'menuitemcheckbox':
|
|
|
|
case 'menuitemradio':
|
|
|
|
case 'radio':
|
|
|
|
case 'scrollbar':
|
|
|
|
case 'searchbox':
|
|
|
|
case 'slider':
|
|
|
|
case 'spinbutton':
|
|
|
|
case 'switch':
|
|
|
|
case 'tab':
|
|
|
|
case 'textbox':
|
|
|
|
case 'tree':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} insideControl
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
isInteresting(insideControl: boolean): boolean {
|
2018-11-02 01:54:51 +00:00
|
|
|
const role = this._role;
|
2020-05-07 10:54:55 +00:00
|
|
|
if (role === 'Ignored' || this._hidden) return false;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
if (this._focusable || this._richlyEditable) return true;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
// If it's not focusable but has a control role, then it's interesting.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (this.isControl()) return true;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
// A non focusable child of a control is not interesting
|
2020-05-07 10:54:55 +00:00
|
|
|
if (insideControl) return false;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
return this.isLeafNode() && !!this._name;
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
serialize(): SerializedAXNode {
|
2020-05-07 10:54:55 +00:00
|
|
|
const properties = new Map<string, number | string | boolean>();
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const property of this._payload.properties || [])
|
|
|
|
properties.set(property.name.toLowerCase(), property.value.value);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (this._payload.name) properties.set('name', this._payload.name.value);
|
|
|
|
if (this._payload.value) properties.set('value', this._payload.value.value);
|
2018-11-02 01:54:51 +00:00
|
|
|
if (this._payload.description)
|
|
|
|
properties.set('description', this._payload.description.value);
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
const node: SerializedAXNode = {
|
2020-05-07 10:54:55 +00:00
|
|
|
role: this._role,
|
2018-11-02 01:54:51 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
type UserStringProperty =
|
|
|
|
| 'name'
|
|
|
|
| 'value'
|
|
|
|
| 'description'
|
|
|
|
| 'keyshortcuts'
|
|
|
|
| 'roledescription'
|
|
|
|
| 'valuetext';
|
2020-04-23 14:35:03 +00:00
|
|
|
|
|
|
|
const userStringProperties: UserStringProperty[] = [
|
2018-11-02 01:54:51 +00:00
|
|
|
'name',
|
|
|
|
'value',
|
|
|
|
'description',
|
|
|
|
'keyshortcuts',
|
|
|
|
'roledescription',
|
|
|
|
'valuetext',
|
|
|
|
];
|
2020-05-07 10:54:55 +00:00
|
|
|
const getUserStringPropertyValue = (key: UserStringProperty): string =>
|
|
|
|
properties.get(key) as string;
|
2020-03-31 08:48:09 +00:00
|
|
|
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const userStringProperty of userStringProperties) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!properties.has(userStringProperty)) continue;
|
2020-03-31 08:48:09 +00:00
|
|
|
|
|
|
|
node[userStringProperty] = getUserStringPropertyValue(userStringProperty);
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
type BooleanProperty =
|
|
|
|
| 'disabled'
|
|
|
|
| 'expanded'
|
|
|
|
| 'focused'
|
|
|
|
| 'modal'
|
|
|
|
| 'multiline'
|
|
|
|
| 'multiselectable'
|
|
|
|
| 'readonly'
|
|
|
|
| 'required'
|
|
|
|
| 'selected';
|
2020-04-23 14:35:03 +00:00
|
|
|
const booleanProperties: BooleanProperty[] = [
|
2018-11-02 01:54:51 +00:00
|
|
|
'disabled',
|
|
|
|
'expanded',
|
|
|
|
'focused',
|
|
|
|
'modal',
|
|
|
|
'multiline',
|
|
|
|
'multiselectable',
|
|
|
|
'readonly',
|
|
|
|
'required',
|
|
|
|
'selected',
|
|
|
|
];
|
2020-05-07 10:54:55 +00:00
|
|
|
const getBooleanPropertyValue = (key: BooleanProperty): boolean =>
|
|
|
|
properties.get(key) as boolean;
|
2020-03-31 08:48:09 +00:00
|
|
|
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const booleanProperty of booleanProperties) {
|
|
|
|
// WebArea's treat focus differently than other nodes. They report whether their frame has focus,
|
|
|
|
// not whether focus is specifically on the root node.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (booleanProperty === 'focused' && this._role === 'WebArea') continue;
|
2020-03-31 08:48:09 +00:00
|
|
|
const value = getBooleanPropertyValue(booleanProperty);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!value) continue;
|
2020-03-31 08:48:09 +00:00
|
|
|
node[booleanProperty] = getBooleanPropertyValue(booleanProperty);
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
type TristateProperty = 'checked' | 'pressed';
|
|
|
|
const tristateProperties: TristateProperty[] = ['checked', 'pressed'];
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const tristateProperty of tristateProperties) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!properties.has(tristateProperty)) continue;
|
2018-11-02 01:54:51 +00:00
|
|
|
const value = properties.get(tristateProperty);
|
2020-05-07 10:54:55 +00:00
|
|
|
node[tristateProperty] =
|
|
|
|
value === 'mixed' ? 'mixed' : value === 'true' ? true : false;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
2020-03-31 08:48:09 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
type NumbericalProperty = 'level' | 'valuemax' | 'valuemin';
|
2020-04-23 14:35:03 +00:00
|
|
|
const numericalProperties: NumbericalProperty[] = [
|
2018-11-02 01:54:51 +00:00
|
|
|
'level',
|
|
|
|
'valuemax',
|
|
|
|
'valuemin',
|
|
|
|
];
|
2020-05-07 10:54:55 +00:00
|
|
|
const getNumericalPropertyValue = (key: NumbericalProperty): number =>
|
|
|
|
properties.get(key) as number;
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const numericalProperty of numericalProperties) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!properties.has(numericalProperty)) continue;
|
2020-03-31 08:48:09 +00:00
|
|
|
node[numericalProperty] = getNumericalPropertyValue(numericalProperty);
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
2020-03-31 08:48:09 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
type TokenProperty =
|
|
|
|
| 'autocomplete'
|
|
|
|
| 'haspopup'
|
|
|
|
| 'invalid'
|
|
|
|
| 'orientation';
|
2020-04-23 14:35:03 +00:00
|
|
|
const tokenProperties: TokenProperty[] = [
|
2018-11-02 01:54:51 +00:00
|
|
|
'autocomplete',
|
|
|
|
'haspopup',
|
|
|
|
'invalid',
|
|
|
|
'orientation',
|
|
|
|
];
|
2020-05-07 10:54:55 +00:00
|
|
|
const getTokenPropertyValue = (key: TokenProperty): string =>
|
|
|
|
properties.get(key) as string;
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const tokenProperty of tokenProperties) {
|
2020-03-31 08:48:09 +00:00
|
|
|
const value = getTokenPropertyValue(tokenProperty);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!value || value === 'false') continue;
|
2020-03-31 08:48:09 +00:00
|
|
|
node[tokenProperty] = getTokenPropertyValue(tokenProperty);
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
static createTree(payloads: Protocol.Accessibility.AXNode[]): AXNode {
|
2018-11-02 01:54:51 +00:00
|
|
|
/** @type {!Map<string, !AXNode>} */
|
|
|
|
const nodeById = new Map();
|
|
|
|
for (const payload of payloads)
|
|
|
|
nodeById.set(payload.nodeId, new AXNode(payload));
|
|
|
|
for (const node of nodeById.values()) {
|
|
|
|
for (const childId of node._payload.childIds || [])
|
|
|
|
node._children.push(nodeById.get(childId));
|
|
|
|
}
|
|
|
|
return nodeById.values().next().value;
|
|
|
|
}
|
|
|
|
}
|