Tim van der Lippe | 706ec96 | 2021-06-04 13:24:42 +0100 | [diff] [blame^] | 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 subselects_1.ensureIsTag(next, options.adapter); |
| 23 | } |
| 24 | exports.compile = compile; |
| 25 | function compileUnsafe(selector, options, context) { |
| 26 | var token = css_what_1.parse(selector, options); |
| 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: "descendant" }; |
| 37 | var FLEXIBLE_DESCENDANT_TOKEN = { |
| 38 | type: "_flexibleDescendant", |
| 39 | }; |
| 40 | var SCOPE_TOKEN = { type: "pseudo", name: "scope", data: null }; |
| 41 | /* |
| 42 | * CSS 4 Spec (Draft): 3.3.1. Absolutizing a Scope-relative Selector |
| 43 | * http://www.w3.org/TR/selectors4/#absolutizing |
| 44 | */ |
| 45 | function absolutize(token, _a, context) { |
| 46 | var adapter = _a.adapter; |
| 47 | // TODO Use better check if the context is a document |
| 48 | var hasContext = !!(context === null || context === void 0 ? void 0 : context.every(function (e) { |
| 49 | var parent = adapter.getParent(e); |
| 50 | return e === subselects_1.PLACEHOLDER_ELEMENT || !!(parent && adapter.isTag(parent)); |
| 51 | })); |
| 52 | for (var _i = 0, token_1 = token; _i < token_1.length; _i++) { |
| 53 | var t = token_1[_i]; |
| 54 | if (t.length > 0 && procedure_1.isTraversal(t[0]) && t[0].type !== "descendant") { |
| 55 | // Don't continue in else branch |
| 56 | } |
| 57 | else if (hasContext && !t.some(includesScopePseudo)) { |
| 58 | t.unshift(DESCENDANT_TOKEN); |
| 59 | } |
| 60 | else { |
| 61 | continue; |
| 62 | } |
| 63 | t.unshift(SCOPE_TOKEN); |
| 64 | } |
| 65 | } |
| 66 | function compileToken(token, options, context) { |
| 67 | var _a; |
| 68 | token = token.filter(function (t) { return t.length > 0; }); |
| 69 | token.forEach(sort_1.default); |
| 70 | context = (_a = options.context) !== null && _a !== void 0 ? _a : context; |
| 71 | var isArrayContext = Array.isArray(context); |
| 72 | var finalContext = context && (Array.isArray(context) ? context : [context]); |
| 73 | absolutize(token, options, finalContext); |
| 74 | var shouldTestNextSiblings = false; |
| 75 | var query = token |
| 76 | .map(function (rules) { |
| 77 | if (rules.length >= 2) { |
| 78 | var first = rules[0], second = rules[1]; |
| 79 | if (first.type !== "pseudo" || first.name !== "scope") { |
| 80 | // Ignore |
| 81 | } |
| 82 | else if (isArrayContext && second.type === "descendant") { |
| 83 | rules[1] = FLEXIBLE_DESCENDANT_TOKEN; |
| 84 | } |
| 85 | else if (second.type === "adjacent" || |
| 86 | second.type === "sibling") { |
| 87 | shouldTestNextSiblings = true; |
| 88 | } |
| 89 | } |
| 90 | return compileRules(rules, options, finalContext); |
| 91 | }) |
| 92 | .reduce(reduceRules, boolbase_1.falseFunc); |
| 93 | query.shouldTestNextSiblings = shouldTestNextSiblings; |
| 94 | return query; |
| 95 | } |
| 96 | exports.compileToken = compileToken; |
| 97 | function compileRules(rules, options, context) { |
| 98 | var _a; |
| 99 | return rules.reduce(function (previous, rule) { |
| 100 | return previous === boolbase_1.falseFunc |
| 101 | ? boolbase_1.falseFunc |
| 102 | : general_1.compileGeneralSelector(previous, rule, options, context, compileToken); |
| 103 | }, (_a = options.rootFunc) !== null && _a !== void 0 ? _a : boolbase_1.trueFunc); |
| 104 | } |
| 105 | function reduceRules(a, b) { |
| 106 | if (b === boolbase_1.falseFunc || a === boolbase_1.trueFunc) { |
| 107 | return a; |
| 108 | } |
| 109 | if (a === boolbase_1.falseFunc || b === boolbase_1.trueFunc) { |
| 110 | return b; |
| 111 | } |
| 112 | return function combine(elem) { |
| 113 | return a(elem) || b(elem); |
| 114 | }; |
| 115 | } |