Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Unified Diff: third_party/WebKit/Source/core/editing/InputMethodController.cpp

Issue 1995333002: Handle newCursorPosition correctly for Android's commitText() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: adjust selection in confirmCompositionOrInsertText() Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/InputMethodController.cpp
diff --git a/third_party/WebKit/Source/core/editing/InputMethodController.cpp b/third_party/WebKit/Source/core/editing/InputMethodController.cpp
index e36cea49358ce24b90a500afbec5b97eb29f8242..ec71e2aa4bfa78c27d564891d18ccf876133cdc0 100644
--- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp
+++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp
@@ -233,8 +233,35 @@ bool InputMethodController::confirmComposition(const String& text, ConfirmCompos
return true;
}
-bool InputMethodController::confirmCompositionOrInsertText(const String& text, ConfirmCompositionBehavior confirmBehavior)
+bool InputMethodController::confirmCompositionOrInsertText(const String& text, ConfirmCompositionBehavior confirmBehavior, int newCursorPosition)
{
+ if (confirmBehavior == DoNotKeepSelection) {
+ // If newCursorPosition > 0, it's relative to the end of the text - 1;
+ // if newCursorPosition <= 0, it is relative to the start of the text.
+ if (newCursorPosition > 0)
+ newCursorPosition = newCursorPosition + text.length() - 1;
Changwan Ryu 2016/08/03 07:37:30 newCursorPosition += text.length() - 1;
yabinh 2016/08/08 07:33:43 Done.
+
+ // According to Android documentation, the new cursor position is
+ // relative to the composing text if any, or relative to the selection
+ // if no composing text.
+ if (hasComposition()) {
+ Element* rootEditableElement = frame().selection().rootEditableElement();
+ if (!rootEditableElement)
+ return false;
yabinh 2016/08/03 05:12:09 Note that |m_compositionRange->startOffset()| is r
+ PlainTextRange compositionRange = PlainTextRange::create(*rootEditableElement, *m_compositionRange);
+
+ if (text.length() == 0)
+ newCursorPosition += compositionRange.end();
Changwan Ryu 2016/08/03 07:37:30 Why should this differ from the else line?
yabinh 2016/08/08 07:33:44 There are 2 cases when hasCompositio() == true:
aelias_OOO_until_Jul13 2016/08/24 08:13:07 Acknowledged, but checking "text.length() == 0" is
aelias_OOO_until_Jul13 2016/08/24 08:16:01 Sorry, please disregard this particular comment, i
+ else
+ newCursorPosition += compositionRange.start();
+ } else {
+ PlainTextRange selectionRange = getSelectionOffsets();
+ if (selectionRange.isNull())
+ return false;
+ newCursorPosition += getSelectionOffsets().start();
+ }
+ }
+
if (!hasComposition()) {
if (!text.length())
return false;
@@ -243,19 +270,23 @@ bool InputMethodController::confirmCompositionOrInsertText(const String& text, C
return false;
editor().insertText(text, 0);
- return true;
- }
-
- if (text.length()) {
+ } else if (text.length()) {
confirmComposition(text);
- return true;
+ } else if (confirmBehavior == DoNotKeepSelection) {
+ if (!confirmComposition(composingText(), DoNotKeepSelection))
+ return false;
+ } else {
+ SelectionOffsetsScope selectionOffsetsScope(this);
+ return confirmComposition();
}
- if (confirmBehavior == DoNotKeepSelection)
- return confirmComposition(composingText(), DoNotKeepSelection);
+ if (confirmBehavior == KeepSelection)
+ return true;
- SelectionOffsetsScope selectionOffsetsScope(this);
- return confirmComposition();
+ PlainTextRange selectedRange = createRangeForSelection(newCursorPosition, newCursorPosition, 0);
+ if (selectedRange.isNull())
+ return false;
+ return setEditableSelectionOffsets(selectedRange);
}
void InputMethodController::cancelComposition()
@@ -524,6 +555,9 @@ PlainTextRange InputMethodController::createRangeForSelection(int start, int end
if (range.isNull())
return PlainTextRange();
yabinh 2016/08/03 05:12:09 There is a DCHECK in the constructor of TextIterat
+ if (frame().document()->needsLayoutTreeUpdate())
Changwan Ryu 2016/08/03 07:37:30 You probably need to call updateStyleAndLayoutTree
yabinh 2016/08/08 07:33:44 I tried to call it in InputMethodController#confir
+ return PlainTextRange();
+
const TextIteratorBehaviorFlags behaviorFlags = TextIteratorEmitsObjectReplacementCharacter | TextIteratorEmitsCharactersBetweenAllVisiblePositions;
TextIterator it(range.startPosition(), range.endPosition(), behaviorFlags);

Powered by Google App Engine
This is Rietveld 408576698