helpers.js 5.2 KB

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