OLD | NEW |
---|---|
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 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
678 } while (frame().selection().start() == frame().selection().end() && | 678 } while (frame().selection().start() == frame().selection().end() && |
679 before <= static_cast<int>(selectionOffsets.start())); | 679 before <= static_cast<int>(selectionOffsets.start())); |
680 // TODO(chongz): Find a way to distinguish Forward and Backward. | 680 // TODO(chongz): Find a way to distinguish Forward and Backward. |
681 dispatchBeforeInputEditorCommand( | 681 dispatchBeforeInputEditorCommand( |
682 m_frame->document()->focusedElement(), | 682 m_frame->document()->focusedElement(), |
683 InputEvent::InputType::DeleteContentBackward, | 683 InputEvent::InputType::DeleteContentBackward, |
684 new RangeVector(1, m_frame->selection().firstRange())); | 684 new RangeVector(1, m_frame->selection().firstRange())); |
685 TypingCommand::deleteSelection(*frame().document()); | 685 TypingCommand::deleteSelection(*frame().document()); |
686 } | 686 } |
687 | 687 |
688 // TODO(yabinh): We should reduce the number of selectionchange events. | |
689 void InputMethodController::deleteSurroundingText(int before, int after) { | |
690 if (!editor().canEdit()) | |
691 return; | |
692 PlainTextRange selectionOffsets(getSelectionOffsets()); | |
693 if (selectionOffsets.isNull()) | |
694 return; | |
695 Element* rootEditableElement = frame().selection().rootEditableElement(); | |
696 if (!rootEditableElement) | |
697 return; | |
698 | |
699 int selectionStart = static_cast<int>(selectionOffsets.start()); | |
700 int selectionEnd = static_cast<int>(selectionOffsets.end()); | |
701 | |
702 if (before > 0 && selectionStart > 0) { | |
703 // In case of exceeding the left boundary. | |
704 int start = std::max(selectionStart - before, 0); | |
705 | |
706 // Select the text to be deleted before selectionStart. | |
707 // We should adjust the start of selection for multi-code text(a grapheme | |
708 // cluster contains more than one code point). | |
709 const EphemeralRange& range = | |
710 PlainTextRange(0, start).createRange(*rootEditableElement); | |
711 if (range.isNull()) | |
712 return; | |
713 const Position& position = range.endPosition(); | |
714 // TODO(yabinh): Deletion should be based on code point instead of grapheme | |
715 // cluster. | |
716 const Position& adjustedPosition = previousPositionOf( | |
yosin_UTC9
2016/10/12 08:34:17
I saw this pattern in another CL. Can we have func
yabinh
2016/10/13 01:44:33
Done.
| |
717 nextPositionOf(position, PositionMoveType::GraphemeCluster), | |
718 PositionMoveType::GraphemeCluster); | |
719 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | |
720 DCHECK_LE(adjustedPosition.computeOffsetInContainerNode(), | |
721 position.computeOffsetInContainerNode()); | |
722 const int diff = adjustedPosition.computeOffsetInContainerNode() - | |
723 position.computeOffsetInContainerNode(); | |
724 start = start + diff; | |
yosin_UTC9
2016/10/12 08:34:17
Let's introduce new variable, e.g. |adjustedStart|
yabinh
2016/10/13 01:44:33
Done.
| |
725 | |
726 if (!setSelectionOffsets(PlainTextRange(start, selectionStart))) | |
727 return; | |
728 TypingCommand::deleteSelection(*frame().document()); | |
729 | |
730 selectionEnd = selectionEnd - (selectionStart - start); | |
731 selectionStart = start; | |
732 } | |
733 | |
734 if (after > 0) { | |
735 // Adjust the deleted range in case of exceeding the right boundary. | |
736 PlainTextRange range(0, selectionEnd + after); | |
737 if (range.isNull()) | |
738 return; | |
739 const EphemeralRange& validRange = range.createRange(*rootEditableElement); | |
740 if (validRange.isNull()) | |
741 return; | |
742 int end = PlainTextRange::create(*rootEditableElement, validRange).end(); | |
743 | |
744 // We also need to adjust the end of selection for multi-code text. | |
745 const Position& position = validRange.endPosition(); | |
746 const Position& adjustedPosition = nextPositionOf( | |
747 previousPositionOf(position, PositionMoveType::GraphemeCluster), | |
748 PositionMoveType::GraphemeCluster); | |
749 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | |
750 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(), | |
751 position.computeOffsetInContainerNode()); | |
752 const int diff = adjustedPosition.computeOffsetInContainerNode() - | |
753 position.computeOffsetInContainerNode(); | |
754 end = end + diff; | |
yosin_UTC9
2016/10/12 08:34:17
Let's introduce new variable, e.g. |adjustedEnd|.
yabinh
2016/10/13 01:44:33
Done.
| |
755 | |
756 if (!setSelectionOffsets(PlainTextRange(selectionEnd, end))) | |
757 return; | |
758 TypingCommand::deleteSelection(*frame().document()); | |
759 } | |
760 | |
761 setSelectionOffsets(PlainTextRange(selectionStart, selectionEnd)); | |
762 } | |
763 | |
688 DEFINE_TRACE(InputMethodController) { | 764 DEFINE_TRACE(InputMethodController) { |
689 visitor->trace(m_frame); | 765 visitor->trace(m_frame); |
690 visitor->trace(m_compositionRange); | 766 visitor->trace(m_compositionRange); |
691 } | 767 } |
692 | 768 |
693 } // namespace blink | 769 } // namespace blink |
OLD | NEW |