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..cdcda6da09a011e497bc9d22b0a0c5dbb79ec5ed 100644 |
| --- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
| +++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
| @@ -347,6 +347,52 @@ 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 = 0; |
|
yabinh
2016/09/26 10:10:32
aelias@ suggests to use std::mismatch(): https://c
Changwan Ryu
2016/09/26 13:51:36
std::mismatch accepts not only char* but any Input
yabinh
2016/09/27 04:30:22
Done.
Implemented a separate static function.
|
| + while (commonPrefixLength < composing.length() && commonPrefixLength < text.length() && composing[commonPrefixLength] == text[commonPrefixLength]) |
| + commonPrefixLength++; |
| + |
| + 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); |
| + if (appending) |
| + commitText(text.substring(commonPrefixLength), 0); |
| + |
| + // 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 +402,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 +505,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(); |