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..31270b8c61ccdbfdae6a6cd5646dce3f095a518c 100644 |
| --- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
| +++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
| @@ -338,6 +338,30 @@ void InputMethodController::cancelCompositionIfSelectionIsInvalid() |
| frame().chromeClient().didCancelCompositionOnSelectionChange(); |
| } |
| +size_t InputMethodController::computeUTF16CommonPrefixLength(const String& str1, const String& str2) const |
|
Changwan Ryu
2016/09/27 09:17:11
could you change the name to something like comput
yabinh
2016/09/27 12:46:47
Done.
|
| +{ |
| + size_t commonPrefixLength = 0; |
| + size_t length1 = str1.length(); |
| + size_t length2 = str2.length(); |
| + while (commonPrefixLength < length1 && commonPrefixLength < length2 && str1[commonPrefixLength] == str2[commonPrefixLength]) |
| + commonPrefixLength++; |
| + if (commonPrefixLength == length1 || commonPrefixLength == length2) |
| + return commonPrefixLength; |
| + |
| + // For multi-code text, we should adjust it for grapheme boundary. |
| + Element* rootEditableElement = frame().selection().rootEditableElement(); |
| + if (!rootEditableElement) |
| + return 0; |
| + const EphemeralRange range = PlainTextRange(0, commonPrefixLength).createRange(*rootEditableElement); |
| + if (range.isNull()) |
| + return 0; |
| + Position position = range.endPosition(); |
|
Changwan Ryu
2016/09/27 09:17:11
The function has dependency on the current selecti
yabinh
2016/09/27 12:46:47
I haven't found that way, so I'll change the funct
|
| + Position adjustedPosition = previousPositionOf(nextPositionOf(position, PositionMoveType::GraphemeCluster), PositionMoveType::GraphemeCluster); |
| + int diff = position.computeOffsetInContainerNode() - adjustedPosition.computeOffsetInContainerNode(); |
| + |
| + return commonPrefixLength - static_cast<size_t>(diff); |
| +} |
| + |
| void InputMethodController::setComposition(const String& text, const Vector<CompositionUnderline>& underlines, int selectionStart, int selectionEnd) |
| { |
| Editor::RevealSelectionScope revealSelectionScope(&editor()); |
| @@ -347,6 +371,61 @@ 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 = computeUTF16CommonPrefixLength(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. Select the text to be deleted (if |
| + // needed) and replace it with the incremental text (or empty text). |
| + if (backspacing) { |
| + PlainTextRange selectionOffsets(getSelectionOffsets()); |
| + size_t deletedLength = composing.length() - commonPrefixLength; |
| + setEditableSelectionOffsets(PlainTextRange(selectionOffsets.start() - deletedLength, selectionOffsets.end()), NotUserTriggered); |
| + } |
| + 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 +435,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 +538,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(); |