Commit 617eb46b authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Introduce AdjustSelectionWithTrailingWhitespace()

This patch introduces |AdjustSelectionWithTrailingWhitespace()| as replacement
of |VisibleSelectionInFlatTree::AppendTrailingWhitespace()| to simplify
|VisibleSelectionTemplate| for improving code health.

Bug: 657237
Change-Id: Iaf5e0157c42ac5a16647da9616fc3bc67c9420f9
Reviewed-on: https://chromium-review.googlesource.com/597557Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491619}
parent 9063cb91
...@@ -562,15 +562,14 @@ bool SelectionController::SelectClosestWordFromHitTestResult( ...@@ -562,15 +562,14 @@ bool SelectionController::SelectClosestWordFromHitTestResult(
visibility = HandleVisibility::kVisible; visibility = HandleVisibility::kVisible;
} }
const VisibleSelectionInFlatTree& adjusted_selection = const SelectionInFlatTree& adjusted_selection =
append_trailing_whitespace == AppendTrailingWhitespace::kShouldAppend append_trailing_whitespace == AppendTrailingWhitespace::kShouldAppend
? new_selection.AppendTrailingWhitespace() ? AdjustSelectionWithTrailingWhitespace(new_selection.AsSelection())
: new_selection; : new_selection.AsSelection();
return UpdateSelectionForMouseDownDispatchingSelectStart( return UpdateSelectionForMouseDownDispatchingSelectStart(
inner_node, inner_node,
ExpandSelectionToRespectUserSelectAll(inner_node, ExpandSelectionToRespectUserSelectAll(inner_node, adjusted_selection),
adjusted_selection.AsSelection()),
TextGranularity::kWord, visibility); TextGranularity::kWord, visibility);
} }
...@@ -608,14 +607,13 @@ void SelectionController::SelectClosestMisspellingFromHitTestResult( ...@@ -608,14 +607,13 @@ void SelectionController::SelectClosestMisspellingFromHitTestResult(
const PositionInFlatTree end(container_node, marker->EndOffset()); const PositionInFlatTree end(container_node, marker->EndOffset());
const VisibleSelectionInFlatTree& new_selection = CreateVisibleSelection( const VisibleSelectionInFlatTree& new_selection = CreateVisibleSelection(
SelectionInFlatTree::Builder().Collapse(start).Extend(end).Build()); SelectionInFlatTree::Builder().Collapse(start).Extend(end).Build());
const VisibleSelectionInFlatTree& adjusted_selection = const SelectionInFlatTree& adjusted_selection =
append_trailing_whitespace == AppendTrailingWhitespace::kShouldAppend append_trailing_whitespace == AppendTrailingWhitespace::kShouldAppend
? new_selection.AppendTrailingWhitespace() ? AdjustSelectionWithTrailingWhitespace(new_selection.AsSelection())
: new_selection; : new_selection.AsSelection();
UpdateSelectionForMouseDownDispatchingSelectStart( UpdateSelectionForMouseDownDispatchingSelectStart(
inner_node, inner_node,
ExpandSelectionToRespectUserSelectAll(inner_node, ExpandSelectionToRespectUserSelectAll(inner_node, adjusted_selection),
adjusted_selection.AsSelection()),
TextGranularity::kWord, HandleVisibility::kNotVisible); TextGranularity::kWord, HandleVisibility::kNotVisible);
} }
......
...@@ -210,22 +210,30 @@ VisibleSelectionTemplate<Strategy>::ToNormalizedEphemeralRange() const { ...@@ -210,22 +210,30 @@ VisibleSelectionTemplate<Strategy>::ToNormalizedEphemeralRange() const {
return NormalizeRange(EphemeralRangeTemplate<Strategy>(Start(), End())); return NormalizeRange(EphemeralRangeTemplate<Strategy>(Start(), End()));
} }
template <typename Strategy> // TODO(editing-dev): We should move |AdjustSelectionWithTrailingWhitespace()|
VisibleSelectionTemplate<Strategy> // to "SelectionController.cpp" as file local function.
VisibleSelectionTemplate<Strategy>::AppendTrailingWhitespace() const { SelectionInFlatTree AdjustSelectionWithTrailingWhitespace(
if (IsNone()) const SelectionInFlatTree& selection) {
return *this; if (selection.IsNone())
if (!IsRange()) return selection;
return *this; if (!selection.IsRange())
const PositionTemplate<Strategy>& new_end = SkipWhitespace(End()); return selection;
if (End() == new_end) const bool base_is_first =
return *this; selection.Base() == selection.ComputeStartPosition();
VisibleSelectionTemplate<Strategy> result = *this; const PositionInFlatTree& end =
if (base_is_first_) base_is_first ? selection.Extent() : selection.Base();
result.extent_ = new_end; DCHECK_EQ(end, selection.ComputeEndPosition());
else const PositionInFlatTree& new_end = SkipWhitespace(end);
result.base_ = new_end; if (end == new_end)
return result; return selection;
if (base_is_first) {
return SelectionInFlatTree::Builder(selection)
.SetBaseAndExtent(selection.Base(), new_end)
.Build();
}
return SelectionInFlatTree::Builder(selection)
.SetBaseAndExtent(new_end, selection.Extent())
.Build();
} }
template <typename Strategy> template <typename Strategy>
......
...@@ -106,8 +106,6 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate { ...@@ -106,8 +106,6 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate {
bool IsBaseFirst() const { return base_is_first_; } bool IsBaseFirst() const { return base_is_first_; }
bool IsDirectional() const { return is_directional_; } bool IsDirectional() const { return is_directional_; }
VisibleSelectionTemplate<Strategy> AppendTrailingWhitespace() const;
// TODO(yosin) Most callers probably don't want these functions, but // TODO(yosin) Most callers probably don't want these functions, but
// are using them for historical reasons. |toNormalizedEphemeralRange()| // are using them for historical reasons. |toNormalizedEphemeralRange()|
// contracts the range around text, and moves the caret most backward // contracts the range around text, and moves the caret most backward
...@@ -199,6 +197,11 @@ PositionInFlatTree ComputeEndRespectingGranularity( ...@@ -199,6 +197,11 @@ PositionInFlatTree ComputeEndRespectingGranularity(
const PositionInFlatTreeWithAffinity&, const PositionInFlatTreeWithAffinity&,
TextGranularity); TextGranularity);
// TODO(editing-dev): We should move |AdjustSelectionWithTrailingWhitespace()|
// to "SelectionController.cpp" as file local function.
CORE_EXPORT SelectionInFlatTree
AdjustSelectionWithTrailingWhitespace(const SelectionInFlatTree&);
} // namespace blink } // namespace blink
#ifndef NDEBUG #ifndef NDEBUG
......
...@@ -71,8 +71,9 @@ VisibleSelectionTemplate<Strategy> ExpandUsingGranularity( ...@@ -71,8 +71,9 @@ VisibleSelectionTemplate<Strategy> ExpandUsingGranularity(
granularity); granularity);
} }
// TODO(editing-dev): We should move this test to "SelectionControllerTest.cpp"
// For http://crbug.com/700368 // For http://crbug.com/700368
TEST_F(VisibleSelectionTest, appendTrailingWhitespaceWithAfterAnchor) { TEST_F(VisibleSelectionTest, AdjustSelectionWithTrailingWhitespace) {
SetBodyContent( SetBodyContent(
"<input type=checkbox>" "<input type=checkbox>"
"<div style='user-select:none'>abc</div>"); "<div style='user-select:none'>abc</div>");
...@@ -82,16 +83,19 @@ TEST_F(VisibleSelectionTest, appendTrailingWhitespaceWithAfterAnchor) { ...@@ -82,16 +83,19 @@ TEST_F(VisibleSelectionTest, appendTrailingWhitespaceWithAfterAnchor) {
// TODO(editing-dev): We should remove above comment once we fix [1]. // TODO(editing-dev): We should remove above comment once we fix [1].
// [1] http://crbug.com/701657 double-click on user-select:none should not // [1] http://crbug.com/701657 double-click on user-select:none should not
// compute selection. // compute selection.
const VisibleSelection selection = CreateVisibleSelectionWithGranularity( const VisibleSelectionInFlatTree& selection =
SelectionInDOMTree::Builder() CreateVisibleSelectionWithGranularity(
.Collapse(Position::BeforeNode(*input)) SelectionInFlatTree::Builder()
.Extend(Position::AfterNode(*input)) .Collapse(PositionInFlatTree::BeforeNode(*input))
.Build(), .Extend(PositionInFlatTree::AfterNode(*input))
TextGranularity::kWord); .Build(),
const VisibleSelection result = selection.AppendTrailingWhitespace(); TextGranularity::kWord);
const SelectionInFlatTree& result =
AdjustSelectionWithTrailingWhitespace(selection.AsSelection());
EXPECT_EQ(Position::BeforeNode(*input), result.Start()); EXPECT_EQ(PositionInFlatTree::BeforeNode(*input),
EXPECT_EQ(Position::AfterNode(*input), result.End()); result.ComputeStartPosition());
EXPECT_EQ(PositionInFlatTree::AfterNode(*input), result.ComputeEndPosition());
} }
TEST_F(VisibleSelectionTest, expandUsingGranularity) { TEST_F(VisibleSelectionTest, expandUsingGranularity) {
......
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