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 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 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 // TODO(yabinh): We should reduce the number of selectionchange events.
552 void InputMethodController::deleteSurroundingText(size_t before, size_t after)
553 {
554 if (!editor().canEdit())
555 return;
556 PlainTextRange selectionOffsets(getSelectionOffsets());
557 if (selectionOffsets.isNull())
558 return;
559 Element* rootEditableElement = frame().selection().rootEditableElement();
560 if (!rootEditableElement)
561 return;
562
563 size_t selectionStart = selectionOffsets.start();
564 size_t selectionEnd = selectionOffsets.end();
565
566 if (before > 0u && selectionStart > 0u) {
567 // In case of exceeding the left boundary.
568 int start = std::max(static_cast<int>(selectionStart) - static_cast<int> (before), 0);
569
570 if (rootEditableElement->innerText()[start] != '\n') {
571 // Select the text to be deleted before selectionStart.
572 // For multi-code text, we can't select it successfully if we only
573 // select the right half of it. So we need to adjust the start of
574 // selection.
575 const EphemeralRange range = PlainTextRange(start, start + 1).create Range(*rootEditableElement);
576 if (range.isNull())
577 return;
578
579 Position position = range.startPosition();
580 Position nextPosition = range.endPosition();
581 Position adjustedPosition = previousPositionOf(nextPosition, Positio nMoveType::GraphemeCluster);
582
583 int diff = adjustedPosition.computeOffsetInContainerNode() - positio n.computeOffsetInContainerNode();
584 start = start + diff;
585 }
586
587 if (!setSelectionOffsets(PlainTextRange(start, static_cast<int>(selectio nStart))))
588 return;
589 TypingCommand::deleteSelection(*frame().document());
590
591 selectionEnd = selectionEnd - (selectionStart - start);
592 selectionStart = start;
593 }
594
595 if (after > 0u) {
596 // Adjust the deleted range in case of exceeding the right boundary.
597 PlainTextRange deletionRange(static_cast<int>(selectionEnd), static_cast <int>(selectionEnd + after));
598 if (deletionRange.isNull())
599 return;
600 const EphemeralRange validRange = deletionRange.createRange(*rootEditabl eElement);
601 if (validRange.isNull())
602 return;
603 PlainTextRange validDeletionRange = PlainTextRange::create(*rootEditable Element, validRange);
604 if (validDeletionRange.isNull())
605 return;
606 int end = validDeletionRange.end();
607
608 if (rootEditableElement->innerText()[end - 1] != '\n') {
609 // We also need to adjust the end of selection for multi-code text.
610 const EphemeralRange range = PlainTextRange(end - 1, end).createRang e(*rootEditableElement);
611 if (range.isNull())
612 return;
613
614 Position position = range.endPosition();
615 Position previousPosition = range.startPosition();
616 Position adjustedPosition = nextPositionOf(previousPosition, Positio nMoveType::GraphemeCluster);
617
618 int diff = adjustedPosition.computeOffsetInContainerNode() - positio n.computeOffsetInContainerNode();
619 end = end + diff;
620 }
621
622 if (!setSelectionOffsets(PlainTextRange(static_cast<int>(selectionEnd), end)))
623 return;
624 TypingCommand::deleteSelection(*frame().document());
625 }
626
627 setSelectionOffsets(PlainTextRange(selectionStart, selectionEnd));
628 }
629
551 DEFINE_TRACE(InputMethodController) 630 DEFINE_TRACE(InputMethodController)
552 { 631 {
553 visitor->trace(m_frame); 632 visitor->trace(m_frame);
554 visitor->trace(m_compositionRange); 633 visitor->trace(m_compositionRange);
555 } 634 }
556 635
557 } // namespace blink 636 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698