querying.js 5.4 KB

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