You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

4 years ago
  1. var attributes = require("./attributes.js");
  2. var Pseudos = require("./pseudos");
  3. /*
  4. all available rules
  5. */
  6. module.exports = {
  7. __proto__: null,
  8. attribute: attributes.compile,
  9. pseudo: Pseudos.compile,
  10. //tags
  11. tag: function(next, data, options) {
  12. var name = data.name;
  13. var adapter = options.adapter;
  14. return function tag(elem) {
  15. return adapter.getName(elem) === name && next(elem);
  16. };
  17. },
  18. //traversal
  19. descendant: function(next, data, options) {
  20. // eslint-disable-next-line no-undef
  21. var isFalseCache = typeof WeakSet !== "undefined" ? new WeakSet() : null;
  22. var adapter = options.adapter;
  23. return function descendant(elem) {
  24. var found = false;
  25. while (!found && (elem = adapter.getParent(elem))) {
  26. if (!isFalseCache || !isFalseCache.has(elem)) {
  27. found = next(elem);
  28. if (!found && isFalseCache) {
  29. isFalseCache.add(elem);
  30. }
  31. }
  32. }
  33. return found;
  34. };
  35. },
  36. _flexibleDescendant: function(next, data, options) {
  37. var adapter = options.adapter;
  38. // Include element itself, only used while querying an array
  39. return function descendant(elem) {
  40. var found = next(elem);
  41. while (!found && (elem = adapter.getParent(elem))) {
  42. found = next(elem);
  43. }
  44. return found;
  45. };
  46. },
  47. parent: function(next, data, options) {
  48. if (options && options.strict) {
  49. throw new Error("Parent selector isn't part of CSS3");
  50. }
  51. var adapter = options.adapter;
  52. return function parent(elem) {
  53. return adapter.getChildren(elem).some(test);
  54. };
  55. function test(elem) {
  56. return adapter.isTag(elem) && next(elem);
  57. }
  58. },
  59. child: function(next, data, options) {
  60. var adapter = options.adapter;
  61. return function child(elem) {
  62. var parent = adapter.getParent(elem);
  63. return !!parent && next(parent);
  64. };
  65. },
  66. sibling: function(next, data, options) {
  67. var adapter = options.adapter;
  68. return function sibling(elem) {
  69. var siblings = adapter.getSiblings(elem);
  70. for (var i = 0; i < siblings.length; i++) {
  71. if (adapter.isTag(siblings[i])) {
  72. if (siblings[i] === elem) break;
  73. if (next(siblings[i])) return true;
  74. }
  75. }
  76. return false;
  77. };
  78. },
  79. adjacent: function(next, data, options) {
  80. var adapter = options.adapter;
  81. return function adjacent(elem) {
  82. var siblings = adapter.getSiblings(elem),
  83. lastElement;
  84. for (var i = 0; i < siblings.length; i++) {
  85. if (adapter.isTag(siblings[i])) {
  86. if (siblings[i] === elem) break;
  87. lastElement = siblings[i];
  88. }
  89. }
  90. return !!lastElement && next(lastElement);
  91. };
  92. },
  93. universal: function(next) {
  94. return next;
  95. }
  96. };