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

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 typedef HashMap<LayoutObject*, SelectionState> SelectedObjectMap;
yosin_UTC9 2017/04/28 03:21:51 nit: better to use |using|
yoichio 2017/04/28 08:00:33 Done.
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 typedef HashMap<LayoutBlock*, SelectionState> SelectedBlockMap;
yosin_UTC9 2017/04/28 03:21:51 nit: better to use |using|
yoichio 2017/04/28 08:00:33 Done.
141 // (intentionally) without updating the layout tree as it doesn't care.
142 // Other callers may want to force recalc style before calling this.
143 141
144 // Make sure both our start and end objects are defined. 142 static std::tuple<SelectedObjectMap, SelectedBlockMap> CollectSelectedMap(
yosin_UTC9 2017/04/28 03:21:51 nit: it is better to use |std::pair<>| since it ha
yoichio 2017/04/28 08:00:34 Done.
145 // Check www.msnbc.com and try clicking around to find the case where this 143 LayoutObject* const selection_start,
146 // happened. 144 LayoutObject* const selection_end,
147 if ((start && !end) || (end && !start)) 145 int selection_end_pos,
148 return; 146 LayoutSelection::SelectionPaintInvalidationMode
149 147 block_paint_invalidation_mode =
150 // Just return if the selection hasn't changed. 148 LayoutSelection::kPaintInvalidationNewXOROld) {
151 if (selection_start_ == start && selection_start_pos_ == start_pos && 149 SelectedObjectMap selected_objects;
152 selection_end_ == end && selection_end_pos_ == end_pos) 150 SelectedBlockMap selected_blocks;
153 return; 151 LayoutObject* layout_object = selection_start;
154 152 LayoutObject* const stop =
155 // Record the old selected objects. These will be used later when we compare 153 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; 154 bool exploring_backwards = false;
184 bool continue_exploring = os && (os != stop); 155 bool continue_exploring = layout_object && (layout_object != stop);
185 while (continue_exploring) { 156 while (continue_exploring) {
186 if ((os->CanBeSelectionLeaf() || os == selection_start_ || 157 if ((layout_object->CanBeSelectionLeaf() ||
187 os == selection_end_) && 158 layout_object == selection_start || layout_object == selection_end) &&
188 os->GetSelectionState() != SelectionNone) { 159 layout_object->GetSelectionState() != SelectionNone) {
189 // Blocks are responsible for painting line gaps and margin gaps. They 160 // Blocks are responsible for painting line gaps and margin gaps. They
190 // must be examined as well. 161 // must be examined as well.
191 old_selected_objects.Set(os, os->GetSelectionState()); 162 selected_objects.Set(layout_object, layout_object->GetSelectionState());
192 if (block_paint_invalidation_mode == kPaintInvalidationNewXOROld) { 163 if (block_paint_invalidation_mode ==
193 LayoutBlock* cb = os->ContainingBlock(); 164 LayoutSelection::kPaintInvalidationNewXOROld) {
165 LayoutBlock* cb = layout_object->ContainingBlock();
194 while (cb && !cb->IsLayoutView()) { 166 while (cb && !cb->IsLayoutView()) {
195 SelectedBlockMap::AddResult result = 167 SelectedBlockMap::AddResult result =
196 old_selected_blocks.insert(cb, cb->GetSelectionState()); 168 selected_blocks.insert(cb, cb->GetSelectionState());
197 if (!result.is_new_entry) 169 if (!result.is_new_entry)
198 break; 170 break;
199 cb = cb->ContainingBlock(); 171 cb = cb->ContainingBlock();
200 } 172 }
201 } 173 }
202 } 174 }
203 175
204 os = GetNextOrPrevLayoutObjectBasedOnDirection(os, stop, continue_exploring, 176 layout_object = GetNextOrPrevLayoutObjectBasedOnDirection(
205 exploring_backwards); 177 layout_object, stop, continue_exploring, exploring_backwards);
206 } 178 }
179 return std::forward_as_tuple(selected_objects, selected_blocks);
yoichio 2017/04/28 02:16:26 Does this correctly move |selected_objects| and |s
yosin_UTC9 2017/04/28 03:21:51 |std::forward_as_tuple()| is used for function arg
yoichio 2017/04/28 08:00:33 Acknowledged.
180 }
207 181
208 // Now clear the selection. 182 static void SetSelectionStateNone(const SelectedObjectMap& map) {
209 SelectedObjectMap::iterator old_objects_end = old_selected_objects.end(); 183 for (const auto& it : map)
210 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); 184 it.key->SetSelectionStateIfNeeded(SelectionNone);
211 i != old_objects_end; ++i) 185 }
212 i->key->SetSelectionStateIfNeeded(SelectionNone);
213 186
214 // set selection start and end 187 static void SetSelectionState(LayoutObject* start,
215 selection_start_ = start; 188 LayoutObject* end,
216 selection_start_pos_ = start_pos; 189 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 190 // Update the selection status of all objects between m_selectionStart and
221 // m_selectionEnd 191 // m_selectionEnd
222 if (start && start == end) { 192 if (start && start == end) {
223 start->SetSelectionStateIfNeeded(SelectionBoth); 193 start->SetSelectionStateIfNeeded(SelectionBoth);
224 } else { 194 } else {
225 if (start) 195 if (start)
226 start->SetSelectionStateIfNeeded(SelectionStart); 196 start->SetSelectionStateIfNeeded(SelectionStart);
227 if (end) 197 if (end)
228 end->SetSelectionStateIfNeeded(SelectionEnd); 198 end->SetSelectionStateIfNeeded(SelectionEnd);
229 } 199 }
230 200
231 LayoutObject* o = start; 201 LayoutObject* layout_object = start;
232 stop = LayoutObjectAfterPosition(end, end_pos); 202 LayoutObject* const stop = LayoutObjectAfterPosition(end, end_pos);
203 while (layout_object && layout_object != stop) {
204 if (layout_object != start && layout_object != end &&
205 layout_object->CanBeSelectionLeaf())
206 layout_object->SetSelectionStateIfNeeded(SelectionInside);
207 layout_object = layout_object->NextInPreOrder();
208 }
209 }
233 210
234 while (o && o != stop) { 211 static void InvalidateLayoutObjects(
235 if (o != start && o != end && o->CanBeSelectionLeaf()) 212 LayoutObject* start,
236 o->SetSelectionStateIfNeeded(SelectionInside); 213 bool is_start_pos_changed,
237 o = o->NextInPreOrder(); 214 LayoutObject* end,
238 } 215 bool is_end_pos_changed,
239 216 const SelectedObjectMap& old_selected_objects,
240 // Now that the selection state has been updated for the new objects, walk 217 const SelectedBlockMap& old_selected_blocks,
241 // them again and put them in the new objects list. 218 SelectedObjectMap& new_selected_objects,
242 o = start; 219 SelectedBlockMap& new_selected_blocks) {
243 exploring_backwards = false;
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
267 // 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?
268 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); 221 for (const auto& it : old_selected_objects) {
269 i != old_objects_end; ++i) { 222 LayoutObject* obj = it.key;
270 LayoutObject* obj = i->key;
271 SelectionState new_selection_state = obj->GetSelectionState(); 223 SelectionState new_selection_state = obj->GetSelectionState();
272 SelectionState old_selection_state = i->value; 224 SelectionState old_selection_state = it.value;
273 if (new_selection_state != old_selection_state || 225 if (new_selection_state != old_selection_state ||
274 (selection_start_ == obj && old_start_pos != selection_start_pos_) || 226 (start == obj && is_start_pos_changed) ||
275 (selection_end_ == obj && old_end_pos != selection_end_pos_)) { 227 (end == obj && is_end_pos_changed)) {
276 obj->SetShouldInvalidateSelection(); 228 obj->SetShouldInvalidateSelection();
277 new_selected_objects.erase(obj); 229 new_selected_objects.erase(obj);
278 } 230 }
279 } 231 }
280 232
281 // 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
282 // they need to be updated. 234 // they need to be updated.
283 SelectedObjectMap::iterator new_objects_end = new_selected_objects.end(); 235 for (const auto& it : new_selected_objects)
284 for (SelectedObjectMap::iterator i = new_selected_objects.begin(); 236 it.key->SetShouldInvalidateSelection();
285 i != new_objects_end; ++i)
286 i->key->SetShouldInvalidateSelection();
287 237
288 // Have any of the old blocks changed? 238 // Have any of the old blocks changed?
289 SelectedBlockMap::iterator old_blocks_end = old_selected_blocks.end(); 239 for (const auto& it : old_selected_blocks) {
290 for (SelectedBlockMap::iterator i = old_selected_blocks.begin(); 240 LayoutBlock* block = it.key;
yosin_UTC9 2017/04/28 03:21:51 nit: s/LayoutBlock*/LayoutBlock* const/ Is it bett
yoichio 2017/04/28 08:00:33 Done.
291 i != old_blocks_end; ++i) {
292 LayoutBlock* block = i->key;
293 SelectionState new_selection_state = block->GetSelectionState(); 241 SelectionState new_selection_state = block->GetSelectionState();
294 SelectionState old_selection_state = i->value; 242 SelectionState old_selection_state = it.value;
yosin_UTC9 2017/04/28 03:21:51 nit: s/SelectionState/const SelectionState/
yoichio 2017/04/28 08:00:33 Done.
295 if (new_selection_state != old_selection_state) { 243 if (new_selection_state != old_selection_state) {
296 block->SetShouldInvalidateSelection(); 244 block->SetShouldInvalidateSelection();
297 new_selected_blocks.erase(block); 245 new_selected_blocks.erase(block);
298 } 246 }
299 } 247 }
300 248
301 // 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
302 // they need to be updated. 250 // they need to be updated.
303 SelectedBlockMap::iterator new_blocks_end = new_selected_blocks.end(); 251 for (const auto& it : new_selected_blocks)
304 for (SelectedBlockMap::iterator i = new_selected_blocks.begin(); 252 it.key->SetShouldInvalidateSelection();
305 i != new_blocks_end; ++i) 253 }
306 i->key->SetShouldInvalidateSelection(); 254
255 void LayoutSelection::SetSelection(
256 LayoutObject* start,
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 SelectedObjectMap old_selected_objects;
278 SelectedBlockMap old_selected_blocks;
279 std::tie(old_selected_objects, old_selected_blocks) =
yosin_UTC9 2017/04/28 03:21:51 I think it is better to introduce |struct Selected
yoichio 2017/04/28 08:00:33 Done.
280 CollectSelectedMap(selection_start_, selection_end_, selection_end_pos_,
281 block_paint_invalidation_mode);
282 SetSelectionStateNone(old_selected_objects);
283
284 SetSelectionState(start, end, end_pos);
285
286 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView());
287 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView())
288 return;
289
290 // Now that the selection state has been updated for the new objects, walk
291 // them again and put them in the new objects list.
292 // FIXME: |new_selected_objects| doesn't really need to store the
293 // SelectionState, it's just more convenient to have it use the same data
294 // structure as |old_selected_objects|.
295 SelectedObjectMap new_selected_objects;
296 // FIXME: |new_selected_blocks| 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_blocks|.
299 SelectedBlockMap new_selected_blocks;
300 std::tie(new_selected_objects, new_selected_blocks) =
301 CollectSelectedMap(start, end, end_pos);
302
303 // Record the old selected offsets. These will be used later when we compare
304 // against the new selected offsets.
305 const bool is_start_pos_changed = selection_start_pos_ != start_pos;
306 const bool is_end_pos_changed = selection_end_pos_ != end_pos;
307 // set selection start and end
308 selection_start_ = start;
309 selection_start_pos_ = start_pos;
310 selection_end_ = end;
311 selection_end_pos_ = end_pos;
312 InvalidateLayoutObjects(selection_start_, is_start_pos_changed,
313 selection_end_, is_end_pos_changed,
314 old_selected_objects, old_selected_blocks,
315 new_selected_objects, new_selected_blocks);
307 } 316 }
308 317
309 void LayoutSelection::SelectionStartEnd(int& start_pos, int& end_pos) { 318 void LayoutSelection::SelectionStartEnd(int& start_pos, int& end_pos) {
310 Commit(); 319 Commit();
311 start_pos = selection_start_pos_; 320 start_pos = selection_start_pos_;
312 end_pos = selection_end_pos_; 321 end_pos = selection_end_pos_;
313 } 322 }
314 323
315 void LayoutSelection::ClearSelection() { 324 void LayoutSelection::ClearSelection() {
316 // For querying Layer::compositingState() 325 // For querying Layer::compositingState()
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 456
448 o->SetShouldInvalidateSelection(); 457 o->SetShouldInvalidateSelection();
449 } 458 }
450 } 459 }
451 460
452 DEFINE_TRACE(LayoutSelection) { 461 DEFINE_TRACE(LayoutSelection) {
453 visitor->Trace(frame_selection_); 462 visitor->Trace(frame_selection_);
454 } 463 }
455 464
456 } // namespace blink 465 } // 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