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

Side by Side Diff: third_party/WebKit/Source/core/editing/commands/InsertIncrementalTextCommand.cpp

Issue 2802953002: Avoid duplicate functions/code in core/editing: computeDistance (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « third_party/WebKit/Source/core/editing/InputMethodController.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 27 matching lines...) Expand all
38 const size_t length1 = str1.length(); 38 const size_t length1 = str1.length();
39 const size_t length2 = str2.length(); 39 const size_t length2 = str2.length();
40 const size_t maxCommonSuffixLength = std::min(length1, length2); 40 const size_t maxCommonSuffixLength = std::min(length1, length2);
41 for (size_t index = 0; index < maxCommonSuffixLength; ++index) { 41 for (size_t index = 0; index < maxCommonSuffixLength; ++index) {
42 if (str1[length1 - index - 1] != str2[length2 - index - 1]) 42 if (str1[length1 - index - 1] != str2[length2 - index - 1])
43 return index; 43 return index;
44 } 44 }
45 return maxCommonSuffixLength; 45 return maxCommonSuffixLength;
46 } 46 }
47 47
48 // If current position is at grapheme boundary, return 0; otherwise, return the
49 // distance to its nearest left grapheme boundary.
50 size_t computeDistanceToLeftGraphemeBoundary(const Position& position) {
51 const Position& adjustedPosition = previousPositionOf(
52 nextPositionOf(position, PositionMoveType::GraphemeCluster),
53 PositionMoveType::GraphemeCluster);
54 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode());
55 DCHECK_GE(position.computeOffsetInContainerNode(),
56 adjustedPosition.computeOffsetInContainerNode());
57 return static_cast<size_t>(position.computeOffsetInContainerNode() -
58 adjustedPosition.computeOffsetInContainerNode());
59 }
60
61 size_t computeCommonGraphemeClusterPrefixLength(const Position& selectionStart, 48 size_t computeCommonGraphemeClusterPrefixLength(const Position& selectionStart,
62 const String& oldText, 49 const String& oldText,
63 const String& newText) { 50 const String& newText) {
64 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText); 51 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText);
65 const int selectionOffset = selectionStart.computeOffsetInContainerNode(); 52 const int selectionOffset = selectionStart.computeOffsetInContainerNode();
66 const ContainerNode* selectionNode = 53 const ContainerNode* selectionNode =
67 selectionStart.computeContainerNode()->parentNode(); 54 selectionStart.computeContainerNode()->parentNode();
68 55
69 // For grapheme cluster, we should adjust it for grapheme boundary. 56 // For grapheme cluster, we should adjust it for grapheme boundary.
70 const EphemeralRange& range = 57 const EphemeralRange& range =
71 PlainTextRange(0, selectionOffset + commonPrefixLength) 58 PlainTextRange(0, selectionOffset + commonPrefixLength)
72 .createRange(*selectionNode); 59 .createRange(*selectionNode);
73 if (range.isNull()) 60 if (range.isNull())
74 return 0; 61 return 0;
75 const Position& position = range.endPosition(); 62 const Position& position = range.endPosition();
76 const size_t diff = computeDistanceToLeftGraphemeBoundary(position); 63 const size_t diff = computeDistanceToLeftGraphemeBoundary(position);
77 DCHECK_GE(commonPrefixLength, diff); 64 DCHECK_GE(commonPrefixLength, diff);
78 return commonPrefixLength - diff; 65 return commonPrefixLength - diff;
79 } 66 }
80 67
81 // If current position is at grapheme boundary, return 0; otherwise, return the
82 // distance to its nearest right grapheme boundary.
83 size_t computeDistanceToRightGraphemeBoundary(const Position& position) {
84 const Position& adjustedPosition = nextPositionOf(
85 previousPositionOf(position, PositionMoveType::GraphemeCluster),
86 PositionMoveType::GraphemeCluster);
87 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode());
88 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(),
89 position.computeOffsetInContainerNode());
90 return static_cast<size_t>(adjustedPosition.computeOffsetInContainerNode() -
91 position.computeOffsetInContainerNode());
92 }
93
94 size_t computeCommonGraphemeClusterSuffixLength(const Position& selectionStart, 68 size_t computeCommonGraphemeClusterSuffixLength(const Position& selectionStart,
95 const String& oldText, 69 const String& oldText,
96 const String& newText) { 70 const String& newText) {
97 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText); 71 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText);
98 const int selectionOffset = selectionStart.computeOffsetInContainerNode(); 72 const int selectionOffset = selectionStart.computeOffsetInContainerNode();
99 const ContainerNode* selectionNode = 73 const ContainerNode* selectionNode =
100 selectionStart.computeContainerNode()->parentNode(); 74 selectionStart.computeContainerNode()->parentNode();
101 75
102 // For grapheme cluster, we should adjust it for grapheme boundary. 76 // For grapheme cluster, we should adjust it for grapheme boundary.
103 const EphemeralRange& range = 77 const EphemeralRange& range =
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 const VisibleSelection& selectionForInsertion = computeSelectionForInsertion( 157 const VisibleSelection& selectionForInsertion = computeSelectionForInsertion(
184 selectionRange, offset, length, endingSelection().isDirectional()); 158 selectionRange, offset, length, endingSelection().isDirectional());
185 159
186 setEndingSelectionWithoutValidation(selectionForInsertion.start(), 160 setEndingSelectionWithoutValidation(selectionForInsertion.start(),
187 selectionForInsertion.end()); 161 selectionForInsertion.end());
188 162
189 InsertTextCommand::doApply(editingState); 163 InsertTextCommand::doApply(editingState);
190 } 164 }
191 165
192 } // namespace blink 166 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/InputMethodController.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698