compile.js 4.2 KB

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