| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/editing/commands/InsertIncrementalTextCommand.h" | 5 #include "core/editing/commands/InsertIncrementalTextCommand.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
| 9 #include "core/dom/Text.h" | 9 #include "core/dom/Text.h" |
| 10 #include "core/editing/EditingUtilities.h" | 10 #include "core/editing/EditingUtilities.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 nextPositionOf(position, PositionMoveType::GraphemeCluster), | 45 nextPositionOf(position, PositionMoveType::GraphemeCluster), |
| 46 PositionMoveType::GraphemeCluster); | 46 PositionMoveType::GraphemeCluster); |
| 47 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | 47 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); |
| 48 DCHECK_GE(position.computeOffsetInContainerNode(), | 48 DCHECK_GE(position.computeOffsetInContainerNode(), |
| 49 adjustedPosition.computeOffsetInContainerNode()); | 49 adjustedPosition.computeOffsetInContainerNode()); |
| 50 return static_cast<size_t>(position.computeOffsetInContainerNode() - | 50 return static_cast<size_t>(position.computeOffsetInContainerNode() - |
| 51 adjustedPosition.computeOffsetInContainerNode()); | 51 adjustedPosition.computeOffsetInContainerNode()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 size_t computeCommonGraphemeClusterPrefixLength( | 54 size_t computeCommonGraphemeClusterPrefixLength( |
| 55 const int selectionStart, |
| 55 const String& oldText, | 56 const String& oldText, |
| 56 const String& newText, | 57 const String& newText, |
| 57 const Element* rootEditableElement) { | 58 const Element* rootEditableElement) { |
| 58 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText); | 59 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText); |
| 59 | 60 |
| 60 // For grapheme cluster, we should adjust it for grapheme boundary. | 61 // For grapheme cluster, we should adjust it for grapheme boundary. |
| 61 const EphemeralRange& range = | 62 const EphemeralRange& range = |
| 62 PlainTextRange(0, commonPrefixLength).createRange(*rootEditableElement); | 63 PlainTextRange(0, selectionStart + commonPrefixLength) |
| 64 .createRange(*rootEditableElement); |
| 63 if (range.isNull()) | 65 if (range.isNull()) |
| 64 return 0; | 66 return 0; |
| 65 const Position& position = range.endPosition(); | 67 const Position& position = range.endPosition(); |
| 66 const size_t diff = computeDistanceToLeftGraphemeBoundary(position); | 68 const size_t diff = computeDistanceToLeftGraphemeBoundary(position); |
| 67 DCHECK_GE(commonPrefixLength, diff); | 69 DCHECK_GE(commonPrefixLength, diff); |
| 68 return commonPrefixLength - diff; | 70 return commonPrefixLength - diff; |
| 69 } | 71 } |
| 70 | 72 |
| 71 // If current position is at grapheme boundary, return 0; otherwise, return the | 73 // If current position is at grapheme boundary, return 0; otherwise, return the |
| 72 // distance to its nearest right grapheme boundary. | 74 // distance to its nearest right grapheme boundary. |
| 73 size_t computeDistanceToRightGraphemeBoundary(const Position& position) { | 75 size_t computeDistanceToRightGraphemeBoundary(const Position& position) { |
| 74 const Position& adjustedPosition = nextPositionOf( | 76 const Position& adjustedPosition = nextPositionOf( |
| 75 previousPositionOf(position, PositionMoveType::GraphemeCluster), | 77 previousPositionOf(position, PositionMoveType::GraphemeCluster), |
| 76 PositionMoveType::GraphemeCluster); | 78 PositionMoveType::GraphemeCluster); |
| 77 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | 79 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); |
| 78 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(), | 80 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(), |
| 79 position.computeOffsetInContainerNode()); | 81 position.computeOffsetInContainerNode()); |
| 80 return static_cast<size_t>(adjustedPosition.computeOffsetInContainerNode() - | 82 return static_cast<size_t>(adjustedPosition.computeOffsetInContainerNode() - |
| 81 position.computeOffsetInContainerNode()); | 83 position.computeOffsetInContainerNode()); |
| 82 } | 84 } |
| 83 | 85 |
| 84 size_t computeCommonGraphemeClusterSuffixLength( | 86 size_t computeCommonGraphemeClusterSuffixLength( |
| 87 const int selectionStart, |
| 85 const String& oldText, | 88 const String& oldText, |
| 86 const String& newText, | 89 const String& newText, |
| 87 const Element* rootEditableElement) { | 90 const Element* rootEditableElement) { |
| 88 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText); | 91 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText); |
| 89 | 92 |
| 90 // For grapheme cluster, we should adjust it for grapheme boundary. | 93 // For grapheme cluster, we should adjust it for grapheme boundary. |
| 91 const EphemeralRange& range = | 94 const EphemeralRange& range = |
| 92 PlainTextRange(0, oldText.length() - commonSuffixLength) | 95 PlainTextRange(0, selectionStart + oldText.length() - commonSuffixLength) |
| 93 .createRange(*rootEditableElement); | 96 .createRange(*rootEditableElement); |
| 94 if (range.isNull()) | 97 if (range.isNull()) |
| 95 return 0; | 98 return 0; |
| 96 const Position& position = range.endPosition(); | 99 const Position& position = range.endPosition(); |
| 97 const size_t diff = computeDistanceToRightGraphemeBoundary(position); | 100 const size_t diff = computeDistanceToRightGraphemeBoundary(position); |
| 98 DCHECK_GE(commonSuffixLength, diff); | 101 DCHECK_GE(commonSuffixLength, diff); |
| 99 return commonSuffixLength - diff; | 102 return commonSuffixLength - diff; |
| 100 } | 103 } |
| 101 | 104 |
| 102 const String computeTextForInsertion(const String& newText, | 105 const String computeTextForInsertion(const String& newText, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 146 |
| 144 void InsertIncrementalTextCommand::doApply(EditingState* editingState) { | 147 void InsertIncrementalTextCommand::doApply(EditingState* editingState) { |
| 145 const Element* element = endingSelection().rootEditableElement(); | 148 const Element* element = endingSelection().rootEditableElement(); |
| 146 DCHECK(element); | 149 DCHECK(element); |
| 147 | 150 |
| 148 const EphemeralRange selectionRange(endingSelection().start(), | 151 const EphemeralRange selectionRange(endingSelection().start(), |
| 149 endingSelection().end()); | 152 endingSelection().end()); |
| 150 const String oldText = plainText(selectionRange); | 153 const String oldText = plainText(selectionRange); |
| 151 const String& newText = m_text; | 154 const String& newText = m_text; |
| 152 | 155 |
| 156 const int selectionStart = |
| 157 endingSelection().start().computeOffsetInContainerNode(); |
| 153 const size_t newTextLength = newText.length(); | 158 const size_t newTextLength = newText.length(); |
| 154 const size_t oldTextLength = oldText.length(); | 159 const size_t oldTextLength = oldText.length(); |
| 155 const size_t commonPrefixLength = | 160 const size_t commonPrefixLength = computeCommonGraphemeClusterPrefixLength( |
| 156 computeCommonGraphemeClusterPrefixLength(oldText, newText, element); | 161 selectionStart, oldText, newText, element); |
| 157 // We should ignore common prefix when finding common suffix. | 162 // We should ignore common prefix when finding common suffix. |
| 158 const size_t commonSuffixLength = computeCommonGraphemeClusterSuffixLength( | 163 const size_t commonSuffixLength = computeCommonGraphemeClusterSuffixLength( |
| 159 oldText.right(oldTextLength - commonPrefixLength), | 164 selectionStart, oldText.right(oldTextLength - commonPrefixLength), |
| 160 newText.right(newTextLength - commonPrefixLength), element); | 165 newText.right(newTextLength - commonPrefixLength), element); |
| 161 DCHECK_GE(oldTextLength, commonPrefixLength + commonSuffixLength); | 166 DCHECK_GE(oldTextLength, commonPrefixLength + commonSuffixLength); |
| 162 | 167 |
| 163 m_text = | 168 m_text = |
| 164 computeTextForInsertion(m_text, commonPrefixLength, commonSuffixLength); | 169 computeTextForInsertion(m_text, commonPrefixLength, commonSuffixLength); |
| 165 | 170 |
| 166 const int offset = static_cast<int>(commonPrefixLength); | 171 const int offset = static_cast<int>(commonPrefixLength); |
| 167 const int length = | 172 const int length = |
| 168 static_cast<int>(oldTextLength - commonPrefixLength - commonSuffixLength); | 173 static_cast<int>(oldTextLength - commonPrefixLength - commonSuffixLength); |
| 169 const VisibleSelection& selectionForInsertion = computeSelectionForInsertion( | 174 const VisibleSelection& selectionForInsertion = computeSelectionForInsertion( |
| 170 selectionRange, offset, length, endingSelection().isDirectional()); | 175 selectionRange, offset, length, endingSelection().isDirectional()); |
| 171 | 176 |
| 172 setEndingSelectionWithoutValidation(selectionForInsertion.start(), | 177 setEndingSelectionWithoutValidation(selectionForInsertion.start(), |
| 173 selectionForInsertion.end()); | 178 selectionForInsertion.end()); |
| 174 | 179 |
| 175 InsertTextCommand::doApply(editingState); | 180 InsertTextCommand::doApply(editingState); |
| 176 } | 181 } |
| 177 | 182 |
| 178 } // namespace blink | 183 } // namespace blink |
| OLD | NEW |