source: trip-planner-front/node_modules/css-select/lib/compile.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.4 KB
Line 
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.compileToken = exports.compileUnsafe = exports.compile = void 0;
7var css_what_1 = require("css-what");
8var boolbase_1 = require("boolbase");
9var sort_1 = __importDefault(require("./sort"));
10var procedure_1 = require("./procedure");
11var general_1 = require("./general");
12var 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 */
20function compile(selector, options, context) {
21 var next = compileUnsafe(selector, options, context);
22 return subselects_1.ensureIsTag(next, options.adapter);
23}
24exports.compile = compile;
25function compileUnsafe(selector, options, context) {
26 var token = typeof selector === "string" ? css_what_1.parse(selector, options) : selector;
27 return compileToken(token, options, context);
28}
29exports.compileUnsafe = compileUnsafe;
30function 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}
36var DESCENDANT_TOKEN = { type: "descendant" };
37var FLEXIBLE_DESCENDANT_TOKEN = {
38 type: "_flexibleDescendant",
39};
40var 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 */
45function 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.isTag(e) && 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}
66function 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}
96exports.compileToken = compileToken;
97function 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}
105function 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}
Note: See TracBrowser for help on using the repository browser.