throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
}
}
Node endPointA;
Node endPointB;
int offsetA;
int offsetB;
if (how == START_TO_START) {
endPointA = sourceRange.getStartContainer();
endPointB = fStartContainer;
offsetA = sourceRange.getStartOffset();
offsetB = fStartOffset;
} else
if (how == START_TO_END) {
endPointA = sourceRange.getStartContainer();
endPointB = fEndContainer;
offsetA = sourceRange.getStartOffset();
offsetB = fEndOffset;
} else
if (how == END_TO_START) {
endPointA = sourceRange.getEndContainer();
endPointB = fStartContainer;
offsetA = sourceRange.getEndOffset();
offsetB = fStartOffset;
} else {
endPointA = sourceRange.getEndContainer();
endPointB = fEndContainer;
offsetA = sourceRange.getEndOffset();
offsetB = fEndOffset;
}
// The DOM Spec outlines four cases that need to be tested
// to compare two range boundary points:
// case 1: same container
// case 2: Child C of container A is ancestor of B
// case 3: Child C of container B is ancestor of A
// case 4: preorder traversal of context tree.
// case 1: same container
if (endPointA == endPointB) {
if (offsetA < offsetB) return 1;
if (offsetA == offsetB) return 0;
return -1;
}
// case 2: Child C of container A is ancestor of B
// This can be quickly tested by walking the parent chain of B
for ( Node c = endPointB, p = c.getParentNode();
p != null;
c = p, p = p.getParentNode())
{
if (p == endPointA) {
int index = indexOf(c, endPointA);
if (offsetA <= index) return 1;
return -1;
}
}
// case 3: Child C of container B is ancestor of A
// This can be quickly tested by walking the parent chain of A
for ( Node c = endPointA, p = c.getParentNode();
p != null;
c = p, p = p.getParentNode())
{
if (p == endPointB) {
int index = indexOf(c, endPointB);
if (index < offsetB) return 1;
return -1;
}
}
// case 4: preorder traversal of context tree.
// Instead of literally walking the context tree in pre-order,
// we use relative node depth walking which is usually faster
int depthDiff = 0;
for ( Node n = endPointA; n != null; n = n.getParentNode() )
depthDiff++;
for ( Node n = endPointB; n != null; n = n.getParentNode() )
depthDiff--;
while (depthDiff > 0) {
endPointA = endPointA.getParentNode();
depthDiff--;
}
while (depthDiff < 0) {
endPointB = endPointB.getParentNode();
depthDiff++;
}
for (Node pA = endPointA.getParentNode(),
pB = endPointB.getParentNode();
pA != pB;
pA = pA.getParentNode(), pB = pB.getParentNode() )
{
endPointA = pA;
endPointB = pB;