2020-01-06 18:22:35 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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-01-03 11:15:43 -08:00
|
|
|
import * as accessibility from '../accessibility';
|
2020-01-09 11:02:55 -08:00
|
|
|
import { WKSession } from './wkConnection';
|
2020-01-03 11:15:43 -08:00
|
|
|
import { Protocol } from './protocol';
|
2020-01-14 16:54:50 -08:00
|
|
|
import * as dom from '../dom';
|
2020-06-25 18:01:18 -07:00
|
|
|
import * as types from '../types';
|
2020-01-03 11:15:43 -08:00
|
|
|
|
2020-01-14 16:54:50 -08:00
|
|
|
export async function getAccessibilityTree(session: WKSession, needle?: dom.ElementHandle) {
|
2020-05-27 17:19:05 -07:00
|
|
|
const objectId = needle ? needle._objectId : undefined;
|
2020-01-14 16:54:50 -08:00
|
|
|
const {axNode} = await session.send('Page.accessibilitySnapshot', { objectId });
|
|
|
|
const tree = new WKAXNode(axNode);
|
|
|
|
return {
|
|
|
|
tree,
|
|
|
|
needle: needle ? tree._findNeedle() : null
|
|
|
|
};
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:54:50 -08:00
|
|
|
const WKRoleToARIARole = new Map(Object.entries({
|
|
|
|
'TextField': 'textbox',
|
|
|
|
}));
|
|
|
|
|
|
|
|
// WebKit localizes role descriptions on mac, but the english versions only add noise.
|
|
|
|
const WKUnhelpfulRoleDescriptions = new Map(Object.entries({
|
|
|
|
'WebArea': 'HTML content',
|
|
|
|
'Summary': 'summary',
|
|
|
|
'DescriptionList': 'description list',
|
|
|
|
'ImageMap': 'image map',
|
|
|
|
'ListMarker': 'list marker',
|
|
|
|
'Video': 'video playback',
|
|
|
|
'Mark': 'highlighted',
|
|
|
|
'contentinfo': 'content information',
|
|
|
|
'Details': 'details',
|
|
|
|
'DescriptionListDetail': 'description',
|
|
|
|
'DescriptionListTerm': 'term',
|
|
|
|
'alertdialog': 'web alert dialog',
|
|
|
|
'dialog': 'web dialog',
|
|
|
|
'status': 'application status',
|
|
|
|
'tabpanel': 'tab panel',
|
|
|
|
'application': 'web application',
|
|
|
|
}));
|
|
|
|
|
2020-01-03 11:15:43 -08:00
|
|
|
class WKAXNode implements accessibility.AXNode {
|
|
|
|
private _payload: Protocol.Page.AXNode;
|
|
|
|
private _children: WKAXNode[];
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
constructor(payload: Protocol.Page.AXNode) {
|
2020-01-03 11:15:43 -08:00
|
|
|
this._payload = payload;
|
|
|
|
|
|
|
|
this._children = [];
|
|
|
|
for (const payload of this._payload.children || [])
|
|
|
|
this._children.push(new WKAXNode(payload));
|
|
|
|
}
|
|
|
|
|
|
|
|
children() {
|
|
|
|
return this._children;
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
_findNeedle(): WKAXNode | null {
|
2020-01-14 16:54:50 -08:00
|
|
|
if (this._payload.found)
|
|
|
|
return this;
|
|
|
|
for (const child of this._children) {
|
|
|
|
const found = child._findNeedle();
|
|
|
|
if (found)
|
|
|
|
return found;
|
|
|
|
}
|
2020-01-03 11:15:43 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
isControl(): boolean {
|
2020-01-03 11:15:43 -08:00
|
|
|
switch (this._payload.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 'TextField':
|
|
|
|
case 'tree':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
_isTextControl(): boolean {
|
2020-01-14 16:54:50 -08:00
|
|
|
switch (this._payload.role) {
|
|
|
|
case 'combobox':
|
|
|
|
case 'searchfield':
|
|
|
|
case 'textbox':
|
|
|
|
case 'TextField':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
_name(): string {
|
2020-01-14 16:54:50 -08:00
|
|
|
if (this._payload.role === 'text')
|
|
|
|
return this._payload.value || '';
|
|
|
|
return this._payload.name || '';
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
isInteresting(insideControl: boolean): boolean {
|
2020-01-14 16:54:50 -08:00
|
|
|
const {role, focusable} = this._payload;
|
|
|
|
const name = this._name();
|
2020-01-03 11:15:43 -08:00
|
|
|
if (role === 'ScrollArea')
|
|
|
|
return false;
|
|
|
|
if (role === 'WebArea')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (focusable || role === 'MenuListOption')
|
|
|
|
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() && !!name;
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:54:50 -08:00
|
|
|
_hasRendundantTextChild() {
|
|
|
|
if (this._children.length !== 1)
|
|
|
|
return false;
|
|
|
|
const child = this._children[0];
|
|
|
|
return child._payload.role === 'text' && this._payload.name === child._payload.value;
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:36:49 -08:00
|
|
|
isLeafNode(): boolean {
|
2020-01-14 16:54:50 -08:00
|
|
|
if (!this._children.length)
|
|
|
|
return true;
|
|
|
|
// WebKit on Linux ignores everything inside text controls, normalize this behavior
|
|
|
|
if (this._isTextControl())
|
|
|
|
return true;
|
|
|
|
// WebKit for mac has text nodes inside heading, li, menuitem, a, and p nodes
|
|
|
|
if (this._hasRendundantTextChild())
|
|
|
|
return true;
|
|
|
|
return false;
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
|
|
|
|
2020-06-25 18:01:18 -07:00
|
|
|
serialize(): types.SerializedAXNode {
|
|
|
|
const node: types.SerializedAXNode = {
|
2020-01-14 16:54:50 -08:00
|
|
|
role: WKRoleToARIARole.get(this._payload.role) || this._payload.role,
|
|
|
|
name: this._name(),
|
2020-01-03 11:15:43 -08:00
|
|
|
};
|
|
|
|
|
2020-01-14 16:54:50 -08:00
|
|
|
if ('description' in this._payload && this._payload.description !== node.name)
|
|
|
|
node.description = this._payload.description;
|
|
|
|
|
|
|
|
if ('roledescription' in this._payload) {
|
|
|
|
const roledescription = this._payload.roledescription;
|
|
|
|
if (roledescription !== this._payload.role && WKUnhelpfulRoleDescriptions.get(this._payload.role) !== roledescription)
|
|
|
|
node.roledescription = roledescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('value' in this._payload && this._payload.role !== 'text')
|
|
|
|
node.value = this._payload.value;
|
|
|
|
|
2020-06-25 18:01:18 -07:00
|
|
|
const userStringProperties: Array<keyof types.SerializedAXNode & keyof Protocol.Page.AXNode> = [
|
2020-01-03 11:15:43 -08:00
|
|
|
'keyshortcuts',
|
|
|
|
'valuetext'
|
|
|
|
];
|
|
|
|
for (const userStringProperty of userStringProperties) {
|
|
|
|
if (!(userStringProperty in this._payload))
|
|
|
|
continue;
|
2020-01-14 16:54:50 -08:00
|
|
|
(node as any)[userStringProperty] = this._payload[userStringProperty];
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
|
|
|
|
2020-06-25 18:01:18 -07:00
|
|
|
const booleanProperties: Array<keyof types.SerializedAXNode & keyof Protocol.Page.AXNode> = [
|
2020-01-03 11:15:43 -08:00
|
|
|
'disabled',
|
|
|
|
'expanded',
|
|
|
|
'focused',
|
|
|
|
'modal',
|
|
|
|
'multiline',
|
|
|
|
'multiselectable',
|
|
|
|
'readonly',
|
|
|
|
'required',
|
|
|
|
'selected',
|
|
|
|
];
|
|
|
|
for (const booleanProperty of booleanProperties) {
|
|
|
|
// WebArea and ScorllArea 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._payload.role === 'WebArea' || this._payload.role === 'ScrollArea'))
|
|
|
|
continue;
|
2020-01-14 16:54:50 -08:00
|
|
|
const value = this._payload[booleanProperty];
|
2020-01-03 11:15:43 -08:00
|
|
|
if (!value)
|
|
|
|
continue;
|
2020-01-13 22:08:35 -08:00
|
|
|
(node as any)[booleanProperty] = value;
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const tristateProperties: ('checked'|'pressed')[] = [
|
|
|
|
'checked',
|
|
|
|
'pressed',
|
|
|
|
];
|
|
|
|
for (const tristateProperty of tristateProperties) {
|
|
|
|
if (!(tristateProperty in this._payload))
|
|
|
|
continue;
|
|
|
|
const value = this._payload[tristateProperty];
|
|
|
|
node[tristateProperty] = value === 'mixed' ? 'mixed' : value === 'true' ? true : false;
|
|
|
|
}
|
2020-06-25 18:01:18 -07:00
|
|
|
const numericalProperties: Array<keyof types.SerializedAXNode & keyof Protocol.Page.AXNode> = [
|
2020-01-03 11:15:43 -08:00
|
|
|
'level',
|
|
|
|
'valuemax',
|
|
|
|
'valuemin',
|
|
|
|
];
|
|
|
|
for (const numericalProperty of numericalProperties) {
|
|
|
|
if (!(numericalProperty in this._payload))
|
|
|
|
continue;
|
2020-01-13 22:08:35 -08:00
|
|
|
(node as any)[numericalProperty] = (this._payload as any)[numericalProperty];
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
2020-06-25 18:01:18 -07:00
|
|
|
const tokenProperties: Array<keyof types.SerializedAXNode & keyof Protocol.Page.AXNode> = [
|
2020-01-03 11:15:43 -08:00
|
|
|
'autocomplete',
|
|
|
|
'haspopup',
|
|
|
|
'invalid',
|
|
|
|
];
|
|
|
|
for (const tokenProperty of tokenProperties) {
|
2020-01-13 22:08:35 -08:00
|
|
|
const value = (this._payload as any)[tokenProperty];
|
2020-01-03 11:15:43 -08:00
|
|
|
if (!value || value === 'false')
|
|
|
|
continue;
|
2020-01-13 22:08:35 -08:00
|
|
|
(node as any)[tokenProperty] = value;
|
2020-01-03 11:15:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const orientationIsApplicable = new Set([
|
|
|
|
'ScrollArea',
|
|
|
|
'scrollbar',
|
|
|
|
'listbox',
|
|
|
|
'combobox',
|
|
|
|
'menu',
|
|
|
|
'tree',
|
|
|
|
'separator',
|
|
|
|
'slider',
|
|
|
|
'tablist',
|
|
|
|
'toolbar',
|
|
|
|
]);
|
|
|
|
if (this._payload.orientation && orientationIsApplicable.has(this._payload.role))
|
|
|
|
node.orientation = this._payload.orientation;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|