traversal.js 2.9 KB

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