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

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: Move caret within composing text. Created 4 years, 3 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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 178
179 // The composition can start inside a composed character sequence, so we hav e to override checks. 179 // The composition can start inside a composed character sequence, so we hav e to override checks.
180 // See <http://bugs.webkit.org/show_bug.cgi?id=15781> 180 // See <http://bugs.webkit.org/show_bug.cgi?id=15781>
181 VisibleSelection selection; 181 VisibleSelection selection;
182 selection.setWithoutValidation(range.startPosition(), range.endPosition()); 182 selection.setWithoutValidation(range.startPosition(), range.endPosition());
183 frame().selection().setSelection(selection, 0); 183 frame().selection().setSelection(selection, 0);
184 } 184 }
185 185
186 bool InputMethodController::confirmComposition() 186 bool InputMethodController::confirmComposition()
187 { 187 {
188 return confirmComposition(composingText()); 188 return replaceComposition(composingText(), KeepSelection);
189 } 189 }
190 190
191 bool InputMethodController::confirmComposition(const String& text, ConfirmCompos itionBehavior confirmBehavior) 191 bool InputMethodController::replaceComposition(const String& text, ConfirmCompos itionBehavior confirmBehavior)
192 { 192 {
193 if (!hasComposition()) 193 if (!hasComposition())
194 return false; 194 return false;
195 195
196 Optional<Editor::RevealSelectionScope> revealSelectionScope; 196 Optional<Editor::RevealSelectionScope> revealSelectionScope;
197 if (confirmBehavior == KeepSelection) 197 if (confirmBehavior == KeepSelection)
198 revealSelectionScope.emplace(&editor()); 198 revealSelectionScope.emplace(&editor());
199 199
200 // If the composition was set from existing text and didn't change, then 200 // If the composition was set from existing text and didn't change, then
201 // there's nothing to do here (and we should avoid doing anything as that 201 // there's nothing to do here (and we should avoid doing anything as that
(...skipping 24 matching lines...) Expand all
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 // relativeCaretPosition is relative to the end of the text.
237 static int computeAbsoluteCaretPosition(size_t textStart, size_t textLength, int relativeCaretPosition)
238 {
239 return textStart + textLength + relativeCaretPosition;
240 }
241
242 bool InputMethodController::replaceCompositionAndMoveCaret(const String& text, i nt relativeCaretPosition)
243 {
244 Element* rootEditableElement = frame().selection().rootEditableElement();
245 if (!rootEditableElement)
246 return false;
247 PlainTextRange compositionRange = PlainTextRange::create(*rootEditableElemen t, *m_compositionRange);
248 if (compositionRange.isNull())
249 return false;
250 int textStart = compositionRange.start();
251
252 if (!replaceComposition(text, DoNotKeepSelection))
253 return false;
254
255 int absoluteCaretPosition = computeAbsoluteCaretPosition(textStart, text.len gth(), relativeCaretPosition);
256 return moveCaret(absoluteCaretPosition);
257 }
258
259 bool InputMethodController::commitComposition(const String& text)
237 { 260 {
238 if (!hasComposition()) { 261 if (!hasComposition()) {
239 if (!text.length()) 262 if (!text.length())
240 return false; 263 return false;
241 264 return insertText(text);
242 if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text) != DispatchEventResult::NotCanceled)
243 return false;
244
245 editor().insertText(text, 0);
246 return true;
247 } 265 }
248 266
249 if (text.length()) { 267 if (!text.length()) {
250 confirmComposition(text); 268 SelectionOffsetsScope selectionOffsetsScope(this);
251 return true; 269 return confirmComposition();
270 }
271 return replaceComposition(text, KeepSelection);
272 }
273
274 bool InputMethodController::commitCompositionAndMoveCaret(const String& text, in t relativeCaretPosition)
275 {
276 if (!hasComposition()) {
277 // We should do nothing in this case, because:
278 // 1. No need to insert text when text is empty.
279 // 2. Shouldn't move caret when relativeCaretPosition == 0 to avoid
280 // duplicate selection change event.
281 if (!text.length() && !relativeCaretPosition)
282 return false;
283 return insertTextAndMoveCaret(text, relativeCaretPosition);
252 } 284 }
253 285
254 if (confirmBehavior == DoNotKeepSelection) 286 if (!text.length())
255 return confirmComposition(composingText(), DoNotKeepSelection); 287 return replaceCompositionAndMoveCaret(composingText(), relativeCaretPosi tion);
288 return replaceCompositionAndMoveCaret(text, relativeCaretPosition);
289 }
256 290
257 SelectionOffsetsScope selectionOffsetsScope(this); 291 bool InputMethodController::insertText(const String& text)
258 return confirmComposition(); 292 {
293 if (dispatchBeforeInputInsertText(frame().document()->focusedElement(), text ) != DispatchEventResult::NotCanceled)
294 return false;
295 editor().insertText(text, 0);
296 return true;
297 }
298
299 bool InputMethodController::insertTextAndMoveCaret(const String& text, int relat iveCaretPosition)
300 {
301 PlainTextRange selectionRange = getSelectionOffsets();
302 if (selectionRange.isNull())
303 return false;
304 int textStart = selectionRange.start();
305
306 if (text.length()) {
307 if (!insertText(text))
308 return false;
309 }
310
311 int absoluteCaretPosition = computeAbsoluteCaretPosition(textStart, text.len gth(), relativeCaretPosition);
312 return moveCaret(absoluteCaretPosition);
259 } 313 }
260 314
261 void InputMethodController::cancelComposition() 315 void InputMethodController::cancelComposition()
262 { 316 {
263 if (!hasComposition()) 317 if (!hasComposition())
264 return; 318 return;
265 319
266 Editor::RevealSelectionScope revealSelectionScope(&editor()); 320 Editor::RevealSelectionScope revealSelectionScope(&editor());
267 321
268 if (frame().selection().isNone()) 322 if (frame().selection().isNone())
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 // Sending a compositionupdate event at this time ensures that at least o ne 390 // Sending a compositionupdate event at this time ensures that at least o ne
337 // compositionupdate event is dispatched. 391 // compositionupdate event is dispatched.
338 // 2. Updating the existing composition node. 392 // 2. Updating the existing composition node.
339 // Send a compositionupdate event when this function updates the existing composition 393 // Send a compositionupdate event when this function updates the existing composition
340 // node, i.e. hasComposition() && !text.isEmpty(). 394 // node, i.e. hasComposition() && !text.isEmpty().
341 // 3. Canceling the ongoing composition. 395 // 3. Canceling the ongoing composition.
342 // Send a compositionend event when function deletes the existing composi tion node, i.e. 396 // Send a compositionend event when function deletes the existing composi tion node, i.e.
343 // !hasComposition() && test.isEmpty(). 397 // !hasComposition() && test.isEmpty().
344 if (text.isEmpty()) { 398 if (text.isEmpty()) {
345 if (hasComposition()) { 399 if (hasComposition()) {
346 confirmComposition(emptyString()); 400 replaceComposition(emptyString(), KeepSelection);
347 } else { 401 } else {
348 // It's weird to call |setComposition()| with empty text outside com position, however some IME 402 // It's weird to call |setComposition()| with empty text outside com position, however some IME
349 // (e.g. Japanese IBus-Anthy) did this, so we simply delete selectio n without sending extra events. 403 // (e.g. Japanese IBus-Anthy) did this, so we simply delete selectio n without sending extra events.
350 TypingCommand::deleteSelection(*frame().document(), TypingCommand::P reventSpellChecking); 404 TypingCommand::deleteSelection(*frame().document(), TypingCommand::P reventSpellChecking);
351 } 405 }
352 406
353 setEditableSelectionOffsets(selectedRange); 407 setEditableSelectionOffsets(selectedRange);
354 return; 408 return;
355 } 409 }
356 410
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 590
537 rightBoundary += textLength; 591 rightBoundary += textLength;
538 592
539 // In case of exceeding the right boundary. 593 // In case of exceeding the right boundary.
540 start = std::min(start, rightBoundary); 594 start = std::min(start, rightBoundary);
541 end = std::min(end, rightBoundary); 595 end = std::min(end, rightBoundary);
542 596
543 return PlainTextRange(start, end); 597 return PlainTextRange(start, end);
544 } 598 }
545 599
600 bool InputMethodController::moveCaret(int newCaretPosition)
601 {
602 frame().document()->updateStyleAndLayoutIgnorePendingStylesheets();
603
604 PlainTextRange selectedRange = createRangeForSelection(newCaretPosition, new CaretPosition, 0);
605 if (selectedRange.isNull())
606 return false;
607 return setEditableSelectionOffsets(selectedRange);
608 }
609
546 void InputMethodController::extendSelectionAndDelete(int before, int after) 610 void InputMethodController::extendSelectionAndDelete(int before, int after)
547 { 611 {
548 if (!editor().canEdit()) 612 if (!editor().canEdit())
549 return; 613 return;
550 PlainTextRange selectionOffsets(getSelectionOffsets()); 614 PlainTextRange selectionOffsets(getSelectionOffsets());
551 if (selectionOffsets.isNull()) 615 if (selectionOffsets.isNull())
552 return; 616 return;
553 617
554 // A common call of before=1 and after=0 will fail if the last character 618 // A common call of before=1 and after=0 will fail if the last character
555 // is multi-code-word UTF-16, including both multi-16bit code-points and 619 // is multi-code-word UTF-16, including both multi-16bit code-points and
(...skipping 19 matching lines...) Expand all
575 TypingCommand::deleteSelection(*frame().document()); 639 TypingCommand::deleteSelection(*frame().document());
576 } 640 }
577 641
578 DEFINE_TRACE(InputMethodController) 642 DEFINE_TRACE(InputMethodController)
579 { 643 {
580 visitor->trace(m_frame); 644 visitor->trace(m_frame);
581 visitor->trace(m_compositionRange); 645 visitor->trace(m_compositionRange);
582 } 646 }
583 647
584 } // namespace blink 648 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698