|
|
import { hasChildren } from "domhandler"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function removeSubsets(nodes) { |
|
|
let idx = nodes.length; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (--idx >= 0) { |
|
|
const node = nodes[idx]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (idx > 0 && nodes.lastIndexOf(node, idx - 1) >= 0) { |
|
|
nodes.splice(idx, 1); |
|
|
continue; |
|
|
} |
|
|
for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) { |
|
|
if (nodes.includes(ancestor)) { |
|
|
nodes.splice(idx, 1); |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
return nodes; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var DocumentPosition; |
|
|
(function (DocumentPosition) { |
|
|
DocumentPosition[DocumentPosition["DISCONNECTED"] = 1] = "DISCONNECTED"; |
|
|
DocumentPosition[DocumentPosition["PRECEDING"] = 2] = "PRECEDING"; |
|
|
DocumentPosition[DocumentPosition["FOLLOWING"] = 4] = "FOLLOWING"; |
|
|
DocumentPosition[DocumentPosition["CONTAINS"] = 8] = "CONTAINS"; |
|
|
DocumentPosition[DocumentPosition["CONTAINED_BY"] = 16] = "CONTAINED_BY"; |
|
|
})(DocumentPosition || (DocumentPosition = {})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function compareDocumentPosition(nodeA, nodeB) { |
|
|
const aParents = []; |
|
|
const bParents = []; |
|
|
if (nodeA === nodeB) { |
|
|
return 0; |
|
|
} |
|
|
let current = hasChildren(nodeA) ? nodeA : nodeA.parent; |
|
|
while (current) { |
|
|
aParents.unshift(current); |
|
|
current = current.parent; |
|
|
} |
|
|
current = hasChildren(nodeB) ? nodeB : nodeB.parent; |
|
|
while (current) { |
|
|
bParents.unshift(current); |
|
|
current = current.parent; |
|
|
} |
|
|
const maxIdx = Math.min(aParents.length, bParents.length); |
|
|
let idx = 0; |
|
|
while (idx < maxIdx && aParents[idx] === bParents[idx]) { |
|
|
idx++; |
|
|
} |
|
|
if (idx === 0) { |
|
|
return DocumentPosition.DISCONNECTED; |
|
|
} |
|
|
const sharedParent = aParents[idx - 1]; |
|
|
const siblings = sharedParent.children; |
|
|
const aSibling = aParents[idx]; |
|
|
const bSibling = bParents[idx]; |
|
|
if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) { |
|
|
if (sharedParent === nodeB) { |
|
|
return DocumentPosition.FOLLOWING | DocumentPosition.CONTAINED_BY; |
|
|
} |
|
|
return DocumentPosition.FOLLOWING; |
|
|
} |
|
|
if (sharedParent === nodeA) { |
|
|
return DocumentPosition.PRECEDING | DocumentPosition.CONTAINS; |
|
|
} |
|
|
return DocumentPosition.PRECEDING; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function uniqueSort(nodes) { |
|
|
nodes = nodes.filter((node, i, arr) => !arr.includes(node, i + 1)); |
|
|
nodes.sort((a, b) => { |
|
|
const relative = compareDocumentPosition(a, b); |
|
|
if (relative & DocumentPosition.PRECEDING) { |
|
|
return -1; |
|
|
} |
|
|
else if (relative & DocumentPosition.FOLLOWING) { |
|
|
return 1; |
|
|
} |
|
|
return 0; |
|
|
}); |
|
|
return nodes; |
|
|
} |
|
|
|