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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { InternalQueryHandler } from './QueryHandler.js';
|
|
|
|
import { ElementHandle, JSHandle } from './JSHandle.js';
|
|
|
|
import { Protocol } from 'devtools-protocol';
|
|
|
|
import { CDPSession } from './Connection.js';
|
2020-11-03 10:39:31 +00:00
|
|
|
import { DOMWorld, PageBinding, WaitForSelectorOptions } from './DOMWorld.js';
|
2022-05-31 14:34:16 +00:00
|
|
|
import { assert } from './assert.js';
|
2020-10-05 06:25:55 +00:00
|
|
|
|
|
|
|
async function queryAXTree(
|
|
|
|
client: CDPSession,
|
|
|
|
element: ElementHandle,
|
|
|
|
accessibleName?: string,
|
|
|
|
role?: string
|
|
|
|
): Promise<Protocol.Accessibility.AXNode[]> {
|
|
|
|
const { nodes } = await client.send('Accessibility.queryAXTree', {
|
|
|
|
objectId: element._remoteObject.objectId,
|
|
|
|
accessibleName,
|
|
|
|
role,
|
|
|
|
});
|
|
|
|
const filteredNodes: Protocol.Accessibility.AXNode[] = nodes.filter(
|
2022-05-31 14:34:16 +00:00
|
|
|
(node: Protocol.Accessibility.AXNode) =>
|
|
|
|
!node.role || node.role.value !== 'StaticText'
|
2020-10-05 06:25:55 +00:00
|
|
|
);
|
|
|
|
return filteredNodes;
|
|
|
|
}
|
|
|
|
|
2021-09-10 19:28:30 +00:00
|
|
|
const normalizeValue = (value: string): string =>
|
|
|
|
value.replace(/ +/g, ' ').trim();
|
|
|
|
const knownAttributes = new Set(['name', 'role']);
|
|
|
|
const attributeRegexp =
|
2021-11-09 12:05:10 +00:00
|
|
|
/\[\s*(?<attribute>\w+)\s*=\s*(?<quote>"|')(?<value>\\.|.*?(?=\k<quote>))\k<quote>\s*\]/g;
|
2021-09-10 19:28:30 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
type ARIAQueryOption = { name?: string; role?: string };
|
|
|
|
function isKnownAttribute(
|
|
|
|
attribute: string
|
|
|
|
): attribute is keyof ARIAQueryOption {
|
|
|
|
return knownAttributes.has(attribute);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
* - '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'.
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
function parseAriaSelector(selector: string): ARIAQueryOption {
|
|
|
|
const queryOptions: ARIAQueryOption = {};
|
2020-10-05 06:25:55 +00:00
|
|
|
const defaultName = selector.replace(
|
|
|
|
attributeRegexp,
|
2022-05-31 14:34:16 +00:00
|
|
|
(_, attribute: string, _quote: string, value: string) => {
|
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 '';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (defaultName && !queryOptions.name)
|
2021-09-10 19:28:30 +00:00
|
|
|
queryOptions.name = normalizeValue(defaultName);
|
2020-10-05 06:25:55 +00:00
|
|
|
return queryOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryOne = async (
|
|
|
|
element: ElementHandle,
|
|
|
|
selector: string
|
|
|
|
): Promise<ElementHandle | null> => {
|
|
|
|
const exeCtx = element.executionContext();
|
|
|
|
const { name, role } = parseAriaSelector(selector);
|
|
|
|
const res = await queryAXTree(exeCtx._client, element, name, role);
|
2022-05-31 14:34:16 +00:00
|
|
|
if (!res[0] || !res[0].backendDOMNodeId) {
|
2020-10-05 06:25:55 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return exeCtx._adoptBackendNodeId(res[0].backendDOMNodeId);
|
|
|
|
};
|
|
|
|
|
2020-10-07 08:49:11 +00:00
|
|
|
const waitFor = async (
|
|
|
|
domWorld: DOMWorld,
|
|
|
|
selector: string,
|
|
|
|
options: WaitForSelectorOptions
|
2022-05-31 14:34:16 +00:00
|
|
|
): Promise<ElementHandle<Element> | null> => {
|
2020-11-03 10:39:31 +00:00
|
|
|
const binding: PageBinding = {
|
|
|
|
name: 'ariaQuerySelector',
|
|
|
|
pptrFunction: async (selector: string) => {
|
2021-12-21 08:53:20 +00:00
|
|
|
const root = options.root || (await domWorld._document());
|
|
|
|
const element = await queryOne(root, selector);
|
2020-11-03 10:39:31 +00:00
|
|
|
return element;
|
|
|
|
},
|
|
|
|
};
|
2020-10-07 08:49:11 +00:00
|
|
|
return domWorld.waitForSelectorInPage(
|
2022-05-31 14:34:16 +00:00
|
|
|
(_: Element, selector: string) =>
|
|
|
|
(
|
|
|
|
globalThis as unknown as { ariaQuerySelector(selector: string): void }
|
|
|
|
).ariaQuerySelector(selector),
|
2020-10-07 08:49:11 +00:00
|
|
|
selector,
|
2020-11-03 10:39:31 +00:00
|
|
|
options,
|
|
|
|
binding
|
2020-10-07 08:49:11 +00:00
|
|
|
);
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const queryAll = async (
|
|
|
|
element: ElementHandle,
|
|
|
|
selector: string
|
|
|
|
): Promise<ElementHandle[]> => {
|
|
|
|
const exeCtx = element.executionContext();
|
|
|
|
const { name, role } = parseAriaSelector(selector);
|
|
|
|
const res = await queryAXTree(exeCtx._client, element, name, role);
|
|
|
|
return Promise.all(
|
|
|
|
res.map((axNode) => exeCtx._adoptBackendNodeId(axNode.backendDOMNodeId))
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const queryAllArray = async (
|
|
|
|
element: ElementHandle,
|
|
|
|
selector: string
|
|
|
|
): Promise<JSHandle> => {
|
|
|
|
const elementHandles = await queryAll(element, selector);
|
|
|
|
const exeCtx = element.executionContext();
|
|
|
|
const jsHandle = exeCtx.evaluateHandle(
|
|
|
|
(...elements) => elements,
|
|
|
|
...elementHandles
|
|
|
|
);
|
|
|
|
return jsHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export const ariaHandler: InternalQueryHandler = {
|
|
|
|
queryOne,
|
|
|
|
waitFor,
|
|
|
|
queryAll,
|
|
|
|
queryAllArray,
|
|
|
|
};
|