Chromium Code Reviews| 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..5f3a4de5afe9863d91d73e2840b20e858f91e8bf 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,31 +233,81 @@ bool InputMethodController::confirmComposition(const String& text, ConfirmCompos |
| return true; |
| } |
|
yabinh
2016/08/29 08:44:01
Move the comments here.
|
| -bool InputMethodController::confirmCompositionOrInsertText(const String& text, ConfirmCompositionBehavior confirmBehavior) |
| +// If > 0, relativeCaretPosition is relative to the end of the text - 1; |
| +// if <= 0, its' relative to the start of the text. |
| +// This is to match Android behavior. See |
| +// https://developer.android.com/reference/android/view/inputmethod/InputConnection.html#commitText(java.lang.CharSequence, int) |
|
tkent
2016/08/30 07:47:11
Even though this follows an Android standard API,
yabinh
2016/08/30 12:33:05
Done.
|
| +static int computeAbsoluteCaretPosition(size_t textStart, size_t textLength, int relativeCaretPosition) |
| { |
| - if (!hasComposition()) { |
| - if (!text.length()) |
| - return false; |
| + if (relativeCaretPosition > 0) |
| + relativeCaretPosition += textLength - 1; |
| + return textStart + relativeCaretPosition; |
| +} |
| - if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text) != DispatchEventResult::NotCanceled) |
| - return false; |
| +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(); |
| - editor().insertText(text, 0); |
| - return true; |
| - } |
| + if (!replaceComposition(text, DoNotKeepSelection)) |
| + return false; |
| + |
| + int absoluteCaretPosition = computeAbsoluteCaretPosition(textStart, text.length(), relativeCaretPosition); |
| + return moveCaret(absoluteCaretPosition); |
| +} |
| + |
| +bool InputMethodController::commitComposition(const String& text) |
| +{ |
| + if (!hasComposition()) |
| + return insertText(text); |
| if (text.length()) { |
| - confirmComposition(text); |
| + replaceComposition(text, KeepSelection); |
| return true; |
| } |
| - if (confirmBehavior == DoNotKeepSelection) |
| - return confirmComposition(composingText(), DoNotKeepSelection); |
| - |
| SelectionOffsetsScope selectionOffsetsScope(this); |
| return confirmComposition(); |
| } |
| +bool InputMethodController::commitCompositionAndMoveCaret(const String& text, int relativeCaretPosition) |
| +{ |
| + if (!hasComposition()) |
| + return insertTextAndMoveCaret(text, relativeCaretPosition); |
| + if (text.length()) |
| + return replaceCompositionAndMoveCaret(text, relativeCaretPosition); |
| + return replaceCompositionAndMoveCaret(composingText(), relativeCaretPosition); |
| +} |
| + |
| +bool InputMethodController::insertText(const String& text) |
| +{ |
| + if (!text.length()) |
| + return false; |
| + 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 (!insertText(text)) |
| + return false; |
| + |
| + int absoluteCaretPosition = computeAbsoluteCaretPosition(textStart, text.length(), relativeCaretPosition); |
| + return moveCaret(absoluteCaretPosition); |
| +} |
| + |
| void InputMethodController::cancelComposition() |
| { |
| if (!hasComposition()) |
| @@ -343,7 +393,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 +593,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()) |