Commit cb5827d8 authored by xji@google.com's avatar xji@google.com

Remove PREVIOUS_GRAPHEME_TRAILING.

Remove const from GetIndexOfPreviousGrapheme() so the override function could
update local data.
Construct SelectionModel correctly in unittest.

BUG=90426
TEST=--use-pure-view.
Review URL: http://codereview.chromium.org/7607018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97029 0039d316-1c4b-4281-b951-d872f2087c98
parent cc033374
...@@ -87,11 +87,15 @@ StyleRange::StyleRange() ...@@ -87,11 +87,15 @@ StyleRange::StyleRange()
} }
SelectionModel::SelectionModel() { SelectionModel::SelectionModel() {
Init(0, 0, 0, PREVIOUS_GRAPHEME_TRAILING); Init(0, 0, 0, LEADING);
} }
SelectionModel::SelectionModel(size_t pos) { SelectionModel::SelectionModel(size_t pos) {
Init(pos, pos, pos, PREVIOUS_GRAPHEME_TRAILING); Init(pos, pos, pos, LEADING);
}
SelectionModel::SelectionModel(size_t start, size_t end) {
Init(start, end, end, LEADING);
} }
SelectionModel::SelectionModel(size_t end, SelectionModel::SelectionModel(size_t end,
...@@ -266,6 +270,8 @@ void RenderText::SelectAll() { ...@@ -266,6 +270,8 @@ void RenderText::SelectAll() {
SetSelectionModel(sel); SetSelectionModel(sel);
} }
// TODO(xji): it does not work for languages do not use space as word breaker,
// such as Chinese. Should use BreakIterator.
void RenderText::SelectWord() { void RenderText::SelectWord() {
size_t selection_start = GetSelectionStart(); size_t selection_start = GetSelectionStart();
size_t cursor_position = GetCursorPosition(); size_t cursor_position = GetCursorPosition();
...@@ -305,7 +311,8 @@ void RenderText::SelectWord() { ...@@ -305,7 +311,8 @@ void RenderText::SelectWord() {
SelectionModel sel(selection_model()); SelectionModel sel(selection_model());
sel.set_selection_start(selection_start); sel.set_selection_start(selection_start);
sel.set_selection_end(cursor_position); sel.set_selection_end(cursor_position);
sel.set_caret_placement(SelectionModel::PREVIOUS_GRAPHEME_TRAILING); sel.set_caret_pos(GetIndexOfPreviousGrapheme(cursor_position));
sel.set_caret_placement(SelectionModel::TRAILING);
SetSelectionModel(sel); SetSelectionModel(sel);
} }
...@@ -542,7 +549,7 @@ SelectionModel RenderText::GetRightSelectionModel(const SelectionModel& current, ...@@ -542,7 +549,7 @@ SelectionModel RenderText::GetRightSelectionModel(const SelectionModel& current,
return SelectionModel(pos, pos, SelectionModel::LEADING); return SelectionModel(pos, pos, SelectionModel::LEADING);
} }
size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) const { size_t RenderText::GetIndexOfPreviousGrapheme(size_t position) {
// TODO(msw): Handle complex script. // TODO(msw): Handle complex script.
return std::max(static_cast<int>(position - 1), static_cast<int>(0)); return std::max(static_cast<int>(position - 1), static_cast<int>(0));
} }
......
...@@ -90,15 +90,13 @@ enum BreakType { ...@@ -90,15 +90,13 @@ enum BreakType {
class UI_EXPORT SelectionModel { class UI_EXPORT SelectionModel {
public: public:
enum CaretPlacement { enum CaretPlacement {
// PREVIOUS_GRAPHEME_TRAILING means cursor is visually attached to the
// trailing edge of previous grapheme.
PREVIOUS_GRAPHEME_TRAILING,
LEADING, LEADING,
TRAILING, TRAILING,
}; };
SelectionModel(); SelectionModel();
explicit SelectionModel(size_t pos); explicit SelectionModel(size_t pos);
SelectionModel(size_t start, size_t end);
SelectionModel(size_t end, size_t pos, CaretPlacement status); SelectionModel(size_t end, size_t pos, CaretPlacement status);
SelectionModel(size_t start, size_t end, size_t pos, CaretPlacement status); SelectionModel(size_t start, size_t end, size_t pos, CaretPlacement status);
...@@ -277,7 +275,7 @@ class UI_EXPORT RenderText { ...@@ -277,7 +275,7 @@ class UI_EXPORT RenderText {
BreakType break_type); BreakType break_type);
// Get the logical index of the grapheme preceeding the argument |position|. // Get the logical index of the grapheme preceeding the argument |position|.
virtual size_t GetIndexOfPreviousGrapheme(size_t position) const; virtual size_t GetIndexOfPreviousGrapheme(size_t position);
// Apply composition style (underline) to composition range and selection // Apply composition style (underline) to composition range and selection
// style (foreground) to selection range. // style (foreground) to selection range.
......
...@@ -414,8 +414,7 @@ void TextfieldViewsModel::GetSelectedRange(ui::Range* range) const { ...@@ -414,8 +414,7 @@ void TextfieldViewsModel::GetSelectedRange(ui::Range* range) const {
} }
void TextfieldViewsModel::SelectRange(const ui::Range& range) { void TextfieldViewsModel::SelectRange(const ui::Range& range) {
gfx::SelectionModel selection(range.start(), range.end(), gfx::SelectionModel selection(range.start(), range.end());
range.end(), gfx::SelectionModel::PREVIOUS_GRAPHEME_TRAILING);
SelectSelectionModel(selection); SelectSelectionModel(selection);
} }
...@@ -589,8 +588,7 @@ void TextfieldViewsModel::SetCompositionText( ...@@ -589,8 +588,7 @@ void TextfieldViewsModel::SetCompositionText(
std::min(range.start() + composition.selection.start(), range.end()); std::min(range.start() + composition.selection.start(), range.end());
size_t end = size_t end =
std::min(range.start() + composition.selection.end(), range.end()); std::min(range.start() + composition.selection.end(), range.end());
gfx::SelectionModel sel(start, end, end, gfx::SelectionModel sel(start, end);
gfx::SelectionModel::PREVIOUS_GRAPHEME_TRAILING);
render_text_->SetSelectionModel(sel); render_text_->SetSelectionModel(sel);
} else { } else {
render_text_->SetCursorPosition(range.end()); render_text_->SetCursorPosition(range.end());
......
...@@ -126,18 +126,11 @@ TEST_F(TextfieldViewsModelTest, Selection) { ...@@ -126,18 +126,11 @@ TEST_F(TextfieldViewsModelTest, Selection) {
EXPECT_EQ(5U, range.end()); EXPECT_EQ(5U, range.end());
// Select and move cursor // Select and move cursor
gfx::SelectionModel selection(1U); model.MoveCursorTo(gfx::SelectionModel(1U, 3U));
model.MoveCursorTo(selection);
selection.set_selection_end(3U);
model.MoveCursorTo(selection);
EXPECT_STR_EQ("EL", model.GetSelectedText()); EXPECT_STR_EQ("EL", model.GetSelectedText());
model.MoveCursorLeft(gfx::CHARACTER_BREAK, false); model.MoveCursorLeft(gfx::CHARACTER_BREAK, false);
EXPECT_EQ(1U, model.GetCursorPosition()); EXPECT_EQ(1U, model.GetCursorPosition());
selection.set_selection_end(1U); model.MoveCursorTo(gfx::SelectionModel(1U, 3U));
selection.set_selection_start(selection.selection_end());
model.MoveCursorTo(selection);
selection.set_selection_end(3U);
model.MoveCursorTo(selection);
model.MoveCursorRight(gfx::CHARACTER_BREAK, false); model.MoveCursorRight(gfx::CHARACTER_BREAK, false);
EXPECT_EQ(3U, model.GetCursorPosition()); EXPECT_EQ(3U, model.GetCursorPosition());
...@@ -342,24 +335,21 @@ TEST_F(TextfieldViewsModelTest, SelectWordTest) { ...@@ -342,24 +335,21 @@ TEST_F(TextfieldViewsModelTest, SelectWordTest) {
SelectWordTestVerifier(model, "HELLO", 7U); SelectWordTestVerifier(model, "HELLO", 7U);
// Test when cursor is at the end of a word. // Test when cursor is at the end of a word.
selection.set_selection_end(15U); selection = gfx::SelectionModel(15U);
selection.set_selection_start(selection.selection_end());
model.MoveCursorTo(selection); model.MoveCursorTo(selection);
model.SelectWord(); model.SelectWord();
SelectWordTestVerifier(model, "WO", 15U); SelectWordTestVerifier(model, "WO", 15U);
// Test when cursor is somewhere in a non-alph-numeric fragment. // Test when cursor is somewhere in a non-alph-numeric fragment.
for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) { for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) {
selection.set_selection_end(cursor_pos); selection = gfx::SelectionModel(cursor_pos);
selection.set_selection_start(selection.selection_end());
model.MoveCursorTo(selection); model.MoveCursorTo(selection);
model.SelectWord(); model.SelectWord();
SelectWordTestVerifier(model, " !! ", 13U); SelectWordTestVerifier(model, " !! ", 13U);
} }
// Test when cursor is somewhere in a whitespace fragment. // Test when cursor is somewhere in a whitespace fragment.
selection.set_selection_end(17U); selection = gfx::SelectionModel(17U);
selection.set_selection_start(selection.selection_end());
model.MoveCursorTo(selection); model.MoveCursorTo(selection);
model.SelectWord(); model.SelectWord();
SelectWordTestVerifier(model, " ", 20U); SelectWordTestVerifier(model, " ", 20U);
...@@ -809,10 +799,7 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) { ...@@ -809,10 +799,7 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) {
model.SetText(ASCIIToUTF16("ABCDE")); model.SetText(ASCIIToUTF16("ABCDE"));
EXPECT_FALSE(model.Redo()); // nothing to redo EXPECT_FALSE(model.Redo()); // nothing to redo
// Cut // Cut
gfx::SelectionModel sel(1); model.MoveCursorTo(gfx::SelectionModel(1, 3));
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
model.Cut(); model.Cut();
EXPECT_STR_EQ("ADE", model.GetText()); EXPECT_STR_EQ("ADE", model.GetText());
EXPECT_EQ(1U, model.GetCursorPosition()); EXPECT_EQ(1U, model.GetCursorPosition());
...@@ -898,11 +885,7 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) { ...@@ -898,11 +885,7 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) {
model.SetText(ASCIIToUTF16("12345")); model.SetText(ASCIIToUTF16("12345"));
EXPECT_STR_EQ("12345", model.GetText()); EXPECT_STR_EQ("12345", model.GetText());
EXPECT_EQ(0U, model.GetCursorPosition()); EXPECT_EQ(0U, model.GetCursorPosition());
sel.set_selection_end(1); model.MoveCursorTo(gfx::SelectionModel(1, 3));
sel.set_selection_start(sel.selection_end());
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
model.Copy(); // Copy "23" model.Copy(); // Copy "23"
EXPECT_STR_EQ("12345", model.GetText()); EXPECT_STR_EQ("12345", model.GetText());
EXPECT_EQ(3U, model.GetCursorPosition()); EXPECT_EQ(3U, model.GetCursorPosition());
...@@ -1026,40 +1009,28 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) { ...@@ -1026,40 +1009,28 @@ TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) {
SCOPED_TRACE("forward & insert by cursor"); SCOPED_TRACE("forward & insert by cursor");
TextfieldViewsModel model(NULL); TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd")); model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(1); model.MoveCursorTo(gfx::SelectionModel(1, 3));
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
RunInsertReplaceTest(model); RunInsertReplaceTest(model);
} }
{ {
SCOPED_TRACE("backward & insert by cursor"); SCOPED_TRACE("backward & insert by cursor");
TextfieldViewsModel model(NULL); TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd")); model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(3); model.MoveCursorTo(gfx::SelectionModel(3, 1));
model.MoveCursorTo(sel);
sel.set_selection_end(1);
model.MoveCursorTo(sel);
RunInsertReplaceTest(model); RunInsertReplaceTest(model);
} }
{ {
SCOPED_TRACE("forward & overwrite by cursor"); SCOPED_TRACE("forward & overwrite by cursor");
TextfieldViewsModel model(NULL); TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd")); model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(1); model.MoveCursorTo(gfx::SelectionModel(1, 3));
model.MoveCursorTo(sel);
sel.set_selection_end(3);
model.MoveCursorTo(sel);
RunOverwriteReplaceTest(model); RunOverwriteReplaceTest(model);
} }
{ {
SCOPED_TRACE("backward & overwrite by cursor"); SCOPED_TRACE("backward & overwrite by cursor");
TextfieldViewsModel model(NULL); TextfieldViewsModel model(NULL);
model.SetText(ASCIIToUTF16("abcd")); model.SetText(ASCIIToUTF16("abcd"));
gfx::SelectionModel sel(3); model.MoveCursorTo(gfx::SelectionModel(3, 1));
model.MoveCursorTo(sel);
sel.set_selection_end(1);
model.MoveCursorTo(sel);
RunOverwriteReplaceTest(model); RunOverwriteReplaceTest(model);
} }
// By SelectRange API // By SelectRange API
......
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