compile.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.compileToken = exports.compileUnsafe = exports.compile = void 0;
  7. var css_what_1 = require("css-what");
  8. var boolbase_1 = require("boolbase");
  9. var sort_1 = __importDefault(require("./sort"));
  10. var procedure_1 = require("./procedure");
  11. var general_1 = require("./general");
  12. var subselects_1 = require("./pseudo-selectors/subselects");
  13. /**
  14. * Compiles a selector to an executable function.
  15. *
  16. * @param selector Selector to compile.
  17. * @param options Compilation options.
  18. * @param context Optional context for the selector.
  19. */
  20. function compile(selector, options, context) {
  21. var next = compileUnsafe(selector, options, context);
  22. return (0, subselects_1.ensureIsTag)(next, options.adapter);
  23. }
  24. exports.compile = compile;
  25. function compileUnsafe(selector, options, context) {
  26. var token = typeof selector === "string" ? (0, css_what_1.parse)(selector) : selector;
  27. return compileToken(token, options, context);
  28. }
  29. exports.compileUnsafe = compileUnsafe;
  30. function includesScopePseudo(t) {
  31. return (t.type === "pseudo" &&
  32. (t.name === "scope" ||
  33. (Array.isArray(t.data) &&
  34. t.data.some(function (data) { return data.some(includesScopePseudo); }))));
  35. }
  36. var DESCENDANT_TOKEN = { type: css_what_1.SelectorType.Descendant };
  37. var FLEXIBLE_DESCENDANT_TOKEN = {
  38. type: "_flexibleDescendant",
  39. };
  40. var SCOPE_TOKEN = {
  41. type: css_what_1.SelectorType.Pseudo,
  42. name: "scope",
  43. data: null,
  44. };
  45. /*
  46. * CSS 4 Spec (Draft): 3.3.1. Absolutizing a Scope-relative Selector
  47. * http://www.w3.org/TR/selectors4/#absolutizing
  48. */
  49. function absolutize(token, _a, context) {
  50. var adapter = _a.adapter;
  51. // TODO Use better check if the context is a document
  52. var hasContext = !!(context === null || context === void 0 ? void 0 : context.every(function (e) {
  53. var parent = adapter.isTag(e) && adapter.getParent(e);
  54. return e === subselects_1.PLACEHOLDER_ELEMENT || (parent && adapter.isTag(parent));
  55. }));
  56. for (var _i = 0, token_1 = token; _i < token_1.length; _i++) {
  57. var t = token_1[_i];
  58. if (t.length > 0 && (0, procedure_1.isTraversal)(t[0]) && t[0].type !== "descendant") {
  59. // Don't continue in else branch
  60. }
  61. else if (hasContext && !t.some(includesScopePseudo)) {
  62. t.unshift(DESCENDANT_TOKEN);
  63. }
  64. else {
  65. continue;
  66. }
  67. t.unshift(SCOPE_TOKEN);
  68. }
  69. }
  70. function compileToken(token, options, context) {
  71. var _a;
  72. token = token.filter(function (t) { return t.length > 0; });
  73. token.forEach(sort_1.default);
  74. context = (_a = options.context) !== null && _a !== void 0 ? _a : context;
  75. var isArrayContext = Array.isArray(context);
  76. var finalContext = context && (Array.isArray(context) ? context : [context]);
  77. absolutize(token, options, finalContext);
  78. var shouldTestNextSiblings = false;
  79. var query = token
  80. .map(function (rules) {
  81. if (rules.length >= 2) {
  82. var first = rules[0], second = rules[1];
  83. if (first.type !== "pseudo" || first.name !== "scope") {
  84. // Ignore
  85. }
  86. else if (isArrayContext && second.type === "descendant") {
  87. rules[1] = FLEXIBLE_DESCENDANT_TOKEN;
  88. }
  89. else if (second.type === "adjacent" ||
  90. second.type === "sibling") {
  91. shouldTestNextSiblings = true;
  92. }
  93. }
  94. return compileRules(rules, options, finalContext);
  95. })
  96. .reduce(reduceRules, boolbase_1.falseFunc);
  97. query.shouldTestNextSiblings = shouldTestNextSiblings;
  98. return query;
  99. }
  100. exports.compileToken = compileToken;
  101. function compileRules(rules, options, context) {
  102. var _a;
  103. return rules.reduce(function (previous, rule) {
  104. return previous === boolbase_1.falseFunc
  105. ? boolbase_1.falseFunc
  106. : (0, general_1.compileGeneralSelector)(previous, rule, options, context, compileToken);
  107. }, (_a = options.rootFunc) !== null && _a !== void 0 ? _a : boolbase_1.trueFunc);
  108. }
  109. function reduceRules(a, b) {
  110. if (b === boolbase_1.falseFunc || a === boolbase_1.trueFunc) {
  111. return a;
  112. }
  113. if (a === boolbase_1.falseFunc || b === boolbase_1.trueFunc) {
  114. return b;
  115. }
  116. return function combine(elem) {
  117. return a(elem) || b(elem);
  118. };
  119. }