querying.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 = [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. let elem = null;
  83. for (let i = 0; i < nodes.length && !elem; i++) {
  84. const node = nodes[i];
  85. if (!isTag(node)) {
  86. continue;
  87. }
  88. else if (test(node)) {
  89. elem = node;
  90. }
  91. else if (recurse && node.children.length > 0) {
  92. elem = findOne(test, node.children, true);
  93. }
  94. }
  95. return elem;
  96. }
  97. /**
  98. * Checks if a tree of nodes contains at least one node passing a test.
  99. *
  100. * @category Querying
  101. * @param test Function to test nodes on.
  102. * @param nodes Array of nodes to search.
  103. * @returns Whether a tree of nodes contains at least one node passing the test.
  104. */
  105. export function existsOne(test, nodes) {
  106. return nodes.some((checked) => isTag(checked) &&
  107. (test(checked) || existsOne(test, checked.children)));
  108. }
  109. /**
  110. * Search an array of nodes and their children for elements passing a test function.
  111. *
  112. * Same as `find`, but limited to elements and with less options, leading to reduced complexity.
  113. *
  114. * @category Querying
  115. * @param test Function to test nodes on.
  116. * @param nodes Array of nodes to search.
  117. * @returns All nodes passing `test`.
  118. */
  119. export function findAll(test, nodes) {
  120. const result = [];
  121. const nodeStack = [nodes];
  122. const indexStack = [0];
  123. for (;;) {
  124. if (indexStack[0] >= nodeStack[0].length) {
  125. if (nodeStack.length === 1) {
  126. return result;
  127. }
  128. // Otherwise, remove the current array from the stack.
  129. nodeStack.shift();
  130. indexStack.shift();
  131. // Loop back to the start to continue with the next array.
  132. continue;
  133. }
  134. const elem = nodeStack[0][indexStack[0]++];
  135. if (!isTag(elem))
  136. continue;
  137. if (test(elem))
  138. result.push(elem);
  139. if (elem.children.length > 0) {
  140. indexStack.unshift(0);
  141. nodeStack.unshift(elem.children);
  142. }
  143. }
  144. }
  145. //# sourceMappingURL=querying.js.map