helpers.js 5.2 KB

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