From 7103887bb5880af37a3c7ae18c931fef496e9c6d Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 19 Oct 2020 07:00:28 +0100 Subject: [PATCH] add type for selector engine (#4174) --- src/client/selectors.ts | 3 ++- src/client/types.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/client/selectors.ts b/src/client/selectors.ts index b71ac27ea5..9819f3ad6a 100644 --- a/src/client/selectors.ts +++ b/src/client/selectors.ts @@ -17,12 +17,13 @@ import { evaluationScript } from './clientHelper'; import * as channels from '../protocol/channels'; import { ChannelOwner } from './channelOwner'; +import { SelectorEngine } from './types'; export class Selectors { private _channels = new Set(); private _registrations: channels.SelectorsRegisterParams[] = []; - async register(name: string, script: string | Function | { path?: string, content?: string }, options: { contentScript?: boolean } = {}): Promise { + async register(name: string, script: string | (() => SelectorEngine) | { path?: string, content?: string }, options: { contentScript?: boolean } = {}): Promise { const source = await evaluationScript(script, undefined, false); const params = { ...options, name, source }; for (const channel of this._channels) diff --git a/src/client/types.ts b/src/client/types.ts index 9891da5ef5..6cb7db2522 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -86,3 +86,19 @@ export type LaunchServerOptions = { port?: number, logger?: Logger, } & FirefoxUserPrefs; + +export type SelectorEngine = { + /** + * Creates a selector that matches given target when queried at the root. + * Can return undefined if unable to create one. + */ + create(root: HTMLElement, target: HTMLElement): string | undefined; + /** + * Returns the first element matching given selector in the root's subtree. + */ + query(root: HTMLElement, selector: string): HTMLElement | null; + /** + * Returns all elements matching given selector in the root's subtree. + */ + queryAll(root: HTMLElement, selector: string): HTMLElement[]; +};