// Compare two positions, taking into account the possibility that one or both // could be inside a shadow tree. Only works for non-null values. int comparePositions(const Position& a, const Position& b) { TreeScope* commonScope = commonTreeScope(a.containerNode(), b.containerNode()); ASSERT(commonScope); if (!commonScope) return 0; Node* nodeA = commonScope->ancestorInThisScope(a.containerNode()); ASSERT(nodeA); bool hasDescendentA = nodeA != a.containerNode(); int offsetA = hasDescendentA ? 0 : a.computeOffsetInContainerNode(); Node* nodeB = commonScope->ancestorInThisScope(b.containerNode()); ASSERT(nodeB); bool hasDescendentB = nodeB != b.containerNode(); int offsetB = hasDescendentB ? 0 : b.computeOffsetInContainerNode(); int bias = 0; if (nodeA == nodeB) { if (hasDescendentA) bias = -1; else if (hasDescendentB) bias = 1; } int result = Range::compareBoundaryPoints(nodeA, offsetA, nodeB, offsetB, IGNORE_EXCEPTION); return result ? result : bias; }
static Position adjustPositionForStart(const Position& currentPosition, Node* endContainerNode) { TreeScope* treeScope = endContainerNode->treeScope(); ASSERT(currentPosition.containerNode()->treeScope() != treeScope); if (Node* ancestor = treeScope->ancestorInThisScope(currentPosition.containerNode())) { if (ancestor->contains(endContainerNode)) return positionBeforeNode(ancestor); return positionAfterNode(ancestor); } if (Node* firstChild = treeScope->rootNode()->firstChild()) return positionBeforeNode(firstChild); return Position(); }