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

Side by Side 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 back to patch set #25 Created 4 years, 2 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 } while (frame().selection().start() == frame().selection().end() && 861 } while (frame().selection().start() == frame().selection().end() &&
862 before <= static_cast<int>(selectionOffsets.start())); 862 before <= static_cast<int>(selectionOffsets.start()));
863 // TODO(chongz): Find a way to distinguish Forward and Backward. 863 // TODO(chongz): Find a way to distinguish Forward and Backward.
864 dispatchBeforeInputEditorCommand( 864 dispatchBeforeInputEditorCommand(
865 m_frame->document()->focusedElement(), 865 m_frame->document()->focusedElement(),
866 InputEvent::InputType::DeleteContentBackward, 866 InputEvent::InputType::DeleteContentBackward,
867 new RangeVector(1, m_frame->selection().firstRange())); 867 new RangeVector(1, m_frame->selection().firstRange()));
868 TypingCommand::deleteSelection(*frame().document()); 868 TypingCommand::deleteSelection(*frame().document());
869 } 869 }
870 870
871 // TODO(yabinh): We should reduce the number of selectionchange events.
872 void InputMethodController::deleteSurroundingText(int before, int after) {
873 if (!editor().canEdit())
874 return;
875 const PlainTextRange selectionOffsets(getSelectionOffsets());
876 if (selectionOffsets.isNull())
877 return;
878 Element* const rootEditableElement =
879 frame().selection().rootEditableElement();
880 if (!rootEditableElement)
881 return;
882 int selectionStart = static_cast<int>(selectionOffsets.start());
883 int selectionEnd = static_cast<int>(selectionOffsets.end());
884
885 // Select the text to be deleted before selectionStart.
886 if (before > 0 && selectionStart > 0) {
887 // In case of exceeding the left boundary.
888 const int start = std::max(selectionStart - before, 0);
889
890 const EphemeralRange& range =
891 PlainTextRange(0, start).createRange(*rootEditableElement);
892 if (range.isNull())
893 return;
894 const Position& position = range.endPosition();
895
896 // Adjust the start of selection for multi-code text(a grapheme cluster
897 // contains more than one code point). TODO(yabinh): Adjustment should be
898 // based on code point instead of grapheme cluster.
899 const size_t diff = computeDistanceToLeftGraphemeBoundary(position);
900 const int adjustedStart = start - static_cast<int>(diff);
901 if (!setSelectionOffsets(PlainTextRange(adjustedStart, selectionStart)))
902 return;
903 TypingCommand::deleteSelection(*frame().document());
904
905 selectionEnd = selectionEnd - (selectionStart - adjustedStart);
906 selectionStart = adjustedStart;
907 }
908
909 // Select the text to be deleted after selectionEnd.
910 if (after > 0) {
911 // Adjust the deleted range in case of exceeding the right boundary.
912 const PlainTextRange range(0, selectionEnd + after);
913 if (range.isNull())
914 return;
915 const EphemeralRange& validRange = range.createRange(*rootEditableElement);
916 if (validRange.isNull())
917 return;
918 const int end =
919 PlainTextRange::create(*rootEditableElement, validRange).end();
920 const Position& position = validRange.endPosition();
921
922 // Adjust the end of selection for multi-code text. TODO(yabinh): Adjustment
923 // should be based on code point instead of grapheme cluster.
924 const size_t diff = computeDistanceToRightGraphemeBoundary(position);
925 const int adjustedEnd = end + static_cast<int>(diff);
926 if (!setSelectionOffsets(PlainTextRange(selectionEnd, adjustedEnd)))
927 return;
928 TypingCommand::deleteSelection(*frame().document());
929 }
930
931 setSelectionOffsets(PlainTextRange(selectionStart, selectionEnd));
932 }
933
871 DEFINE_TRACE(InputMethodController) { 934 DEFINE_TRACE(InputMethodController) {
872 visitor->trace(m_frame); 935 visitor->trace(m_frame);
873 visitor->trace(m_compositionRange); 936 visitor->trace(m_compositionRange);
874 } 937 }
875 938
876 } // namespace blink 939 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698