traversal.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getChildren = getChildren;
  4. exports.getParent = getParent;
  5. exports.getSiblings = getSiblings;
  6. exports.getAttributeValue = getAttributeValue;
  7. exports.hasAttrib = hasAttrib;
  8. exports.getName = getName;
  9. exports.nextElementSibling = nextElementSibling;
  10. exports.prevElementSibling = prevElementSibling;
  11. var domhandler_1 = require("domhandler");
  12. /**
  13. * Get a node's children.
  14. *
  15. * @category Traversal
  16. * @param elem Node to get the children of.
  17. * @returns `elem`'s children, or an empty array.
  18. */
  19. function getChildren(elem) {
  20. return (0, domhandler_1.hasChildren)(elem) ? elem.children : [];
  21. }
  22. /**
  23. * Get a node's parent.
  24. *
  25. * @category Traversal
  26. * @param elem Node to get the parent of.
  27. * @returns `elem`'s parent node, or `null` if `elem` is a root node.
  28. */
  29. function getParent(elem) {
  30. return elem.parent || null;
  31. }
  32. /**
  33. * Gets an elements siblings, including the element itself.
  34. *
  35. * Attempts to get the children through the element's parent first. If we don't
  36. * have a parent (the element is a root node), we walk the element's `prev` &
  37. * `next` to get all remaining nodes.
  38. *
  39. * @category Traversal
  40. * @param elem Element to get the siblings of.
  41. * @returns `elem`'s siblings, including `elem`.
  42. */
  43. function getSiblings(elem) {
  44. var _a, _b;
  45. var parent = getParent(elem);
  46. if (parent != null)
  47. return getChildren(parent);
  48. var siblings = [elem];
  49. var prev = elem.prev, next = elem.next;
  50. while (prev != null) {
  51. siblings.unshift(prev);
  52. (_a = prev, prev = _a.prev);
  53. }
  54. while (next != null) {
  55. siblings.push(next);
  56. (_b = next, next = _b.next);
  57. }
  58. return siblings;
  59. }
  60. /**
  61. * Gets an attribute from an element.
  62. *
  63. * @category Traversal
  64. * @param elem Element to check.
  65. * @param name Attribute name to retrieve.
  66. * @returns The element's attribute value, or `undefined`.
  67. */
  68. function getAttributeValue(elem, name) {
  69. var _a;
  70. return (_a = elem.attribs) === null || _a === void 0 ? void 0 : _a[name];
  71. }
  72. /**
  73. * Checks whether an element has an attribute.
  74. *
  75. * @category Traversal
  76. * @param elem Element to check.
  77. * @param name Attribute name to look for.
  78. * @returns Returns whether `elem` has the attribute `name`.
  79. */
  80. function hasAttrib(elem, name) {
  81. return (elem.attribs != null &&
  82. Object.prototype.hasOwnProperty.call(elem.attribs, name) &&
  83. elem.attribs[name] != null);
  84. }
  85. /**
  86. * Get the tag name of an element.
  87. *
  88. * @category Traversal
  89. * @param elem The element to get the name for.
  90. * @returns The tag name of `elem`.
  91. */
  92. function getName(elem) {
  93. return elem.name;
  94. }
  95. /**
  96. * Returns the next element sibling of a node.
  97. *
  98. * @category Traversal
  99. * @param elem The element to get the next sibling of.
  100. * @returns `elem`'s next sibling that is a tag, or `null` if there is no next
  101. * sibling.
  102. */
  103. function nextElementSibling(elem) {
  104. var _a;
  105. var next = elem.next;
  106. while (next !== null && !(0, domhandler_1.isTag)(next))
  107. (_a = next, next = _a.next);
  108. return next;
  109. }
  110. /**
  111. * Returns the previous element sibling of a node.
  112. *
  113. * @category Traversal
  114. * @param elem The element to get the previous sibling of.
  115. * @returns `elem`'s previous sibling that is a tag, or `null` if there is no
  116. * previous sibling.
  117. */
  118. function prevElementSibling(elem) {
  119. var _a;
  120. var prev = elem.prev;
  121. while (prev !== null && !(0, domhandler_1.isTag)(prev))
  122. (_a = prev, prev = _a.prev);
  123. return prev;
  124. }
  125. //# sourceMappingURL=traversal.js.map