Chromium Code Reviews| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 const Position& adjustedPosition = previousPositionOf( | 44 const Position& adjustedPosition = previousPositionOf( |
| 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(const Position& selectionStart, |
| 55 const int selectionStart, | 55 const String& oldText, |
| 56 const String& oldText, | 56 const String& newText) { |
| 57 const String& newText, | |
| 58 const Element* rootEditableElement) { | |
| 59 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText); | 57 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText); |
| 58 const int selectionOffset = selectionStart.computeOffsetInContainerNode(); | |
| 59 const ContainerNode* selectionNode = | |
| 60 selectionStart.computeContainerNode()->parentNode(); | |
|
Changwan Ryu
2017/03/23 21:20:17
Is it safe not to have a null check here? I mean,
| |
| 60 | 61 |
| 61 // For grapheme cluster, we should adjust it for grapheme boundary. | 62 // For grapheme cluster, we should adjust it for grapheme boundary. |
| 62 const EphemeralRange& range = | 63 const EphemeralRange& range = |
| 63 PlainTextRange(0, selectionStart + commonPrefixLength) | 64 PlainTextRange(0, selectionOffset + commonPrefixLength) |
| 64 .createRange(*rootEditableElement); | 65 .createRange(*selectionNode); |
| 65 if (range.isNull()) | 66 if (range.isNull()) |
| 66 return 0; | 67 return 0; |
| 67 const Position& position = range.endPosition(); | 68 const Position& position = range.endPosition(); |
| 68 const size_t diff = computeDistanceToLeftGraphemeBoundary(position); | 69 const size_t diff = computeDistanceToLeftGraphemeBoundary(position); |
|
yabinh
2017/03/22 02:38:32
We don't need replace the DCHECK in [prefix] case.
| |
| 69 DCHECK_GE(commonPrefixLength, diff); | 70 DCHECK_GE(commonPrefixLength, diff); |
| 70 return commonPrefixLength - diff; | 71 return commonPrefixLength - diff; |
| 71 } | 72 } |
| 72 | 73 |
| 73 // If current position is at grapheme boundary, return 0; otherwise, return the | 74 // If current position is at grapheme boundary, return 0; otherwise, return the |
| 74 // distance to its nearest right grapheme boundary. | 75 // distance to its nearest right grapheme boundary. |
| 75 size_t computeDistanceToRightGraphemeBoundary(const Position& position) { | 76 size_t computeDistanceToRightGraphemeBoundary(const Position& position) { |
| 76 const Position& adjustedPosition = nextPositionOf( | 77 const Position& adjustedPosition = nextPositionOf( |
| 77 previousPositionOf(position, PositionMoveType::GraphemeCluster), | 78 previousPositionOf(position, PositionMoveType::GraphemeCluster), |
| 78 PositionMoveType::GraphemeCluster); | 79 PositionMoveType::GraphemeCluster); |
| 79 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | 80 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); |
| 80 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(), | 81 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(), |
| 81 position.computeOffsetInContainerNode()); | 82 position.computeOffsetInContainerNode()); |
| 82 return static_cast<size_t>(adjustedPosition.computeOffsetInContainerNode() - | 83 return static_cast<size_t>(adjustedPosition.computeOffsetInContainerNode() - |
| 83 position.computeOffsetInContainerNode()); | 84 position.computeOffsetInContainerNode()); |
| 84 } | 85 } |
| 85 | 86 |
| 86 size_t computeCommonGraphemeClusterSuffixLength( | 87 size_t computeCommonGraphemeClusterSuffixLength(const Position& selectionStart, |
| 87 const int selectionStart, | 88 const String& oldText, |
| 88 const String& oldText, | 89 const String& newText) { |
| 89 const String& newText, | |
| 90 const Element* rootEditableElement) { | |
| 91 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText); | 90 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText); |
| 91 const int selectionOffset = selectionStart.computeOffsetInContainerNode(); | |
| 92 const ContainerNode* selectionNode = | |
| 93 selectionStart.computeContainerNode()->parentNode(); | |
|
Changwan Ryu
2017/03/23 21:20:17
Same comment applies here.
| |
| 92 | 94 |
| 93 // For grapheme cluster, we should adjust it for grapheme boundary. | 95 // For grapheme cluster, we should adjust it for grapheme boundary. |
| 94 const EphemeralRange& range = | 96 const EphemeralRange& range = |
| 95 PlainTextRange(0, selectionStart + oldText.length() - commonSuffixLength) | 97 PlainTextRange(0, selectionOffset + oldText.length() - commonSuffixLength) |
| 96 .createRange(*rootEditableElement); | 98 .createRange(*selectionNode); |
| 97 if (range.isNull()) | 99 if (range.isNull()) |
| 98 return 0; | 100 return 0; |
| 99 const Position& position = range.endPosition(); | 101 const Position& position = range.endPosition(); |
| 100 const size_t diff = computeDistanceToRightGraphemeBoundary(position); | 102 const size_t diff = computeDistanceToRightGraphemeBoundary(position); |
|
yabinh
2017/03/21 10:13:44
BTW, I don't think we need worry about the DCHECK
yosin_UTC9
2017/03/22 02:22:39
I'm confused.
Do you want to keep "newly added" i
yabinh
2017/03/22 02:35:34
Sorry for the confusion. I want to replace DCHECK
| |
| 101 DCHECK_GE(commonSuffixLength, diff); | 103 if (diff > commonSuffixLength) |
| 104 return 0; | |
| 102 return commonSuffixLength - diff; | 105 return commonSuffixLength - diff; |
| 103 } | 106 } |
| 104 | 107 |
| 105 const String computeTextForInsertion(const String& newText, | 108 const String computeTextForInsertion(const String& newText, |
| 106 const size_t commonPrefixLength, | 109 const size_t commonPrefixLength, |
| 107 const size_t commonSuffixLength) { | 110 const size_t commonSuffixLength) { |
| 108 return newText.substring( | 111 return newText.substring( |
| 109 commonPrefixLength, | 112 commonPrefixLength, |
| 110 newText.length() - commonPrefixLength - commonSuffixLength); | 113 newText.length() - commonPrefixLength - commonSuffixLength); |
| 111 } | 114 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 | 149 |
| 147 void InsertIncrementalTextCommand::doApply(EditingState* editingState) { | 150 void InsertIncrementalTextCommand::doApply(EditingState* editingState) { |
| 148 const Element* element = endingSelection().rootEditableElement(); | 151 const Element* element = endingSelection().rootEditableElement(); |
| 149 DCHECK(element); | 152 DCHECK(element); |
| 150 | 153 |
| 151 const EphemeralRange selectionRange(endingSelection().start(), | 154 const EphemeralRange selectionRange(endingSelection().start(), |
| 152 endingSelection().end()); | 155 endingSelection().end()); |
| 153 const String oldText = plainText(selectionRange); | 156 const String oldText = plainText(selectionRange); |
| 154 const String& newText = m_text; | 157 const String& newText = m_text; |
| 155 | 158 |
| 156 const int selectionStart = | 159 const Position& selectionStart = endingSelection().start(); |
| 157 endingSelection().start().computeOffsetInContainerNode(); | |
| 158 const size_t newTextLength = newText.length(); | 160 const size_t newTextLength = newText.length(); |
| 159 const size_t oldTextLength = oldText.length(); | 161 const size_t oldTextLength = oldText.length(); |
| 160 const size_t commonPrefixLength = computeCommonGraphemeClusterPrefixLength( | 162 const size_t commonPrefixLength = computeCommonGraphemeClusterPrefixLength( |
| 161 selectionStart, oldText, newText, element); | 163 selectionStart, oldText, newText); |
| 162 // We should ignore common prefix when finding common suffix. | 164 // We should ignore common prefix when finding common suffix. |
| 163 const size_t commonSuffixLength = computeCommonGraphemeClusterSuffixLength( | 165 const size_t commonSuffixLength = computeCommonGraphemeClusterSuffixLength( |
| 164 selectionStart, oldText.right(oldTextLength - commonPrefixLength), | 166 selectionStart, oldText.right(oldTextLength - commonPrefixLength), |
| 165 newText.right(newTextLength - commonPrefixLength), element); | 167 newText.right(newTextLength - commonPrefixLength)); |
| 166 DCHECK_GE(oldTextLength, commonPrefixLength + commonSuffixLength); | 168 DCHECK_GE(oldTextLength, commonPrefixLength + commonSuffixLength); |
| 167 | 169 |
| 168 m_text = | 170 m_text = |
| 169 computeTextForInsertion(m_text, commonPrefixLength, commonSuffixLength); | 171 computeTextForInsertion(m_text, commonPrefixLength, commonSuffixLength); |
| 170 | 172 |
| 171 const int offset = static_cast<int>(commonPrefixLength); | 173 const int offset = static_cast<int>(commonPrefixLength); |
| 172 const int length = | 174 const int length = |
| 173 static_cast<int>(oldTextLength - commonPrefixLength - commonSuffixLength); | 175 static_cast<int>(oldTextLength - commonPrefixLength - commonSuffixLength); |
| 174 const VisibleSelection& selectionForInsertion = computeSelectionForInsertion( | 176 const VisibleSelection& selectionForInsertion = computeSelectionForInsertion( |
| 175 selectionRange, offset, length, endingSelection().isDirectional()); | 177 selectionRange, offset, length, endingSelection().isDirectional()); |
| 176 | 178 |
| 177 setEndingSelectionWithoutValidation(selectionForInsertion.start(), | 179 setEndingSelectionWithoutValidation(selectionForInsertion.start(), |
| 178 selectionForInsertion.end()); | 180 selectionForInsertion.end()); |
| 179 | 181 |
| 180 InsertTextCommand::doApply(editingState); | 182 InsertTextCommand::doApply(editingState); |
| 181 } | 183 } |
| 182 | 184 |
| 183 } // namespace blink | 185 } // namespace blink |
| OLD | NEW |