querying.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { isTag, hasChildren } from "domhandler";
  2. /**
  3. * Search a node and its children for nodes passing a test function. If `node` is not an array, it will be wrapped in one.
  4. *
  5. * @category Querying
  6. * @param test Function to test nodes on.
  7. * @param node Node to search. Will be included in the result set if it matches.
  8. * @param recurse Also consider child nodes.
  9. * @param limit Maximum number of nodes to return.
  10. * @returns All nodes passing `test`.
  11. */
  12. export function filter(test, node, recurse = true, limit = Infinity) {
  13. return find(test, Array.isArray(node) ? node : [node], recurse, limit);
  14. }
  15. /**
  16. * Search an array of nodes and their children for nodes passing a test function.
  17. *
  18. * @category Querying
  19. * @param test Function to test nodes on.
  20. * @param nodes Array of nodes to search.
  21. * @param recurse Also consider child nodes.
  22. * @param limit Maximum number of nodes to return.
  23. * @returns All nodes passing `test`.
  24. */
  25. export function find(test, nodes, recurse, limit) {
  26. const result = [];
  27. /** Stack of the arrays we are looking at. */
  28. const nodeStack = [Array.isArray(nodes) ? nodes : [nodes]];
  29. /** Stack of the indices within the arrays. */
  30. const indexStack = [0];
  31. for (;;) {
  32. // First, check if the current array has any more elements to look at.
  33. if (indexStack[0] >= nodeStack[0].length) {
  34. // If we have no more arrays to look at, we are done.
  35. if (indexStack.length === 1) {
  36. return result;
  37. }
  38. // Otherwise, remove the current array from the stack.
  39. nodeStack.shift();
  40. indexStack.shift();
  41. // Loop back to the start to continue with the next array.
  42. continue;
  43. }
  44. const elem = nodeStack[0][indexStack[0]++];
  45. if (test(elem)) {
  46. result.push(elem);
  47. if (--limit <= 0)
  48. return result;
  49. }
  50. if (recurse && hasChildren(elem) && elem.children.length > 0) {
  51. /*
  52. * Add the children to the stack. We are depth-first, so this is
  53. * the next array we look at.
  54. */
  55. indexStack.unshift(0);
  56. nodeStack.unshift(elem.children);
  57. }
  58. }
  59. }
  60. /**
  61. * Finds the first element inside of an array that matches a test function. This is an alias for `Array.prototype.find`.
  62. *
  63. * @category Querying
  64. * @param test Function to test nodes on.
  65. * @param nodes Array of nodes to search.
  66. * @returns The first node in the array that passes `test`.
  67. * @deprecated Use `Array.prototype.find` directly.
  68. */
  69. export function findOneChild(test, nodes) {
  70. return nodes.find(test);
  71. }
  72. /**
  73. * Finds one element in a tree that passes a test.
  74. *
  75. * @category Querying
  76. * @param test Function to test nodes on.
  77. * @param nodes Node or array of nodes to search.
  78. * @param recurse Also consider child nodes.
  79. * @returns The first node that passes `test`.
  80. */
  81. export function findOne(test, nodes, recurse = true) {
  82. const searchedNodes = Array.isArray(nodes) ? nodes : [nodes];
  83. for (let i = 0; i < searchedNodes.length; i++) {
  84. const node = searchedNodes[i];
  85. if (isTag(node) && test(node)) {
  86. return node;
  87. }
  88. if (recurse && hasChildren(node) && node.children.length > 0) {
  89. return findOne(test, node.children, true);
  90. }
  91. }
  92. return null;
  93. }
  94. /**
  95. * Checks if a tree of nodes contains at least one node passing a test.
  96. *
  97. * @category Querying
  98. * @param test Function to test nodes on.
  99. * @param nodes Array of nodes to search.
  100. * @returns Whether a tree of nodes contains at least one node passing the test.
  101. */
  102. export function existsOne(test, nodes) {
  103. return (Array.isArray(nodes) ? nodes : [nodes]).some((node) => (isTag(node) && test(node)) ||
  104. (hasChildren(node) && existsOne(test, node.children)));
  105. }
  106. /**
  107. * Search an array of nodes and their children for elements passing a test function.
  108. *
  109. * Same as `find`, but limited to elements and with less options, leading to reduced complexity.
  110. *
  111. * @category Querying
  112. * @param test Function to test nodes on.
  113. * @param nodes Array of nodes to search.
  114. * @returns All nodes passing `test`.
  115. */
  116. export function findAll(test, nodes) {
  117. const result = [];
  118. const nodeStack = [Array.isArray(nodes) ? nodes : [nodes]];
  119. const indexStack = [0];
  120. for (;;) {
  121. if (indexStack[0] >= nodeStack[0].length) {
  122. if (nodeStack.length === 1) {
  123. return result;
  124. }
  125. // Otherwise, remove the current array from the stack.
  126. nodeStack.shift();
  127. indexStack.shift();
  128. // Loop back to the start to continue with the next array.
  129. continue;
  130. }
  131. const elem = nodeStack[0][indexStack[0]++];
  132. if (isTag(elem) && test(elem))
  133. result.push(elem);
  134. if (hasChildren(elem) && elem.children.length > 0) {
  135. indexStack.unshift(0);
  136. nodeStack.unshift(elem.children);
  137. }
  138. }
  139. }
  140. //# sourceMappingURL=querying.js.map