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 0d88e0f740a014a088b732b6340f8ca8bef0e15e..82000749ed97755646e9b2c8ab72fa8621b216c5 100644 |
--- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
+++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
@@ -31,8 +31,10 @@ |
#include "core/dom/Text.h" |
#include "core/editing/EditingUtilities.h" |
#include "core/editing/Editor.h" |
+#include "core/editing/commands/ReplaceSelectionCommand.h" |
#include "core/editing/commands/TypingCommand.h" |
#include "core/editing/markers/DocumentMarkerController.h" |
+#include "core/editing/serializers/Serialization.h" |
#include "core/events/CompositionEvent.h" |
#include "core/frame/LocalFrame.h" |
#include "core/html/HTMLTextAreaElement.h" |
@@ -468,6 +470,62 @@ void InputMethodController::extendSelectionAndDelete(int before, int after) |
TypingCommand::deleteSelection(*frame().document()); |
} |
+void InputMethodController::deleteSurroundingText(size_t before, size_t after) |
+{ |
+ if (!editor().canEdit()) |
+ return; |
+ PlainTextRange selectionOffsets(getSelectionOffsets()); |
+ if (selectionOffsets.isNull()) |
+ return; |
+ |
+ size_t selectionStart = selectionOffsets.start(); |
+ size_t selectionEnd = selectionOffsets.end(); |
+ int replaceStart = static_cast<int>(selectionStart); |
+ int replaceEnd = static_cast<int>(selectionEnd); |
+ |
+ Element* rootEditableElement = frame().selection().rootEditableElement(); |
+ if (!rootEditableElement) |
+ return; |
+ |
+ if (before > 0u) { |
+ // In case of exceeding the left boundary. |
+ before = std::min(selectionStart, before); |
Changwan Ryu
2016/05/02 05:58:28
This should be checked before the if statement. Wh
|
+ |
+ // Select the text to be deleted before selectionStart. |
+ // For multi-code text, we can't select it successfully if we only |
+ // select the right half of it. So we need to adjust the start of |
+ // selection. |
+ Position position(frame().selection().start().anchorNode(), selectionStart - before + 1); |
+ Position adjustedPosition = previousPositionOf(position, PositionMoveType::GraphemeCluster); |
+ replaceStart = adjustedPosition.computeOffsetInContainerNode(); |
+ } |
+ |
+ if (after > 0u) { |
+ // In case of exceeding the right boundary. |
+ PlainTextRange range(static_cast<int>(selectionEnd), static_cast<int>(selectionEnd + after)); |
+ if (range.isNull()) |
+ return; |
+ const EphemeralRange adjustedRange = range.createRange(*rootEditableElement); |
+ if (adjustedRange.isNull()) |
+ return; |
+ |
+ // Select the text to be deleted after selectionEnd. |
+ // We also need to adjust the end of selection for multi-code text. |
+ Position position(frame().selection().start().anchorNode(), adjustedRange.endPosition().computeOffsetInContainerNode() - 1); |
+ Position adjustedPosition = nextPositionOf(position, PositionMoveType::GraphemeCluster); |
+ replaceEnd = adjustedPosition.computeOffsetInContainerNode(); |
+ } |
+ |
+ String selectedText = frame().selection().selectedText(); |
+ if (!setSelectionOffsets(PlainTextRange(replaceStart, replaceEnd))) |
+ return; |
+ const EphemeralRange selectionRange = selectionOffsets.createRange(*rootEditableElement); |
+ if (selectionRange.isNull()) |
+ return; |
+ DocumentFragment* fragment = createFragmentFromText(selectionRange, selectedText); |
+ ReplaceSelectionCommand::create(*frame().document(), fragment, ReplaceSelectionCommand::SelectReplacement)->apply(); |
Changwan Ryu
2016/05/02 05:58:28
You shouldn't try to replace the selection with ne
|
+} |
+ |
DEFINE_TRACE(InputMethodController) |
{ |
visitor->trace(m_frame); |