2020-01-28 11:20:34 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-06-19 15:22:26 -07:00
|
|
|
import { visitAllSelectorParts, InvalidSelectorError, type ParsedSelector, parseSelector, stringifySelector } from '../utils/isomorphic/selectorParser';
|
2022-05-02 10:32:50 -06:00
|
|
|
import { createGuid } from '../utils';
|
2020-01-28 11:20:34 -08:00
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
export class Selectors {
|
2023-02-21 14:08:51 -08:00
|
|
|
private readonly _builtinEngines: Set<string>;
|
|
|
|
private readonly _builtinEnginesInMainWorld: Set<string>;
|
2020-03-25 20:59:48 -07:00
|
|
|
readonly _engines: Map<string, { source: string, contentScript: boolean }>;
|
2021-04-20 23:03:56 -07:00
|
|
|
readonly guid = `selectors@${createGuid()}`;
|
2022-11-08 12:04:43 -08:00
|
|
|
private _testIdAttributeName: string = 'data-testid';
|
2020-01-28 11:20:34 -08:00
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
constructor() {
|
2021-01-08 14:51:43 -08:00
|
|
|
// Note: keep in sync with InjectedScript class.
|
2020-04-15 10:16:29 -07:00
|
|
|
this._builtinEngines = new Set([
|
|
|
|
'css', 'css:light',
|
|
|
|
'xpath', 'xpath:light',
|
2021-08-11 03:21:16 +03:00
|
|
|
'_react', '_vue',
|
2020-04-15 10:16:29 -07:00
|
|
|
'text', 'text:light',
|
|
|
|
'id', 'id:light',
|
|
|
|
'data-testid', 'data-testid:light',
|
|
|
|
'data-test-id', 'data-test-id:light',
|
2020-12-04 06:51:18 -08:00
|
|
|
'data-test', 'data-test:light',
|
2023-04-05 14:13:28 -07:00
|
|
|
'nth', 'visible', 'internal:control',
|
|
|
|
'internal:has', 'internal:has-not',
|
|
|
|
'internal:has-text', 'internal:has-not-text',
|
2023-07-14 12:21:45 -07:00
|
|
|
'internal:and', 'internal:or', 'internal:chain',
|
2022-11-08 12:04:43 -08:00
|
|
|
'role', 'internal:attr', 'internal:label', 'internal:text', 'internal:role', 'internal:testid',
|
2020-04-15 10:16:29 -07:00
|
|
|
]);
|
2021-08-08 02:51:39 +03:00
|
|
|
this._builtinEnginesInMainWorld = new Set([
|
2021-08-11 03:21:16 +03:00
|
|
|
'_react', '_vue',
|
2021-08-08 02:51:39 +03:00
|
|
|
]);
|
2020-02-28 15:34:07 -08:00
|
|
|
this._engines = new Map();
|
2020-01-28 11:20:34 -08:00
|
|
|
}
|
|
|
|
|
2020-08-19 13:27:58 -07:00
|
|
|
async register(name: string, source: string, contentScript: boolean = false): Promise<void> {
|
2020-02-28 15:34:07 -08:00
|
|
|
if (!name.match(/^[a-zA-Z_0-9-]+$/))
|
|
|
|
throw new Error('Selector engine name may only contain [a-zA-Z0-9_] characters');
|
2020-03-25 14:08:46 -07:00
|
|
|
// Note: we keep 'zs' for future use.
|
2020-04-15 10:16:29 -07:00
|
|
|
if (this._builtinEngines.has(name) || name === 'zs' || name === 'zs:light')
|
2020-02-28 15:34:07 -08:00
|
|
|
throw new Error(`"${name}" is a predefined selector engine`);
|
|
|
|
if (this._engines.has(name))
|
|
|
|
throw new Error(`"${name}" selector engine has been already registered`);
|
2020-03-25 20:59:48 -07:00
|
|
|
this._engines.set(name, { source, contentScript });
|
2020-01-28 11:20:34 -08:00
|
|
|
}
|
|
|
|
|
2022-11-08 12:04:43 -08:00
|
|
|
testIdAttributeName(): string {
|
|
|
|
return this._testIdAttributeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTestIdAttributeName(testIdAttributeName: string) {
|
|
|
|
this._testIdAttributeName = testIdAttributeName;
|
|
|
|
}
|
|
|
|
|
2021-04-16 11:14:57 -07:00
|
|
|
unregisterAll() {
|
|
|
|
this._engines.clear();
|
|
|
|
}
|
|
|
|
|
2023-02-21 14:08:51 -08:00
|
|
|
parseSelector(selector: string | ParsedSelector, strict: boolean) {
|
2021-11-04 12:28:35 -08:00
|
|
|
const parsed = typeof selector === 'string' ? parseSelector(selector) : selector;
|
2020-12-17 17:01:46 -08:00
|
|
|
let needsMainWorld = false;
|
2023-06-19 15:22:26 -07:00
|
|
|
visitAllSelectorParts(parsed, part => {
|
|
|
|
const name = part.name;
|
2022-02-02 16:55:50 -08:00
|
|
|
const custom = this._engines.get(name);
|
|
|
|
if (!custom && !this._builtinEngines.has(name))
|
|
|
|
throw new InvalidSelectorError(`Unknown engine "${name}" while parsing selector ${stringifySelector(parsed)}`);
|
2021-07-27 12:53:12 -07:00
|
|
|
if (custom && !custom.contentScript)
|
|
|
|
needsMainWorld = true;
|
2022-02-02 16:55:50 -08:00
|
|
|
if (this._builtinEnginesInMainWorld.has(name))
|
2021-08-08 02:51:39 +03:00
|
|
|
needsMainWorld = true;
|
2023-06-19 15:22:26 -07:00
|
|
|
});
|
2020-06-24 17:03:28 -07:00
|
|
|
return {
|
|
|
|
parsed,
|
2023-02-21 14:08:51 -08:00
|
|
|
world: needsMainWorld ? 'main' as const : 'utility' as const,
|
2021-07-26 22:00:23 -07:00
|
|
|
strict,
|
2020-06-24 17:03:28 -07:00
|
|
|
};
|
2020-01-28 11:20:34 -08:00
|
|
|
}
|
|
|
|
}
|