compile.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.compileToken = exports.compileUnsafe = exports.compile = void 0;
  30. var css_what_1 = require("css-what");
  31. var boolbase_1 = __importDefault(require("boolbase"));
  32. var sort_js_1 = __importStar(require("./sort.js"));
  33. var general_js_1 = require("./general.js");
  34. var subselects_js_1 = require("./pseudo-selectors/subselects.js");
  35. /**
  36. * Compiles a selector to an executable function.
  37. *
  38. * @param selector Selector to compile.
  39. * @param options Compilation options.
  40. * @param context Optional context for the selector.
  41. */
  42. function compile(selector, options, context) {
  43. var next = compileUnsafe(selector, options, context);
  44. return (0, subselects_js_1.ensureIsTag)(next, options.adapter);
  45. }
  46. exports.compile = compile;
  47. function compileUnsafe(selector, options, context) {
  48. var token = typeof selector === "string" ? (0, css_what_1.parse)(selector) : selector;
  49. return compileToken(token, options, context);
  50. }
  51. exports.compileUnsafe = compileUnsafe;
  52. function includesScopePseudo(t) {
  53. return (t.type === css_what_1.SelectorType.Pseudo &&
  54. (t.name === "scope" ||
  55. (Array.isArray(t.data) &&
  56. t.data.some(function (data) { return data.some(includesScopePseudo); }))));
  57. }
  58. var DESCENDANT_TOKEN = { type: css_what_1.SelectorType.Descendant };
  59. var FLEXIBLE_DESCENDANT_TOKEN = {
  60. type: "_flexibleDescendant",
  61. };
  62. var SCOPE_TOKEN = {
  63. type: css_what_1.SelectorType.Pseudo,
  64. name: "scope",
  65. data: null,
  66. };
  67. /*
  68. * CSS 4 Spec (Draft): 3.4.1. Absolutizing a Relative Selector
  69. * http://www.w3.org/TR/selectors4/#absolutizing
  70. */
  71. function absolutize(token, _a, context) {
  72. var adapter = _a.adapter;
  73. // TODO Use better check if the context is a document
  74. var hasContext = !!(context === null || context === void 0 ? void 0 : context.every(function (e) {
  75. var parent = adapter.isTag(e) && adapter.getParent(e);
  76. return e === subselects_js_1.PLACEHOLDER_ELEMENT || (parent && adapter.isTag(parent));
  77. }));
  78. for (var _i = 0, token_1 = token; _i < token_1.length; _i++) {
  79. var t = token_1[_i];
  80. if (t.length > 0 &&
  81. (0, sort_js_1.isTraversal)(t[0]) &&
  82. t[0].type !== css_what_1.SelectorType.Descendant) {
  83. // Don't continue in else branch
  84. }
  85. else if (hasContext && !t.some(includesScopePseudo)) {
  86. t.unshift(DESCENDANT_TOKEN);
  87. }
  88. else {
  89. continue;
  90. }
  91. t.unshift(SCOPE_TOKEN);
  92. }
  93. }
  94. function compileToken(token, options, context) {
  95. var _a;
  96. token.forEach(sort_js_1.default);
  97. context = (_a = options.context) !== null && _a !== void 0 ? _a : context;
  98. var isArrayContext = Array.isArray(context);
  99. var finalContext = context && (Array.isArray(context) ? context : [context]);
  100. // Check if the selector is relative
  101. if (options.relativeSelector !== false) {
  102. absolutize(token, options, finalContext);
  103. }
  104. else if (token.some(function (t) { return t.length > 0 && (0, sort_js_1.isTraversal)(t[0]); })) {
  105. throw new Error("Relative selectors are not allowed when the `relativeSelector` option is disabled");
  106. }
  107. var shouldTestNextSiblings = false;
  108. var query = token
  109. .map(function (rules) {
  110. if (rules.length >= 2) {
  111. var first = rules[0], second = rules[1];
  112. if (first.type !== css_what_1.SelectorType.Pseudo ||
  113. first.name !== "scope") {
  114. // Ignore
  115. }
  116. else if (isArrayContext &&
  117. second.type === css_what_1.SelectorType.Descendant) {
  118. rules[1] = FLEXIBLE_DESCENDANT_TOKEN;
  119. }
  120. else if (second.type === css_what_1.SelectorType.Adjacent ||
  121. second.type === css_what_1.SelectorType.Sibling) {
  122. shouldTestNextSiblings = true;
  123. }
  124. }
  125. return compileRules(rules, options, finalContext);
  126. })
  127. .reduce(reduceRules, boolbase_1.default.falseFunc);
  128. query.shouldTestNextSiblings = shouldTestNextSiblings;
  129. return query;
  130. }
  131. exports.compileToken = compileToken;
  132. function compileRules(rules, options, context) {
  133. var _a;
  134. return rules.reduce(function (previous, rule) {
  135. return previous === boolbase_1.default.falseFunc
  136. ? boolbase_1.default.falseFunc
  137. : (0, general_js_1.compileGeneralSelector)(previous, rule, options, context, compileToken);
  138. }, (_a = options.rootFunc) !== null && _a !== void 0 ? _a : boolbase_1.default.trueFunc);
  139. }
  140. function reduceRules(a, b) {
  141. if (b === boolbase_1.default.falseFunc || a === boolbase_1.default.trueFunc) {
  142. return a;
  143. }
  144. if (a === boolbase_1.default.falseFunc || b === boolbase_1.default.trueFunc) {
  145. return b;
  146. }
  147. return function combine(elem) {
  148. return a(elem) || b(elem);
  149. };
  150. }
  151. //# sourceMappingURL=compile.js.map