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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
|
|
|
import {ElementHandle} from './JSHandle.js';
|
|
|
|
import {Protocol} from 'devtools-protocol';
|
2020-04-23 14:35:03 +00:00
|
|
|
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Represents a Node and the properties of it that are relevant to Accessibility.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface SerializedAXNode {
|
|
|
|
/**
|
|
|
|
* The {@link https://www.w3.org/TR/wai-aria/#usage_intro | role} of the node.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
role: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* A human readable name for the node.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
name?: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* The current value of the node.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
value?: string | number;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* An additional human readable description of the node.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
description?: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Any keyboard shortcuts associated with this node.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
keyshortcuts?: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* A human readable alternative to the role.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
roledescription?: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* A description of the current value.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
valuetext?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
expanded?: boolean;
|
|
|
|
focused?: boolean;
|
|
|
|
modal?: boolean;
|
|
|
|
multiline?: boolean;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Whether more than one child can be selected.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
multiselectable?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
required?: boolean;
|
|
|
|
selected?: boolean;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
2020-06-19 14:39:03 +00:00
|
|
|
* Whether the checkbox is checked, or in a
|
|
|
|
* {@link https://www.w3.org/TR/wai-aria-practices/examples/checkbox/checkbox-2/checkbox-2.html | mixed state}.
|
2020-06-05 14:20:11 +00:00
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
checked?: boolean | 'mixed';
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Whether the node is checked or in a mixed state.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
pressed?: boolean | 'mixed';
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* The level of a heading.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
level?: number;
|
|
|
|
valuemin?: number;
|
|
|
|
valuemax?: number;
|
|
|
|
autocomplete?: string;
|
|
|
|
haspopup?: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Whether and in what way this node's value is invalid.
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
invalid?: string;
|
|
|
|
orientation?: string;
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Children of this node, if there are any.
|
|
|
|
*/
|
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-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface SnapshotOptions {
|
|
|
|
/**
|
2020-08-14 12:18:46 +00:00
|
|
|
* Prune uninteresting nodes from the tree.
|
2020-06-05 14:20:11 +00:00
|
|
|
* @defaultValue true
|
|
|
|
*/
|
|
|
|
interestingOnly?: boolean;
|
|
|
|
/**
|
2020-08-14 12:18:46 +00:00
|
|
|
* Root node to get the accessibility tree for
|
2020-06-05 14:20:11 +00:00
|
|
|
* @defaultValue The root node of the entire page.
|
|
|
|
*/
|
|
|
|
root?: ElementHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Accessibility class provides methods for inspecting Chromium's
|
|
|
|
* accessibility tree. The accessibility tree is used by assistive technology
|
|
|
|
* such as {@link https://en.wikipedia.org/wiki/Screen_reader | screen readers} or
|
|
|
|
* {@link https://en.wikipedia.org/wiki/Switch_access | switches}.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
*
|
|
|
|
* Accessibility is a very platform-specific thing. On different platforms,
|
|
|
|
* there are different screen readers that might have wildly different output.
|
|
|
|
*
|
|
|
|
* Blink - Chrome's rendering engine - has a concept of "accessibility tree",
|
|
|
|
* which is then translated into different platform-specific APIs. Accessibility
|
|
|
|
* namespace gives users access to the Blink Accessibility Tree.
|
|
|
|
*
|
|
|
|
* Most of the accessibility tree gets filtered out when converting from Blink
|
|
|
|
* AX Tree to Platform-specific AX-Tree or by assistive technologies themselves.
|
|
|
|
* By default, Puppeteer tries to approximate this filtering, exposing only
|
|
|
|
* the "interesting" nodes of the tree.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
export class Accessibility {
|
2022-06-13 09:16:25 +00:00
|
|
|
#client: CDPSession;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-23 14:35:03 +00:00
|
|
|
constructor(client: CDPSession) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client = client;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 14:20:11 +00:00
|
|
|
/**
|
|
|
|
* Captures the current state of the accessibility tree.
|
|
|
|
* The returned object represents the root accessible node of the page.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
*
|
2020-06-19 14:39:03 +00:00
|
|
|
* **NOTE** The Chromium accessibility tree contains nodes that go unused on
|
|
|
|
* most platforms and by most screen readers. Puppeteer will discard them as
|
|
|
|
* well for an easier to process tree, unless `interestingOnly` is set to
|
|
|
|
* `false`.
|
2020-06-05 14:20:11 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* An example of dumping the entire accessibility tree:
|
|
|
|
* ```js
|
|
|
|
* const snapshot = await page.accessibility.snapshot();
|
|
|
|
* console.log(snapshot);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* An example of logging the focused node's name:
|
|
|
|
* ```js
|
|
|
|
* const snapshot = await page.accessibility.snapshot();
|
|
|
|
* const node = findFocusedNode(snapshot);
|
|
|
|
* console.log(node && node.name);
|
|
|
|
*
|
|
|
|
* function findFocusedNode(node) {
|
|
|
|
* if (node.focused)
|
|
|
|
* return node;
|
|
|
|
* for (const child of node.children || []) {
|
|
|
|
* const foundNode = findFocusedNode(child);
|
|
|
|
* return foundNode;
|
|
|
|
* }
|
|
|
|
* return null;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2020-06-19 13:30:28 +00:00
|
|
|
* @returns An AXNode object representing the snapshot.
|
2020-06-05 14:20:11 +00:00
|
|
|
*
|
|
|
|
*/
|
2020-05-07 16:26:05 +00:00
|
|
|
public async snapshot(
|
2020-06-05 14:20:11 +00:00
|
|
|
options: SnapshotOptions = {}
|
2022-05-31 14:34:16 +00:00
|
|
|
): Promise<SerializedAXNode | null> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {interestingOnly = true, root = null} = options;
|
|
|
|
const {nodes} = await this.#client.send('Accessibility.getFullAXTree');
|
2022-05-31 14:34:16 +00:00
|
|
|
let backendNodeId: number | undefined;
|
2019-05-10 06:39:42 +00:00
|
|
|
if (root) {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {node} = await this.#client.send('DOM.describeNode', {
|
2020-05-07 10:54:55 +00:00
|
|
|
objectId: root._remoteObject.objectId,
|
|
|
|
});
|
2019-05-10 06:39:42 +00:00
|
|
|
backendNodeId = node.backendNodeId;
|
|
|
|
}
|
|
|
|
const defaultRoot = AXNode.createTree(nodes);
|
2022-05-31 14:34:16 +00:00
|
|
|
let needle: AXNode | null = defaultRoot;
|
2019-05-10 06:39:42 +00:00
|
|
|
if (backendNodeId) {
|
2022-06-22 13:25:44 +00:00
|
|
|
needle = defaultRoot.find(node => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return node.payload.backendDOMNodeId === backendNodeId;
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!needle) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!interestingOnly) {
|
|
|
|
return this.serializeTree(needle)[0] ?? null;
|
2019-05-10 06:39:42 +00:00
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
const interestingNodes = new Set<AXNode>();
|
2020-05-07 16:26:05 +00:00
|
|
|
this.collectInterestingNodes(interestingNodes, defaultRoot, false);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!interestingNodes.has(needle)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-31 14:34:16 +00:00
|
|
|
return this.serializeTree(needle, interestingNodes)[0] ?? null;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
private serializeTree(
|
|
|
|
node: AXNode,
|
2020-08-05 15:08:19 +00:00
|
|
|
interestingNodes?: Set<AXNode>
|
2020-05-07 16:26:05 +00:00
|
|
|
): SerializedAXNode[] {
|
|
|
|
const children: SerializedAXNode[] = [];
|
2022-06-14 11:55:35 +00:00
|
|
|
for (const child of node.children) {
|
2020-08-05 15:08:19 +00:00
|
|
|
children.push(...this.serializeTree(child, interestingNodes));
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (interestingNodes && !interestingNodes.has(node)) {
|
|
|
|
return children;
|
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
const serializedNode = node.serialize();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (children.length) {
|
|
|
|
serializedNode.children = children;
|
|
|
|
}
|
2020-05-07 16:26:05 +00:00
|
|
|
return [serializedNode];
|
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
private collectInterestingNodes(
|
|
|
|
collection: Set<AXNode>,
|
|
|
|
node: AXNode,
|
|
|
|
insideControl: boolean
|
|
|
|
): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (node.isInteresting(insideControl)) {
|
|
|
|
collection.add(node);
|
|
|
|
}
|
|
|
|
if (node.isLeafNode()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-07 16:26:05 +00:00
|
|
|
insideControl = insideControl || node.isControl();
|
2022-06-14 11:55:35 +00:00
|
|
|
for (const child of node.children) {
|
2020-05-07 16:26:05 +00:00
|
|
|
this.collectInterestingNodes(collection, child, insideControl);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class AXNode {
|
2020-05-07 16:26:05 +00:00
|
|
|
public payload: Protocol.Accessibility.AXNode;
|
|
|
|
public children: AXNode[] = [];
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#richlyEditable = false;
|
|
|
|
#editable = false;
|
|
|
|
#focusable = false;
|
|
|
|
#hidden = false;
|
|
|
|
#name: string;
|
|
|
|
#role: string;
|
|
|
|
#ignored: boolean;
|
|
|
|
#cachedHasFocusableChild?: boolean;
|
2020-04-23 14:35:03 +00:00
|
|
|
|
|
|
|
constructor(payload: Protocol.Accessibility.AXNode) {
|
2020-05-07 16:26:05 +00:00
|
|
|
this.payload = payload;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#name = this.payload.name ? this.payload.name.value : '';
|
|
|
|
this.#role = this.payload.role ? this.payload.role.value : 'Unknown';
|
|
|
|
this.#ignored = this.payload.ignored;
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
for (const property of this.payload.properties || []) {
|
2018-11-02 01:54:51 +00:00
|
|
|
if (property.name === 'editable') {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#richlyEditable = property.value.value === 'richtext';
|
|
|
|
this.#editable = true;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
2022-06-14 11:55:35 +00:00
|
|
|
if (property.name === 'focusable') {
|
|
|
|
this.#focusable = property.value.value;
|
|
|
|
}
|
|
|
|
if (property.name === 'hidden') {
|
|
|
|
this.#hidden = property.value.value;
|
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#isPlainTextField(): boolean {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#richlyEditable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.#editable) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#role === 'textbox' || this.#role === 'searchbox';
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#isTextOnlyObject(): boolean {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#hasFocusableChild(): boolean {
|
|
|
|
if (this.#cachedHasFocusableChild === undefined) {
|
|
|
|
this.#cachedHasFocusableChild = false;
|
2020-05-07 16:26:05 +00:00
|
|
|
for (const child of this.children) {
|
2022-06-13 09:16:25 +00:00
|
|
|
if (child.#focusable || child.#hasFocusableChild()) {
|
|
|
|
this.#cachedHasFocusableChild = true;
|
2018-11-02 01:54:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#cachedHasFocusableChild;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
public find(predicate: (x: AXNode) => boolean): AXNode | null {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (predicate(this)) {
|
|
|
|
return this;
|
|
|
|
}
|
2020-05-07 16:26:05 +00:00
|
|
|
for (const child of this.children) {
|
2019-05-10 06:39:42 +00:00
|
|
|
const result = child.find(predicate);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-10 06:39:42 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
public isLeafNode(): boolean {
|
2022-06-14 11:55:35 +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.
|
2022-06-14 11:55:35 +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.)
|
2022-06-13 09:16:25 +00:00
|
|
|
switch (this.#role) {
|
2018-11-02 01:54:51 +00:00
|
|
|
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
|
2022-06-14 11:55:35 +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-05-07 16:26:05 +00:00
|
|
|
public isControl(): boolean {
|
2022-06-13 09:16:25 +00:00
|
|
|
switch (this.#role) {
|
2018-11-02 01:54:51 +00:00
|
|
|
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':
|
2020-08-14 12:18:46 +00:00
|
|
|
case 'treeitem':
|
2018-11-02 01:54:51 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
public isInteresting(insideControl: boolean): boolean {
|
2022-06-13 09:16:25 +00:00
|
|
|
const role = this.#role;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (role === 'Ignored' || this.#hidden || this.#ignored) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2022-06-14 11:55:35 +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.
|
2022-06-14 11:55:35 +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
|
2022-06-14 11:55:35 +00:00
|
|
|
if (insideControl) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.isLeafNode() && !!this.#name;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
public serialize(): SerializedAXNode {
|
2020-05-07 10:54:55 +00:00
|
|
|
const properties = new Map<string, number | string | boolean>();
|
2022-06-14 11:55:35 +00:00
|
|
|
for (const property of this.payload.properties || []) {
|
2018-11-02 01:54:51 +00:00
|
|
|
properties.set(property.name.toLowerCase(), property.value.value);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
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) {
|
2020-05-07 16:26:05 +00:00
|
|
|
properties.set('description', this.payload.description.value);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
|
2020-04-23 14:35:03 +00:00
|
|
|
const node: SerializedAXNode = {
|
2022-06-13 09:16:25 +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',
|
|
|
|
];
|
2022-06-15 10:42:21 +00:00
|
|
|
const getUserStringPropertyValue = (key: UserStringProperty): string => {
|
|
|
|
return 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) {
|
2022-06-14 11:55:35 +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',
|
|
|
|
];
|
2022-06-15 10:42:21 +00:00
|
|
|
const getBooleanPropertyValue = (key: BooleanProperty): boolean => {
|
|
|
|
return 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) {
|
2021-05-26 16:44:29 +00:00
|
|
|
// RootWebArea's treat focus differently than other nodes. They report whether
|
2020-06-19 14:39:03 +00:00
|
|
|
// their frame has focus, not whether focus is specifically on the root
|
|
|
|
// node.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (booleanProperty === 'focused' && this.#role === 'RootWebArea') {
|
2021-05-26 16:44:29 +00:00
|
|
|
continue;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-03-31 08:48:09 +00:00
|
|
|
const value = getBooleanPropertyValue(booleanProperty);
|
2022-06-14 11:55:35 +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) {
|
2022-06-14 11:55:35 +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',
|
|
|
|
];
|
2022-06-15 10:42:21 +00:00
|
|
|
const getNumericalPropertyValue = (key: NumbericalProperty): number => {
|
|
|
|
return properties.get(key) as number;
|
|
|
|
};
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const numericalProperty of numericalProperties) {
|
2022-06-14 11:55:35 +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',
|
|
|
|
];
|
2022-06-15 10:42:21 +00:00
|
|
|
const getTokenPropertyValue = (key: TokenProperty): string => {
|
|
|
|
return 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);
|
2022-06-14 11:55:35 +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-05-07 16:26:05 +00:00
|
|
|
public static createTree(payloads: Protocol.Accessibility.AXNode[]): AXNode {
|
|
|
|
const nodeById = new Map<string, AXNode>();
|
2022-06-14 11:55:35 +00:00
|
|
|
for (const payload of payloads) {
|
2018-11-02 01:54:51 +00:00
|
|
|
nodeById.set(payload.nodeId, new AXNode(payload));
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
for (const node of nodeById.values()) {
|
2022-06-14 11:55:35 +00:00
|
|
|
for (const childId of node.payload.childIds || []) {
|
2022-05-31 14:34:16 +00:00
|
|
|
node.children.push(nodeById.get(childId)!);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
return nodeById.values().next().value;
|
|
|
|
}
|
|
|
|
}
|