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(
visibility = HandleVisibility::kVisible;
}
const VisibleSelectionInFlatTree& adjusted_selection =
const SelectionInFlatTree& adjusted_selection =
append_trailing_whitespace == AppendTrailingWhitespace::kShouldAppend
? new_selection.AppendTrailingWhitespace()
: new_selection;
? AdjustSelectionWithTrailingWhitespace(new_selection.AsSelection())
: new_selection.AsSelection();
return UpdateSelectionForMouseDownDispatchingSelectStart(
inner_node,
ExpandSelectionToRespectUserSelectAll(inner_node,
adjusted_selection.AsSelection()),
ExpandSelectionToRespectUserSelectAll(inner_node, adjusted_selection),
TextGranularity::kWord, visibility);
}
......@@ -608,14 +607,13 @@ void SelectionController::SelectClosestMisspellingFromHitTestResult(
const PositionInFlatTree end(container_node, marker->EndOffset());
const VisibleSelectionInFlatTree& new_selection = CreateVisibleSelection(
SelectionInFlatTree::Builder().Collapse(start).Extend(end).Build());
const VisibleSelectionInFlatTree& adjusted_selection =
const SelectionInFlatTree& adjusted_selection =
append_trailing_whitespace == AppendTrailingWhitespace::kShouldAppend
? new_selection.AppendTrailingWhitespace()
: new_selection;
? AdjustSelectionWithTrailingWhitespace(new_selection.AsSelection())
: new_selection.AsSelection();
UpdateSelectionForMouseDownDispatchingSelectStart(
inner_node,
ExpandSelectionToRespectUserSelectAll(inner_node,
adjusted_selection.AsSelection()),
ExpandSelectionToRespectUserSelectAll(inner_node, adjusted_selection),
TextGranularity::kWord, HandleVisibility::kNotVisible);
}
......
......@@ -210,22 +210,30 @@ VisibleSelectionTemplate<Strategy>::ToNormalizedEphemeralRange() const {
return NormalizeRange(EphemeralRangeTemplate<Strategy>(Start(), End()));
}
template <typename Strategy>
VisibleSelectionTemplate<Strategy>
VisibleSelectionTemplate<Strategy>::AppendTrailingWhitespace() const {
if (IsNone())
return *this;
if (!IsRange())
return *this;
const PositionTemplate<Strategy>& new_end = SkipWhitespace(End());
if (End() == new_end)
return *this;
VisibleSelectionTemplate<Strategy> result = *this;
if (base_is_first_)
result.extent_ = new_end;
else
result.base_ = new_end;
return result;
// TODO(editing-dev): We should move |AdjustSelectionWithTrailingWhitespace()|
// to "SelectionController.cpp" as file local function.
SelectionInFlatTree AdjustSelectionWithTrailingWhitespace(
const SelectionInFlatTree& selection) {
if (selection.IsNone())
return selection;
if (!selection.IsRange())
return selection;
const bool base_is_first =
selection.Base() == selection.ComputeStartPosition();
const PositionInFlatTree& end =
base_is_first ? selection.Extent() : selection.Base();
DCHECK_EQ(end, selection.ComputeEndPosition());
const PositionInFlatTree& new_end = SkipWhitespace(end);
if (end == new_end)
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>
......
......@@ -106,8 +106,6 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate {
bool IsBaseFirst() const { return base_is_first_; }
bool IsDirectional() const { return is_directional_; }
VisibleSelectionTemplate<Strategy> AppendTrailingWhitespace() const;
// TODO(yosin) Most callers probably don't want these functions, but
// are using them for historical reasons. |toNormalizedEphemeralRange()|
// contracts the range around text, and moves the caret most backward
......@@ -199,6 +197,11 @@ PositionInFlatTree ComputeEndRespectingGranularity(
const PositionInFlatTreeWithAffinity&,
TextGranularity);
// TODO(editing-dev): We should move |AdjustSelectionWithTrailingWhitespace()|
// to "SelectionController.cpp" as file local function.
CORE_EXPORT SelectionInFlatTree
AdjustSelectionWithTrailingWhitespace(const SelectionInFlatTree&);
} // namespace blink
#ifndef NDEBUG
......
......@@ -71,8 +71,9 @@ VisibleSelectionTemplate<Strategy> ExpandUsingGranularity(
granularity);
}
// TODO(editing-dev): We should move this test to "SelectionControllerTest.cpp"
// For http://crbug.com/700368
TEST_F(VisibleSelectionTest, appendTrailingWhitespaceWithAfterAnchor) {
TEST_F(VisibleSelectionTest, AdjustSelectionWithTrailingWhitespace) {
SetBodyContent(
"<input type=checkbox>"
"<div style='user-select:none'>abc</div>");
......@@ -82,16 +83,19 @@ TEST_F(VisibleSelectionTest, appendTrailingWhitespaceWithAfterAnchor) {
// 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
// compute selection.
const VisibleSelection selection = CreateVisibleSelectionWithGranularity(
SelectionInDOMTree::Builder()
.Collapse(Position::BeforeNode(*input))
.Extend(Position::AfterNode(*input))
const VisibleSelectionInFlatTree& selection =
CreateVisibleSelectionWithGranularity(
SelectionInFlatTree::Builder()
.Collapse(PositionInFlatTree::BeforeNode(*input))
.Extend(PositionInFlatTree::AfterNode(*input))
.Build(),
TextGranularity::kWord);
const VisibleSelection result = selection.AppendTrailingWhitespace();
const SelectionInFlatTree& result =
AdjustSelectionWithTrailingWhitespace(selection.AsSelection());
EXPECT_EQ(Position::BeforeNode(*input), result.Start());
EXPECT_EQ(Position::AfterNode(*input), result.End());
EXPECT_EQ(PositionInFlatTree::BeforeNode(*input),
result.ComputeStartPosition());
EXPECT_EQ(PositionInFlatTree::AfterNode(*input), result.ComputeEndPosition());
}
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