helpers.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { hasChildren } from "domhandler";
  2. /**
  3. * Given an array of nodes, remove any member that is contained by another
  4. * member.
  5. *
  6. * @category Helpers
  7. * @param nodes Nodes to filter.
  8. * @returns Remaining nodes that aren't contained by other nodes.
  9. */
  10. export function removeSubsets(nodes) {
  11. let idx = nodes.length;
  12. /*
  13. * Check if each node (or one of its ancestors) is already contained in the
  14. * array.
  15. */
  16. while (--idx >= 0) {
  17. const node = nodes[idx];
  18. /*
  19. * Remove the node if it is not unique.
  20. * We are going through the array from the end, so we only
  21. * have to check nodes that preceed the node under consideration in the array.
  22. */
  23. if (idx > 0 && nodes.lastIndexOf(node, idx - 1) >= 0) {
  24. nodes.splice(idx, 1);
  25. continue;
  26. }
  27. for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
  28. if (nodes.includes(ancestor)) {
  29. nodes.splice(idx, 1);
  30. break;
  31. }
  32. }
  33. }
  34. return nodes;
  35. }
  36. /**
  37. * @category Helpers
  38. * @see {@link http://dom.spec.whatwg.org/#dom-node-comparedocumentposition}
  39. */
  40. export var DocumentPosition;
  41. (function (DocumentPosition) {
  42. DocumentPosition[DocumentPosition["DISCONNECTED"] = 1] = "DISCONNECTED";
  43. DocumentPosition[DocumentPosition["PRECEDING"] = 2] = "PRECEDING";
  44. DocumentPosition[DocumentPosition["FOLLOWING"] = 4] = "FOLLOWING";
  45. DocumentPosition[DocumentPosition["CONTAINS"] = 8] = "CONTAINS";
  46. DocumentPosition[DocumentPosition["CONTAINED_BY"] = 16] = "CONTAINED_BY";
  47. })(DocumentPosition || (DocumentPosition = {}));
  48. /**
  49. * Compare the position of one node against another node in any other document,
  50. * returning a bitmask with the values from {@link DocumentPosition}.
  51. *
  52. * Document order:
  53. * > There is an ordering, document order, defined on all the nodes in the
  54. * > document corresponding to the order in which the first character of the
  55. * > XML representation of each node occurs in the XML representation of the
  56. * > document after expansion of general entities. Thus, the document element
  57. * > node will be the first node. Element nodes occur before their children.
  58. * > Thus, document order orders element nodes in order of the occurrence of
  59. * > their start-tag in the XML (after expansion of entities). The attribute
  60. * > nodes of an element occur after the element and before its children. The
  61. * > relative order of attribute nodes is implementation-dependent.
  62. *
  63. * Source:
  64. * http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order
  65. *
  66. * @category Helpers
  67. * @param nodeA The first node to use in the comparison
  68. * @param nodeB The second node to use in the comparison
  69. * @returns A bitmask describing the input nodes' relative position.
  70. *
  71. * See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for
  72. * a description of these values.
  73. */
  74. export function compareDocumentPosition(nodeA, nodeB) {
  75. const aParents = [];
  76. const bParents = [];
  77. if (nodeA === nodeB) {
  78. return 0;
  79. }
  80. let current = hasChildren(nodeA) ? nodeA : nodeA.parent;
  81. while (current) {
  82. aParents.unshift(current);
  83. current = current.parent;
  84. }
  85. current = hasChildren(nodeB) ? nodeB : nodeB.parent;
  86. while (current) {
  87. bParents.unshift(current);
  88. current = current.parent;
  89. }
  90. const maxIdx = Math.min(aParents.length, bParents.length);
  91. let idx = 0;
  92. while (idx < maxIdx && aParents[idx] === bParents[idx]) {
  93. idx++;
  94. }
  95. if (idx === 0) {
  96. return DocumentPosition.DISCONNECTED;
  97. }
  98. const sharedParent = aParents[idx - 1];
  99. const siblings = sharedParent.children;
  100. const aSibling = aParents[idx];
  101. const bSibling = bParents[idx];
  102. if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) {
  103. if (sharedParent === nodeB) {
  104. return DocumentPosition.FOLLOWING | DocumentPosition.CONTAINED_BY;
  105. }
  106. return DocumentPosition.FOLLOWING;
  107. }
  108. if (sharedParent === nodeA) {
  109. return DocumentPosition.PRECEDING | DocumentPosition.CONTAINS;
  110. }
  111. return DocumentPosition.PRECEDING;
  112. }
  113. /**
  114. * Sort an array of nodes based on their relative position in the document,
  115. * removing any duplicate nodes. If the array contains nodes that do not belong
  116. * to the same document, sort order is unspecified.
  117. *
  118. * @category Helpers
  119. * @param nodes Array of DOM nodes.
  120. * @returns Collection of unique nodes, sorted in document order.
  121. */
  122. export function uniqueSort(nodes) {
  123. nodes = nodes.filter((node, i, arr) => !arr.includes(node, i + 1));
  124. nodes.sort((a, b) => {
  125. const relative = compareDocumentPosition(a, b);
  126. if (relative & DocumentPosition.PRECEDING) {
  127. return -1;
  128. }
  129. else if (relative & DocumentPosition.FOLLOWING) {
  130. return 1;
  131. }
  132. return 0;
  133. });
  134. return nodes;
  135. }
  136. //# sourceMappingURL=helpers.js.map