-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
selectorGenerator.ts
356 lines (310 loc) · 14.6 KB
/
selectorGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Copyright (c) Microsoft Corporation.
*
* 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 type InjectedScript from '../../injected/injectedScript';
import { elementText } from '../../injected/selectorEvaluator';
type SelectorToken = {
engine: string;
selector: string;
score: number; // Lower is better.
};
const cacheAllowText = new Map<Element, SelectorToken[] | null>();
const cacheDisallowText = new Map<Element, SelectorToken[] | null>();
export function querySelector(injectedScript: InjectedScript, selector: string, ownerDocument: Document): { selector: string, elements: Element[] } {
const parsedSelector = injectedScript.parseSelector(selector);
return {
selector,
elements: injectedScript.querySelectorAll(parsedSelector, ownerDocument)
};
}
export function generateSelector(injectedScript: InjectedScript, targetElement: Element): { selector: string, elements: Element[] } {
injectedScript._evaluator.begin();
try {
targetElement = targetElement.closest('button,select,input,[role=button],[role=checkbox],[role=radio]') || targetElement;
const targetTokens = generateSelectorFor(injectedScript, targetElement);
const bestTokens = targetTokens || [cssFallback(injectedScript, targetElement)];
const selector = joinTokens(bestTokens);
const parsedSelector = injectedScript.parseSelector(selector);
return {
selector,
elements: injectedScript.querySelectorAll(parsedSelector, targetElement.ownerDocument)
};
} finally {
cacheAllowText.clear();
cacheDisallowText.clear();
injectedScript._evaluator.end();
}
}
function filterRegexTokens(textCandidates: SelectorToken[][]): SelectorToken[][] {
// Filter out regex-based selectors for better performance.
return textCandidates.filter(c => c[0].selector[0] !== '/');
}
function generateSelectorFor(injectedScript: InjectedScript, targetElement: Element): SelectorToken[] | null {
if (targetElement.ownerDocument.documentElement === targetElement)
return [{ engine: 'css', selector: 'html', score: 1 }];
const calculate = (element: Element, allowText: boolean): SelectorToken[] | null => {
const allowNthMatch = element === targetElement;
let textCandidates = allowText ? buildTextCandidates(injectedScript, element, element === targetElement).map(token => [token]) : [];
if (element !== targetElement) {
// Do not use regex for parent elements (for performance).
textCandidates = filterRegexTokens(textCandidates);
}
const noTextCandidates = buildCandidates(injectedScript, element).map(token => [token]);
// First check all text and non-text candidates for the element.
let result = chooseFirstSelector(injectedScript, targetElement.ownerDocument, element, [...textCandidates, ...noTextCandidates], allowNthMatch);
// Do not use regex for chained selectors (for performance).
textCandidates = filterRegexTokens(textCandidates);
const checkWithText = (textCandidatesToUse: SelectorToken[][]) => {
// Use the deepest possible text selector - works pretty good and saves on compute time.
const allowParentText = allowText && !textCandidatesToUse.length;
const candidates = [...textCandidatesToUse, ...noTextCandidates].filter(c => {
if (!result)
return true;
return combineScores(c) < combineScores(result);
});
// This is best theoretically possible candidate from the current parent.
// We use the fact that widening the scope to grand-parent makes any selector
// even less likely to match.
let bestPossibleInParent: SelectorToken[] | null = candidates[0];
if (!bestPossibleInParent)
return;
for (let parent = parentElementOrShadowHost(element); parent; parent = parentElementOrShadowHost(parent)) {
const parentTokens = calculateCached(parent, allowParentText);
if (!parentTokens)
continue;
// Even the best selector won't be too good - skip this parent.
if (result && combineScores([...parentTokens, ...bestPossibleInParent]) >= combineScores(result))
continue;
// Update the best candidate that finds "element" in the "parent".
bestPossibleInParent = chooseFirstSelector(injectedScript, parent, element, candidates, allowNthMatch);
if (!bestPossibleInParent)
return;
const combined = [...parentTokens, ...bestPossibleInParent];
if (!result || combineScores(combined) < combineScores(result))
result = combined;
}
};
checkWithText(textCandidates);
// Allow skipping text on the target element, and using text on one of the parents.
if (element === targetElement && textCandidates.length)
checkWithText([]);
return result;
};
const calculateCached = (element: Element, allowText: boolean): SelectorToken[] | null => {
const cache = allowText ? cacheAllowText : cacheDisallowText;
let value = cache.get(element);
if (value === undefined) {
value = calculate(element, allowText);
cache.set(element, value);
}
return value;
};
return calculateCached(targetElement, true);
}
function buildCandidates(injectedScript: InjectedScript, element: Element): SelectorToken[] {
const candidates: SelectorToken[] = [];
for (const attribute of ['data-testid', 'data-test-id', 'data-test']) {
if (element.hasAttribute(attribute))
candidates.push({ engine: 'css', selector: `[${attribute}=${quoteString(element.getAttribute(attribute)!)}]`, score: 1 });
}
if (element.nodeName === 'INPUT') {
const input = element as HTMLInputElement;
if (input.placeholder)
candidates.push({ engine: 'css', selector: `[placeholder=${quoteString(input.placeholder)}]`, score: 10 });
}
if (element.hasAttribute('aria-label'))
candidates.push({ engine: 'css', selector: `[aria-label=${quoteString(element.getAttribute('aria-label')!)}]`, score: 10 });
if (element.nodeName === 'IMG' && element.getAttribute('alt'))
candidates.push({ engine: 'css', selector: `img[alt=${quoteString(element.getAttribute('alt')!)}]`, score: 10 });
if (element.hasAttribute('role'))
candidates.push({ engine: 'css', selector: `${element.nodeName.toLocaleLowerCase()}[role=${quoteString(element.getAttribute('role')!)}]` , score: 50 });
if (['INPUT', 'TEXTAREA'].includes(element.nodeName) && element.getAttribute('type') !== 'hidden') {
if (element.getAttribute('name'))
candidates.push({ engine: 'css', selector: `${element.nodeName.toLowerCase()}[name=${quoteString(element.getAttribute('name')!)}]`, score: 50 });
if (element.getAttribute('type'))
candidates.push({ engine: 'css', selector: `${element.nodeName.toLowerCase()}[type=${quoteString(element.getAttribute('type')!)}]`, score: 50 });
}
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(element.nodeName))
candidates.push({ engine: 'css', selector: element.nodeName.toLowerCase(), score: 50 });
const idAttr = element.getAttribute('id');
if (idAttr && !isGuidLike(idAttr))
candidates.push({ engine: 'css', selector: `#${idAttr}`, score: 100 });
candidates.push({ engine: 'css', selector: element.nodeName.toLocaleLowerCase(), score: 200 });
return candidates;
}
function buildTextCandidates(injectedScript: InjectedScript, element: Element, allowHasText: boolean): SelectorToken[] {
if (element.nodeName === 'SELECT')
return [];
const text = elementText(injectedScript._evaluator, element).trim().replace(/\s+/g, ' ').substring(0, 80);
if (!text)
return [];
const candidates: SelectorToken[] = [];
let escaped = text;
if (text.includes('"') || text.includes('>>') || text[0] === '/')
escaped = `/.*${escapeForRegex(text)}.*/`;
candidates.push({ engine: 'text', selector: escaped, score: 10 });
if (allowHasText && escaped === text) {
let prefix = element.nodeName.toLocaleLowerCase();
if (element.hasAttribute('role'))
prefix += `[role=${quoteString(element.getAttribute('role')!)}]`;
candidates.push({ engine: 'css', selector: `${prefix}:has-text("${text}")`, score: 30 });
}
return candidates;
}
function parentElementOrShadowHost(element: Element): Element | null {
if (element.parentElement)
return element.parentElement;
if (!element.parentNode)
return null;
if (element.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE && (element.parentNode as ShadowRoot).host)
return (element.parentNode as ShadowRoot).host;
return null;
}
function cssFallback(injectedScript: InjectedScript, targetElement: Element): SelectorToken {
const kFallbackScore = 10000000;
const root: Node = targetElement.ownerDocument;
const tokens: string[] = [];
function uniqueCSSSelector(prefix?: string): string | undefined {
const path = tokens.slice();
if (prefix)
path.unshift(prefix);
const selector = path.join(' ');
const parsedSelector = injectedScript.parseSelector(selector);
const node = injectedScript.querySelector(parsedSelector, targetElement.ownerDocument);
return node === targetElement ? selector : undefined;
}
for (let element: Element | null = targetElement; element && element !== root; element = parentElementOrShadowHost(element)) {
const nodeName = element.nodeName.toLowerCase();
// Element ID is the strongest signal, use it.
let bestTokenForLevel: string = '';
if (element.id) {
const token = /^[a-zA-Z][a-zA-Z0-9\-\_]+$/.test(element.id) ? '#' + element.id : `[id="${element.id}"]`;
const selector = uniqueCSSSelector(token);
if (selector)
return { engine: 'css', selector, score: kFallbackScore };
bestTokenForLevel = token;
}
const parent = element.parentNode as (Element | ShadowRoot);
// Combine class names until unique.
const classes = Array.from(element.classList);
for (let i = 0; i < classes.length; ++i) {
const token = '.' + classes.slice(0, i + 1).join('.');
const selector = uniqueCSSSelector(token);
if (selector)
return { engine: 'css', selector, score: kFallbackScore };
// Even if not unique, does this subset of classes uniquely identify node as a child?
if (!bestTokenForLevel && parent) {
const sameClassSiblings = parent.querySelectorAll(token);
if (sameClassSiblings.length === 1)
bestTokenForLevel = token;
}
}
// Ordinal is the weakest signal.
if (parent) {
const siblings = Array.from(parent.children);
const sameTagSiblings = siblings.filter(sibling => (sibling).nodeName.toLowerCase() === nodeName);
const token = sameTagSiblings.indexOf(element) === 0 ? nodeName : `${nodeName}:nth-child(${1 + siblings.indexOf(element)})`;
const selector = uniqueCSSSelector(token);
if (selector)
return { engine: 'css', selector, score: kFallbackScore };
if (!bestTokenForLevel)
bestTokenForLevel = token;
} else if (!bestTokenForLevel) {
bestTokenForLevel = nodeName;
}
tokens.unshift(bestTokenForLevel);
}
return { engine: 'css', selector: uniqueCSSSelector()!, score: kFallbackScore };
}
function escapeForRegex(text: string): string {
return text.replace(/[.*+?^>${}()|[\]\\]/g, '\\$&');
}
function quoteString(text: string): string {
return `"${text.replaceAll(/"/g, '\\"').replaceAll(/\n/g, '\\n')}"`;
}
function joinTokens(tokens: SelectorToken[]): string {
const parts = [];
let lastEngine = '';
for (const { engine, selector } of tokens) {
if (parts.length && (lastEngine !== 'css' || engine !== 'css' || selector.startsWith(':nth-match(')))
parts.push('>>');
lastEngine = engine;
if (engine === 'css')
parts.push(selector);
else
parts.push(`${engine}=${selector}`);
}
return parts.join(' ');
}
function combineScores(tokens: SelectorToken[]): number {
let score = 0;
for (let i = 0; i < tokens.length; i++)
score += tokens[i].score * (tokens.length - i);
return score;
}
function chooseFirstSelector(injectedScript: InjectedScript, scope: Element | Document, targetElement: Element, selectors: SelectorToken[][], allowNthMatch: boolean): SelectorToken[] | null {
const joined = selectors.map(tokens => ({ tokens, score: combineScores(tokens) }));
joined.sort((a, b) => a.score - b.score);
let bestWithIndex: SelectorToken[] | null = null;
for (const { tokens } of joined) {
const parsedSelector = injectedScript.parseSelector(joinTokens(tokens));
const result = injectedScript.querySelectorAll(parsedSelector, scope);
const index = result.indexOf(targetElement);
if (index === 0) {
// We are the first match - found the best selector.
return tokens;
}
// Otherwise, perhaps we can get nth-match?
if (!allowNthMatch || bestWithIndex || index === -1 || result.length > 5)
continue;
// To use nth-match, we must convert everything to css.
const allCss = tokens.map(token => {
if (token.engine !== 'text')
return token;
if (token.selector.startsWith('/') && token.selector.endsWith('/'))
return { engine: 'css', selector: `:text-matches("${token.selector.substring(1, token.selector.length - 1)}")`, score: token.score };
return { engine: 'css', selector: `:text("${token.selector}")`, score: token.score };
});
const combined = joinTokens(allCss);
bestWithIndex = [{ engine: 'css', selector: `:nth-match(${combined}, ${index + 1})`, score: combineScores(allCss) + 1000 }];
}
return bestWithIndex;
}
function isGuidLike(id: string): boolean {
let lastCharacterType: 'lower' | 'upper' | 'digit' | 'other' | undefined;
let transitionCount = 0;
for (let i = 0; i < id.length; ++i) {
const c = id[i];
let characterType: 'lower' | 'upper' | 'digit' | 'other';
if (c === '-' || c === '_')
continue;
if (c >= 'a' && c <= 'z')
characterType = 'lower';
else if (c >= 'A' && c <= 'Z')
characterType = 'upper';
else if (c >= '0' && c <= '9')
characterType = 'digit';
else
characterType = 'other';
if (characterType === 'lower' && lastCharacterType === 'upper') {
lastCharacterType = characterType;
continue;
}
if (lastCharacterType && lastCharacterType !== characterType)
++transitionCount;
lastCharacterType = characterType;
}
return transitionCount >= id.length / 4;
}