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 0bd3b86d91b2380d6a7380e89bc63feae01cc7e0..da90163bcb938013076bc6d06deb593c842c1506 100644 |
--- a/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
+++ b/third_party/WebKit/Source/core/editing/InputMethodController.cpp |
@@ -548,6 +548,85 @@ void InputMethodController::extendSelectionAndDelete(int before, int after) |
TypingCommand::deleteSelection(*frame().document()); |
} |
+// TODO(yabinh): We should reduce the number of selectionchange events. |
+void InputMethodController::deleteSurroundingText(size_t before, size_t after) |
+{ |
+ if (!editor().canEdit()) |
+ return; |
+ PlainTextRange selectionOffsets(getSelectionOffsets()); |
+ if (selectionOffsets.isNull()) |
+ return; |
+ Element* rootEditableElement = frame().selection().rootEditableElement(); |
+ if (!rootEditableElement) |
+ return; |
+ |
+ size_t selectionStart = selectionOffsets.start(); |
+ size_t selectionEnd = selectionOffsets.end(); |
+ |
+ if (before > 0u && selectionStart > 0u) { |
+ // In case of exceeding the left boundary. |
+ int start = std::max(static_cast<int>(selectionStart) - static_cast<int>(before), 0); |
+ |
+ if (rootEditableElement->innerText()[start] != '\n') { |
+ // 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. |
+ const EphemeralRange range = PlainTextRange(start, start + 1).createRange(*rootEditableElement); |
+ if (range.isNull()) |
+ return; |
+ |
+ Position position = range.startPosition(); |
+ Position nextPosition = range.endPosition(); |
+ Position adjustedPosition = previousPositionOf(nextPosition, PositionMoveType::GraphemeCluster); |
+ |
+ int diff = adjustedPosition.computeOffsetInContainerNode() - position.computeOffsetInContainerNode(); |
+ start = start + diff; |
+ } |
+ |
+ if (!setSelectionOffsets(PlainTextRange(start, static_cast<int>(selectionStart)))) |
+ return; |
+ TypingCommand::deleteSelection(*frame().document()); |
+ |
+ selectionEnd = selectionEnd - (selectionStart - start); |
+ selectionStart = start; |
+ } |
+ |
+ if (after > 0u) { |
+ // Adjust the deleted range in case of exceeding the right boundary. |
+ PlainTextRange deletionRange(static_cast<int>(selectionEnd), static_cast<int>(selectionEnd + after)); |
+ if (deletionRange.isNull()) |
+ return; |
+ const EphemeralRange validRange = deletionRange.createRange(*rootEditableElement); |
+ if (validRange.isNull()) |
+ return; |
+ PlainTextRange validDeletionRange = PlainTextRange::create(*rootEditableElement, validRange); |
+ if (validDeletionRange.isNull()) |
+ return; |
+ int end = validDeletionRange.end(); |
+ |
+ if (rootEditableElement->innerText()[end - 1] != '\n') { |
+ // We also need to adjust the end of selection for multi-code text. |
+ const EphemeralRange range = PlainTextRange(end - 1, end).createRange(*rootEditableElement); |
+ if (range.isNull()) |
+ return; |
+ |
+ Position position = range.endPosition(); |
+ Position previousPosition = range.startPosition(); |
+ Position adjustedPosition = nextPositionOf(previousPosition, PositionMoveType::GraphemeCluster); |
+ |
+ int diff = adjustedPosition.computeOffsetInContainerNode() - position.computeOffsetInContainerNode(); |
+ end = end + diff; |
+ } |
+ |
+ if (!setSelectionOffsets(PlainTextRange(static_cast<int>(selectionEnd), end))) |
+ return; |
+ TypingCommand::deleteSelection(*frame().document()); |
+ } |
+ |
+ setSelectionOffsets(PlainTextRange(selectionStart, selectionEnd)); |
+} |
+ |
DEFINE_TRACE(InputMethodController) |
{ |
visitor->trace(m_frame); |