2 // Given an array of nodes, remove any member that is contained by another.
3 exports.removeSubsets = function(nodes) {
4 var idx = nodes.length, node, ancestor, replace;
6 // Check if each node (or one of its ancestors) is already contained in the
9 node = ancestor = nodes[idx];
11 // Temporarily remove the node under consideration
16 if (nodes.indexOf(ancestor) > -1) {
21 ancestor = ancestor.parent;
24 // If the node has been found to be unique, re-insert it.
33 // Source: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition
42 // Compare the position of one node against another node in any other document.
43 // The return value is a bitmask with the following values:
46 // > There is an ordering, document order, defined on all the nodes in the
47 // > document corresponding to the order in which the first character of the
48 // > XML representation of each node occurs in the XML representation of the
49 // > document after expansion of general entities. Thus, the document element
50 // > node will be the first node. Element nodes occur before their children.
51 // > Thus, document order orders element nodes in order of the occurrence of
52 // > their start-tag in the XML (after expansion of entities). The attribute
53 // > nodes of an element occur after the element and before its children. The
54 // > relative order of attribute nodes is implementation-dependent./
56 // http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order
58 // @argument {Node} nodaA The first node to use in the comparison
59 // @argument {Node} nodeB The second node to use in the comparison
61 // @return {Number} A bitmask describing the input nodes' relative position.
62 // See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for
63 // a description of these values.
64 var comparePos = exports.compareDocumentPosition = function(nodeA, nodeB) {
67 var current, sharedParent, siblings, aSibling, bSibling, idx;
69 if (nodeA === nodeB) {
75 aParents.unshift(current);
76 current = current.parent;
80 bParents.unshift(current);
81 current = current.parent;
85 while (aParents[idx] === bParents[idx]) {
90 return POSITION.DISCONNECTED;
93 sharedParent = aParents[idx - 1];
94 siblings = sharedParent.children;
95 aSibling = aParents[idx];
96 bSibling = bParents[idx];
98 if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) {
99 if (sharedParent === nodeB) {
100 return POSITION.FOLLOWING | POSITION.CONTAINED_BY;
102 return POSITION.FOLLOWING;
104 if (sharedParent === nodeA) {
105 return POSITION.PRECEDING | POSITION.CONTAINS;
107 return POSITION.PRECEDING;
111 // Sort an array of nodes based on their relative position in the document and
112 // remove any duplicate nodes. If the array contains nodes that do not belong
113 // to the same document, sort order is unspecified.
115 // @argument {Array} nodes Array of DOM nodes
117 // @returns {Array} collection of unique nodes, sorted in document order
118 exports.uniqueSort = function(nodes) {
119 var idx = nodes.length, node, position;
121 nodes = nodes.slice();
125 position = nodes.indexOf(node);
126 if (position > -1 && position < idx) {
127 nodes.splice(idx, 1);
130 nodes.sort(function(a, b) {
131 var relative = comparePos(a, b);
132 if (relative & POSITION.PRECEDING) {
134 } else if (relative & POSITION.FOLLOWING) {