UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.compileGeneralSelector = void 0;
4var attributes_1 = require("./attributes");
5var pseudo_selectors_1 = require("./pseudo-selectors");
6/*
7 * All available rules
8 */
9function compileGeneralSelector(next, selector, options, context, compileToken) {
10 var adapter = options.adapter, equals = options.equals;
11 switch (selector.type) {
12 case "pseudo-element":
13 throw new Error("Pseudo-elements are not supported by css-select");
14 case "attribute":
15 return attributes_1.attributeRules[selector.action](next, selector, options);
16 case "pseudo":
17 return (0, pseudo_selectors_1.compilePseudoSelector)(next, selector, options, context, compileToken);
18 // Tags
19 case "tag":
20 return function tag(elem) {
21 return adapter.getName(elem) === selector.name && next(elem);
22 };
23 // Traversal
24 case "descendant":
25 if (options.cacheResults === false ||
26 typeof WeakSet === "undefined") {
27 return function descendant(elem) {
28 var current = elem;
29 while ((current = adapter.getParent(current))) {
30 if (adapter.isTag(current) && next(current)) {
31 return true;
32 }
33 }
34 return false;
35 };
36 }
37 // @ts-expect-error `ElementNode` is not extending object
38 // eslint-disable-next-line no-case-declarations
39 var isFalseCache_1 = new WeakSet();
40 return function cachedDescendant(elem) {
41 var current = elem;
42 while ((current = adapter.getParent(current))) {
43 if (!isFalseCache_1.has(current)) {
44 if (adapter.isTag(current) && next(current)) {
45 return true;
46 }
47 isFalseCache_1.add(current);
48 }
49 }
50 return false;
51 };
52 case "_flexibleDescendant":
53 // Include element itself, only used while querying an array
54 return function flexibleDescendant(elem) {
55 var current = elem;
56 do {
57 if (adapter.isTag(current) && next(current))
58 return true;
59 } while ((current = adapter.getParent(current)));
60 return false;
61 };
62 case "parent":
63 return function parent(elem) {
64 return adapter
65 .getChildren(elem)
66 .some(function (elem) { return adapter.isTag(elem) && next(elem); });
67 };
68 case "child":
69 return function child(elem) {
70 var parent = adapter.getParent(elem);
71 return parent != null && adapter.isTag(parent) && next(parent);
72 };
73 case "sibling":
74 return function sibling(elem) {
75 var siblings = adapter.getSiblings(elem);
76 for (var i = 0; i < siblings.length; i++) {
77 var currentSibling = siblings[i];
78 if (equals(elem, currentSibling))
79 break;
80 if (adapter.isTag(currentSibling) && next(currentSibling)) {
81 return true;
82 }
83 }
84 return false;
85 };
86 case "adjacent":
87 return function adjacent(elem) {
88 var siblings = adapter.getSiblings(elem);
89 var lastElement;
90 for (var i = 0; i < siblings.length; i++) {
91 var currentSibling = siblings[i];
92 if (equals(elem, currentSibling))
93 break;
94 if (adapter.isTag(currentSibling)) {
95 lastElement = currentSibling;
96 }
97 }
98 return !!lastElement && next(lastElement);
99 };
100 case "universal":
101 return next;
102 }
103}
104exports.compileGeneralSelector = compileGeneralSelector;