2020-10-05 06:25:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2020 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 {Protocol} from 'devtools-protocol';
|
2022-11-10 16:11:18 +00:00
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
import {ElementHandle} from '../api/ElementHandle.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
2022-11-10 16:11:18 +00:00
|
|
|
import type {Frame} from './Frame.js';
|
2023-02-14 21:31:30 +00:00
|
|
|
import type {WaitForSelectorOptions} from './IsolatedWorld.js';
|
|
|
|
import {IterableUtil} from './IterableUtil.js';
|
|
|
|
import {QueryHandler, QuerySelector} from './QueryHandler.js';
|
|
|
|
import {AwaitableIterable} from './types.js';
|
2020-10-05 06:25:55 +00:00
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
const queryAXTree = async (
|
2020-10-05 06:25:55 +00:00
|
|
|
client: CDPSession,
|
2022-07-06 07:05:37 +00:00
|
|
|
element: ElementHandle<Node>,
|
2020-10-05 06:25:55 +00:00
|
|
|
accessibleName?: string,
|
|
|
|
role?: string
|
2023-02-14 21:31:30 +00:00
|
|
|
): Promise<Protocol.Accessibility.AXNode[]> => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {nodes} = await client.send('Accessibility.queryAXTree', {
|
2022-08-11 09:45:35 +00:00
|
|
|
objectId: element.remoteObject().objectId,
|
2020-10-05 06:25:55 +00:00
|
|
|
accessibleName,
|
|
|
|
role,
|
|
|
|
});
|
2023-02-14 21:31:30 +00:00
|
|
|
return nodes.filter((node: Protocol.Accessibility.AXNode) => {
|
|
|
|
return !node.role || node.role.value !== 'StaticText';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
type ARIASelector = {name?: string; role?: string};
|
|
|
|
|
|
|
|
const KNOWN_ATTRIBUTES = Object.freeze(['name', 'role']);
|
|
|
|
const isKnownAttribute = (
|
|
|
|
attribute: string
|
|
|
|
): attribute is keyof ARIASelector => {
|
|
|
|
return KNOWN_ATTRIBUTES.includes(attribute);
|
|
|
|
};
|
2020-10-05 06:25:55 +00:00
|
|
|
|
2022-06-15 10:42:21 +00:00
|
|
|
const normalizeValue = (value: string): string => {
|
|
|
|
return value.replace(/ +/g, ' ').trim();
|
|
|
|
};
|
2022-05-31 14:34:16 +00:00
|
|
|
|
2022-08-12 12:15:26 +00:00
|
|
|
/**
|
2020-10-05 06:25:55 +00:00
|
|
|
* The selectors consist of an accessible name to query for and optionally
|
|
|
|
* further aria attributes on the form `[<attribute>=<value>]`.
|
|
|
|
* Currently, we only support the `name` and `role` attribute.
|
|
|
|
* The following examples showcase how the syntax works wrt. querying:
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2020-10-05 06:25:55 +00:00
|
|
|
* - 'title[role="heading"]' queries for elements with name 'title' and role 'heading'.
|
|
|
|
* - '[role="img"]' queries for elements with role 'img' and any name.
|
|
|
|
* - 'label' queries for elements with name 'label' and any role.
|
|
|
|
* - '[name=""][role="button"]' queries for elements with no name and role 'button'.
|
|
|
|
*/
|
2023-02-14 21:31:30 +00:00
|
|
|
const ATTRIBUTE_REGEXP =
|
|
|
|
/\[\s*(?<attribute>\w+)\s*=\s*(?<quote>"|')(?<value>\\.|.*?(?=\k<quote>))\k<quote>\s*\]/g;
|
|
|
|
const parseARIASelector = (selector: string): ARIASelector => {
|
|
|
|
const queryOptions: ARIASelector = {};
|
2020-10-05 06:25:55 +00:00
|
|
|
const defaultName = selector.replace(
|
2023-02-14 21:31:30 +00:00
|
|
|
ATTRIBUTE_REGEXP,
|
|
|
|
(_, attribute, __, value) => {
|
2020-10-05 06:25:55 +00:00
|
|
|
attribute = attribute.trim();
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(
|
|
|
|
isKnownAttribute(attribute),
|
|
|
|
`Unknown aria attribute "${attribute}" in selector`
|
|
|
|
);
|
2021-09-10 19:28:30 +00:00
|
|
|
queryOptions[attribute] = normalizeValue(value);
|
2020-10-05 06:25:55 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (defaultName && !queryOptions.name) {
|
2021-09-10 19:28:30 +00:00
|
|
|
queryOptions.name = normalizeValue(defaultName);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-10-05 06:25:55 +00:00
|
|
|
return queryOptions;
|
|
|
|
};
|
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export interface ARIAQuerySelectorContext {
|
|
|
|
__ariaQuerySelector(node: Node, selector: string): Promise<Node | null>;
|
|
|
|
}
|
2022-09-15 06:22:20 +00:00
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class ARIAQueryHandler extends QueryHandler {
|
|
|
|
static override querySelector: QuerySelector = async (node, selector) => {
|
|
|
|
const context = globalThis as unknown as ARIAQuerySelectorContext;
|
|
|
|
return context.__ariaQuerySelector(node, selector);
|
2020-11-03 10:39:31 +00:00
|
|
|
};
|
2022-09-15 06:22:20 +00:00
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
static override async *queryAll(
|
|
|
|
element: ElementHandle<Node>,
|
|
|
|
selector: string
|
|
|
|
): AwaitableIterable<ElementHandle<Node>> {
|
|
|
|
const context = element.executionContext();
|
|
|
|
const {name, role} = parseARIASelector(selector);
|
|
|
|
const results = await queryAXTree(context._client, element, name, role);
|
|
|
|
const world = context._world!;
|
|
|
|
yield* IterableUtil.map(results, node => {
|
|
|
|
return world.adoptBackendNode(node.backendDOMNodeId) as Promise<
|
2022-08-09 13:17:42 +00:00
|
|
|
ElementHandle<Node>
|
|
|
|
>;
|
2023-02-14 21:31:30 +00:00
|
|
|
});
|
|
|
|
}
|
2020-10-05 06:25:55 +00:00
|
|
|
|
2023-02-14 21:31:30 +00:00
|
|
|
static override queryOne = async (
|
|
|
|
element: ElementHandle<Node>,
|
|
|
|
selector: string
|
|
|
|
): Promise<ElementHandle<Node> | null> => {
|
|
|
|
return (await IterableUtil.first(this.queryAll(element, selector))) ?? null;
|
|
|
|
};
|
|
|
|
|
|
|
|
static override async waitFor(
|
|
|
|
elementOrFrame: ElementHandle<Node> | Frame,
|
|
|
|
selector: string,
|
|
|
|
options: WaitForSelectorOptions
|
|
|
|
): Promise<ElementHandle<Node> | null> {
|
|
|
|
return super.waitFor(
|
|
|
|
elementOrFrame,
|
|
|
|
selector,
|
|
|
|
options,
|
|
|
|
new Map([['__ariaQuerySelector', this.queryOne]])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|