helpers.js 5.1 KB

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