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-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-08-25 19:29:56 +00:00
|
|
|
import {ElementHandle} from './ElementHandle.js';
|
2022-08-26 10:55:30 +00:00
|
|
|
import {Frame} from './Frame.js';
|
2022-09-15 06:22:20 +00:00
|
|
|
import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorld.js';
|
2022-09-15 06:48:59 +00:00
|
|
|
import {PuppeteerQueryHandler} from './QueryHandler.js';
|
2020-10-05 06:25:55 +00:00
|
|
|
|
|
|
|
async function queryAXTree(
|
|
|
|
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
|
|
|
|
): 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,
|
|
|
|
});
|
|
|
|
const filteredNodes: Protocol.Accessibility.AXNode[] = nodes.filter(
|
2022-06-15 10:42:21 +00:00
|
|
|
(node: Protocol.Accessibility.AXNode) => {
|
|
|
|
return !node.role || node.role.value !== 'StaticText';
|
|
|
|
}
|
2020-10-05 06:25:55 +00:00
|
|
|
);
|
|
|
|
return filteredNodes;
|
|
|
|
}
|
|
|
|
|
2022-06-15 10:42:21 +00:00
|
|
|
const normalizeValue = (value: string): string => {
|
|
|
|
return value.replace(/ +/g, ' ').trim();
|
|
|
|
};
|
2021-09-10 19:28:30 +00:00
|
|
|
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-06-22 13:25:44 +00:00
|
|
|
type ARIAQueryOption = {name?: string; role?: string};
|
2022-05-31 14:34:16 +00:00
|
|
|
function isKnownAttribute(
|
|
|
|
attribute: string
|
|
|
|
): attribute is keyof ARIAQueryOption {
|
|
|
|
return knownAttributes.has(attribute);
|
|
|
|
}
|
|
|
|
|
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'.
|
|
|
|
*/
|
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 '';
|
|
|
|
}
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-26 10:55:30 +00:00
|
|
|
const queryOneId = async (element: ElementHandle<Node>, selector: string) => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {name, role} = parseAriaSelector(selector);
|
2022-08-26 10:55:30 +00:00
|
|
|
const res = await queryAXTree(element.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;
|
|
|
|
}
|
2022-08-26 10:55:30 +00:00
|
|
|
return res[0].backendDOMNodeId;
|
|
|
|
};
|
|
|
|
|
2022-09-15 06:48:59 +00:00
|
|
|
const queryOne: PuppeteerQueryHandler['queryOne'] = async (
|
2022-08-26 10:55:30 +00:00
|
|
|
element,
|
|
|
|
selector
|
|
|
|
) => {
|
|
|
|
const id = await queryOneId(element, selector);
|
|
|
|
if (!id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (await element.frame.worlds[MAIN_WORLD].adoptBackendNode(
|
|
|
|
id
|
2022-08-09 12:55:18 +00:00
|
|
|
)) as ElementHandle<Node>;
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
2022-09-15 06:48:59 +00:00
|
|
|
const waitFor: PuppeteerQueryHandler['waitFor'] = async (
|
2022-08-26 10:55:30 +00:00
|
|
|
elementOrFrame,
|
|
|
|
selector,
|
|
|
|
options
|
|
|
|
) => {
|
|
|
|
let frame: Frame;
|
|
|
|
let element: ElementHandle<Node> | undefined;
|
|
|
|
if (elementOrFrame instanceof Frame) {
|
|
|
|
frame = elementOrFrame;
|
|
|
|
} else {
|
|
|
|
frame = elementOrFrame.frame;
|
|
|
|
element = await frame.worlds[PUPPETEER_WORLD].adoptHandle(elementOrFrame);
|
|
|
|
}
|
2022-09-15 06:22:20 +00:00
|
|
|
|
|
|
|
const ariaQuerySelector = async (selector: string) => {
|
|
|
|
const id = await queryOneId(
|
|
|
|
element || (await frame.worlds[PUPPETEER_WORLD].document()),
|
|
|
|
selector
|
|
|
|
);
|
|
|
|
if (!id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (await frame.worlds[PUPPETEER_WORLD].adoptBackendNode(
|
|
|
|
id
|
|
|
|
)) as ElementHandle<Node>;
|
2020-11-03 10:39:31 +00:00
|
|
|
};
|
2022-09-15 06:22:20 +00:00
|
|
|
|
2022-08-26 10:55:30 +00:00
|
|
|
const result = await frame.worlds[PUPPETEER_WORLD]._waitForSelectorInPage(
|
2022-06-15 10:42:21 +00:00
|
|
|
(_: Element, selector: string) => {
|
|
|
|
return (
|
2022-08-04 13:45:21 +00:00
|
|
|
globalThis as unknown as {
|
2022-08-26 10:55:30 +00:00
|
|
|
ariaQuerySelector(selector: string): Node | null;
|
2022-06-15 10:09:22 +00:00
|
|
|
}
|
2022-06-15 10:42:21 +00:00
|
|
|
).ariaQuerySelector(selector);
|
|
|
|
},
|
2022-08-26 10:55:30 +00:00
|
|
|
element,
|
2020-10-07 08:49:11 +00:00
|
|
|
selector,
|
2020-11-03 10:39:31 +00:00
|
|
|
options,
|
2022-09-21 08:16:54 +00:00
|
|
|
new Map([['ariaQuerySelector', ariaQuerySelector]])
|
2022-08-26 10:55:30 +00:00
|
|
|
);
|
|
|
|
if (element) {
|
|
|
|
await element.dispose();
|
|
|
|
}
|
|
|
|
if (!(result instanceof ElementHandle)) {
|
2022-09-15 06:22:20 +00:00
|
|
|
await result?.dispose();
|
2022-08-26 10:55:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return result.frame.worlds[MAIN_WORLD].transferHandle(result);
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
2022-09-15 06:48:59 +00:00
|
|
|
const queryAll: PuppeteerQueryHandler['queryAll'] = async (
|
2022-08-26 10:55:30 +00:00
|
|
|
element,
|
|
|
|
selector
|
|
|
|
) => {
|
2020-10-05 06:25:55 +00:00
|
|
|
const exeCtx = element.executionContext();
|
2022-06-22 13:25:44 +00:00
|
|
|
const {name, role} = parseAriaSelector(selector);
|
2020-10-05 06:25:55 +00:00
|
|
|
const res = await queryAXTree(exeCtx._client, element, name, role);
|
2022-08-09 12:55:18 +00:00
|
|
|
const world = exeCtx._world!;
|
2022-08-09 13:17:42 +00:00
|
|
|
return Promise.all(
|
2022-06-22 13:25:44 +00:00
|
|
|
res.map(axNode => {
|
2022-08-09 13:17:42 +00:00
|
|
|
return world.adoptBackendNode(axNode.backendDOMNodeId) as Promise<
|
|
|
|
ElementHandle<Node>
|
|
|
|
>;
|
2022-06-15 10:42:21 +00:00
|
|
|
})
|
2022-08-09 13:17:42 +00:00
|
|
|
);
|
2020-10-05 06:25:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-09-15 06:48:59 +00:00
|
|
|
export const ariaHandler: PuppeteerQueryHandler = {
|
2020-10-05 06:25:55 +00:00
|
|
|
queryOne,
|
|
|
|
waitFor,
|
|
|
|
queryAll,
|
|
|
|
};
|