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

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: Move caret within composing text. 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 343e89aad7a0aaf3644dca5f79449d6d75138bc9..a41f39ca7914157aa65620fe70f9be88cc95ed44 100644
--- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp
+++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp
@@ -185,10 +185,10 @@ void InputMethodController::selectComposition() const
bool InputMethodController::confirmComposition()
{
- return confirmComposition(composingText());
+ return replaceComposition(composingText(), KeepSelection);
}
-bool InputMethodController::confirmComposition(const String& text, ConfirmCompositionBehavior confirmBehavior)
+bool InputMethodController::replaceComposition(const String& text, ConfirmCompositionBehavior confirmBehavior)
{
if (!hasComposition())
return false;
@@ -233,29 +233,83 @@ bool InputMethodController::confirmComposition(const String& text, ConfirmCompos
return true;
}
-bool InputMethodController::confirmCompositionOrInsertText(const String& text, ConfirmCompositionBehavior confirmBehavior)
+// relativeCaretPosition is relative to the end of the text.
+static int computeAbsoluteCaretPosition(size_t textStart, size_t textLength, int relativeCaretPosition)
+{
+ return textStart + textLength + relativeCaretPosition;
+}
+
+bool InputMethodController::replaceCompositionAndMoveCaret(const String& text, int relativeCaretPosition)
+{
+ Element* rootEditableElement = frame().selection().rootEditableElement();
+ if (!rootEditableElement)
+ return false;
+ PlainTextRange compositionRange = PlainTextRange::create(*rootEditableElement, *m_compositionRange);
+ if (compositionRange.isNull())
+ return false;
+ int textStart = compositionRange.start();
+
+ if (!replaceComposition(text, DoNotKeepSelection))
+ return false;
+
+ int absoluteCaretPosition = computeAbsoluteCaretPosition(textStart, text.length(), relativeCaretPosition);
+ return moveCaret(absoluteCaretPosition);
+}
+
+bool InputMethodController::commitComposition(const String& text)
{
if (!hasComposition()) {
if (!text.length())
return false;
+ return insertText(text);
+ }
- if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text) != DispatchEventResult::NotCanceled)
- return false;
+ if (!text.length()) {
+ SelectionOffsetsScope selectionOffsetsScope(this);
+ return confirmComposition();
+ }
+ return replaceComposition(text, KeepSelection);
+}
- editor().insertText(text, 0);
- return true;
+bool InputMethodController::commitCompositionAndMoveCaret(const String& text, int relativeCaretPosition)
+{
+ if (!hasComposition()) {
+ // We should do nothing in this case, because:
+ // 1. No need to insert text when text is empty.
+ // 2. Shouldn't move caret when relativeCaretPosition == 0 to avoid
+ // duplicate selection change event.
+ if (!text.length() && !relativeCaretPosition)
+ return false;
+ return insertTextAndMoveCaret(text, relativeCaretPosition);
}
+ if (!text.length())
+ return replaceCompositionAndMoveCaret(composingText(), relativeCaretPosition);
+ return replaceCompositionAndMoveCaret(text, relativeCaretPosition);
+}
+
+bool InputMethodController::insertText(const String& text)
+{
+ if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text) != DispatchEventResult::NotCanceled)
+ return false;
+ editor().insertText(text, 0);
+ return true;
+}
+
+bool InputMethodController::insertTextAndMoveCaret(const String& text, int relativeCaretPosition)
+{
+ PlainTextRange selectionRange = getSelectionOffsets();
+ if (selectionRange.isNull())
+ return false;
+ int textStart = selectionRange.start();
+
if (text.length()) {
- confirmComposition(text);
- return true;
+ if (!insertText(text))
+ return false;
}
- if (confirmBehavior == DoNotKeepSelection)
- return confirmComposition(composingText(), DoNotKeepSelection);
-
- SelectionOffsetsScope selectionOffsetsScope(this);
- return confirmComposition();
+ int absoluteCaretPosition = computeAbsoluteCaretPosition(textStart, text.length(), relativeCaretPosition);
+ return moveCaret(absoluteCaretPosition);
}
void InputMethodController::cancelComposition()
@@ -343,7 +397,7 @@ void InputMethodController::setComposition(const String& text, const Vector<Comp
// !hasComposition() && test.isEmpty().
if (text.isEmpty()) {
if (hasComposition()) {
- confirmComposition(emptyString());
+ replaceComposition(emptyString(), KeepSelection);
} else {
// It's weird to call |setComposition()| with empty text outside composition, however some IME
// (e.g. Japanese IBus-Anthy) did this, so we simply delete selection without sending extra events.
@@ -543,6 +597,16 @@ PlainTextRange InputMethodController::createRangeForSelection(int start, int end
return PlainTextRange(start, end);
}
+bool InputMethodController::moveCaret(int newCaretPosition)
+{
+ frame().document()->updateStyleAndLayoutIgnorePendingStylesheets();
+
+ PlainTextRange selectedRange = createRangeForSelection(newCaretPosition, newCaretPosition, 0);
+ if (selectedRange.isNull())
+ return false;
+ return setEditableSelectionOffsets(selectedRange);
+}
+
void InputMethodController::extendSelectionAndDelete(int before, int after)
{
if (!editor().canEdit())

Powered by Google App Engine
This is Rietveld 408576698