traversal.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. var emptyArray = [];
  6. /**
  7. * Get a node's children.
  8. *
  9. * @param elem Node to get the children of.
  10. * @returns `elem`'s children, or an empty array.
  11. */
  12. function getChildren(elem) {
  13. var _a;
  14. return (_a = elem.children) !== null && _a !== void 0 ? _a : emptyArray;
  15. }
  16. exports.getChildren = getChildren;
  17. /**
  18. * Get a node's parent.
  19. *
  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.
  31. * If we don't have a parent (the element is a root node),
  32. * we walk the element's `prev` & `next` to get all remaining nodes.
  33. *
  34. * @param elem Element to get the siblings of.
  35. * @returns `elem`'s siblings.
  36. */
  37. function getSiblings(elem) {
  38. var _a, _b;
  39. var parent = getParent(elem);
  40. if (parent != null)
  41. return getChildren(parent);
  42. var siblings = [elem];
  43. var prev = elem.prev, next = elem.next;
  44. while (prev != null) {
  45. siblings.unshift(prev);
  46. (_a = prev, prev = _a.prev);
  47. }
  48. while (next != null) {
  49. siblings.push(next);
  50. (_b = next, next = _b.next);
  51. }
  52. return siblings;
  53. }
  54. exports.getSiblings = getSiblings;
  55. /**
  56. * Gets an attribute from an element.
  57. *
  58. * @param elem Element to check.
  59. * @param name Attribute name to retrieve.
  60. * @returns The element's attribute value, or `undefined`.
  61. */
  62. function getAttributeValue(elem, name) {
  63. var _a;
  64. return (_a = elem.attribs) === null || _a === void 0 ? void 0 : _a[name];
  65. }
  66. exports.getAttributeValue = getAttributeValue;
  67. /**
  68. * Checks whether an element has an attribute.
  69. *
  70. * @param elem Element to check.
  71. * @param name Attribute name to look for.
  72. * @returns Returns whether `elem` has the attribute `name`.
  73. */
  74. function hasAttrib(elem, name) {
  75. return (elem.attribs != null &&
  76. Object.prototype.hasOwnProperty.call(elem.attribs, name) &&
  77. elem.attribs[name] != null);
  78. }
  79. exports.hasAttrib = hasAttrib;
  80. /**
  81. * Get the tag name of an element.
  82. *
  83. * @param elem The element to get the name for.
  84. * @returns The tag name of `elem`.
  85. */
  86. function getName(elem) {
  87. return elem.name;
  88. }
  89. exports.getName = getName;
  90. /**
  91. * Returns the next element sibling of a node.
  92. *
  93. * @param elem The element to get the next sibling of.
  94. * @returns `elem`'s next sibling that is a tag.
  95. */
  96. function nextElementSibling(elem) {
  97. var _a;
  98. var next = elem.next;
  99. while (next !== null && !(0, domhandler_1.isTag)(next))
  100. (_a = next, next = _a.next);
  101. return next;
  102. }
  103. exports.nextElementSibling = nextElementSibling;
  104. /**
  105. * Returns the previous element sibling of a node.
  106. *
  107. * @param elem The element to get the previous sibling of.
  108. * @returns `elem`'s previous sibling that is a tag.
  109. */
  110. function prevElementSibling(elem) {
  111. var _a;
  112. var prev = elem.prev;
  113. while (prev !== null && !(0, domhandler_1.isTag)(prev))
  114. (_a = prev, prev = _a.prev);
  115. return prev;
  116. }
  117. exports.prevElementSibling = prevElementSibling;