source: imaps-frontend/node_modules/css-select/lib/compile.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • 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 (0, subselects_1.ensureIsTag)(next, options.adapter);
23}
24exports.compile = compile;
25function compileUnsafe(selector, options, context) {
26 var token = typeof selector === "string" ? (0, css_what_1.parse)(selector) : 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: css_what_1.SelectorType.Descendant };
37var FLEXIBLE_DESCENDANT_TOKEN = {
38 type: "_flexibleDescendant",
39};
40var 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 */
49function 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}
70function 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}
100exports.compileToken = compileToken;
101function 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}
109function 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}
Note: See TracBrowser for help on using the repository browser.