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

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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 exploring_backwards = !next && (next != stop); 123 exploring_backwards = !next && (next != stop);
124 if (exploring_backwards) { 124 if (exploring_backwards) {
125 next = stop->PreviousInPreOrder(); 125 next = stop->PreviousInPreOrder();
126 continue_exploring = next && !next->IsLayoutView(); 126 continue_exploring = next && !next->IsLayoutView();
127 } 127 }
128 } 128 }
129 129
130 return next; 130 return next;
131 } 131 }
132 132
133 void LayoutSelection::SetSelection( 133 // Objects each have a single selection rect to examine.
134 LayoutObject* start, 134 using SelectedObjectMap = HashMap<LayoutObject*, SelectionState>;
135 int start_pos, 135 // Blocks contain selected objects and fill gaps between them, either on the
136 LayoutObject* end, 136 // left, right, or in between lines and blocks.
137 int end_pos, 137 // In order to get the visual rect right, we have to examine left, middle, and
138 SelectionPaintInvalidationMode block_paint_invalidation_mode) { 138 // right rects individually, since otherwise the union of those rects might
139 // This code makes no assumptions as to if the layout tree is up to date or 139 // remain the same even when changes have occurred.
140 // not and will not try to update it. Currently clearSelection calls this 140 using SelectedBlockMap = HashMap<LayoutBlock*, SelectionState>;
141 // (intentionally) without updating the layout tree as it doesn't care. 141 using SelectedMap = std::pair<SelectedObjectMap, SelectedBlockMap>;
142 // Other callers may want to force recalc style before calling this.
143 142
144 // Make sure both our start and end objects are defined. 143 static SelectedMap CollectSelectedMap(
145 // Check www.msnbc.com and try clicking around to find the case where this 144 LayoutObject* const selection_start,
146 // happened. 145 LayoutObject* const selection_end,
147 if ((start && !end) || (end && !start)) 146 int selection_end_pos,
148 return; 147 LayoutSelection::SelectionPaintInvalidationMode
149 148 block_paint_invalidation_mode =
150 // Just return if the selection hasn't changed. 149 LayoutSelection::kPaintInvalidationNewXOROld) {
151 if (selection_start_ == start && selection_start_pos_ == start_pos && 150 SelectedObjectMap selected_objects;
152 selection_end_ == end && selection_end_pos_ == end_pos) 151 SelectedBlockMap selected_blocks;
153 return; 152 LayoutObject* layout_object = selection_start;
154 153 LayoutObject* const stop =
155 // Record the old selected objects. These will be used later when we compare 154 LayoutObjectAfterPosition(selection_end, selection_end_pos);
156 // against the new selected objects.
157 int old_start_pos = selection_start_pos_;
158 int old_end_pos = selection_end_pos_;
159
160 // Objects each have a single selection rect to examine.
161 typedef HashMap<LayoutObject*, SelectionState> SelectedObjectMap;
162 SelectedObjectMap old_selected_objects;
163 // FIXME: |newSelectedObjects| doesn't really need to store the
164 // SelectionState, it's just more convenient to have it use the same data
165 // structure as |oldSelectedObjects|.
166 SelectedObjectMap new_selected_objects;
167
168 // Blocks contain selected objects and fill gaps between them, either on the
169 // left, right, or in between lines and blocks.
170 // In order to get the visual rect right, we have to examine left, middle, and
171 // right rects individually, since otherwise the union of those rects might
172 // remain the same even when changes have occurred.
173 typedef HashMap<LayoutBlock*, SelectionState> SelectedBlockMap;
174 SelectedBlockMap old_selected_blocks;
175 // FIXME: |newSelectedBlocks| doesn't really need to store the SelectionState,
176 // it's just more convenient to have it use the same data structure as
177 // |oldSelectedBlocks|.
178 SelectedBlockMap new_selected_blocks;
179
180 LayoutObject* os = selection_start_;
181 LayoutObject* stop =
182 LayoutObjectAfterPosition(selection_end_, selection_end_pos_);
183 bool exploring_backwards = false; 155 bool exploring_backwards = false;
184 bool continue_exploring = os && (os != stop); 156 bool continue_exploring = layout_object && (layout_object != stop);
185 while (continue_exploring) { 157 while (continue_exploring) {
186 if ((os->CanBeSelectionLeaf() || os == selection_start_ || 158 if ((layout_object->CanBeSelectionLeaf() ||
187 os == selection_end_) && 159 layout_object == selection_start || layout_object == selection_end) &&
188 os->GetSelectionState() != SelectionNone) { 160 layout_object->GetSelectionState() != SelectionNone) {
189 // Blocks are responsible for painting line gaps and margin gaps. They 161 // Blocks are responsible for painting line gaps and margin gaps. They
190 // must be examined as well. 162 // must be examined as well.
191 old_selected_objects.Set(os, os->GetSelectionState()); 163 selected_objects.Set(layout_object, layout_object->GetSelectionState());
192 if (block_paint_invalidation_mode == kPaintInvalidationNewXOROld) { 164 if (block_paint_invalidation_mode ==
193 LayoutBlock* cb = os->ContainingBlock(); 165 LayoutSelection::kPaintInvalidationNewXOROld) {
166 LayoutBlock* cb = layout_object->ContainingBlock();
194 while (cb && !cb->IsLayoutView()) { 167 while (cb && !cb->IsLayoutView()) {
195 SelectedBlockMap::AddResult result = 168 SelectedBlockMap::AddResult result =
196 old_selected_blocks.insert(cb, cb->GetSelectionState()); 169 selected_blocks.insert(cb, cb->GetSelectionState());
197 if (!result.is_new_entry) 170 if (!result.is_new_entry)
198 break; 171 break;
199 cb = cb->ContainingBlock(); 172 cb = cb->ContainingBlock();
200 } 173 }
201 } 174 }
202 } 175 }
203 176
204 os = GetNextOrPrevLayoutObjectBasedOnDirection(os, stop, continue_exploring, 177 layout_object = GetNextOrPrevLayoutObjectBasedOnDirection(
205 exploring_backwards); 178 layout_object, stop, continue_exploring, exploring_backwards);
206 } 179 }
180 return std::make_pair(selected_objects, selected_blocks);
yoichio 2017/04/28 08:00:34 I'm not sure if this is optimized by copy elision.
yosin_UTC9 2017/04/28 09:05:07 RVO can use move semantics[1], to make sure RVO, w
yoichio 2017/05/10 06:19:33 RVO can be applied when return expression type mee
181 }
207 182
208 // Now clear the selection. 183 static void SetSelectionStateNone(const SelectedObjectMap& map) {
209 SelectedObjectMap::iterator old_objects_end = old_selected_objects.end(); 184 for (const auto& it : map)
210 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); 185 it.key->SetSelectionStateIfNeeded(SelectionNone);
211 i != old_objects_end; ++i) 186 }
212 i->key->SetSelectionStateIfNeeded(SelectionNone);
213 187
214 // set selection start and end 188 static void SetSelectionState(LayoutObject* start,
215 selection_start_ = start; 189 LayoutObject* end,
216 selection_start_pos_ = start_pos; 190 int end_pos) {
217 selection_end_ = end;
218 selection_end_pos_ = end_pos;
219
220 // Update the selection status of all objects between m_selectionStart and 191 // Update the selection status of all objects between m_selectionStart and
221 // m_selectionEnd 192 // m_selectionEnd
222 if (start && start == end) { 193 if (start && start == end) {
223 start->SetSelectionStateIfNeeded(SelectionBoth); 194 start->SetSelectionStateIfNeeded(SelectionBoth);
224 } else { 195 } else {
225 if (start) 196 if (start)
226 start->SetSelectionStateIfNeeded(SelectionStart); 197 start->SetSelectionStateIfNeeded(SelectionStart);
227 if (end) 198 if (end)
228 end->SetSelectionStateIfNeeded(SelectionEnd); 199 end->SetSelectionStateIfNeeded(SelectionEnd);
229 } 200 }
230 201
231 LayoutObject* o = start; 202 LayoutObject* layout_object = start;
232 stop = LayoutObjectAfterPosition(end, end_pos); 203 LayoutObject* const stop = LayoutObjectAfterPosition(end, end_pos);
204 while (layout_object && layout_object != stop) {
205 if (layout_object != start && layout_object != end &&
206 layout_object->CanBeSelectionLeaf())
207 layout_object->SetSelectionStateIfNeeded(SelectionInside);
208 layout_object = layout_object->NextInPreOrder();
209 }
210 }
233 211
234 while (o && o != stop) { 212 static void InvalidateLayoutObjects(LayoutObject* start,
235 if (o != start && o != end && o->CanBeSelectionLeaf()) 213 bool is_start_pos_changed,
236 o->SetSelectionStateIfNeeded(SelectionInside); 214 LayoutObject* end,
237 o = o->NextInPreOrder(); 215 bool is_end_pos_changed,
238 } 216 const SelectedMap& old_selected_map,
239 217 SelectedMap& new_selected_map) {
240 // Now that the selection state has been updated for the new objects, walk 218 const SelectedObjectMap& old_selected_objects = old_selected_map.first;
241 // them again and put them in the new objects list. 219 const SelectedBlockMap& old_selected_blocks = old_selected_map.second;
242 o = start; 220 SelectedObjectMap& new_selected_objects = new_selected_map.first;
243 exploring_backwards = false; 221 SelectedBlockMap& new_selected_blocks = new_selected_map.second;
244 continue_exploring = o && (o != stop);
245 while (continue_exploring) {
246 if ((o->CanBeSelectionLeaf() || o == start || o == end) &&
247 o->GetSelectionState() != SelectionNone) {
248 new_selected_objects.Set(o, o->GetSelectionState());
249 LayoutBlock* cb = o->ContainingBlock();
250 while (cb && !cb->IsLayoutView()) {
251 SelectedBlockMap::AddResult result =
252 new_selected_blocks.insert(cb, cb->GetSelectionState());
253 if (!result.is_new_entry)
254 break;
255 cb = cb->ContainingBlock();
256 }
257 }
258
259 o = GetNextOrPrevLayoutObjectBasedOnDirection(o, stop, continue_exploring,
260 exploring_backwards);
261 }
262
263 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView());
264 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView())
265 return;
266 222
267 // Have any of the old selected objects changed compared to the new selection? 223 // Have any of the old selected objects changed compared to the new selection?
268 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); 224 for (const auto& it : old_selected_objects) {
269 i != old_objects_end; ++i) { 225 LayoutObject* const obj = it.key;
270 LayoutObject* obj = i->key; 226 const SelectionState new_selection_state = obj->GetSelectionState();
271 SelectionState new_selection_state = obj->GetSelectionState(); 227 const SelectionState old_selection_state = it.value;
272 SelectionState old_selection_state = i->value;
273 if (new_selection_state != old_selection_state || 228 if (new_selection_state != old_selection_state ||
274 (selection_start_ == obj && old_start_pos != selection_start_pos_) || 229 (start == obj && is_start_pos_changed) ||
275 (selection_end_ == obj && old_end_pos != selection_end_pos_)) { 230 (end == obj && is_end_pos_changed)) {
276 obj->SetShouldInvalidateSelection(); 231 obj->SetShouldInvalidateSelection();
277 new_selected_objects.erase(obj); 232 new_selected_objects.erase(obj);
278 } 233 }
279 } 234 }
280 235
281 // Any new objects that remain were not found in the old objects dict, and so 236 // Any new objects that remain were not found in the old objects dict, and so
282 // they need to be updated. 237 // they need to be updated.
283 SelectedObjectMap::iterator new_objects_end = new_selected_objects.end(); 238 for (const auto& it : new_selected_objects)
284 for (SelectedObjectMap::iterator i = new_selected_objects.begin(); 239 it.key->SetShouldInvalidateSelection();
285 i != new_objects_end; ++i)
286 i->key->SetShouldInvalidateSelection();
287 240
288 // Have any of the old blocks changed? 241 // Have any of the old blocks changed?
289 SelectedBlockMap::iterator old_blocks_end = old_selected_blocks.end(); 242 for (const auto& it : old_selected_blocks) {
290 for (SelectedBlockMap::iterator i = old_selected_blocks.begin(); 243 LayoutBlock* const block = it.key;
291 i != old_blocks_end; ++i) { 244 const SelectionState new_selection_state = block->GetSelectionState();
292 LayoutBlock* block = i->key; 245 const SelectionState old_selection_state = it.value;
293 SelectionState new_selection_state = block->GetSelectionState();
294 SelectionState old_selection_state = i->value;
295 if (new_selection_state != old_selection_state) { 246 if (new_selection_state != old_selection_state) {
296 block->SetShouldInvalidateSelection(); 247 block->SetShouldInvalidateSelection();
297 new_selected_blocks.erase(block); 248 new_selected_blocks.erase(block);
298 } 249 }
299 } 250 }
300 251
301 // Any new blocks that remain were not found in the old blocks dict, and so 252 // Any new blocks that remain were not found in the old blocks dict, and so
302 // they need to be updated. 253 // they need to be updated.
303 SelectedBlockMap::iterator new_blocks_end = new_selected_blocks.end(); 254 for (const auto& it : new_selected_blocks)
304 for (SelectedBlockMap::iterator i = new_selected_blocks.begin(); 255 it.key->SetShouldInvalidateSelection();
305 i != new_blocks_end; ++i) 256 }
306 i->key->SetShouldInvalidateSelection(); 257
258 void LayoutSelection::SetSelection(
259 LayoutObject* start,
260 int start_pos,
261 LayoutObject* end,
262 int end_pos,
263 SelectionPaintInvalidationMode block_paint_invalidation_mode) {
264 // This code makes no assumptions as to if the layout tree is up to date or
265 // not and will not try to update it. Currently clearSelection calls this
266 // (intentionally) without updating the layout tree as it doesn't care.
267 // Other callers may want to force recalc style before calling this.
268
269 // Make sure both our start and end objects are defined.
270 // Check www.msnbc.com and try clicking around to find the case where this
271 // happened.
272 if ((start && !end) || (end && !start))
273 return;
274
275 // Just return if the selection hasn't changed.
276 if (selection_start_ == start && selection_start_pos_ == start_pos &&
277 selection_end_ == end && selection_end_pos_ == end_pos)
278 return;
279
280 SelectedMap old_selected_map =
281 CollectSelectedMap(selection_start_, selection_end_, selection_end_pos_,
282 block_paint_invalidation_mode);
283 SetSelectionStateNone(old_selected_map.first);
284
285 SetSelectionState(start, end, end_pos);
286
287 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView());
288 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView())
Xiaocheng 2017/04/28 22:07:43 We shouldn't change the execution order in a refac
yoichio 2017/05/10 06:19:33 Done.
289 return;
290
291 // Now that the selection state has been updated for the new objects, walk
292 // them again and put them in the new objects list.
293 // FIXME: |new_selected_map| doesn't really need to store the
294 // SelectionState, it's just more convenient to have it use the same data
295 // structure as |old_selected_map|.
296 SelectedMap new_selected_map = CollectSelectedMap(start, end, end_pos);
297
298 // Record the old selected offsets. These will be used later when we compare
299 // against the new selected offsets.
300 const bool is_start_pos_changed = selection_start_pos_ != start_pos;
301 const bool is_end_pos_changed = selection_end_pos_ != end_pos;
302 // set selection start and end
303 selection_start_ = start;
Xiaocheng 2017/04/28 22:07:44 These four lines should be done right after SetSel
yoichio 2017/05/10 06:19:33 Done.
304 selection_start_pos_ = start_pos;
305 selection_end_ = end;
306 selection_end_pos_ = end_pos;
307 InvalidateLayoutObjects(selection_start_, is_start_pos_changed,
308 selection_end_, is_end_pos_changed, old_selected_map,
309 new_selected_map);
307 } 310 }
308 311
309 void LayoutSelection::SelectionStartEnd(int& start_pos, int& end_pos) { 312 void LayoutSelection::SelectionStartEnd(int& start_pos, int& end_pos) {
310 Commit(); 313 Commit();
311 start_pos = selection_start_pos_; 314 start_pos = selection_start_pos_;
312 end_pos = selection_end_pos_; 315 end_pos = selection_end_pos_;
313 } 316 }
314 317
315 void LayoutSelection::ClearSelection() { 318 void LayoutSelection::ClearSelection() {
316 // For querying Layer::compositingState() 319 // For querying Layer::compositingState()
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 450
448 o->SetShouldInvalidateSelection(); 451 o->SetShouldInvalidateSelection();
449 } 452 }
450 } 453 }
451 454
452 DEFINE_TRACE(LayoutSelection) { 455 DEFINE_TRACE(LayoutSelection) {
453 visitor->Trace(frame_selection_); 456 visitor->Trace(frame_selection_);
454 } 457 }
455 458
456 } // namespace blink 459 } // 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