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

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

Issue 2843463006: Refactor LayoutSelection::SetSelection() (Closed)
Patch Set: update Created 3 years, 7 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 | « no previous file | 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 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights
4 * reserved. 4 * reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 exploring_backwards = !next && (next != stop); 119 exploring_backwards = !next && (next != stop);
120 if (exploring_backwards) { 120 if (exploring_backwards) {
121 next = stop->PreviousInPreOrder(); 121 next = stop->PreviousInPreOrder();
122 continue_exploring = next && !next->IsLayoutView(); 122 continue_exploring = next && !next->IsLayoutView();
123 } 123 }
124 } 124 }
125 125
126 return next; 126 return next;
127 } 127 }
128 128
129 void LayoutSelection::SetSelection( 129 // Objects each have a single selection rect to examine.
130 LayoutObject* start, 130 using SelectedObjectMap = HashMap<LayoutObject*, SelectionState>;
131 int start_pos, 131 // Blocks contain selected objects and fill gaps between them, either on the
132 LayoutObject* end, 132 // left, right, or in between lines and blocks.
133 int end_pos, 133 // In order to get the visual rect right, we have to examine left, middle, and
134 SelectionPaintInvalidationMode block_paint_invalidation_mode) { 134 // right rects individually, since otherwise the union of those rects might
135 // This code makes no assumptions as to if the layout tree is up to date or 135 // remain the same even when changes have occurred.
136 // not and will not try to update it. Currently clearSelection calls this 136 using SelectedBlockMap = HashMap<LayoutBlock*, SelectionState>;
137 // (intentionally) without updating the layout tree as it doesn't care. 137 using SelectedMap = std::pair<SelectedObjectMap, SelectedBlockMap>;
138 // Other callers may want to force recalc style before calling this.
139 138
140 // Make sure both our start and end objects are defined. 139 static SelectedMap CollectSelectedMap(
141 // Check www.msnbc.com and try clicking around to find the case where this 140 LayoutObject* const selection_start,
142 // happened. 141 LayoutObject* const selection_end,
143 if ((start && !end) || (end && !start)) 142 int selection_end_pos,
144 return; 143 LayoutSelection::SelectionPaintInvalidationMode
145 144 block_paint_invalidation_mode =
146 // Just return if the selection hasn't changed. 145 LayoutSelection::kPaintInvalidationNewXOROld) {
147 if (selection_start_ == start && selection_start_pos_ == start_pos && 146 SelectedObjectMap selected_objects;
148 selection_end_ == end && selection_end_pos_ == end_pos) 147 SelectedBlockMap selected_blocks;
149 return; 148 LayoutObject* layout_object = selection_start;
150 149 LayoutObject* const stop =
151 // Record the old selected objects. These will be used later when we compare 150 LayoutObjectAfterPosition(selection_end, selection_end_pos);
152 // against the new selected objects.
153 int old_start_pos = selection_start_pos_;
154 int old_end_pos = selection_end_pos_;
155
156 // Objects each have a single selection rect to examine.
157 typedef HashMap<LayoutObject*, SelectionState> SelectedObjectMap;
158 SelectedObjectMap old_selected_objects;
159 // FIXME: |newSelectedObjects| doesn't really need to store the
160 // SelectionState, it's just more convenient to have it use the same data
161 // structure as |oldSelectedObjects|.
162 SelectedObjectMap new_selected_objects;
163
164 // Blocks contain selected objects and fill gaps between them, either on the
165 // left, right, or in between lines and blocks.
166 // In order to get the visual rect right, we have to examine left, middle, and
167 // right rects individually, since otherwise the union of those rects might
168 // remain the same even when changes have occurred.
169 typedef HashMap<LayoutBlock*, SelectionState> SelectedBlockMap;
170 SelectedBlockMap old_selected_blocks;
171 // FIXME: |newSelectedBlocks| doesn't really need to store the SelectionState,
172 // it's just more convenient to have it use the same data structure as
173 // |oldSelectedBlocks|.
174 SelectedBlockMap new_selected_blocks;
175
176 LayoutObject* os = selection_start_;
177 LayoutObject* stop =
178 LayoutObjectAfterPosition(selection_end_, selection_end_pos_);
179 bool exploring_backwards = false; 151 bool exploring_backwards = false;
180 bool continue_exploring = os && (os != stop); 152 bool continue_exploring = layout_object && (layout_object != stop);
181 while (continue_exploring) { 153 while (continue_exploring) {
182 if ((os->CanBeSelectionLeaf() || os == selection_start_ || 154 if ((layout_object->CanBeSelectionLeaf() ||
183 os == selection_end_) && 155 layout_object == selection_start || layout_object == selection_end) &&
184 os->GetSelectionState() != SelectionNone) { 156 layout_object->GetSelectionState() != SelectionNone) {
185 // Blocks are responsible for painting line gaps and margin gaps. They 157 // Blocks are responsible for painting line gaps and margin gaps. They
186 // must be examined as well. 158 // must be examined as well.
187 old_selected_objects.Set(os, os->GetSelectionState()); 159 selected_objects.Set(layout_object, layout_object->GetSelectionState());
188 if (block_paint_invalidation_mode == kPaintInvalidationNewXOROld) { 160 if (block_paint_invalidation_mode ==
189 LayoutBlock* cb = os->ContainingBlock(); 161 LayoutSelection::kPaintInvalidationNewXOROld) {
162 LayoutBlock* cb = layout_object->ContainingBlock();
190 while (cb && !cb->IsLayoutView()) { 163 while (cb && !cb->IsLayoutView()) {
191 SelectedBlockMap::AddResult result = 164 SelectedBlockMap::AddResult result =
192 old_selected_blocks.insert(cb, cb->GetSelectionState()); 165 selected_blocks.insert(cb, cb->GetSelectionState());
193 if (!result.is_new_entry) 166 if (!result.is_new_entry)
194 break; 167 break;
195 cb = cb->ContainingBlock(); 168 cb = cb->ContainingBlock();
196 } 169 }
197 } 170 }
198 } 171 }
199 172
200 os = GetNextOrPrevLayoutObjectBasedOnDirection(os, stop, continue_exploring, 173 layout_object = GetNextOrPrevLayoutObjectBasedOnDirection(
201 exploring_backwards); 174 layout_object, stop, continue_exploring, exploring_backwards);
202 } 175 }
176 return std::make_pair(std::move(selected_objects),
177 std::move(selected_blocks));
178 }
203 179
204 // Now clear the selection. 180 static void SetSelectionStateNone(const SelectedObjectMap& map) {
205 SelectedObjectMap::iterator old_objects_end = old_selected_objects.end(); 181 for (const auto& it : map)
206 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); 182 it.key->SetSelectionStateIfNeeded(SelectionNone);
207 i != old_objects_end; ++i) 183 }
208 i->key->SetSelectionStateIfNeeded(SelectionNone);
209 184
210 // set selection start and end 185 static void SetSelectionState(LayoutObject* start,
211 selection_start_ = start; 186 LayoutObject* end,
212 selection_start_pos_ = start_pos; 187 int end_pos) {
213 selection_end_ = end;
214 selection_end_pos_ = end_pos;
215
216 // Update the selection status of all objects between m_selectionStart and 188 // Update the selection status of all objects between m_selectionStart and
217 // m_selectionEnd 189 // m_selectionEnd
218 if (start && start == end) { 190 if (start && start == end) {
219 start->SetSelectionStateIfNeeded(SelectionBoth); 191 start->SetSelectionStateIfNeeded(SelectionBoth);
220 } else { 192 } else {
221 if (start) 193 if (start)
222 start->SetSelectionStateIfNeeded(SelectionStart); 194 start->SetSelectionStateIfNeeded(SelectionStart);
223 if (end) 195 if (end)
224 end->SetSelectionStateIfNeeded(SelectionEnd); 196 end->SetSelectionStateIfNeeded(SelectionEnd);
225 } 197 }
226 198
227 LayoutObject* o = start; 199 LayoutObject* layout_object = start;
228 stop = LayoutObjectAfterPosition(end, end_pos); 200 LayoutObject* const stop = LayoutObjectAfterPosition(end, end_pos);
201 while (layout_object && layout_object != stop) {
202 if (layout_object != start && layout_object != end &&
203 layout_object->CanBeSelectionLeaf())
204 layout_object->SetSelectionStateIfNeeded(SelectionInside);
205 layout_object = layout_object->NextInPreOrder();
206 }
207 }
229 208
230 while (o && o != stop) { 209 static void InvalidateLayoutObjects(LayoutObject* start,
231 if (o != start && o != end && o->CanBeSelectionLeaf()) 210 bool is_start_pos_changed,
232 o->SetSelectionStateIfNeeded(SelectionInside); 211 LayoutObject* end,
233 o = o->NextInPreOrder(); 212 bool is_end_pos_changed,
234 } 213 const SelectedMap& old_selected_map,
235 214 SelectedMap& new_selected_map) {
yosin_UTC9 2017/05/10 07:00:57 s/SelectedMap&/SelectedMap*/ We use reference for
236 // Now that the selection state has been updated for the new objects, walk 215 const SelectedObjectMap& old_selected_objects = old_selected_map.first;
237 // them again and put them in the new objects list. 216 const SelectedBlockMap& old_selected_blocks = old_selected_map.second;
238 o = start; 217 SelectedObjectMap& new_selected_objects = new_selected_map.first;
239 exploring_backwards = false; 218 SelectedBlockMap& new_selected_blocks = new_selected_map.second;
240 continue_exploring = o && (o != stop);
241 while (continue_exploring) {
242 if ((o->CanBeSelectionLeaf() || o == start || o == end) &&
243 o->GetSelectionState() != SelectionNone) {
244 new_selected_objects.Set(o, o->GetSelectionState());
245 LayoutBlock* cb = o->ContainingBlock();
246 while (cb && !cb->IsLayoutView()) {
247 SelectedBlockMap::AddResult result =
248 new_selected_blocks.insert(cb, cb->GetSelectionState());
249 if (!result.is_new_entry)
250 break;
251 cb = cb->ContainingBlock();
252 }
253 }
254
255 o = GetNextOrPrevLayoutObjectBasedOnDirection(o, stop, continue_exploring,
256 exploring_backwards);
257 }
258
259 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView());
260 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView())
261 return;
262 219
263 // Have any of the old selected objects changed compared to the new selection? 220 // Have any of the old selected objects changed compared to the new selection?
264 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); 221 for (const auto& it : old_selected_objects) {
265 i != old_objects_end; ++i) { 222 LayoutObject* const obj = it.key;
266 LayoutObject* obj = i->key; 223 const SelectionState new_selection_state = obj->GetSelectionState();
267 SelectionState new_selection_state = obj->GetSelectionState(); 224 const SelectionState old_selection_state = it.value;
268 SelectionState old_selection_state = i->value;
269 if (new_selection_state != old_selection_state || 225 if (new_selection_state != old_selection_state ||
270 (selection_start_ == obj && old_start_pos != selection_start_pos_) || 226 (start == obj && is_start_pos_changed) ||
271 (selection_end_ == obj && old_end_pos != selection_end_pos_)) { 227 (end == obj && is_end_pos_changed)) {
272 obj->SetShouldInvalidateSelection(); 228 obj->SetShouldInvalidateSelection();
273 new_selected_objects.erase(obj); 229 new_selected_objects.erase(obj);
274 } 230 }
275 } 231 }
276 232
277 // Any new objects that remain were not found in the old objects dict, and so 233 // Any new objects that remain were not found in the old objects dict, and so
278 // they need to be updated. 234 // they need to be updated.
279 SelectedObjectMap::iterator new_objects_end = new_selected_objects.end(); 235 for (const auto& it : new_selected_objects)
280 for (SelectedObjectMap::iterator i = new_selected_objects.begin(); 236 it.key->SetShouldInvalidateSelection();
281 i != new_objects_end; ++i)
282 i->key->SetShouldInvalidateSelection();
283 237
284 // Have any of the old blocks changed? 238 // Have any of the old blocks changed?
285 SelectedBlockMap::iterator old_blocks_end = old_selected_blocks.end(); 239 for (const auto& it : old_selected_blocks) {
286 for (SelectedBlockMap::iterator i = old_selected_blocks.begin(); 240 LayoutBlock* const block = it.key;
287 i != old_blocks_end; ++i) { 241 const SelectionState new_selection_state = block->GetSelectionState();
288 LayoutBlock* block = i->key; 242 const SelectionState old_selection_state = it.value;
289 SelectionState new_selection_state = block->GetSelectionState();
290 SelectionState old_selection_state = i->value;
291 if (new_selection_state != old_selection_state) { 243 if (new_selection_state != old_selection_state) {
292 block->SetShouldInvalidateSelection(); 244 block->SetShouldInvalidateSelection();
293 new_selected_blocks.erase(block); 245 new_selected_blocks.erase(block);
294 } 246 }
295 } 247 }
296 248
297 // Any new blocks that remain were not found in the old blocks dict, and so 249 // Any new blocks that remain were not found in the old blocks dict, and so
298 // they need to be updated. 250 // they need to be updated.
299 SelectedBlockMap::iterator new_blocks_end = new_selected_blocks.end(); 251 for (const auto& it : new_selected_blocks)
300 for (SelectedBlockMap::iterator i = new_selected_blocks.begin(); 252 it.key->SetShouldInvalidateSelection();
301 i != new_blocks_end; ++i) 253 }
302 i->key->SetShouldInvalidateSelection(); 254
255 void LayoutSelection::SetSelection(
256 LayoutObject* start,
yosin_UTC9 2017/05/10 07:00:57 Introducing |struct LayoutObjectWithOffset| before
257 int start_pos,
258 LayoutObject* end,
259 int end_pos,
260 SelectionPaintInvalidationMode block_paint_invalidation_mode) {
261 // This code makes no assumptions as to if the layout tree is up to date or
262 // not and will not try to update it. Currently clearSelection calls this
263 // (intentionally) without updating the layout tree as it doesn't care.
264 // Other callers may want to force recalc style before calling this.
265
266 // Make sure both our start and end objects are defined.
267 // Check www.msnbc.com and try clicking around to find the case where this
268 // happened.
269 if ((start && !end) || (end && !start))
270 return;
271
272 // Just return if the selection hasn't changed.
273 if (selection_start_ == start && selection_start_pos_ == start_pos &&
274 selection_end_ == end && selection_end_pos_ == end_pos)
275 return;
276
277 SelectedMap old_selected_map =
278 CollectSelectedMap(selection_start_, selection_end_, selection_end_pos_,
279 block_paint_invalidation_mode);
280 SetSelectionStateNone(old_selected_map.first);
281
282 SetSelectionState(start, end, end_pos);
283
284 // Record the old selected offsets. These will be used later when we compare
285 // against the new selected offsets.
286 const bool is_start_pos_changed = selection_start_pos_ != start_pos;
287 const bool is_end_pos_changed = selection_end_pos_ != end_pos;
288 // set selection start and end
289 selection_start_ = start;
290 selection_start_pos_ = start_pos;
291 selection_end_ = end;
292 selection_end_pos_ = end_pos;
293
294 // Now that the selection state has been updated for the new objects, walk
295 // them again and put them in the new objects list.
296 // FIXME: |new_selected_map| doesn't really need to store the
297 // SelectionState, it's just more convenient to have it use the same data
298 // structure as |old_selected_map|.
299 SelectedMap new_selected_map = CollectSelectedMap(start, end, end_pos);
300 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView());
301 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView())
yosin_UTC9 2017/05/10 07:00:57 Can we move this if-statement at start of function
302 return;
303
304 InvalidateLayoutObjects(selection_start_, is_start_pos_changed,
yosin_UTC9 2017/05/10 07:00:57 It is better to pass old selection_{start,end}_pos
305 selection_end_, is_end_pos_changed, old_selected_map,
306 new_selected_map);
yosin_UTC9 2017/05/10 07:00:57 Could you move |SelectedMap new_selected_map = Col
303 } 307 }
304 308
305 std::pair<int, int> LayoutSelection::SelectionStartEnd() { 309 std::pair<int, int> LayoutSelection::SelectionStartEnd() {
306 Commit(); 310 Commit();
307 return std::make_pair(selection_start_pos_, selection_end_pos_); 311 return std::make_pair(selection_start_pos_, selection_end_pos_);
308 } 312 }
309 313
310 void LayoutSelection::ClearSelection() { 314 void LayoutSelection::ClearSelection() {
311 // For querying Layer::compositingState() 315 // For querying Layer::compositingState()
312 // This is correct, since destroying layout objects needs to cause eager paint 316 // This is correct, since destroying layout objects needs to cause eager paint
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 446
443 o->SetShouldInvalidateSelection(); 447 o->SetShouldInvalidateSelection();
444 } 448 }
445 } 449 }
446 450
447 DEFINE_TRACE(LayoutSelection) { 451 DEFINE_TRACE(LayoutSelection) {
448 visitor->Trace(frame_selection_); 452 visitor->Trace(frame_selection_);
449 } 453 }
450 454
451 } // namespace blink 455 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698