mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-30 19:16:39 +00:00
Merge "Rewrite ImmutableRange::findCommonAncestorContainer for speed"
This commit is contained in:
commit
e2f396fc99
|
@ -47,19 +47,34 @@ class ImmutableRange {
|
||||||
$ancestorsA = [];
|
$ancestorsA = [];
|
||||||
$ancestorsB = [];
|
$ancestorsB = [];
|
||||||
|
|
||||||
|
$parent = $a;
|
||||||
do {
|
do {
|
||||||
$ancestorsA[] = $a;
|
// While walking up the parents of $a we found $b is a parent of $a or even identical
|
||||||
} while ( ( $a = $a->parentNode ) );
|
if ( $parent === $b ) {
|
||||||
|
return $b;
|
||||||
|
}
|
||||||
|
$ancestorsA[] = $parent;
|
||||||
|
} while ( $parent = $parent->parentNode );
|
||||||
|
|
||||||
|
$parent = $b;
|
||||||
do {
|
do {
|
||||||
$ancestorsB[] = $b;
|
// While walking up the parents of $b we found $a is a parent of $b or even identical
|
||||||
} while ( ( $b = $b->parentNode ) );
|
if ( $parent === $a ) {
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
$ancestorsB[] = $parent;
|
||||||
|
} while ( $parent = $parent->parentNode );
|
||||||
|
|
||||||
$node = null;
|
$node = null;
|
||||||
while ( end( $ancestorsA ) && end( $ancestorsA ) === end( $ancestorsB ) ) {
|
// Start with the top-most (hopefully) identical root node, walk down, skip everything
|
||||||
$node = end( $ancestorsA );
|
// that's identical, and stop at the first mismatch
|
||||||
array_pop( $ancestorsA );
|
$indexA = count( $ancestorsA );
|
||||||
array_pop( $ancestorsB );
|
$indexB = count( $ancestorsB );
|
||||||
|
while ( $indexA-- && $indexB-- && $ancestorsA[$indexA] === $ancestorsB[$indexB] ) {
|
||||||
|
// Remember the last match closest to $a and $b
|
||||||
|
$node = $ancestorsA[$indexA];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !$node ) {
|
if ( !$node ) {
|
||||||
throw new DOMException( 'Nodes are not in the same document' );
|
throw new DOMException( 'Nodes are not in the same document' );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue