Commit 192df4b6 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

ime: Maintain selection direction in FinishComposingText.

FinishComposingText saves the old selection in order to reapply it
after the composition is finished. However, it needs to also maintain
the direction (the order of base vs. extent), or else a backward
selection will be restored as a forward selection after calling
FinishComposingText.

Bug: 1145727, b/172284191
Change-Id: Ie1e8ef69fcabcb11aebbbc5003e044f70e6535d3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518309
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824323}
parent 5c7c88d7
......@@ -580,7 +580,14 @@ bool InputMethodController::FinishComposingText(
// eventually move back to the old selection offsets.
const bool is_handle_visible = GetFrame().Selection().IsHandleVisible();
// Maintain to direction of the original selection as it affects how the
// selection can be extended.
const PlainTextRange& old_offsets = GetSelectionOffsets();
const bool is_forward_selection =
GetFrame()
.Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()
.IsBaseFirst();
RevealSelectionScope reveal_selection_scope(GetFrame());
if (is_too_long) {
......@@ -599,9 +606,12 @@ bool InputMethodController::FinishComposingText(
if (old_selection_range.IsNull())
return false;
const SelectionInDOMTree& selection =
SelectionInDOMTree::Builder()
.SetBaseAndExtent(old_selection_range)
.Build();
is_forward_selection ? SelectionInDOMTree::Builder()
.SetAsForwardSelection(old_selection_range)
.Build()
: SelectionInDOMTree::Builder()
.SetAsBackwardSelection(old_selection_range)
.Build();
GetFrame().Selection().SetSelection(
selection, SetSelectionOptions::Builder()
.SetShouldCloseTyping(true)
......
......@@ -417,6 +417,15 @@ TEST_F(InputMethodControllerTest, FinishComposingTextKeepingStyle) {
EXPECT_EQ("abc1<b>2</b>3hello7<b>8</b>9", div->innerHTML());
}
TEST_F(InputMethodControllerTest, FinishComposingTextKeepingBackwardSelection) {
GetFrame().Selection().SetSelectionAndEndTyping(
SetSelectionTextToBody("<div contenteditable>|abc^</div>"));
Controller().FinishComposingText(InputMethodController::kKeepSelection);
EXPECT_EQ("<div contenteditable>|abc^</div>", GetSelectionTextFromBody());
}
TEST_F(InputMethodControllerTest, CommitTextKeepingStyle) {
Element* div = InsertHTMLElement(
"<div id='sample' "
......
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