Fix right-to-left selection not working. (flutter/engine#18951)

Fixes https://github.com/flutter/flutter/issues/57636
This commit is contained in:
Robert Ancell
2020-06-11 11:13:57 +12:00
committed by GitHub
parent 8c8eaf36f0
commit e3ed835eff
3 changed files with 132 additions and 15 deletions

View File

@@ -37,11 +37,7 @@ TextInputModel::~TextInputModel() = default;
bool TextInputModel::SetEditingState(size_t selection_base,
size_t selection_extent,
const std::string& text) {
if (selection_base > selection_extent) {
return false;
}
// Only checks extent since it is implicitly greater-than-or-equal-to base.
if (selection_extent > text.size()) {
if (selection_base > text.size() || selection_extent > text.size()) {
return false;
}
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
@@ -53,8 +49,7 @@ bool TextInputModel::SetEditingState(size_t selection_base,
}
void TextInputModel::DeleteSelected() {
selection_base_ = text_.erase(selection_base_, selection_extent_);
// Moves extent back to base, so that it is a single cursor placement again.
selection_base_ = text_.erase(selection_start(), selection_end());
selection_extent_ = selection_base_;
}
@@ -178,7 +173,8 @@ bool TextInputModel::MoveCursorToEnd() {
bool TextInputModel::MoveCursorForward() {
// If about to move set to the end of the highlight (when not selecting).
if (selection_base_ != selection_extent_) {
selection_base_ = selection_extent_;
selection_base_ = selection_end();
selection_extent_ = selection_base_;
return true;
}
// If not at the end, move the extent forward.
@@ -195,6 +191,7 @@ bool TextInputModel::MoveCursorBack() {
// If about to move set to the beginning of the highlight
// (when not selecting).
if (selection_base_ != selection_extent_) {
selection_base_ = selection_start();
selection_extent_ = selection_base_;
return true;
}

View File

@@ -73,14 +73,14 @@ class TextInputModel {
// Attempts to move the cursor backward.
//
// Returns true if the cursor could be moved. Changes base and extent to be
// equal to either the extent (if extent is at the end of the string), or
// for extent to be equal to
// Returns true if the cursor could be moved. If a selection is active, moves
// to the start of the selection.
bool MoveCursorBack();
// Attempts to move the cursor forward.
//
// Returns true if the cursor could be moved.
// Returns true if the cursor could be moved. If a selection is active, moves
// to the end of the selection.
bool MoveCursorForward();
// Attempts to move the cursor to the beginning.
@@ -100,12 +100,12 @@ class TextInputModel {
// GetText().
int GetCursorOffset() const;
// The position of the cursor
// The position where the selection starts.
int selection_base() const {
return static_cast<int>(selection_base_ - text_.begin());
}
// The end of the selection
// The position of the cursor.
int selection_extent() const {
return static_cast<int>(selection_extent_ - text_.begin());
}
@@ -116,6 +116,18 @@ class TextInputModel {
std::u16string text_;
std::u16string::iterator selection_base_;
std::u16string::iterator selection_extent_;
// Returns the left hand side of the selection.
std::u16string::iterator selection_start() {
return selection_base_ < selection_extent_ ? selection_base_
: selection_extent_;
}
// Returns the right hand side of the selection.
std::u16string::iterator selection_end() {
return selection_base_ > selection_extent_ ? selection_base_
: selection_extent_;
}
};
} // namespace flutter

View File

@@ -46,7 +46,10 @@ TEST(TextInputModel, SetEditingStateSelection) {
TEST(TextInputModel, SetEditingStateReverseSelection) {
auto model = std::make_unique<TextInputModel>();
EXPECT_FALSE(model->SetEditingState(3, 1, "ABCDE"));
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
EXPECT_EQ(model->selection_base(), 4);
EXPECT_EQ(model->selection_extent(), 1);
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, SetEditingStateOutsideString) {
@@ -85,6 +88,15 @@ TEST(TextInputModel, AddCodePointSelection) {
EXPECT_STREQ(model->GetText().c_str(), "AxE");
}
TEST(TextInputModel, AddCodePointReverseSelection) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
model->AddCodePoint('x');
EXPECT_EQ(model->selection_base(), 2);
EXPECT_EQ(model->selection_extent(), 2);
EXPECT_STREQ(model->GetText().c_str(), "AxE");
}
TEST(TextInputModel, AddCodePointSelectionWideCharacter) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(1, 4, "ABCDE"));
@@ -94,6 +106,15 @@ TEST(TextInputModel, AddCodePointSelectionWideCharacter) {
EXPECT_STREQ(model->GetText().c_str(), "A😄E");
}
TEST(TextInputModel, AddCodePointReverseSelectionWideCharacter) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
model->AddCodePoint(0x1f604);
EXPECT_EQ(model->selection_base(), 3);
EXPECT_EQ(model->selection_extent(), 3);
EXPECT_STREQ(model->GetText().c_str(), "A😄E");
}
TEST(TextInputModel, AddText) {
auto model = std::make_unique<TextInputModel>();
model->AddText(u"ABCDE");
@@ -113,6 +134,15 @@ TEST(TextInputModel, AddTextSelection) {
EXPECT_STREQ(model->GetText().c_str(), "AxyE");
}
TEST(TextInputModel, AddTextReverseSelection) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
model->AddText("xy");
EXPECT_EQ(model->selection_base(), 3);
EXPECT_EQ(model->selection_extent(), 3);
EXPECT_STREQ(model->GetText().c_str(), "AxyE");
}
TEST(TextInputModel, AddTextSelectionWideCharacter) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(1, 4, "ABCDE"));
@@ -122,6 +152,15 @@ TEST(TextInputModel, AddTextSelectionWideCharacter) {
EXPECT_STREQ(model->GetText().c_str(), "A😄🙃E");
}
TEST(TextInputModel, AddTextReverseSelectionWideCharacter) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
model->AddText(u"😄🙃");
EXPECT_EQ(model->selection_base(), 5);
EXPECT_EQ(model->selection_extent(), 5);
EXPECT_STREQ(model->GetText().c_str(), "A😄🙃E");
}
TEST(TextInputModel, DeleteStart) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(0, 0, "ABCDE"));
@@ -167,6 +206,15 @@ TEST(TextInputModel, DeleteSelection) {
EXPECT_STREQ(model->GetText().c_str(), "AE");
}
TEST(TextInputModel, DeleteReverseSelection) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
ASSERT_TRUE(model->Delete());
EXPECT_EQ(model->selection_base(), 1);
EXPECT_EQ(model->selection_extent(), 1);
EXPECT_STREQ(model->GetText().c_str(), "AE");
}
TEST(TextInputModel, DeleteSurroundingAtCursor) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(2, 2, "ABCDE");
@@ -257,6 +305,15 @@ TEST(TextInputModel, DeleteSurroundingSelection) {
EXPECT_STREQ(model->GetText().c_str(), "ABCE");
}
TEST(TextInputModel, DeleteSurroundingReverseSelection) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(4, 3, "ABCDE");
EXPECT_TRUE(model->DeleteSurrounding(0, 1));
EXPECT_EQ(model->selection_base(), 3);
EXPECT_EQ(model->selection_extent(), 3);
EXPECT_STREQ(model->GetText().c_str(), "ABCE");
}
TEST(TextInputModel, BackspaceStart) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(0, 0, "ABCDE"));
@@ -302,6 +359,15 @@ TEST(TextInputModel, BackspaceSelection) {
EXPECT_STREQ(model->GetText().c_str(), "AE");
}
TEST(TextInputModel, BackspaceReverseSelection) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
ASSERT_TRUE(model->Delete());
EXPECT_EQ(model->selection_base(), 1);
EXPECT_EQ(model->selection_extent(), 1);
EXPECT_STREQ(model->GetText().c_str(), "AE");
}
TEST(TextInputModel, MoveCursorForwardStart) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(0, 0, "ABCDE"));
@@ -347,6 +413,15 @@ TEST(TextInputModel, MoveCursorForwardSelection) {
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorForwardReverseSelection) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(4, 1, "ABCDE"));
EXPECT_TRUE(model->MoveCursorForward());
EXPECT_EQ(model->selection_base(), 4);
EXPECT_EQ(model->selection_extent(), 4);
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorBackStart) {
auto model = std::make_unique<TextInputModel>();
EXPECT_TRUE(model->SetEditingState(0, 0, "ABCDE"));
@@ -392,6 +467,15 @@ TEST(TextInputModel, MoveCursorBackSelection) {
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorBackReverseSelection) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(4, 1, "ABCDE");
EXPECT_TRUE(model->MoveCursorBack());
EXPECT_EQ(model->selection_base(), 1);
EXPECT_EQ(model->selection_extent(), 1);
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorToBeginningStart) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(0, 0, "ABCDE");
@@ -428,6 +512,15 @@ TEST(TextInputModel, MoveCursorToBeginningSelection) {
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorToBeginningReverseSelection) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(4, 1, "ABCDE");
EXPECT_TRUE(model->MoveCursorToBeginning());
EXPECT_EQ(model->selection_base(), 0);
EXPECT_EQ(model->selection_extent(), 0);
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorToEndStart) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(0, 0, "ABCDE");
@@ -464,6 +557,15 @@ TEST(TextInputModel, MoveCursorToEndSelection) {
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, MoveCursorToEndReverseSelection) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(4, 1, "ABCDE");
EXPECT_TRUE(model->MoveCursorToEnd());
EXPECT_EQ(model->selection_base(), 5);
EXPECT_EQ(model->selection_extent(), 5);
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
}
TEST(TextInputModel, GetCursorOffset) {
auto model = std::make_unique<TextInputModel>();
// These characters take 1, 2, 3 and 4 bytes in UTF-8.
@@ -485,4 +587,10 @@ TEST(TextInputModel, GetCursorOffsetSelection) {
EXPECT_EQ(model->GetCursorOffset(), 4);
}
TEST(TextInputModel, GetCursorOffsetReverseSelection) {
auto model = std::make_unique<TextInputModel>();
model->SetEditingState(4, 1, "ABCDE");
EXPECT_EQ(model->GetCursorOffset(), 1);
}
} // namespace flutter