31 lines
606 B
JavaScript
31 lines
606 B
JavaScript
|
|
export default function(end) {
|
||
|
|
var start = this,
|
||
|
|
ancestor = leastCommonAncestor(start, end),
|
||
|
|
nodes = [start];
|
||
|
|
while (start !== ancestor) {
|
||
|
|
start = start.parent;
|
||
|
|
nodes.push(start);
|
||
|
|
}
|
||
|
|
var k = nodes.length;
|
||
|
|
while (end !== ancestor) {
|
||
|
|
nodes.splice(k, 0, end);
|
||
|
|
end = end.parent;
|
||
|
|
}
|
||
|
|
return nodes;
|
||
|
|
}
|
||
|
|
|
||
|
|
function leastCommonAncestor(a, b) {
|
||
|
|
if (a === b) return a;
|
||
|
|
var aNodes = a.ancestors(),
|
||
|
|
bNodes = b.ancestors(),
|
||
|
|
c = null;
|
||
|
|
a = aNodes.pop();
|
||
|
|
b = bNodes.pop();
|
||
|
|
while (a === b) {
|
||
|
|
c = a;
|
||
|
|
a = aNodes.pop();
|
||
|
|
b = bNodes.pop();
|
||
|
|
}
|
||
|
|
return c;
|
||
|
|
}
|