general.js 5.9 KB

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