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 df935114b462563caaec2e0304e1baef383c096a..84dc6673e0bccb51c3c1d1a1b046781ed25a363f 100644 |
| --- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
| +++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
| @@ -338,6 +338,16 @@ void InputMethodController::cancelCompositionIfSelectionIsInvalid() |
| frame().chromeClient().didCancelCompositionOnSelectionChange(); |
| } |
| +static size_t computeCommonPrefixLength(const String& str1, const String& str2) |
| +{ |
| + size_t commonPrefixLength = 0; |
| + size_t length1 = str1.length(); |
| + size_t length2 = str2.length(); |
| + while (commonPrefixLength < length1 && commonPrefixLength < length2 && str1[commonPrefixLength] == str2[commonPrefixLength]) |
|
Changwan Ryu
2016/09/27 05:23:03
Can you skip a loop for surrogate pairs? Things ca
yabinh
2016/09/27 08:47:20
Done.
|
| + commonPrefixLength++; |
| + return commonPrefixLength; |
| +} |
| + |
| void InputMethodController::setComposition(const String& text, const Vector<CompositionUnderline>& underlines, int selectionStart, int selectionEnd) |
| { |
| Editor::RevealSelectionScope revealSelectionScope(&editor()); |
| @@ -347,6 +357,58 @@ void InputMethodController::setComposition(const String& text, const Vector<Comp |
| // See https://bugs.webkit.org/show_bug.cgi?id=46868 |
| frame().document()->updateStyleAndLayoutTree(); |
| + // When the IME only wants to change a few characters at the end of the |
| + // composition, only touch those characters in order to preserve rich text |
| + // substructure. |
| + if (hasComposition() && text.length()) { |
| + String composing = composingText(); |
| + size_t commonPrefixLength = computeCommonPrefixLength(composing, text); |
| + |
| + bool appending = text.length() > commonPrefixLength; |
| + bool backspacing = composing.length() > commonPrefixLength; |
| + if (appending || backspacing) { |
| + const EphemeralRange range = compositionEphemeralRange(); |
| + Element* editable = frame().selection().rootEditableElement(); |
| + if (!editable) |
| + return; |
| + |
| + // Move selection to the end of the composition. |
| + PlainTextRange compositionPlainOffsets = PlainTextRange::create(*editable, range); |
| + VisibleSelection selection; |
| + selection.setWithoutValidation(range.endPosition(), range.endPosition()); |
| + frame().selection().setSelection(selection, 0); |
| + clear(); |
| + |
| + Element* target = frame().document()->focusedElement(); |
| + if (!target) |
| + return; |
| + |
| + // Apply the incremental change. |
| + if (backspacing) |
| + extendSelectionAndDelete(composing.length() - commonPrefixLength, 0); |
|
aelias_OOO_until_Jul13
2016/09/27 04:55:58
Hmm, this has the same JS event problem as the oth
yabinh
2016/09/27 08:47:20
Done.
|
| + if (appending) |
| + insertTextDuringCompositionWithEvents(frame(), text.substring(commonPrefixLength), TypingCommand::PreventSpellChecking, TypingCommand::TextCompositionUpdate); |
| + |
| + // Event handlers might destroy document. |
| + if (!frame().document()) |
| + return; |
| + |
| + // TODO(yosin): The use of updateStyleAndLayoutIgnorePendingStylesheets |
| + // needs to be audited. see http://crbug.com/590369 for more details. |
| + frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); |
| + |
| + // Now recreate the composition starting at its original start, and |
| + // apply the specified final selection offsets. |
| + setCompositionFromExistingText(underlines, compositionPlainOffsets.start(), compositionPlainOffsets.start() + text.length()); |
| + selectComposition(); |
| + PlainTextRange selectedRange = createSelectionRangeForSetComposition(selectionStart, selectionEnd, text.length()); |
| + // We shouldn't close typing in the middle of setComposition. |
| + setEditableSelectionOffsets(selectedRange, NotUserTriggered); |
| + m_isDirty = true; |
| + return; |
| + } |
| + } |
| + |
| selectComposition(); |
| if (frame().selection().isNone()) |
| @@ -356,14 +418,7 @@ void InputMethodController::setComposition(const String& text, const Vector<Comp |
| if (!target) |
| return; |
| - // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets |
| - // needs to be audited. see http://crbug.com/590369 for more details. |
| - frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); |
| - |
| - int selectionOffsetsStart = static_cast<int>(getSelectionOffsets().start()); |
| - int start = selectionOffsetsStart + selectionStart; |
| - int end = selectionOffsetsStart + selectionEnd; |
| - PlainTextRange selectedRange = createRangeForSelection(start, end, text.length()); |
| + PlainTextRange selectedRange = createSelectionRangeForSetComposition(selectionStart, selectionEnd, text.length()); |
| // Dispatch an appropriate composition event to the focused node. |
| // We check the composition status and choose an appropriate composition event since this |
| @@ -466,6 +521,18 @@ void InputMethodController::setComposition(const String& text, const Vector<Comp |
| } |
| } |
| +PlainTextRange InputMethodController::createSelectionRangeForSetComposition(int selectionStart, int selectionEnd, size_t textLength) const |
| +{ |
| + // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets |
| + // needs to be audited. see http://crbug.com/590369 for more details. |
| + frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); |
| + |
| + int selectionOffsetsStart = static_cast<int>(getSelectionOffsets().start()); |
| + int start = selectionOffsetsStart + selectionStart; |
| + int end = selectionOffsetsStart + selectionEnd; |
| + return createRangeForSelection(start, end, textLength); |
| +} |
| + |
| void InputMethodController::setCompositionFromExistingText(const Vector<CompositionUnderline>& underlines, unsigned compositionStart, unsigned compositionEnd) |
| { |
| Element* editable = frame().selection().rootEditableElement(); |