querying.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.findAll = findAll;
  4. exports.findOne = findOne;
  5. exports.getNextSiblings = getNextSiblings;
  6. exports.getElementParent = getElementParent;
  7. /**
  8. * Find all elements matching the query. If not in XML mode, the query will ignore
  9. * the contents of `<template>` elements.
  10. *
  11. * @param query - Function that returns true if the element matches the query.
  12. * @param elems - Nodes to query. If a node is an element, its children will be queried.
  13. * @param options - Options for querying the document.
  14. * @returns All matching elements.
  15. */
  16. function findAll(query, elems, options) {
  17. var adapter = options.adapter, _a = options.xmlMode, xmlMode = _a === void 0 ? false : _a;
  18. var result = [];
  19. /** Stack of the arrays we are looking at. */
  20. var nodeStack = [elems];
  21. /** Stack of the indices within the arrays. */
  22. var indexStack = [0];
  23. for (;;) {
  24. // First, check if the current array has any more elements to look at.
  25. if (indexStack[0] >= nodeStack[0].length) {
  26. // If we have no more arrays to look at, we are done.
  27. if (nodeStack.length === 1) {
  28. return result;
  29. }
  30. nodeStack.shift();
  31. indexStack.shift();
  32. // Loop back to the start to continue with the next array.
  33. continue;
  34. }
  35. var elem = nodeStack[0][indexStack[0]++];
  36. if (!adapter.isTag(elem))
  37. continue;
  38. if (query(elem))
  39. result.push(elem);
  40. if (xmlMode || adapter.getName(elem) !== "template") {
  41. /*
  42. * Add the children to the stack. We are depth-first, so this is
  43. * the next array we look at.
  44. */
  45. var children = adapter.getChildren(elem);
  46. if (children.length > 0) {
  47. nodeStack.unshift(children);
  48. indexStack.unshift(0);
  49. }
  50. }
  51. }
  52. }
  53. /**
  54. * Find the first element matching the query. If not in XML mode, the query will ignore
  55. * the contents of `<template>` elements.
  56. *
  57. * @param query - Function that returns true if the element matches the query.
  58. * @param elems - Nodes to query. If a node is an element, its children will be queried.
  59. * @param options - Options for querying the document.
  60. * @returns The first matching element, or null if there was no match.
  61. */
  62. function findOne(query, elems, options) {
  63. var adapter = options.adapter, _a = options.xmlMode, xmlMode = _a === void 0 ? false : _a;
  64. /** Stack of the arrays we are looking at. */
  65. var nodeStack = [elems];
  66. /** Stack of the indices within the arrays. */
  67. var indexStack = [0];
  68. for (;;) {
  69. // First, check if the current array has any more elements to look at.
  70. if (indexStack[0] >= nodeStack[0].length) {
  71. // If we have no more arrays to look at, we are done.
  72. if (nodeStack.length === 1) {
  73. return null;
  74. }
  75. nodeStack.shift();
  76. indexStack.shift();
  77. // Loop back to the start to continue with the next array.
  78. continue;
  79. }
  80. var elem = nodeStack[0][indexStack[0]++];
  81. if (!adapter.isTag(elem))
  82. continue;
  83. if (query(elem))
  84. return elem;
  85. if (xmlMode || adapter.getName(elem) !== "template") {
  86. /*
  87. * Add the children to the stack. We are depth-first, so this is
  88. * the next array we look at.
  89. */
  90. var children = adapter.getChildren(elem);
  91. if (children.length > 0) {
  92. nodeStack.unshift(children);
  93. indexStack.unshift(0);
  94. }
  95. }
  96. }
  97. }
  98. function getNextSiblings(elem, adapter) {
  99. var siblings = adapter.getSiblings(elem);
  100. if (siblings.length <= 1)
  101. return [];
  102. var elemIndex = siblings.indexOf(elem);
  103. if (elemIndex < 0 || elemIndex === siblings.length - 1)
  104. return [];
  105. return siblings.slice(elemIndex + 1).filter(adapter.isTag);
  106. }
  107. function getElementParent(node, adapter) {
  108. var parent = adapter.getParent(node);
  109. return parent != null && adapter.isTag(parent) ? parent : null;
  110. }
  111. //# sourceMappingURL=querying.js.map