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 2874153002: Introduce CollectSelectedMap() in 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 } 126 }
127 127
128 // Objects each have a single selection rect to examine. 128 // Objects each have a single selection rect to examine.
129 using SelectedObjectMap = HashMap<LayoutObject*, SelectionState>; 129 using SelectedObjectMap = HashMap<LayoutObject*, SelectionState>;
130 // Blocks contain selected objects and fill gaps between them, either on the 130 // Blocks contain selected objects and fill gaps between them, either on the
131 // left, right, or in between lines and blocks. 131 // left, right, or in between lines and blocks.
132 // In order to get the visual rect right, we have to examine left, middle, and 132 // In order to get the visual rect right, we have to examine left, middle, and
133 // right rects individually, since otherwise the union of those rects might 133 // right rects individually, since otherwise the union of those rects might
134 // remain the same even when changes have occurred. 134 // remain the same even when changes have occurred.
135 using SelectedBlockMap = HashMap<LayoutBlock*, SelectionState>; 135 using SelectedBlockMap = HashMap<LayoutBlock*, SelectionState>;
136 using SelectedMap = std::pair<SelectedObjectMap, SelectedBlockMap>; 136 struct SelectedMap {
137 STACK_ALLOCATED();
138
139 SelectedMap() {}
yosin_UTC9 2017/05/12 05:16:11 nit: s/{}/= default/
yoichio 2017/05/12 05:46:43 Done.
140 SelectedMap(SelectedMap&& other) {
141 object_map = std::move(other.object_map);
142 block_map = std::move(other.block_map);
143 }
144 SelectedObjectMap object_map;
yosin_UTC9 2017/05/12 05:16:11 Please move member variable declaration before cto
yoichio 2017/05/12 05:46:43 Done.
145 SelectedBlockMap block_map;
146
147 private:
148 DISALLOW_COPY_AND_ASSIGN(SelectedMap);
149 };
150
151 static SelectedMap CollectSelectedMap(
152 LayoutObject* selection_start,
153 LayoutObject* selection_end,
154 int selection_end_pos,
155 LayoutSelection::SelectionPaintInvalidationMode
156 block_paint_invalidation_mode =
157 LayoutSelection::kPaintInvalidationNewXOROld) {
yosin_UTC9 2017/05/12 05:16:11 Since |CollectSelectedMap()| is used in two places
yoichio 2017/05/12 05:46:43 Done.
158 SelectedMap selected_map;
159 LayoutObject* iter = selection_start;
yosin_UTC9 2017/05/12 05:16:11 nit: s/iter/runner/ or s/iter/layout_object/ Blink
yoichio 2017/05/12 05:46:43 Done.
160 LayoutObject* const stop =
161 LayoutObjectAfterPosition(selection_end, selection_end_pos);
162 bool exploring_backwards = false;
163 bool continue_exploring = iter && (iter != stop);
164 while (continue_exploring) {
165 if ((iter->CanBeSelectionLeaf() || iter == selection_start ||
166 iter == selection_end) &&
167 iter->GetSelectionState() != SelectionNone) {
168 // Blocks are responsible for painting line gaps and margin gaps. They
169 // must be examined as well.
170 selected_map.object_map.Set(iter, iter->GetSelectionState());
171 if (block_paint_invalidation_mode ==
172 LayoutSelection::kPaintInvalidationNewXOROld) {
173 LayoutBlock* containing_block = iter->ContainingBlock();
174 while (containing_block && !containing_block->IsLayoutView()) {
175 SelectedBlockMap::AddResult result = selected_map.block_map.insert(
176 containing_block, containing_block->GetSelectionState());
177 if (!result.is_new_entry)
178 break;
179 containing_block = containing_block->ContainingBlock();
180 }
181 }
182 }
183
184 iter = GetNextOrPrevLayoutObjectBasedOnDirection(
185 iter, stop, continue_exploring, exploring_backwards);
186 }
187 return selected_map;
188 }
137 189
138 void LayoutSelection::SetSelection( 190 void LayoutSelection::SetSelection(
139 LayoutObject* start, 191 LayoutObject* start,
140 int start_pos, 192 int start_pos,
141 LayoutObject* end, 193 LayoutObject* end,
142 int end_pos, 194 int end_pos,
143 SelectionPaintInvalidationMode block_paint_invalidation_mode) { 195 SelectionPaintInvalidationMode block_paint_invalidation_mode) {
144 // This code makes no assumptions as to if the layout tree is up to date or 196 // This code makes no assumptions as to if the layout tree is up to date or
145 // not and will not try to update it. Currently clearSelection calls this 197 // not and will not try to update it. Currently clearSelection calls this
146 // (intentionally) without updating the layout tree as it doesn't care. 198 // (intentionally) without updating the layout tree as it doesn't care.
(...skipping 10 matching lines...) Expand all
157 selection_end_ == end && selection_end_pos_ == end_pos) 209 selection_end_ == end && selection_end_pos_ == end_pos)
158 return; 210 return;
159 211
160 DCHECK(frame_selection_->GetDocument().GetLayoutView()->GetFrameView()); 212 DCHECK(frame_selection_->GetDocument().GetLayoutView()->GetFrameView());
161 213
162 // Record the old selected objects. These will be used later when we compare 214 // Record the old selected objects. These will be used later when we compare
163 // against the new selected objects. 215 // against the new selected objects.
164 int old_start_pos = selection_start_pos_; 216 int old_start_pos = selection_start_pos_;
165 int old_end_pos = selection_end_pos_; 217 int old_end_pos = selection_end_pos_;
166 218
167 SelectedMap old_selected_map; 219 SelectedMap old_selected_map =
168 LayoutObject* os = selection_start_; 220 CollectSelectedMap(selection_start_, selection_end_, selection_end_pos_,
169 LayoutObject* stop = 221 block_paint_invalidation_mode);
170 LayoutObjectAfterPosition(selection_end_, selection_end_pos_);
171 bool exploring_backwards = false;
172 bool continue_exploring = os && (os != stop);
173 while (continue_exploring) {
174 if ((os->CanBeSelectionLeaf() || os == selection_start_ ||
175 os == selection_end_) &&
176 os->GetSelectionState() != SelectionNone) {
177 // Blocks are responsible for painting line gaps and margin gaps. They
178 // must be examined as well.
179 old_selected_map.first.Set(os, os->GetSelectionState());
180 if (block_paint_invalidation_mode == kPaintInvalidationNewXOROld) {
181 LayoutBlock* cb = os->ContainingBlock();
182 while (cb && !cb->IsLayoutView()) {
183 SelectedBlockMap::AddResult result =
184 old_selected_map.second.insert(cb, cb->GetSelectionState());
185 if (!result.is_new_entry)
186 break;
187 cb = cb->ContainingBlock();
188 }
189 }
190 }
191
192 os = GetNextOrPrevLayoutObjectBasedOnDirection(os, stop, continue_exploring,
193 exploring_backwards);
194 }
195 222
196 // Now clear the selection. 223 // Now clear the selection.
197 SelectedObjectMap::iterator old_objects_end = old_selected_map.first.end(); 224 SelectedObjectMap::iterator old_objects_end =
198 for (SelectedObjectMap::iterator i = old_selected_map.first.begin(); 225 old_selected_map.object_map.end();
226 for (SelectedObjectMap::iterator i = old_selected_map.object_map.begin();
199 i != old_objects_end; ++i) 227 i != old_objects_end; ++i)
200 i->key->SetSelectionStateIfNeeded(SelectionNone); 228 i->key->SetSelectionStateIfNeeded(SelectionNone);
201 229
202 // set selection start and end 230 // set selection start and end
203 selection_start_ = start; 231 selection_start_ = start;
204 selection_start_pos_ = start_pos; 232 selection_start_pos_ = start_pos;
205 selection_end_ = end; 233 selection_end_ = end;
206 selection_end_pos_ = end_pos; 234 selection_end_pos_ = end_pos;
207 235
208 // Update the selection status of all objects between m_selectionStart and 236 // Update the selection status of all objects between m_selectionStart and
209 // m_selectionEnd 237 // m_selectionEnd
210 if (start && start == end) { 238 if (start && start == end) {
211 start->SetSelectionStateIfNeeded(SelectionBoth); 239 start->SetSelectionStateIfNeeded(SelectionBoth);
212 } else { 240 } else {
213 if (start) 241 if (start)
214 start->SetSelectionStateIfNeeded(SelectionStart); 242 start->SetSelectionStateIfNeeded(SelectionStart);
215 if (end) 243 if (end)
216 end->SetSelectionStateIfNeeded(SelectionEnd); 244 end->SetSelectionStateIfNeeded(SelectionEnd);
217 } 245 }
218 246
219 LayoutObject* o = start; 247 LayoutObject* o = start;
220 stop = LayoutObjectAfterPosition(end, end_pos); 248 LayoutObject* const stop = LayoutObjectAfterPosition(end, end_pos);
221 249
222 while (o && o != stop) { 250 while (o && o != stop) {
223 if (o != start && o != end && o->CanBeSelectionLeaf()) 251 if (o != start && o != end && o->CanBeSelectionLeaf())
224 o->SetSelectionStateIfNeeded(SelectionInside); 252 o->SetSelectionStateIfNeeded(SelectionInside);
225 o = o->NextInPreOrder(); 253 o = o->NextInPreOrder();
226 } 254 }
227 255
228 // Now that the selection state has been updated for the new objects, walk 256 // Now that the selection state has been updated for the new objects, walk
229 // them again and put them in the new objects list. 257 // them again and put them in the new objects list.
230 // FIXME: |new_selected_map| doesn't really need to store the 258 // TODO(editing-dev): |new_selected_map| doesn't really need to store the
231 // SelectionState, it's just more convenient to have it use the same data 259 // SelectionState, it's just more convenient to have it use the same data
232 // structure as |old_selected_map|. 260 // structure as |old_selected_map|.
233 SelectedMap new_selected_map; 261 SelectedMap new_selected_map = CollectSelectedMap(start, end, end_pos);
234 o = start;
235 exploring_backwards = false;
236 continue_exploring = o && (o != stop);
237 while (continue_exploring) {
238 if ((o->CanBeSelectionLeaf() || o == start || o == end) &&
239 o->GetSelectionState() != SelectionNone) {
240 new_selected_map.first.Set(o, o->GetSelectionState());
241 LayoutBlock* cb = o->ContainingBlock();
242 while (cb && !cb->IsLayoutView()) {
243 SelectedBlockMap::AddResult result =
244 new_selected_map.second.insert(cb, cb->GetSelectionState());
245 if (!result.is_new_entry)
246 break;
247 cb = cb->ContainingBlock();
248 }
249 }
250
251 o = GetNextOrPrevLayoutObjectBasedOnDirection(o, stop, continue_exploring,
252 exploring_backwards);
253 }
254 262
255 // Have any of the old selected objects changed compared to the new selection? 263 // Have any of the old selected objects changed compared to the new selection?
256 for (SelectedObjectMap::iterator i = old_selected_map.first.begin(); 264 for (SelectedObjectMap::iterator i = old_selected_map.object_map.begin();
257 i != old_objects_end; ++i) { 265 i != old_objects_end; ++i) {
258 LayoutObject* obj = i->key; 266 LayoutObject* obj = i->key;
259 SelectionState new_selection_state = obj->GetSelectionState(); 267 SelectionState new_selection_state = obj->GetSelectionState();
260 SelectionState old_selection_state = i->value; 268 SelectionState old_selection_state = i->value;
261 if (new_selection_state != old_selection_state || 269 if (new_selection_state != old_selection_state ||
262 (selection_start_ == obj && old_start_pos != selection_start_pos_) || 270 (selection_start_ == obj && old_start_pos != selection_start_pos_) ||
263 (selection_end_ == obj && old_end_pos != selection_end_pos_)) { 271 (selection_end_ == obj && old_end_pos != selection_end_pos_)) {
264 obj->SetShouldInvalidateSelection(); 272 obj->SetShouldInvalidateSelection();
265 new_selected_map.first.erase(obj); 273 new_selected_map.object_map.erase(obj);
266 } 274 }
267 } 275 }
268 276
269 // Any new objects that remain were not found in the old objects dict, and so 277 // Any new objects that remain were not found in the old objects dict, and so
270 // they need to be updated. 278 // they need to be updated.
271 SelectedObjectMap::iterator new_objects_end = new_selected_map.first.end(); 279 SelectedObjectMap::iterator new_objects_end =
272 for (SelectedObjectMap::iterator i = new_selected_map.first.begin(); 280 new_selected_map.object_map.end();
281 for (SelectedObjectMap::iterator i = new_selected_map.object_map.begin();
273 i != new_objects_end; ++i) 282 i != new_objects_end; ++i)
274 i->key->SetShouldInvalidateSelection(); 283 i->key->SetShouldInvalidateSelection();
275 284
276 // Have any of the old blocks changed? 285 // Have any of the old blocks changed?
277 SelectedBlockMap::iterator old_blocks_end = old_selected_map.second.end(); 286 SelectedBlockMap::iterator old_blocks_end = old_selected_map.block_map.end();
278 for (SelectedBlockMap::iterator i = old_selected_map.second.begin(); 287 for (SelectedBlockMap::iterator i = old_selected_map.block_map.begin();
279 i != old_blocks_end; ++i) { 288 i != old_blocks_end; ++i) {
280 LayoutBlock* block = i->key; 289 LayoutBlock* block = i->key;
281 SelectionState new_selection_state = block->GetSelectionState(); 290 SelectionState new_selection_state = block->GetSelectionState();
282 SelectionState old_selection_state = i->value; 291 SelectionState old_selection_state = i->value;
283 if (new_selection_state != old_selection_state) { 292 if (new_selection_state != old_selection_state) {
284 block->SetShouldInvalidateSelection(); 293 block->SetShouldInvalidateSelection();
285 new_selected_map.second.erase(block); 294 new_selected_map.block_map.erase(block);
286 } 295 }
287 } 296 }
288 297
289 // Any new blocks that remain were not found in the old blocks dict, and so 298 // Any new blocks that remain were not found in the old blocks dict, and so
290 // they need to be updated. 299 // they need to be updated.
291 SelectedBlockMap::iterator new_blocks_end = new_selected_map.second.end(); 300 SelectedBlockMap::iterator new_blocks_end = new_selected_map.block_map.end();
292 for (SelectedBlockMap::iterator i = new_selected_map.second.begin(); 301 for (SelectedBlockMap::iterator i = new_selected_map.block_map.begin();
293 i != new_blocks_end; ++i) 302 i != new_blocks_end; ++i)
294 i->key->SetShouldInvalidateSelection(); 303 i->key->SetShouldInvalidateSelection();
295 } 304 }
296 305
297 std::pair<int, int> LayoutSelection::SelectionStartEnd() { 306 std::pair<int, int> LayoutSelection::SelectionStartEnd() {
298 Commit(); 307 Commit();
299 return std::make_pair(selection_start_pos_, selection_end_pos_); 308 return std::make_pair(selection_start_pos_, selection_end_pos_);
300 } 309 }
301 310
302 void LayoutSelection::ClearSelection() { 311 void LayoutSelection::ClearSelection() {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 434
426 o->SetShouldInvalidateSelection(); 435 o->SetShouldInvalidateSelection();
427 } 436 }
428 } 437 }
429 438
430 DEFINE_TRACE(LayoutSelection) { 439 DEFINE_TRACE(LayoutSelection) {
431 visitor->Trace(frame_selection_); 440 visitor->Trace(frame_selection_);
432 } 441 }
433 442
434 } // namespace blink 443 } // 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