[79a0317] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | exports.compileGeneralSelector = void 0;
|
---|
| 4 | var attributes_1 = require("./attributes");
|
---|
| 5 | var pseudo_selectors_1 = require("./pseudo-selectors");
|
---|
| 6 | var css_what_1 = require("css-what");
|
---|
| 7 | /*
|
---|
| 8 | * All available rules
|
---|
| 9 | */
|
---|
| 10 | function compileGeneralSelector(next, selector, options, context, compileToken) {
|
---|
| 11 | var adapter = options.adapter, equals = options.equals;
|
---|
| 12 | switch (selector.type) {
|
---|
| 13 | case css_what_1.SelectorType.PseudoElement: {
|
---|
| 14 | throw new Error("Pseudo-elements are not supported by css-select");
|
---|
| 15 | }
|
---|
| 16 | case css_what_1.SelectorType.ColumnCombinator: {
|
---|
| 17 | throw new Error("Column combinators are not yet supported by css-select");
|
---|
| 18 | }
|
---|
| 19 | case css_what_1.SelectorType.Attribute: {
|
---|
| 20 | if (selector.namespace != null) {
|
---|
| 21 | throw new Error("Namespaced attributes are not yet supported by css-select");
|
---|
| 22 | }
|
---|
| 23 | if (!options.xmlMode || options.lowerCaseAttributeNames) {
|
---|
| 24 | selector.name = selector.name.toLowerCase();
|
---|
| 25 | }
|
---|
| 26 | return attributes_1.attributeRules[selector.action](next, selector, options);
|
---|
| 27 | }
|
---|
| 28 | case css_what_1.SelectorType.Pseudo: {
|
---|
| 29 | return (0, pseudo_selectors_1.compilePseudoSelector)(next, selector, options, context, compileToken);
|
---|
| 30 | }
|
---|
| 31 | // Tags
|
---|
| 32 | case css_what_1.SelectorType.Tag: {
|
---|
| 33 | if (selector.namespace != null) {
|
---|
| 34 | throw new Error("Namespaced tag names are not yet supported by css-select");
|
---|
| 35 | }
|
---|
| 36 | var name_1 = selector.name;
|
---|
| 37 | if (!options.xmlMode || options.lowerCaseTags) {
|
---|
| 38 | name_1 = name_1.toLowerCase();
|
---|
| 39 | }
|
---|
| 40 | return function tag(elem) {
|
---|
| 41 | return adapter.getName(elem) === name_1 && next(elem);
|
---|
| 42 | };
|
---|
| 43 | }
|
---|
| 44 | // Traversal
|
---|
| 45 | case css_what_1.SelectorType.Descendant: {
|
---|
| 46 | if (options.cacheResults === false ||
|
---|
| 47 | typeof WeakSet === "undefined") {
|
---|
| 48 | return function descendant(elem) {
|
---|
| 49 | var current = elem;
|
---|
| 50 | while ((current = adapter.getParent(current))) {
|
---|
| 51 | if (adapter.isTag(current) && next(current)) {
|
---|
| 52 | return true;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | return false;
|
---|
| 56 | };
|
---|
| 57 | }
|
---|
| 58 | // @ts-expect-error `ElementNode` is not extending object
|
---|
| 59 | var isFalseCache_1 = new WeakSet();
|
---|
| 60 | return function cachedDescendant(elem) {
|
---|
| 61 | var current = elem;
|
---|
| 62 | while ((current = adapter.getParent(current))) {
|
---|
| 63 | if (!isFalseCache_1.has(current)) {
|
---|
| 64 | if (adapter.isTag(current) && next(current)) {
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 | isFalseCache_1.add(current);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | return false;
|
---|
| 71 | };
|
---|
| 72 | }
|
---|
| 73 | case "_flexibleDescendant": {
|
---|
| 74 | // Include element itself, only used while querying an array
|
---|
| 75 | return function flexibleDescendant(elem) {
|
---|
| 76 | var current = elem;
|
---|
| 77 | do {
|
---|
| 78 | if (adapter.isTag(current) && next(current))
|
---|
| 79 | return true;
|
---|
| 80 | } while ((current = adapter.getParent(current)));
|
---|
| 81 | return false;
|
---|
| 82 | };
|
---|
| 83 | }
|
---|
| 84 | case css_what_1.SelectorType.Parent: {
|
---|
| 85 | return function parent(elem) {
|
---|
| 86 | return adapter
|
---|
| 87 | .getChildren(elem)
|
---|
| 88 | .some(function (elem) { return adapter.isTag(elem) && next(elem); });
|
---|
| 89 | };
|
---|
| 90 | }
|
---|
| 91 | case css_what_1.SelectorType.Child: {
|
---|
| 92 | return function child(elem) {
|
---|
| 93 | var parent = adapter.getParent(elem);
|
---|
| 94 | return parent != null && adapter.isTag(parent) && next(parent);
|
---|
| 95 | };
|
---|
| 96 | }
|
---|
| 97 | case css_what_1.SelectorType.Sibling: {
|
---|
| 98 | return function sibling(elem) {
|
---|
| 99 | var siblings = adapter.getSiblings(elem);
|
---|
| 100 | for (var i = 0; i < siblings.length; i++) {
|
---|
| 101 | var currentSibling = siblings[i];
|
---|
| 102 | if (equals(elem, currentSibling))
|
---|
| 103 | break;
|
---|
| 104 | if (adapter.isTag(currentSibling) && next(currentSibling)) {
|
---|
| 105 | return true;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | return false;
|
---|
| 109 | };
|
---|
| 110 | }
|
---|
| 111 | case css_what_1.SelectorType.Adjacent: {
|
---|
| 112 | if (adapter.prevElementSibling) {
|
---|
| 113 | return function adjacent(elem) {
|
---|
| 114 | var previous = adapter.prevElementSibling(elem);
|
---|
| 115 | return previous != null && next(previous);
|
---|
| 116 | };
|
---|
| 117 | }
|
---|
| 118 | return function adjacent(elem) {
|
---|
| 119 | var siblings = adapter.getSiblings(elem);
|
---|
| 120 | var lastElement;
|
---|
| 121 | for (var i = 0; i < siblings.length; i++) {
|
---|
| 122 | var currentSibling = siblings[i];
|
---|
| 123 | if (equals(elem, currentSibling))
|
---|
| 124 | break;
|
---|
| 125 | if (adapter.isTag(currentSibling)) {
|
---|
| 126 | lastElement = currentSibling;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | return !!lastElement && next(lastElement);
|
---|
| 130 | };
|
---|
| 131 | }
|
---|
| 132 | case css_what_1.SelectorType.Universal: {
|
---|
| 133 | if (selector.namespace != null && selector.namespace !== "*") {
|
---|
| 134 | throw new Error("Namespaced universal selectors are not yet supported by css-select");
|
---|
| 135 | }
|
---|
| 136 | return next;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | exports.compileGeneralSelector = compileGeneralSelector;
|
---|