Commit aba91b8a authored by yosin@chromium.org's avatar yosin@chromium.org

Introduce firstEditablePositionAfterPositionInRoot()

This patch introduces |firstEditablePositionAfterPositionInRoot()| as pair of
|lastEditablePositionBeforePositionInRoot()| to make debugging easier for
improving code health.

This patch is a preparation of making selection to handle granularity for web
component, http://crrev.com/1277863002

BUG=513568
TEST=n/a; No behavior changes

Review URL: https://codereview.chromium.org/1316063002

git-svn-id: svn://svn.chromium.org/blink/trunk@201220 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 6c3e8431
...@@ -453,28 +453,44 @@ PositionInComposedTree previousVisuallyDistinctCandidate(const PositionInCompose ...@@ -453,28 +453,44 @@ PositionInComposedTree previousVisuallyDistinctCandidate(const PositionInCompose
} }
VisiblePosition firstEditableVisiblePositionAfterPositionInRoot(const Position& position, ContainerNode* highestRoot) VisiblePosition firstEditableVisiblePositionAfterPositionInRoot(const Position& position, ContainerNode* highestRoot)
{
return VisiblePosition(firstEditablePositionAfterPositionInRoot(position, highestRoot));
}
template <typename Strategy>
PositionAlgorithm<Strategy> firstEditablePositionAfterPositionInRootAlgorithm(const PositionAlgorithm<Strategy>& position, Node* highestRoot)
{ {
// position falls before highestRoot. // position falls before highestRoot.
if (comparePositions(position, firstPositionInNode(highestRoot)) == -1 && highestRoot->hasEditableStyle()) if (position.compareTo(PositionAlgorithm<Strategy>::firstPositionInNode(highestRoot)) == -1 && highestRoot->hasEditableStyle())
return VisiblePosition(firstPositionInNode(highestRoot)); return PositionAlgorithm<Strategy>::firstPositionInNode(highestRoot);
Position editablePosition = position; PositionAlgorithm<Strategy> editablePosition = position;
if (position.anchorNode()->treeScope() != highestRoot->treeScope()) { if (position.anchorNode()->treeScope() != highestRoot->treeScope()) {
Node* shadowAncestor = highestRoot->treeScope().ancestorInThisScope(editablePosition.anchorNode()); Node* shadowAncestor = highestRoot->treeScope().ancestorInThisScope(editablePosition.anchorNode());
if (!shadowAncestor) if (!shadowAncestor)
return VisiblePosition(); return PositionAlgorithm<Strategy>();
editablePosition = positionAfterNode(shadowAncestor); editablePosition = PositionAlgorithm<Strategy>::afterNode(shadowAncestor);
} }
while (editablePosition.anchorNode() && !isEditablePosition(editablePosition) && editablePosition.anchorNode()->isDescendantOf(highestRoot)) while (editablePosition.anchorNode() && !isEditablePosition(editablePosition) && editablePosition.anchorNode()->isDescendantOf(highestRoot))
editablePosition = isAtomicNode(editablePosition.anchorNode()) ? positionInParentAfterNode(*editablePosition.anchorNode()) : nextVisuallyDistinctCandidate(editablePosition); editablePosition = isAtomicNode(editablePosition.anchorNode()) ? PositionAlgorithm<Strategy>::inParentAfterNode(*editablePosition.anchorNode()) : nextVisuallyDistinctCandidate(editablePosition);
if (editablePosition.anchorNode() && editablePosition.anchorNode() != highestRoot && !editablePosition.anchorNode()->isDescendantOf(highestRoot)) if (editablePosition.anchorNode() && editablePosition.anchorNode() != highestRoot && !editablePosition.anchorNode()->isDescendantOf(highestRoot))
return VisiblePosition(); return PositionAlgorithm<Strategy>();
return VisiblePosition(editablePosition); return editablePosition;
}
Position firstEditablePositionAfterPositionInRoot(const Position& position, Node* highestRoot)
{
return firstEditablePositionAfterPositionInRootAlgorithm<EditingStrategy>(position, highestRoot);
}
PositionInComposedTree firstEditablePositionAfterPositionInRoot(const PositionInComposedTree& position, Node* highestRoot)
{
return firstEditablePositionAfterPositionInRootAlgorithm<EditingInComposedTreeStrategy>(position, highestRoot);
} }
VisiblePosition lastEditableVisiblePositionBeforePositionInRoot(const Position& position, ContainerNode* highestRoot) VisiblePosition lastEditableVisiblePositionBeforePositionInRoot(const Position& position, ContainerNode* highestRoot)
......
...@@ -180,7 +180,9 @@ inline Position lastPositionInOrAfterNode(Node* node) ...@@ -180,7 +180,9 @@ inline Position lastPositionInOrAfterNode(Node* node)
return Position::lastPositionInOrAfterNode(node); return Position::lastPositionInOrAfterNode(node);
} }
CORE_EXPORT Position firstEditablePositionAfterPositionInRoot(const Position&, Node*);
Position lastEditablePositionBeforePositionInRoot(const Position&, Node*); Position lastEditablePositionBeforePositionInRoot(const Position&, Node*);
CORE_EXPORT PositionInComposedTree firstEditablePositionAfterPositionInRoot(const PositionInComposedTree&, Node*);
PositionInComposedTree lastEditablePositionBeforePositionInRoot(const PositionInComposedTree&, Node*); PositionInComposedTree lastEditablePositionBeforePositionInRoot(const PositionInComposedTree&, Node*);
// Move up or down the DOM by one position. // Move up or down the DOM by one position.
......
...@@ -12,6 +12,25 @@ namespace blink { ...@@ -12,6 +12,25 @@ namespace blink {
class EditingUtilitiesTest : public EditingTestBase { class EditingUtilitiesTest : public EditingTestBase {
}; };
TEST_F(EditingUtilitiesTest, firstEditablePositionAfterPositionInRoot)
{
const char* bodyContent = "<p id='host' contenteditable><b id='one'></b><b id='two'>22</b></p>";
const char* shadowContent = "<content select=#two></content><content select=#one></content><b id='three'>333</b>";
setBodyContent(bodyContent);
RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent);
updateLayoutAndStyleForPainting();
Node* host = document().getElementById("host");
Node* one = document().getElementById("one");
Node* three = shadowRoot->getElementById("three");
EXPECT_EQ(Position(one, 0), firstEditablePositionAfterPositionInRoot(Position(one, 0), host));
EXPECT_EQ(PositionInComposedTree(one, 0), firstEditablePositionAfterPositionInRoot(PositionInComposedTree(one, 0), host));
EXPECT_EQ(Position::firstPositionInNode(host), firstEditablePositionAfterPositionInRoot(Position(three, 0), host));
EXPECT_EQ(PositionInComposedTree::afterNode(host), firstEditablePositionAfterPositionInRoot(PositionInComposedTree(three, 0), host));
}
TEST_F(EditingUtilitiesTest, enclosingNodeOfType) TEST_F(EditingUtilitiesTest, enclosingNodeOfType)
{ {
const char* bodyContent = "<p id='host'><b id='one'>11</b></p>"; const char* bodyContent = "<p id='host'><b id='one'>11</b></p>";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment