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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} SerializedAXNode
|
|
|
|
* @property {string} role
|
|
|
|
*
|
|
|
|
* @property {string=} name
|
|
|
|
* @property {string|number=} value
|
|
|
|
* @property {string=} description
|
|
|
|
*
|
|
|
|
* @property {string=} keyshortcuts
|
|
|
|
* @property {string=} roledescription
|
|
|
|
* @property {string=} valuetext
|
|
|
|
*
|
|
|
|
* @property {boolean=} disabled
|
|
|
|
* @property {boolean=} expanded
|
|
|
|
* @property {boolean=} focused
|
|
|
|
* @property {boolean=} modal
|
|
|
|
* @property {boolean=} multiline
|
|
|
|
* @property {boolean=} multiselectable
|
|
|
|
* @property {boolean=} readonly
|
|
|
|
* @property {boolean=} required
|
|
|
|
* @property {boolean=} selected
|
|
|
|
*
|
|
|
|
* @property {boolean|"mixed"=} checked
|
|
|
|
* @property {boolean|"mixed"=} pressed
|
|
|
|
*
|
|
|
|
* @property {number=} level
|
|
|
|
* @property {number=} valuemin
|
|
|
|
* @property {number=} valuemax
|
|
|
|
*
|
|
|
|
* @property {string=} autocomplete
|
|
|
|
* @property {string=} haspopup
|
|
|
|
* @property {string=} invalid
|
|
|
|
* @property {string=} orientation
|
|
|
|
*
|
|
|
|
* @property {Array<SerializedAXNode>=} children
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Accessibility {
|
|
|
|
/**
|
|
|
|
* @param {!Puppeteer.CDPSession} client
|
|
|
|
*/
|
|
|
|
constructor(client) {
|
|
|
|
this._client = client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-10 06:39:42 +00:00
|
|
|
* @param {{interestingOnly?: boolean, root?: ?Puppeteer.ElementHandle}=} options
|
2018-11-02 01:54:51 +00:00
|
|
|
* @return {!Promise<!SerializedAXNode>}
|
|
|
|
*/
|
|
|
|
async snapshot(options = {}) {
|
2019-05-10 06:39:42 +00:00
|
|
|
const {
|
|
|
|
interestingOnly = true,
|
|
|
|
root = null,
|
|
|
|
} = options;
|
2018-11-02 01:54:51 +00:00
|
|
|
const {nodes} = await this._client.send('Accessibility.getFullAXTree');
|
2019-05-10 06:39:42 +00:00
|
|
|
let backendNodeId = null;
|
|
|
|
if (root) {
|
|
|
|
const {node} = await this._client.send('DOM.describeNode', {objectId: root._remoteObject.objectId});
|
|
|
|
backendNodeId = node.backendNodeId;
|
|
|
|
}
|
|
|
|
const defaultRoot = AXNode.createTree(nodes);
|
|
|
|
let needle = defaultRoot;
|
|
|
|
if (backendNodeId) {
|
|
|
|
needle = defaultRoot.find(node => node._payload.backendDOMNodeId === backendNodeId);
|
|
|
|
if (!needle)
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
if (!interestingOnly)
|
2019-05-10 06:39:42 +00:00
|
|
|
return serializeTree(needle)[0];
|
2018-11-02 01:54:51 +00:00
|
|
|
|
|
|
|
/** @type {!Set<!AXNode>} */
|
|
|
|
const interestingNodes = new Set();
|
2019-05-10 06:39:42 +00:00
|
|
|
collectInterestingNodes(interestingNodes, defaultRoot, false);
|
|
|
|
if (!interestingNodes.has(needle))
|
|
|
|
return null;
|
|
|
|
return serializeTree(needle, interestingNodes)[0];
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Set<!AXNode>} collection
|
|
|
|
* @param {!AXNode} node
|
|
|
|
* @param {boolean} insideControl
|
|
|
|
*/
|
|
|
|
function collectInterestingNodes(collection, node, insideControl) {
|
|
|
|
if (node.isInteresting(insideControl))
|
|
|
|
collection.add(node);
|
|
|
|
if (node.isLeafNode())
|
|
|
|
return;
|
|
|
|
insideControl = insideControl || node.isControl();
|
|
|
|
for (const child of node._children)
|
|
|
|
collectInterestingNodes(collection, child, insideControl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!AXNode} node
|
|
|
|
* @param {!Set<!AXNode>=} whitelistedNodes
|
|
|
|
* @return {!Array<!SerializedAXNode>}
|
|
|
|
*/
|
|
|
|
function serializeTree(node, whitelistedNodes) {
|
|
|
|
/** @type {!Array<!SerializedAXNode>} */
|
|
|
|
const children = [];
|
|
|
|
for (const child of node._children)
|
|
|
|
children.push(...serializeTree(child, whitelistedNodes));
|
|
|
|
|
|
|
|
if (whitelistedNodes && !whitelistedNodes.has(node))
|
|
|
|
return children;
|
|
|
|
|
|
|
|
const serializedNode = node.serialize();
|
|
|
|
if (children.length)
|
|
|
|
serializedNode.children = children;
|
|
|
|
return [serializedNode];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AXNode {
|
|
|
|
/**
|
|
|
|
* @param {!Protocol.Accessibility.AXNode} payload
|
|
|
|
*/
|
|
|
|
constructor(payload) {
|
|
|
|
this._payload = payload;
|
|
|
|
|
|
|
|
/** @type {!Array<!AXNode>} */
|
|
|
|
this._children = [];
|
|
|
|
|
|
|
|
this._richlyEditable = false;
|
|
|
|
this._editable = false;
|
|
|
|
this._focusable = false;
|
|
|
|
this._expanded = false;
|
2019-10-23 09:41:01 +00:00
|
|
|
this._hidden = false;
|
2018-11-02 01:54:51 +00:00
|
|
|
this._name = this._payload.name ? this._payload.name.value : '';
|
|
|
|
this._role = this._payload.role ? this._payload.role.value : 'Unknown';
|
|
|
|
this._cachedHasFocusableChild;
|
|
|
|
|
|
|
|
for (const property of this._payload.properties || []) {
|
|
|
|
if (property.name === 'editable') {
|
|
|
|
this._richlyEditable = property.value.value === 'richtext';
|
|
|
|
this._editable = true;
|
|
|
|
}
|
|
|
|
if (property.name === 'focusable')
|
|
|
|
this._focusable = property.value.value;
|
|
|
|
if (property.name === 'expanded')
|
|
|
|
this._expanded = property.value.value;
|
2019-10-23 09:41:01 +00:00
|
|
|
if (property.name === 'hidden')
|
|
|
|
this._hidden = property.value.value;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
_isPlainTextField() {
|
|
|
|
if (this._richlyEditable)
|
|
|
|
return false;
|
|
|
|
if (this._editable)
|
|
|
|
return true;
|
|
|
|
return this._role === 'textbox' || this._role === 'ComboBox' || this._role === 'searchbox';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
_isTextOnlyObject() {
|
|
|
|
const role = this._role;
|
|
|
|
return (role === 'LineBreak' || role === 'text' ||
|
|
|
|
role === 'InlineTextBox');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
_hasFocusableChild() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-10 06:39:42 +00:00
|
|
|
/**
|
|
|
|
* @param {function(AXNode):boolean} predicate
|
|
|
|
* @return {?AXNode}
|
|
|
|
*/
|
|
|
|
find(predicate) {
|
|
|
|
if (predicate(this))
|
|
|
|
return this;
|
|
|
|
for (const child of this._children) {
|
|
|
|
const result = child.find(predicate);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:54:51 +00:00
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isLeafNode() {
|
|
|
|
if (!this._children.length)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (this._isPlainTextField() || this._isTextOnlyObject())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if (this._hasFocusableChild())
|
|
|
|
return false;
|
|
|
|
if (this._focusable && this._name)
|
|
|
|
return true;
|
|
|
|
if (this._role === 'heading' && this._name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isControl() {
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
isInteresting(insideControl) {
|
|
|
|
const role = this._role;
|
2019-10-23 09:41:01 +00:00
|
|
|
if (role === 'Ignored' || this._hidden)
|
2018-11-02 01:54:51 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (this._focusable || this._richlyEditable)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If it's not focusable but has a control role, then it's interesting.
|
|
|
|
if (this.isControl())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// A non focusable child of a control is not interesting
|
|
|
|
if (insideControl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return this.isLeafNode() && !!this._name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!SerializedAXNode}
|
|
|
|
*/
|
|
|
|
serialize() {
|
|
|
|
/** @type {!Map<string, number|string|boolean>} */
|
|
|
|
const properties = new Map();
|
|
|
|
for (const property of this._payload.properties || [])
|
|
|
|
properties.set(property.name.toLowerCase(), property.value.value);
|
|
|
|
if (this._payload.name)
|
|
|
|
properties.set('name', this._payload.name.value);
|
|
|
|
if (this._payload.value)
|
|
|
|
properties.set('value', this._payload.value.value);
|
|
|
|
if (this._payload.description)
|
|
|
|
properties.set('description', this._payload.description.value);
|
|
|
|
|
|
|
|
/** @type {SerializedAXNode} */
|
|
|
|
const node = {
|
|
|
|
role: this._role
|
|
|
|
};
|
|
|
|
|
2020-03-31 08:48:09 +00:00
|
|
|
/** @enum {'name'|'value'|'description'|'keyshortcuts'|'roledescription'|'valuetext'} */
|
|
|
|
let UserStringProperties; // eslint-disable-line no-unused-vars
|
|
|
|
/** @type {!Array<UserStringProperties>} */
|
2018-11-02 01:54:51 +00:00
|
|
|
const userStringProperties = [
|
|
|
|
'name',
|
|
|
|
'value',
|
|
|
|
'description',
|
|
|
|
'keyshortcuts',
|
|
|
|
'roledescription',
|
|
|
|
'valuetext',
|
|
|
|
];
|
2020-03-31 08:48:09 +00:00
|
|
|
/**
|
|
|
|
* @param {UserStringProperties} key
|
|
|
|
*/
|
|
|
|
const getUserStringPropertyValue = key => /** @type string */(properties.get(key));
|
|
|
|
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const userStringProperty of userStringProperties) {
|
|
|
|
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-03-31 08:48:09 +00:00
|
|
|
/** @enum {'disabled'|'expanded'|'focused'|'modal'|'multiline'|'multiselectable'|'readonly'|'required'|'selected'} */
|
|
|
|
let BooleanProperties; // eslint-disable-line no-unused-vars
|
|
|
|
/** @type {!Array<BooleanProperties>} */
|
2018-11-02 01:54:51 +00:00
|
|
|
const booleanProperties = [
|
|
|
|
'disabled',
|
|
|
|
'expanded',
|
|
|
|
'focused',
|
|
|
|
'modal',
|
|
|
|
'multiline',
|
|
|
|
'multiselectable',
|
|
|
|
'readonly',
|
|
|
|
'required',
|
|
|
|
'selected',
|
|
|
|
];
|
2020-03-31 08:48:09 +00:00
|
|
|
/**
|
|
|
|
* @param {BooleanProperties} key
|
|
|
|
*/
|
|
|
|
const getBooleanPropertyValue = key => /** @type boolean */(properties.get(key));
|
|
|
|
|
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.
|
|
|
|
if (booleanProperty === 'focused' && this._role === 'WebArea')
|
|
|
|
continue;
|
2020-03-31 08:48:09 +00:00
|
|
|
const value = getBooleanPropertyValue(booleanProperty);
|
2018-11-02 01:54:51 +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-03-31 08:48:09 +00:00
|
|
|
/** @enum {'checked'|'pressed'} */
|
|
|
|
let TristateProperties; // eslint-disable-line no-unused-vars
|
|
|
|
/** @type {!Array<TristateProperties>} */
|
2018-11-02 01:54:51 +00:00
|
|
|
const tristateProperties = [
|
|
|
|
'checked',
|
|
|
|
'pressed',
|
|
|
|
];
|
|
|
|
for (const tristateProperty of tristateProperties) {
|
|
|
|
if (!properties.has(tristateProperty))
|
|
|
|
continue;
|
|
|
|
const value = properties.get(tristateProperty);
|
|
|
|
node[tristateProperty] = value === 'mixed' ? 'mixed' : value === 'true' ? true : false;
|
|
|
|
}
|
2020-03-31 08:48:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** @enum {'level'|'valuemax'|'valuemin'} */
|
|
|
|
let NumericalProperties; // eslint-disable-line no-unused-vars
|
|
|
|
/** @type {!Array<NumericalProperties>} */
|
2018-11-02 01:54:51 +00:00
|
|
|
const numericalProperties = [
|
|
|
|
'level',
|
|
|
|
'valuemax',
|
|
|
|
'valuemin',
|
|
|
|
];
|
2020-03-31 08:48:09 +00:00
|
|
|
/**
|
|
|
|
* @param {NumericalProperties} key
|
|
|
|
*/
|
|
|
|
const getNumericalPropertyValue = key => /** @type number */(properties.get(key));
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const numericalProperty of numericalProperties) {
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
/** @enum {'autocomplete'|'haspopup'|'invalid'|'orientation'} */
|
|
|
|
let TokenProperties; // eslint-disable-line no-unused-vars
|
|
|
|
/** @type {!Array<TokenProperties>} */
|
2018-11-02 01:54:51 +00:00
|
|
|
const tokenProperties = [
|
|
|
|
'autocomplete',
|
|
|
|
'haspopup',
|
|
|
|
'invalid',
|
|
|
|
'orientation',
|
|
|
|
];
|
2020-03-31 08:48:09 +00:00
|
|
|
/**
|
|
|
|
* @param {TokenProperties} key
|
|
|
|
*/
|
|
|
|
const getTokenPropertyValue = key => /** @type string */(properties.get(key));
|
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);
|
2018-11-02 01:54:51 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Array<!Protocol.Accessibility.AXNode>} payloads
|
|
|
|
* @return {!AXNode}
|
|
|
|
*/
|
|
|
|
static createTree(payloads) {
|
|
|
|
/** @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {Accessibility};
|