OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/GranularityStrategy.h" | 5 #include "core/editing/GranularityStrategy.h" |
6 | 6 |
7 #include "core/editing/EditingUtilities.h" | 7 #include "core/editing/EditingUtilities.h" |
8 #include "core/editing/FrameSelection.h" | 8 #include "core/editing/FrameSelection.h" |
9 | 9 |
10 namespace blink { | 10 namespace blink { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 CharacterGranularityStrategy::CharacterGranularityStrategy() {} | 55 CharacterGranularityStrategy::CharacterGranularityStrategy() {} |
56 | 56 |
57 CharacterGranularityStrategy::~CharacterGranularityStrategy() {} | 57 CharacterGranularityStrategy::~CharacterGranularityStrategy() {} |
58 | 58 |
59 SelectionStrategy CharacterGranularityStrategy::GetType() const { | 59 SelectionStrategy CharacterGranularityStrategy::GetType() const { |
60 return SelectionStrategy::Character; | 60 return SelectionStrategy::Character; |
61 } | 61 } |
62 | 62 |
63 void CharacterGranularityStrategy::Clear() {} | 63 void CharacterGranularityStrategy::Clear() {} |
64 | 64 |
65 VisibleSelection CharacterGranularityStrategy::updateExtent( | 65 SelectionInDOMTree CharacterGranularityStrategy::updateExtent( |
66 const IntPoint& extentPoint, | 66 const IntPoint& extentPoint, |
67 LocalFrame* frame) { | 67 LocalFrame* frame) { |
68 const VisiblePosition& extentPosition = | 68 const VisiblePosition& extentPosition = |
69 visiblePositionForContentsPoint(extentPoint, frame); | 69 visiblePositionForContentsPoint(extentPoint, frame); |
70 const VisibleSelection& selection = | 70 const VisibleSelection& selection = |
71 frame->selection().computeVisibleSelectionInDOMTreeDeprecated(); | 71 frame->selection().computeVisibleSelectionInDOMTreeDeprecated(); |
72 if (selection.visibleBase().deepEquivalent() == | 72 if (selection.visibleBase().deepEquivalent() == |
73 extentPosition.deepEquivalent()) | 73 extentPosition.deepEquivalent()) |
74 return selection; | 74 return selection.asSelection(); |
75 return createVisibleSelection(SelectionInDOMTree::Builder() | 75 return SelectionInDOMTree::Builder() |
76 .collapse(selection.base()) | 76 .collapse(selection.base()) |
77 .extend(extentPosition.deepEquivalent()) | 77 .extend(extentPosition.deepEquivalent()) |
78 .setAffinity(selection.affinity()) | 78 .setAffinity(selection.affinity()) |
79 .build()); | 79 .build(); |
80 } | 80 } |
81 | 81 |
82 DirectionGranularityStrategy::DirectionGranularityStrategy() | 82 DirectionGranularityStrategy::DirectionGranularityStrategy() |
83 : m_state(StrategyState::Cleared), | 83 : m_state(StrategyState::Cleared), |
84 m_granularity(CharacterGranularity), | 84 m_granularity(CharacterGranularity), |
85 m_offset(0) {} | 85 m_offset(0) {} |
86 | 86 |
87 DirectionGranularityStrategy::~DirectionGranularityStrategy() {} | 87 DirectionGranularityStrategy::~DirectionGranularityStrategy() {} |
88 | 88 |
89 SelectionStrategy DirectionGranularityStrategy::GetType() const { | 89 SelectionStrategy DirectionGranularityStrategy::GetType() const { |
90 return SelectionStrategy::Direction; | 90 return SelectionStrategy::Direction; |
91 } | 91 } |
92 | 92 |
93 void DirectionGranularityStrategy::Clear() { | 93 void DirectionGranularityStrategy::Clear() { |
94 m_state = StrategyState::Cleared; | 94 m_state = StrategyState::Cleared; |
95 m_granularity = CharacterGranularity; | 95 m_granularity = CharacterGranularity; |
96 m_offset = 0; | 96 m_offset = 0; |
97 m_diffExtentPointFromExtentPosition = IntSize(); | 97 m_diffExtentPointFromExtentPosition = IntSize(); |
98 } | 98 } |
99 | 99 |
100 VisibleSelection DirectionGranularityStrategy::updateExtent( | 100 SelectionInDOMTree DirectionGranularityStrategy::updateExtent( |
101 const IntPoint& extentPoint, | 101 const IntPoint& extentPoint, |
102 LocalFrame* frame) { | 102 LocalFrame* frame) { |
103 const VisibleSelection& selection = | 103 const VisibleSelection& selection = |
104 frame->selection().computeVisibleSelectionInDOMTreeDeprecated(); | 104 frame->selection().computeVisibleSelectionInDOMTreeDeprecated(); |
105 | 105 |
106 if (m_state == StrategyState::Cleared) | 106 if (m_state == StrategyState::Cleared) |
107 m_state = StrategyState::Expanding; | 107 m_state = StrategyState::Expanding; |
108 | 108 |
109 VisiblePosition oldOffsetExtentPosition = selection.visibleExtent(); | 109 VisiblePosition oldOffsetExtentPosition = selection.visibleExtent(); |
110 IntPoint oldExtentLocation = positionLocation(oldOffsetExtentPosition); | 110 IntPoint oldExtentLocation = positionLocation(oldOffsetExtentPosition); |
(...skipping 26 matching lines...) Expand all Loading... |
137 m_granularity = CharacterGranularity; | 137 m_granularity = CharacterGranularity; |
138 newOffsetExtentPoint = extentPoint; | 138 newOffsetExtentPoint = extentPoint; |
139 newOffsetExtentPosition = | 139 newOffsetExtentPosition = |
140 visiblePositionForContentsPoint(extentPoint, frame); | 140 visiblePositionForContentsPoint(extentPoint, frame); |
141 } | 141 } |
142 | 142 |
143 const VisiblePosition base = selection.visibleBase(); | 143 const VisiblePosition base = selection.visibleBase(); |
144 | 144 |
145 // Do not allow empty selection. | 145 // Do not allow empty selection. |
146 if (newOffsetExtentPosition.deepEquivalent() == base.deepEquivalent()) | 146 if (newOffsetExtentPosition.deepEquivalent() == base.deepEquivalent()) |
147 return selection; | 147 return selection.asSelection(); |
148 | 148 |
149 // The direction granularity strategy, particularly the "offset" feature | 149 // The direction granularity strategy, particularly the "offset" feature |
150 // doesn't work with non-horizontal text (e.g. when the text is rotated). | 150 // doesn't work with non-horizontal text (e.g. when the text is rotated). |
151 // So revert to the behavior equivalent to the character granularity | 151 // So revert to the behavior equivalent to the character granularity |
152 // strategy if we detect that the text's baseline coordinate changed | 152 // strategy if we detect that the text's baseline coordinate changed |
153 // without a line change. | 153 // without a line change. |
154 if (verticalChange && | 154 if (verticalChange && |
155 inSameLine(newOffsetExtentPosition, oldOffsetExtentPosition)) { | 155 inSameLine(newOffsetExtentPosition, oldOffsetExtentPosition)) { |
156 return createVisibleSelection( | 156 return SelectionInDOMTree::Builder() |
157 SelectionInDOMTree::Builder() | 157 .collapse(selection.base()) |
158 .collapse(selection.base()) | 158 .extend(newOffsetExtentPosition.deepEquivalent()) |
159 .extend(newOffsetExtentPosition.deepEquivalent()) | 159 .setAffinity(selection.affinity()) |
160 .setAffinity(selection.affinity()) | 160 .build(); |
161 .build()); | |
162 } | 161 } |
163 | 162 |
164 int oldExtentBaseOrder = selection.isBaseFirst() ? 1 : -1; | 163 int oldExtentBaseOrder = selection.isBaseFirst() ? 1 : -1; |
165 | 164 |
166 int newExtentBaseOrder; | 165 int newExtentBaseOrder; |
167 bool thisMoveShrunkSelection; | 166 bool thisMoveShrunkSelection; |
168 if (newOffsetExtentPosition.deepEquivalent() == | 167 if (newOffsetExtentPosition.deepEquivalent() == |
169 oldOffsetExtentPosition.deepEquivalent()) { | 168 oldOffsetExtentPosition.deepEquivalent()) { |
170 if (m_granularity == CharacterGranularity) | 169 if (m_granularity == CharacterGranularity) |
171 return selection; | 170 return selection.asSelection(); |
172 | 171 |
173 // If we are in Word granularity, we cannot exit here, since we may pass | 172 // If we are in Word granularity, we cannot exit here, since we may pass |
174 // the middle of the word without changing the position (in which case | 173 // the middle of the word without changing the position (in which case |
175 // the selection needs to expand). | 174 // the selection needs to expand). |
176 thisMoveShrunkSelection = false; | 175 thisMoveShrunkSelection = false; |
177 newExtentBaseOrder = oldExtentBaseOrder; | 176 newExtentBaseOrder = oldExtentBaseOrder; |
178 } else { | 177 } else { |
179 bool selectionExpanded = arePositionsInSpecifiedOrder( | 178 bool selectionExpanded = arePositionsInSpecifiedOrder( |
180 newOffsetExtentPosition, oldOffsetExtentPosition, oldExtentBaseOrder); | 179 newOffsetExtentPosition, oldOffsetExtentPosition, oldExtentBaseOrder); |
181 bool extentBaseOrderSwitched = | 180 bool extentBaseOrderSwitched = |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 | 262 |
264 // Only update the state if the selection actually changed as a result of | 263 // Only update the state if the selection actually changed as a result of |
265 // this move. | 264 // this move. |
266 if (newSelectionExtent.deepEquivalent() != | 265 if (newSelectionExtent.deepEquivalent() != |
267 selection.visibleExtent().deepEquivalent()) | 266 selection.visibleExtent().deepEquivalent()) |
268 m_state = thisMoveShrunkSelection ? StrategyState::Shrinking | 267 m_state = thisMoveShrunkSelection ? StrategyState::Shrinking |
269 : StrategyState::Expanding; | 268 : StrategyState::Expanding; |
270 | 269 |
271 m_diffExtentPointFromExtentPosition = | 270 m_diffExtentPointFromExtentPosition = |
272 extentPoint + IntSize(m_offset, 0) - positionLocation(newSelectionExtent); | 271 extentPoint + IntSize(m_offset, 0) - positionLocation(newSelectionExtent); |
273 return createVisibleSelection( | 272 return SelectionInDOMTree::Builder(selection.asSelection()) |
274 SelectionInDOMTree::Builder(selection.asSelection()) | 273 .collapse(selection.base()) |
275 .collapse(selection.base()) | 274 .extend(newSelectionExtent.deepEquivalent()) |
276 .extend(newSelectionExtent.deepEquivalent()) | 275 .build(); |
277 .build()); | |
278 } | 276 } |
279 | 277 |
280 } // namespace blink | 278 } // namespace blink |
OLD | NEW |