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

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: Same to patch set 13, except adding a test for multiple nodes. Created 4 years, 6 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 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 if (before == 0) 541 if (before == 0)
542 break; 542 break;
543 ++before; 543 ++before;
544 } while (frame().selection().start() == frame().selection().end() && before <= static_cast<int>(selectionOffsets.start())); 544 } while (frame().selection().start() == frame().selection().end() && before <= static_cast<int>(selectionOffsets.start()));
545 // TODO(chongz): New spec might want to change InputType. 545 // TODO(chongz): New spec might want to change InputType.
546 // https://github.com/w3c/editing/issues/125#issuecomment-213041256 546 // https://github.com/w3c/editing/issues/125#issuecomment-213041256
547 dispatchBeforeInputEditorCommand(m_frame->document()->focusedElement(), Inpu tEvent::InputType::DeleteContent, emptyString(), new RangeVector(1, m_frame->sel ection().firstRange())); 547 dispatchBeforeInputEditorCommand(m_frame->document()->focusedElement(), Inpu tEvent::InputType::DeleteContent, emptyString(), new RangeVector(1, m_frame->sel ection().firstRange()));
548 TypingCommand::deleteSelection(*frame().document()); 548 TypingCommand::deleteSelection(*frame().document());
549 } 549 }
550 550
551 void InputMethodController::deleteSurroundingText(size_t before, size_t after)
Changwan Ryu 2016/06/20 10:03:24 Could you add a TODO to reduce the number of selec
yabinh 2016/06/20 10:22:23 Acknowledged.
552 {
553 if (!editor().canEdit())
554 return;
555 PlainTextRange selectionOffsets(getSelectionOffsets());
556 if (selectionOffsets.isNull())
557 return;
558
559 size_t selectionStart = selectionOffsets.start();
560 size_t selectionEnd = selectionOffsets.end();
561
562 if (before > 0u && selectionStart > 0u) {
563 // In case of exceeding the left boundary.
564 before = std::min(selectionStart, before);
565
566 // Select the text to be deleted before selectionStart.
567 // For multi-code text, we can't select it successfully if we only
568 // select the right half of it. So we need to adjust the start of
569 // selection.
570 Position position(frame().selection().start().anchorNode(), selectionSta rt - before + 1);
571 Position adjustedPosition = previousPositionOf(position, PositionMoveTyp e::GraphemeCluster);
572 int adjustedStart = adjustedPosition.computeOffsetInContainerNode();
573
574 if (!setSelectionOffsets(PlainTextRange(adjustedStart, static_cast<int>( selectionStart))))
575 return;
576 TypingCommand::deleteSelection(*frame().document());
577
578 selectionEnd = selectionEnd - (selectionStart - adjustedStart);
579 selectionStart = adjustedStart;
580 }
581
582 if (after > 0u) {
583 // Adjust the deleted range in case of exceeding the right boundary.
584 PlainTextRange range(static_cast<int>(selectionEnd), static_cast<int>(se lectionEnd + after));
585 if (range.isNull())
586 return;
587 Element* rootEditableElement = frame().selection().rootEditableElement() ;
588 if (!rootEditableElement)
589 return;
590 const EphemeralRange adjustedRange = range.createRange(*rootEditableElem ent);
591 if (adjustedRange.isNull())
592 return;
593
594 // Select the text to be deleted after selectionEnd.
595 // We also need to adjust the end of selection for multi-code text.
596 Position position(frame().selection().start().anchorNode(), adjustedRang e.endPosition().computeOffsetInContainerNode() - 1);
597 Position adjustedPosition = nextPositionOf(position, PositionMoveType::G raphemeCluster);
598 int adjustedEnd = adjustedPosition.computeOffsetInContainerNode();
599
600 if (!setSelectionOffsets(PlainTextRange(static_cast<int>(selectionEnd), adjustedEnd)))
601 return;
602 TypingCommand::deleteSelection(*frame().document());
603 }
604
605 setSelectionOffsets(PlainTextRange(selectionStart, selectionEnd));
606 }
607
551 DEFINE_TRACE(InputMethodController) 608 DEFINE_TRACE(InputMethodController)
552 { 609 {
553 visitor->trace(m_frame); 610 visitor->trace(m_frame);
554 visitor->trace(m_compositionRange); 611 visitor->trace(m_compositionRange);
555 } 612 }
556 613
557 } // namespace blink 614 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698