helpers.js 4.9 KB

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