Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1166)

Unified Diff: third_party/WebKit/Source/core/editing/InputMethodController.cpp

Issue 1889053003: Fix InputConnection.deleteSurroundingText() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: change for changwan@'s review Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);

Powered by Google App Engine
This is Rietveld 408576698