selectors.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Selectors = void 0;
  6. var _selectorParser = require("../utils/isomorphic/selectorParser");
  7. var _utils = require("../utils");
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class Selectors {
  24. constructor() {
  25. this._builtinEngines = void 0;
  26. this._builtinEnginesInMainWorld = void 0;
  27. this._engines = void 0;
  28. this.guid = `selectors@${(0, _utils.createGuid)()}`;
  29. this._testIdAttributeName = 'data-testid';
  30. // Note: keep in sync with InjectedScript class.
  31. this._builtinEngines = new Set(['css', 'css:light', 'xpath', 'xpath:light', '_react', '_vue', 'text', 'text:light', 'id', 'id:light', 'data-testid', 'data-testid:light', 'data-test-id', 'data-test-id:light', 'data-test', 'data-test:light', 'nth', 'visible', 'internal:control', 'internal:has', 'internal:has-not', 'internal:has-text', 'internal:has-not-text', 'internal:and', 'internal:or', 'internal:chain', 'role', 'internal:attr', 'internal:label', 'internal:text', 'internal:role', 'internal:testid']);
  32. this._builtinEnginesInMainWorld = new Set(['_react', '_vue']);
  33. this._engines = new Map();
  34. }
  35. async register(name, source, contentScript = false) {
  36. if (!name.match(/^[a-zA-Z_0-9-]+$/)) throw new Error('Selector engine name may only contain [a-zA-Z0-9_] characters');
  37. // Note: we keep 'zs' for future use.
  38. if (this._builtinEngines.has(name) || name === 'zs' || name === 'zs:light') throw new Error(`"${name}" is a predefined selector engine`);
  39. if (this._engines.has(name)) throw new Error(`"${name}" selector engine has been already registered`);
  40. this._engines.set(name, {
  41. source,
  42. contentScript
  43. });
  44. }
  45. testIdAttributeName() {
  46. return this._testIdAttributeName;
  47. }
  48. setTestIdAttributeName(testIdAttributeName) {
  49. this._testIdAttributeName = testIdAttributeName;
  50. }
  51. unregisterAll() {
  52. this._engines.clear();
  53. }
  54. parseSelector(selector, strict) {
  55. const parsed = typeof selector === 'string' ? (0, _selectorParser.parseSelector)(selector) : selector;
  56. let needsMainWorld = false;
  57. (0, _selectorParser.visitAllSelectorParts)(parsed, part => {
  58. const name = part.name;
  59. const custom = this._engines.get(name);
  60. if (!custom && !this._builtinEngines.has(name)) throw new _selectorParser.InvalidSelectorError(`Unknown engine "${name}" while parsing selector ${(0, _selectorParser.stringifySelector)(parsed)}`);
  61. if (custom && !custom.contentScript) needsMainWorld = true;
  62. if (this._builtinEnginesInMainWorld.has(name)) needsMainWorld = true;
  63. });
  64. return {
  65. parsed,
  66. world: needsMainWorld ? 'main' : 'utility',
  67. strict
  68. };
  69. }
  70. }
  71. exports.Selectors = Selectors;