2020-04-30 11:45:52 +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.
|
|
|
|
*/
|
|
|
|
|
2020-09-23 14:02:22 +00:00
|
|
|
import { WaitForSelectorOptions, DOMWorld } from './DOMWorld.js';
|
|
|
|
import { ElementHandle, JSHandle } from './JSHandle.js';
|
2020-10-05 06:25:55 +00:00
|
|
|
import { ariaHandler } from './AriaQueryHandler.js';
|
2020-09-23 14:02:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-10-05 06:25:55 +00:00
|
|
|
export interface InternalQueryHandler {
|
2020-09-23 14:02:22 +00:00
|
|
|
queryOne?: (
|
|
|
|
element: ElementHandle,
|
|
|
|
selector: string
|
|
|
|
) => Promise<ElementHandle | null>;
|
|
|
|
waitFor?: (
|
|
|
|
domWorld: DOMWorld,
|
|
|
|
selector: string,
|
|
|
|
options: WaitForSelectorOptions
|
|
|
|
) => Promise<ElementHandle | null>;
|
|
|
|
queryAll?: (
|
|
|
|
element: ElementHandle,
|
|
|
|
selector: string
|
|
|
|
) => Promise<ElementHandle[]>;
|
|
|
|
queryAllArray?: (
|
|
|
|
element: ElementHandle,
|
|
|
|
selector: string
|
|
|
|
) => Promise<JSHandle>;
|
|
|
|
}
|
|
|
|
|
2020-10-07 08:43:46 +00:00
|
|
|
/**
|
|
|
|
* Contains two functions `queryOne` and `queryAll` that can
|
|
|
|
* be {@link Puppeteer.__experimental_registerCustomQueryHandler | registered}
|
|
|
|
* as alternative querying strategies. The functions `queryOne` and `queryAll`
|
|
|
|
* are executed in the page context. `queryOne` should take an `Element` and a
|
|
|
|
* selector string as argument and return a single `Element` or `null` if no
|
|
|
|
* element is found. `queryAll` takes the same arguments but should instead
|
|
|
|
* return a `NodeListOf<Element>` or `Array<Element>` with all the elements
|
|
|
|
* that match the given query selector.
|
|
|
|
* @public
|
|
|
|
*/
|
2020-09-23 14:02:22 +00:00
|
|
|
export interface CustomQueryHandler {
|
2020-07-17 05:29:42 +00:00
|
|
|
queryOne?: (element: Element | Document, selector: string) => Element | null;
|
|
|
|
queryAll?: (
|
|
|
|
element: Element | Document,
|
|
|
|
selector: string
|
|
|
|
) => Element[] | NodeListOf<Element>;
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 14:02:22 +00:00
|
|
|
function makeQueryHandler(handler: CustomQueryHandler): InternalQueryHandler {
|
|
|
|
const internalHandler: InternalQueryHandler = {};
|
|
|
|
|
|
|
|
if (handler.queryOne) {
|
|
|
|
internalHandler.queryOne = async (element, selector) => {
|
|
|
|
const jsHandle = await element.evaluateHandle(handler.queryOne, selector);
|
|
|
|
const elementHandle = jsHandle.asElement();
|
|
|
|
if (elementHandle) return elementHandle;
|
|
|
|
await jsHandle.dispose();
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
internalHandler.waitFor = (
|
|
|
|
domWorld: DOMWorld,
|
|
|
|
selector: string,
|
|
|
|
options: WaitForSelectorOptions
|
|
|
|
) => domWorld.waitForSelectorInPage(handler.queryOne, selector, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler.queryAll) {
|
|
|
|
internalHandler.queryAll = async (element, selector) => {
|
|
|
|
const jsHandle = await element.evaluateHandle(handler.queryAll, selector);
|
|
|
|
const properties = await jsHandle.getProperties();
|
|
|
|
await jsHandle.dispose();
|
|
|
|
const result = [];
|
|
|
|
for (const property of properties.values()) {
|
|
|
|
const elementHandle = property.asElement();
|
|
|
|
if (elementHandle) result.push(elementHandle);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
internalHandler.queryAllArray = async (element, selector) => {
|
|
|
|
const resultHandle = await element.evaluateHandle(
|
|
|
|
handler.queryAll,
|
|
|
|
selector
|
|
|
|
);
|
|
|
|
const arrayHandle = await resultHandle.evaluateHandle(
|
|
|
|
(res: Element[] | NodeListOf<Element>) => Array.from(res)
|
|
|
|
);
|
|
|
|
return arrayHandle;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return internalHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
const _defaultHandler = makeQueryHandler({
|
|
|
|
queryOne: (element: Element, selector: string) =>
|
|
|
|
element.querySelector(selector),
|
|
|
|
queryAll: (element: Element, selector: string) =>
|
|
|
|
element.querySelectorAll(selector),
|
|
|
|
});
|
2020-04-30 11:45:52 +00:00
|
|
|
|
2020-10-07 08:15:54 +00:00
|
|
|
const _builtInHandlers = new Map([['aria', ariaHandler]]);
|
|
|
|
const _queryHandlers = new Map(_builtInHandlers);
|
2020-10-05 06:25:55 +00:00
|
|
|
|
2020-10-07 08:43:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
export function registerCustomQueryHandler(
|
|
|
|
name: string,
|
2020-09-23 14:02:22 +00:00
|
|
|
handler: CustomQueryHandler
|
2020-05-07 10:54:55 +00:00
|
|
|
): void {
|
2020-09-23 14:02:22 +00:00
|
|
|
if (_queryHandlers.get(name))
|
2020-04-30 11:45:52 +00:00
|
|
|
throw new Error(`A custom query handler named "${name}" already exists`);
|
|
|
|
|
|
|
|
const isValidName = /^[a-zA-Z]+$/.test(name);
|
|
|
|
if (!isValidName)
|
|
|
|
throw new Error(`Custom query handler names may only contain [a-zA-Z]`);
|
|
|
|
|
2020-09-23 14:02:22 +00:00
|
|
|
const internalHandler = makeQueryHandler(handler);
|
|
|
|
|
|
|
|
_queryHandlers.set(name, internalHandler);
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-07 08:43:46 +00:00
|
|
|
* @internal
|
2020-04-30 11:45:52 +00:00
|
|
|
*/
|
|
|
|
export function unregisterCustomQueryHandler(name: string): void {
|
2020-10-07 08:15:54 +00:00
|
|
|
if (_queryHandlers.has(name) && !_builtInHandlers.has(name)) {
|
2020-09-23 14:02:22 +00:00
|
|
|
_queryHandlers.delete(name);
|
|
|
|
}
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 08:43:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-09-23 14:02:22 +00:00
|
|
|
export function customQueryHandlerNames(): string[] {
|
2020-10-07 08:15:54 +00:00
|
|
|
return [..._queryHandlers.keys()].filter(
|
|
|
|
(name) => !_builtInHandlers.has(name)
|
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 08:43:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-09-23 14:02:22 +00:00
|
|
|
export function clearCustomQueryHandlers(): void {
|
2020-10-07 08:15:54 +00:00
|
|
|
customQueryHandlerNames().forEach(unregisterCustomQueryHandler);
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 08:43:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
export function getQueryHandlerAndSelector(
|
2020-07-17 05:29:42 +00:00
|
|
|
selector: string
|
2020-09-23 14:02:22 +00:00
|
|
|
): { updatedSelector: string; queryHandler: InternalQueryHandler } {
|
2020-04-30 11:45:52 +00:00
|
|
|
const hasCustomQueryHandler = /^[a-zA-Z]+\//.test(selector);
|
|
|
|
if (!hasCustomQueryHandler)
|
2020-09-23 14:02:22 +00:00
|
|
|
return { updatedSelector: selector, queryHandler: _defaultHandler };
|
2020-04-30 11:45:52 +00:00
|
|
|
|
|
|
|
const index = selector.indexOf('/');
|
|
|
|
const name = selector.slice(0, index);
|
|
|
|
const updatedSelector = selector.slice(index + 1);
|
2020-09-23 14:02:22 +00:00
|
|
|
const queryHandler = _queryHandlers.get(name);
|
2020-04-30 11:45:52 +00:00
|
|
|
if (!queryHandler)
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
`Query set to use "${name}", but no query handler of that name was found`
|
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
updatedSelector,
|
2020-05-07 10:54:55 +00:00
|
|
|
queryHandler,
|
2020-04-30 11:45:52 +00:00
|
|
|
};
|
|
|
|
}
|