traversal.js 3.5 KB

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