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

Side by Side Diff: third_party/WebKit/Source/core/editing/InputMethodController.cpp

Issue 1995333002: Handle newCursorPosition correctly for Android's commitText() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: adjust selection in confirmCompositionOrInsertText() Created 4 years, 4 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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 // Event handler might destroy document. 226 // Event handler might destroy document.
227 if (!frame().document()) 227 if (!frame().document())
228 return false; 228 return false;
229 229
230 // No DOM update after 'compositionend'. 230 // No DOM update after 'compositionend'.
231 dispatchCompositionEndEvent(frame(), text); 231 dispatchCompositionEndEvent(frame(), text);
232 232
233 return true; 233 return true;
234 } 234 }
235 235
236 bool InputMethodController::confirmCompositionOrInsertText(const String& text, C onfirmCompositionBehavior confirmBehavior) 236 bool InputMethodController::confirmCompositionOrInsertText(const String& text, C onfirmCompositionBehavior confirmBehavior, int newCursorPosition)
237 { 237 {
238 if (confirmBehavior == DoNotKeepSelection) {
239 // If newCursorPosition > 0, it's relative to the end of the text - 1;
240 // if newCursorPosition <= 0, it is relative to the start of the text.
241 if (newCursorPosition > 0)
242 newCursorPosition = newCursorPosition + text.length() - 1;
Changwan Ryu 2016/08/03 07:37:30 newCursorPosition += text.length() - 1;
yabinh 2016/08/08 07:33:43 Done.
243
244 // According to Android documentation, the new cursor position is
245 // relative to the composing text if any, or relative to the selection
246 // if no composing text.
247 if (hasComposition()) {
248 Element* rootEditableElement = frame().selection().rootEditableEleme nt();
249 if (!rootEditableElement)
250 return false;
yabinh 2016/08/03 05:12:09 Note that |m_compositionRange->startOffset()| is r
251 PlainTextRange compositionRange = PlainTextRange::create(*rootEditab leElement, *m_compositionRange);
252
253 if (text.length() == 0)
254 newCursorPosition += compositionRange.end();
Changwan Ryu 2016/08/03 07:37:30 Why should this differ from the else line?
yabinh 2016/08/08 07:33:44 There are 2 cases when hasCompositio() == true:
aelias_OOO_until_Jul13 2016/08/24 08:13:07 Acknowledged, but checking "text.length() == 0" is
aelias_OOO_until_Jul13 2016/08/24 08:16:01 Sorry, please disregard this particular comment, i
255 else
256 newCursorPosition += compositionRange.start();
257 } else {
258 PlainTextRange selectionRange = getSelectionOffsets();
259 if (selectionRange.isNull())
260 return false;
261 newCursorPosition += getSelectionOffsets().start();
262 }
263 }
264
238 if (!hasComposition()) { 265 if (!hasComposition()) {
239 if (!text.length()) 266 if (!text.length())
240 return false; 267 return false;
241 268
242 if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text) != DispatchEventResult::NotCanceled) 269 if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text) != DispatchEventResult::NotCanceled)
243 return false; 270 return false;
244 271
245 editor().insertText(text, 0); 272 editor().insertText(text, 0);
246 return true; 273 } else if (text.length()) {
274 confirmComposition(text);
275 } else if (confirmBehavior == DoNotKeepSelection) {
276 if (!confirmComposition(composingText(), DoNotKeepSelection))
277 return false;
278 } else {
279 SelectionOffsetsScope selectionOffsetsScope(this);
280 return confirmComposition();
247 } 281 }
248 282
249 if (text.length()) { 283 if (confirmBehavior == KeepSelection)
250 confirmComposition(text);
251 return true; 284 return true;
252 }
253 285
254 if (confirmBehavior == DoNotKeepSelection) 286 PlainTextRange selectedRange = createRangeForSelection(newCursorPosition, ne wCursorPosition, 0);
255 return confirmComposition(composingText(), DoNotKeepSelection); 287 if (selectedRange.isNull())
256 288 return false;
257 SelectionOffsetsScope selectionOffsetsScope(this); 289 return setEditableSelectionOffsets(selectedRange);
258 return confirmComposition();
259 } 290 }
260 291
261 void InputMethodController::cancelComposition() 292 void InputMethodController::cancelComposition()
262 { 293 {
263 if (!hasComposition()) 294 if (!hasComposition())
264 return; 295 return;
265 296
266 Editor::RevealSelectionScope revealSelectionScope(&editor()); 297 Editor::RevealSelectionScope revealSelectionScope(&editor());
267 298
268 if (frame().selection().isNone()) 299 if (frame().selection().isNone())
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 start = std::max(start, 0); 547 start = std::max(start, 0);
517 end = std::max(end, start); 548 end = std::max(end, start);
518 549
519 // In case of exceeding the right boundary. 550 // In case of exceeding the right boundary.
520 Element* rootEditableElement = frame().selection().rootEditableElement(); 551 Element* rootEditableElement = frame().selection().rootEditableElement();
521 if (!rootEditableElement) 552 if (!rootEditableElement)
522 return PlainTextRange(); 553 return PlainTextRange();
523 const EphemeralRange& range = EphemeralRange::rangeOfContents(*rootEditableE lement); 554 const EphemeralRange& range = EphemeralRange::rangeOfContents(*rootEditableE lement);
524 if (range.isNull()) 555 if (range.isNull())
525 return PlainTextRange(); 556 return PlainTextRange();
526 557
yabinh 2016/08/03 05:12:09 There is a DCHECK in the constructor of TextIterat
558 if (frame().document()->needsLayoutTreeUpdate())
Changwan Ryu 2016/08/03 07:37:30 You probably need to call updateStyleAndLayoutTree
yabinh 2016/08/08 07:33:44 I tried to call it in InputMethodController#confir
559 return PlainTextRange();
560
527 const TextIteratorBehaviorFlags behaviorFlags = TextIteratorEmitsObjectRepla cementCharacter | TextIteratorEmitsCharactersBetweenAllVisiblePositions; 561 const TextIteratorBehaviorFlags behaviorFlags = TextIteratorEmitsObjectRepla cementCharacter | TextIteratorEmitsCharactersBetweenAllVisiblePositions;
528 TextIterator it(range.startPosition(), range.endPosition(), behaviorFlags); 562 TextIterator it(range.startPosition(), range.endPosition(), behaviorFlags);
529 563
530 int rightBoundary = 0; 564 int rightBoundary = 0;
531 for (; !it.atEnd(); it.advance()) 565 for (; !it.atEnd(); it.advance())
532 rightBoundary += it.length(); 566 rightBoundary += it.length();
533 567
534 if (hasComposition()) 568 if (hasComposition())
535 rightBoundary -= compositionRange()->text().length(); 569 rightBoundary -= compositionRange()->text().length();
536 570
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 TypingCommand::deleteSelection(*frame().document()); 608 TypingCommand::deleteSelection(*frame().document());
575 } 609 }
576 610
577 DEFINE_TRACE(InputMethodController) 611 DEFINE_TRACE(InputMethodController)
578 { 612 {
579 visitor->trace(m_frame); 613 visitor->trace(m_frame);
580 visitor->trace(m_compositionRange); 614 visitor->trace(m_compositionRange);
581 } 615 }
582 616
583 } // namespace blink 617 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698