|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | import * as types from '../types';
|
| 18 | +import { createAttributeEngine } from './attributeSelectorEngine'; |
| 19 | +import { createCSSEngine } from './cssSelectorEngine'; |
| 20 | +import { SelectorEngine, SelectorRoot } from './selectorEngine'; |
| 21 | +import { createTextSelector } from './textSelectorEngine'; |
| 22 | +import { XPathEngine } from './xpathSelectorEngine'; |
18 | 23 |
|
19 | 24 | type Predicate<T> = () => T;
|
20 |
| -export type InjectedResult<T = undefined> = |
21 |
| - (T extends undefined ? { status: 'success', value?: T} : { status: 'success', value: T }) | |
22 |
| - { status: 'notconnected' } | |
23 |
| - { status: 'timeout' } | |
24 |
| - { status: 'error', error: string }; |
25 | 25 |
|
26 |
| -export class Injected { |
| 26 | +export default class InjectedScript { |
| 27 | + readonly engines: Map<string, SelectorEngine>; |
| 28 | + |
| 29 | + constructor(customEngines: { name: string, engine: SelectorEngine}[]) { |
| 30 | + this.engines = new Map(); |
| 31 | + // Note: keep predefined names in sync with Selectors class. |
| 32 | + this.engines.set('css', createCSSEngine(true)); |
| 33 | + this.engines.set('css:light', createCSSEngine(false)); |
| 34 | + this.engines.set('xpath', XPathEngine); |
| 35 | + this.engines.set('xpath:light', XPathEngine); |
| 36 | + this.engines.set('text', createTextSelector(true)); |
| 37 | + this.engines.set('text:light', createTextSelector(false)); |
| 38 | + this.engines.set('id', createAttributeEngine('id', true)); |
| 39 | + this.engines.set('id:light', createAttributeEngine('id', false)); |
| 40 | + this.engines.set('data-testid', createAttributeEngine('data-testid', true)); |
| 41 | + this.engines.set('data-testid:light', createAttributeEngine('data-testid', false)); |
| 42 | + this.engines.set('data-test-id', createAttributeEngine('data-test-id', true)); |
| 43 | + this.engines.set('data-test-id:light', createAttributeEngine('data-test-id', false)); |
| 44 | + this.engines.set('data-test', createAttributeEngine('data-test', true)); |
| 45 | + this.engines.set('data-test:light', createAttributeEngine('data-test', false)); |
| 46 | + for (const {name, engine} of customEngines) |
| 47 | + this.engines.set(name, engine); |
| 48 | + } |
| 49 | + |
| 50 | + querySelector(selector: types.ParsedSelector, root: Node): Element | undefined { |
| 51 | + if (!(root as any)['querySelector']) |
| 52 | + throw new Error('Node is not queryable.'); |
| 53 | + return this._querySelectorRecursively(root as SelectorRoot, selector, 0); |
| 54 | + } |
| 55 | + |
| 56 | + private _querySelectorRecursively(root: SelectorRoot, selector: types.ParsedSelector, index: number): Element | undefined { |
| 57 | + const current = selector.parts[index]; |
| 58 | + if (index === selector.parts.length - 1) |
| 59 | + return this.engines.get(current.name)!.query(root, current.body); |
| 60 | + const all = this.engines.get(current.name)!.queryAll(root, current.body); |
| 61 | + for (const next of all) { |
| 62 | + const result = this._querySelectorRecursively(next, selector, index + 1); |
| 63 | + if (result) |
| 64 | + return selector.capture === index ? next : result; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + querySelectorAll(selector: types.ParsedSelector, root: Node): Element[] { |
| 69 | + if (!(root as any)['querySelectorAll']) |
| 70 | + throw new Error('Node is not queryable.'); |
| 71 | + const capture = selector.capture === undefined ? selector.parts.length - 1 : selector.capture; |
| 72 | + // Query all elements up to the capture. |
| 73 | + const partsToQuerAll = selector.parts.slice(0, capture + 1); |
| 74 | + // Check they have a descendant matching everything after the capture. |
| 75 | + const partsToCheckOne = selector.parts.slice(capture + 1); |
| 76 | + let set = new Set<SelectorRoot>([ root as SelectorRoot ]); |
| 77 | + for (const { name, body } of partsToQuerAll) { |
| 78 | + const newSet = new Set<Element>(); |
| 79 | + for (const prev of set) { |
| 80 | + for (const next of this.engines.get(name)!.queryAll(prev, body)) { |
| 81 | + if (newSet.has(next)) |
| 82 | + continue; |
| 83 | + newSet.add(next); |
| 84 | + } |
| 85 | + } |
| 86 | + set = newSet; |
| 87 | + } |
| 88 | + const candidates = Array.from(set) as Element[]; |
| 89 | + if (!partsToCheckOne.length) |
| 90 | + return candidates; |
| 91 | + const partial = { parts: partsToCheckOne }; |
| 92 | + return candidates.filter(e => !!this._querySelectorRecursively(e, partial, 0)); |
| 93 | + } |
| 94 | + |
27 | 95 | isVisible(element: Element): boolean {
|
28 | 96 | // Note: this logic should be similar to waitForDisplayedAtStablePosition() to avoid surprises.
|
29 | 97 | if (!element.ownerDocument || !element.ownerDocument.defaultView)
|
@@ -95,7 +163,7 @@ export class Injected {
|
95 | 163 | return { left: parseInt(style.borderLeftWidth || '', 10), top: parseInt(style.borderTopWidth || '', 10) };
|
96 | 164 | }
|
97 | 165 |
|
98 |
| - selectOptions(node: Node, optionsToSelect: (Node | types.SelectOption)[]): InjectedResult<string[]> { |
| 166 | + selectOptions(node: Node, optionsToSelect: (Node | types.SelectOption)[]): types.InjectedScriptResult<string[]> { |
99 | 167 | if (node.nodeName.toLowerCase() !== 'select')
|
100 | 168 | return { status: 'error', error: 'Element is not a <select> element.' };
|
101 | 169 | if (!node.isConnected)
|
@@ -126,7 +194,7 @@ export class Injected {
|
126 | 194 | return { status: 'success', value: options.filter(option => option.selected).map(option => option.value) };
|
127 | 195 | }
|
128 | 196 |
|
129 |
| - fill(node: Node, value: string): InjectedResult<boolean> { |
| 197 | + fill(node: Node, value: string): types.InjectedScriptResult<boolean> { |
130 | 198 | if (node.nodeType !== Node.ELEMENT_NODE)
|
131 | 199 | return { status: 'error', error: 'Node is not of type HTMLElement' };
|
132 | 200 | const element = node as HTMLElement;
|
@@ -175,7 +243,7 @@ export class Injected {
|
175 | 243 | return result;
|
176 | 244 | }
|
177 | 245 |
|
178 |
| - selectText(node: Node): InjectedResult { |
| 246 | + selectText(node: Node): types.InjectedScriptResult { |
179 | 247 | if (node.nodeType !== Node.ELEMENT_NODE)
|
180 | 248 | return { status: 'error', error: 'Node is not of type HTMLElement' };
|
181 | 249 | if (!node.isConnected)
|
@@ -207,7 +275,7 @@ export class Injected {
|
207 | 275 | return { status: 'success' };
|
208 | 276 | }
|
209 | 277 |
|
210 |
| - focusNode(node: Node): InjectedResult { |
| 278 | + focusNode(node: Node): types.InjectedScriptResult { |
211 | 279 | if (!node.isConnected)
|
212 | 280 | return { status: 'notconnected' };
|
213 | 281 | if (!(node as any)['focus'])
|
@@ -262,7 +330,7 @@ export class Injected {
|
262 | 330 | input.dispatchEvent(new Event('change', { 'bubbles': true }));
|
263 | 331 | }
|
264 | 332 |
|
265 |
| - async waitForDisplayedAtStablePosition(node: Node, rafCount: number, timeout: number): Promise<InjectedResult> { |
| 333 | + async waitForDisplayedAtStablePosition(node: Node, rafCount: number, timeout: number): Promise<types.InjectedScriptResult> { |
266 | 334 | if (!node.isConnected)
|
267 | 335 | return { status: 'notconnected' };
|
268 | 336 | const element = node.nodeType === Node.ELEMENT_NODE ? (node as Element) : node.parentElement;
|
@@ -305,7 +373,7 @@ export class Injected {
|
305 | 373 | return { status: result === 'notconnected' ? 'notconnected' : (result ? 'success' : 'timeout') };
|
306 | 374 | }
|
307 | 375 |
|
308 |
| - checkHitTargetAt(node: Node, point: types.Point): InjectedResult<boolean> { |
| 376 | + checkHitTargetAt(node: Node, point: types.Point): types.InjectedScriptResult<boolean> { |
309 | 377 | let element = node.nodeType === Node.ELEMENT_NODE ? (node as Element) : node.parentElement;
|
310 | 378 | while (element && window.getComputedStyle(element).pointerEvents === 'none')
|
311 | 379 | element = element.parentElement;
|
|
0 commit comments