Commit 853e1218 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Unify base/extent and start/end in VisbileSelectionTemplate

This patch unifies base/extent and start/end in |VisbileSelectionTemplate| to
simplify |VisbileSelectionTemplate| for improving code health.

Before this patch, base/extent were used for holding start position of
granularity expansion and non-visibly shrunk positions, e.g.
start/end hold |Most{Back,For}wardCaretPostion()|.

After this patch base/extent and start/end are identical with considering
direction, in forward selection base == start and extent == end, in backward
selection base == end and extent == start.

For changes in "TypeCommand.cpp" and "SelectionController.cpp" are canceling
|Most{Back,For}wardCaretPostion()| of start/end for checking equality, for
example: "|<b>foo</>" (most backward caret position) vs. "<b>|foo</b>" (most
forward position). Note: |VisiblePosition| is tend to be most backward caret
position.

Following patch will get rid of |start_| and |end_| from
|VisbileSelectionTemplate|.

Note:
Changes for "TypeCommand.cpp" are for "undo-delete-boundary.html":
Changes for "SelectionController.cpp" are for "selection-bidi.html":

Bug: 230267
Change-Id: I255cbeb331a1da36ae6a569ce7bfd9c59b018eab
Reviewed-on: https://chromium-review.googlesource.com/569507Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486716}
parent f70200fd
...@@ -765,8 +765,10 @@ void SelectionController::SetNonDirectionalSelectionIfNeeded( ...@@ -765,8 +765,10 @@ void SelectionController::SetNonDirectionalSelectionIfNeeded(
builder.SetBaseAndExtent(adjusted_selection.Base(), builder.SetBaseAndExtent(adjusted_selection.Base(),
adjusted_selection.Extent()); adjusted_selection.Extent());
} else if (original_base.IsNotNull()) { } else if (original_base.IsNotNull()) {
if (Selection().ComputeVisibleSelectionInFlatTree().Base() == if (CreateVisiblePosition(
new_selection.Base()) { Selection().ComputeVisibleSelectionInFlatTree().Base())
.DeepEquivalent() ==
CreateVisiblePosition(new_selection.Base()).DeepEquivalent()) {
builder.SetBaseAndExtent(original_base.DeepEquivalent(), builder.SetBaseAndExtent(original_base.DeepEquivalent(),
new_selection.Extent()); new_selection.Extent());
} }
......
...@@ -58,7 +58,6 @@ TEST_F(SelectionControllerTest, setNonDirectionalSelectionIfNeeded) { ...@@ -58,7 +58,6 @@ TEST_F(SelectionControllerTest, setNonDirectionalSelectionIfNeeded) {
Node* top = GetDocument().getElementById("top")->firstChild(); Node* top = GetDocument().getElementById("top")->firstChild();
Node* bottom = shadow_root->getElementById("bottom")->firstChild(); Node* bottom = shadow_root->getElementById("bottom")->firstChild();
Node* host = GetDocument().getElementById("host");
// top to bottom // top to bottom
SetNonDirectionalSelectionIfNeeded(SelectionInFlatTree::Builder() SetNonDirectionalSelectionIfNeeded(SelectionInFlatTree::Builder()
...@@ -66,8 +65,10 @@ TEST_F(SelectionControllerTest, setNonDirectionalSelectionIfNeeded) { ...@@ -66,8 +65,10 @@ TEST_F(SelectionControllerTest, setNonDirectionalSelectionIfNeeded) {
.Extend(PositionInFlatTree(bottom, 3)) .Extend(PositionInFlatTree(bottom, 3))
.Build(), .Build(),
TextGranularity::kCharacter); TextGranularity::kCharacter);
EXPECT_EQ(Position(top, 1), VisibleSelectionInDOMTree().Base()); EXPECT_EQ(VisibleSelectionInDOMTree().Start(),
EXPECT_EQ(Position::BeforeNode(*host), VisibleSelectionInDOMTree().Extent()); VisibleSelectionInDOMTree().Base());
EXPECT_EQ(VisibleSelectionInDOMTree().End(),
VisibleSelectionInDOMTree().Extent());
EXPECT_EQ(Position(top, 1), VisibleSelectionInDOMTree().Start()); EXPECT_EQ(Position(top, 1), VisibleSelectionInDOMTree().Start());
EXPECT_EQ(Position(top, 3), VisibleSelectionInDOMTree().End()); EXPECT_EQ(Position(top, 3), VisibleSelectionInDOMTree().End());
...@@ -86,8 +87,9 @@ TEST_F(SelectionControllerTest, setNonDirectionalSelectionIfNeeded) { ...@@ -86,8 +87,9 @@ TEST_F(SelectionControllerTest, setNonDirectionalSelectionIfNeeded) {
.Extend(PositionInFlatTree(top, 1)) .Extend(PositionInFlatTree(top, 1))
.Build(), .Build(),
TextGranularity::kCharacter); TextGranularity::kCharacter);
EXPECT_EQ(Position(bottom, 3), VisibleSelectionInDOMTree().Base()); EXPECT_EQ(VisibleSelectionInDOMTree().End(),
EXPECT_EQ(Position::BeforeNode(*bottom->parentNode()), VisibleSelectionInDOMTree().Base());
EXPECT_EQ(VisibleSelectionInDOMTree().Start(),
VisibleSelectionInDOMTree().Extent()); VisibleSelectionInDOMTree().Extent());
EXPECT_EQ(Position(bottom, 0), VisibleSelectionInDOMTree().Start()); EXPECT_EQ(Position(bottom, 0), VisibleSelectionInDOMTree().Start());
EXPECT_EQ(Position(bottom, 3), VisibleSelectionInDOMTree().End()); EXPECT_EQ(Position(bottom, 3), VisibleSelectionInDOMTree().End());
......
...@@ -487,20 +487,21 @@ void VisibleSelectionTemplate<Strategy>::Validate( ...@@ -487,20 +487,21 @@ void VisibleSelectionTemplate<Strategy>::Validate(
AdjustSelectionToAvoidCrossingEditingBoundaries(); AdjustSelectionToAvoidCrossingEditingBoundaries();
UpdateSelectionType(); UpdateSelectionType();
if (GetSelectionType() != kRangeSelection) if (GetSelectionType() == kRangeSelection) {
return; // "Constrain" the selection to be the smallest equivalent range of
// nodes. This is a somewhat arbitrary choice, but experience shows that
// "Constrain" the selection to be the smallest equivalent range of // it is useful to make to make the selection "canonical" (if only for
// nodes. This is a somewhat arbitrary choice, but experience shows that // purposes of comparing selections). This is an ideal point of the code
// it is useful to make to make the selection "canonical" (if only for // to do this operation, since all selection changes that result in a
// purposes of comparing selections). This is an ideal point of the code // RANGE come through here before anyone uses it.
// to do this operation, since all selection changes that result in a // TODO(yosin) Canonicalizing is good, but haven't we already done it
// RANGE come through here before anyone uses it. // (when we set these two positions to |VisiblePosition|
// TODO(yosin) Canonicalizing is good, but haven't we already done it // |DeepEquivalent()|s above)?
// (when we set these two positions to |VisiblePosition| start_ = MostForwardCaretPosition(start_);
// |deepEquivalent()|s above)? end_ = MostBackwardCaretPosition(end_);
start_ = MostForwardCaretPosition(start_); }
end_ = MostBackwardCaretPosition(end_); base_ = base_is_first_ ? start_ : end_;
extent_ = base_is_first_ ? end_ : start_;
} }
template <typename Strategy> template <typename Strategy>
......
...@@ -124,13 +124,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) { ...@@ -124,13 +124,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) {
selection_in_flat_tree = selection_in_flat_tree =
ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord); ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord);
EXPECT_EQ(Position(one, 1), selection.Base()); EXPECT_EQ(selection.Start(), selection.Base());
EXPECT_EQ(Position(one, 1), selection.Extent()); EXPECT_EQ(selection.End(), selection.Extent());
EXPECT_EQ(Position(one, 0), selection.Start()); EXPECT_EQ(Position(one, 0), selection.Start());
EXPECT_EQ(Position(two, 2), selection.End()); EXPECT_EQ(Position(two, 2), selection.End());
EXPECT_EQ(PositionInFlatTree(one, 1), selection_in_flat_tree.Base()); EXPECT_EQ(selection_in_flat_tree.Start(), selection_in_flat_tree.Base());
EXPECT_EQ(PositionInFlatTree(one, 1), selection_in_flat_tree.Extent()); EXPECT_EQ(selection_in_flat_tree.End(), selection_in_flat_tree.Extent());
EXPECT_EQ(PositionInFlatTree(one, 0), selection_in_flat_tree.Start()); EXPECT_EQ(PositionInFlatTree(one, 0), selection_in_flat_tree.Start());
EXPECT_EQ(PositionInFlatTree(five, 5), selection_in_flat_tree.End()); EXPECT_EQ(PositionInFlatTree(five, 5), selection_in_flat_tree.End());
...@@ -145,13 +145,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) { ...@@ -145,13 +145,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) {
selection_in_flat_tree = selection_in_flat_tree =
ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord); ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord);
EXPECT_EQ(Position(two, 1), selection.Base()); EXPECT_EQ(selection.Start(), selection.Base());
EXPECT_EQ(Position(two, 1), selection.Extent()); EXPECT_EQ(selection.End(), selection.Extent());
EXPECT_EQ(Position(one, 0), selection.Start()); EXPECT_EQ(Position(one, 0), selection.Start());
EXPECT_EQ(Position(two, 2), selection.End()); EXPECT_EQ(Position(two, 2), selection.End());
EXPECT_EQ(PositionInFlatTree(two, 1), selection_in_flat_tree.Base()); EXPECT_EQ(selection_in_flat_tree.Start(), selection_in_flat_tree.Base());
EXPECT_EQ(PositionInFlatTree(two, 1), selection_in_flat_tree.Extent()); EXPECT_EQ(selection_in_flat_tree.End(), selection_in_flat_tree.Extent());
EXPECT_EQ(PositionInFlatTree(three, 0), selection_in_flat_tree.Start()); EXPECT_EQ(PositionInFlatTree(three, 0), selection_in_flat_tree.Start());
EXPECT_EQ(PositionInFlatTree(four, 4), selection_in_flat_tree.End()); EXPECT_EQ(PositionInFlatTree(four, 4), selection_in_flat_tree.End());
...@@ -166,13 +166,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) { ...@@ -166,13 +166,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) {
selection_in_flat_tree = selection_in_flat_tree =
ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord); ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord);
EXPECT_EQ(Position(three, 1), selection.Base()); EXPECT_EQ(selection.Start(), selection.Base());
EXPECT_EQ(Position(three, 1), selection.Extent()); EXPECT_EQ(selection.End(), selection.Extent());
EXPECT_EQ(Position(three, 0), selection.Start()); EXPECT_EQ(Position(three, 0), selection.Start());
EXPECT_EQ(Position(four, 4), selection.End()); EXPECT_EQ(Position(four, 4), selection.End());
EXPECT_EQ(PositionInFlatTree(three, 1), selection_in_flat_tree.Base()); EXPECT_EQ(selection_in_flat_tree.Start(), selection_in_flat_tree.Base());
EXPECT_EQ(PositionInFlatTree(three, 1), selection_in_flat_tree.Extent()); EXPECT_EQ(selection_in_flat_tree.End(), selection_in_flat_tree.Extent());
EXPECT_EQ(PositionInFlatTree(three, 0), selection_in_flat_tree.Start()); EXPECT_EQ(PositionInFlatTree(three, 0), selection_in_flat_tree.Start());
EXPECT_EQ(PositionInFlatTree(four, 4), selection_in_flat_tree.End()); EXPECT_EQ(PositionInFlatTree(four, 4), selection_in_flat_tree.End());
...@@ -187,13 +187,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) { ...@@ -187,13 +187,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) {
selection_in_flat_tree = selection_in_flat_tree =
ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord); ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord);
EXPECT_EQ(Position(four, 1), selection.Base()); EXPECT_EQ(selection.Start(), selection.Base());
EXPECT_EQ(Position(four, 1), selection.Extent()); EXPECT_EQ(selection.End(), selection.Extent());
EXPECT_EQ(Position(three, 0), selection.Start()); EXPECT_EQ(Position(three, 0), selection.Start());
EXPECT_EQ(Position(four, 4), selection.End()); EXPECT_EQ(Position(four, 4), selection.End());
EXPECT_EQ(PositionInFlatTree(four, 1), selection_in_flat_tree.Base()); EXPECT_EQ(selection_in_flat_tree.Start(), selection_in_flat_tree.Base());
EXPECT_EQ(PositionInFlatTree(four, 1), selection_in_flat_tree.Extent()); EXPECT_EQ(selection_in_flat_tree.End(), selection_in_flat_tree.Extent());
EXPECT_EQ(PositionInFlatTree(three, 0), selection_in_flat_tree.Start()); EXPECT_EQ(PositionInFlatTree(three, 0), selection_in_flat_tree.Start());
EXPECT_EQ(PositionInFlatTree(four, 4), selection_in_flat_tree.End()); EXPECT_EQ(PositionInFlatTree(four, 4), selection_in_flat_tree.End());
...@@ -208,13 +208,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) { ...@@ -208,13 +208,13 @@ TEST_F(VisibleSelectionTest, expandUsingGranularity) {
selection_in_flat_tree = selection_in_flat_tree =
ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord); ExpandUsingGranularity(selection_in_flat_tree, TextGranularity::kWord);
EXPECT_EQ(Position(five, 1), selection.Base()); EXPECT_EQ(selection.Start(), selection.Base());
EXPECT_EQ(Position(five, 1), selection.Extent()); EXPECT_EQ(selection.End(), selection.Extent());
EXPECT_EQ(Position(five, 0), selection.Start()); EXPECT_EQ(Position(five, 0), selection.Start());
EXPECT_EQ(Position(five, 5), selection.End()); EXPECT_EQ(Position(five, 5), selection.End());
EXPECT_EQ(PositionInFlatTree(five, 1), selection_in_flat_tree.Base()); EXPECT_EQ(selection_in_flat_tree.Start(), selection_in_flat_tree.Base());
EXPECT_EQ(PositionInFlatTree(five, 1), selection_in_flat_tree.Extent()); EXPECT_EQ(selection_in_flat_tree.End(), selection_in_flat_tree.Extent());
EXPECT_EQ(PositionInFlatTree(one, 0), selection_in_flat_tree.Start()); EXPECT_EQ(PositionInFlatTree(one, 0), selection_in_flat_tree.Start());
EXPECT_EQ(PositionInFlatTree(five, 5), selection_in_flat_tree.End()); EXPECT_EQ(PositionInFlatTree(five, 5), selection_in_flat_tree.End());
} }
......
...@@ -833,7 +833,8 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity, ...@@ -833,7 +833,8 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
// current state of the document and we'll get the wrong result. // current state of the document and we'll get the wrong result.
const VisibleSelection& selection_after_undo = const VisibleSelection& selection_after_undo =
VisibleSelection::CreateWithoutValidationDeprecated( VisibleSelection::CreateWithoutValidationDeprecated(
StartingSelection().End(), selection_to_delete.Extent(), StartingSelection().End(),
CreateVisiblePosition(selection_to_delete.Extent()).DeepEquivalent(),
selection_to_delete.Affinity()); selection_to_delete.Affinity());
DeleteKeyPressedInternal(selection_to_delete, selection_after_undo, kill_ring, DeleteKeyPressedInternal(selection_to_delete, selection_after_undo, kill_ring,
editing_state); editing_state);
...@@ -961,7 +962,8 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity, ...@@ -961,7 +962,8 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity,
const VisibleSelection& selection_to_delete = const VisibleSelection& selection_to_delete =
selection_modifier.Selection(); selection_modifier.Selection();
if (!StartingSelection().IsRange() || if (!StartingSelection().IsRange() ||
selection_to_delete.Base() != StartingSelection().Start()) { MostBackwardCaretPosition(selection_to_delete.Base()) !=
StartingSelection().Start()) {
ForwardDeleteKeyPressedInternal( ForwardDeleteKeyPressedInternal(
selection_to_delete, selection_to_delete, kill_ring, editing_state); selection_to_delete, selection_to_delete, kill_ring, editing_state);
return; return;
......
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