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

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

Issue 2812423002: [DMC #1] Refactor DocumentMarkerController on top of new DocumentMarkerListEditor class (Closed)
Patch Set: Respond to comments Created 3 years, 8 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 | « third_party/WebKit/Source/core/editing/markers/DocumentMarkerController.h ('k') | 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 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights
7 * reserved. 7 * reserved.
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/) 9 * (http://www.torchmobile.com/)
10 * Copyright (C) Research In Motion Limited 2010. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 markers->Grow(DocumentMarker::kMarkerTypeIndexesCount); 227 markers->Grow(DocumentMarker::kMarkerTypeIndexesCount);
228 } 228 }
229 229
230 DocumentMarker::MarkerTypeIndex marker_list_index = 230 DocumentMarker::MarkerTypeIndex marker_list_index =
231 MarkerTypeToMarkerIndex(new_marker.GetType()); 231 MarkerTypeToMarkerIndex(new_marker.GetType());
232 if (!markers->at(marker_list_index)) { 232 if (!markers->at(marker_list_index)) {
233 markers->at(marker_list_index) = new MarkerList; 233 markers->at(marker_list_index) = new MarkerList;
234 } 234 }
235 235
236 Member<MarkerList>& list = markers->at(marker_list_index); 236 Member<MarkerList>& list = markers->at(marker_list_index);
237 RenderedDocumentMarker* new_rendered_marker = 237 DocumentMarkerListEditor::AddMarker(list, &new_marker);
238 RenderedDocumentMarker::Create(new_marker);
239 if (list->IsEmpty() || list->back()->EndOffset() < new_marker.StartOffset()) {
240 list->push_back(new_rendered_marker);
241 } else {
242 if (new_marker.GetType() != DocumentMarker::kTextMatch &&
243 new_marker.GetType() != DocumentMarker::kComposition) {
244 MergeOverlapping(list.Get(), new_rendered_marker);
245 } else {
246 MarkerList::iterator pos = std::lower_bound(list->begin(), list->end(),
247 &new_marker, StartsFurther);
248 list->insert(pos - list->begin(), new_rendered_marker);
249 }
250 }
251 238
252 // repaint the affected node 239 // repaint the affected node
253 if (node->GetLayoutObject()) { 240 if (node->GetLayoutObject()) {
254 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 241 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
255 kPaintInvalidationDocumentMarkerChange); 242 kPaintInvalidationDocumentMarkerChange);
256 } 243 }
257 } 244 }
258 245
259 void DocumentMarkerController::MergeOverlapping( 246 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
247 // TODO(rlanday): this method was created by cutting and pasting code from
248 // DocumentMarkerController::AddMarker(), it should be refactored in a future CL
249 void DocumentMarkerListEditor::AddMarker(MarkerList* list,
250 const DocumentMarker* marker) {
251 RenderedDocumentMarker* rendered_marker =
252 RenderedDocumentMarker::Create(*marker);
253 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) {
254 list->push_back(rendered_marker);
255 } else {
256 if (marker->GetType() != DocumentMarker::kTextMatch &&
257 marker->GetType() != DocumentMarker::kComposition) {
258 MergeOverlapping(list, rendered_marker);
259 } else {
260 MarkerList::iterator pos =
261 std::lower_bound(list->begin(), list->end(), marker, StartsFurther);
262 list->insert(pos - list->begin(), rendered_marker);
263 }
264 }
265 }
266
267 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
268 void DocumentMarkerListEditor::MergeOverlapping(
260 MarkerList* list, 269 MarkerList* list,
261 RenderedDocumentMarker* to_insert) { 270 RenderedDocumentMarker* to_insert) {
262 MarkerList::iterator first_overlapping = 271 MarkerList::iterator first_overlapping =
263 std::lower_bound(list->begin(), list->end(), to_insert, DoesNotOverlap); 272 std::lower_bound(list->begin(), list->end(), to_insert, DoesNotOverlap);
264 size_t index = first_overlapping - list->begin(); 273 size_t index = first_overlapping - list->begin();
265 list->insert(index, to_insert); 274 list->insert(index, to_insert);
266 MarkerList::iterator inserted = list->begin() + index; 275 MarkerList::iterator inserted = list->begin() + index;
267 first_overlapping = inserted + 1; 276 first_overlapping = inserted + 1;
268 for (MarkerList::iterator i = first_overlapping; 277 for (MarkerList::iterator i = first_overlapping;
269 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { 278 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 ++marker_list_index) { 356 ++marker_list_index) {
348 Member<MarkerList>& list = (*markers)[marker_list_index]; 357 Member<MarkerList>& list = (*markers)[marker_list_index];
349 if (!list || list->IsEmpty()) { 358 if (!list || list->IsEmpty()) {
350 if (list.Get() && list->IsEmpty()) 359 if (list.Get() && list->IsEmpty())
351 list.Clear(); 360 list.Clear();
352 ++empty_lists_count; 361 ++empty_lists_count;
353 continue; 362 continue;
354 } 363 }
355 if (!marker_types.Contains((*list->begin())->GetType())) 364 if (!marker_types.Contains((*list->begin())->GetType()))
356 continue; 365 continue;
357 unsigned end_offset = start_offset + length;
358 MarkerList::iterator start_pos =
359 std::upper_bound(list->begin(), list->end(), start_offset, EndsBefore);
360 for (MarkerList::iterator i = start_pos; i != list->end();) {
361 DocumentMarker marker(*i->Get());
362 366
363 // markers are returned in order, so stop if we are now past the specified 367 if (DocumentMarkerListEditor::RemoveMarkers(list, start_offset, length))
364 // range
365 if (marker.StartOffset() >= end_offset)
366 break;
367
368 list->erase(i - list->begin());
369 doc_dirty = true; 368 doc_dirty = true;
370 }
371 369
372 if (list->IsEmpty()) { 370 if (list->IsEmpty()) {
373 list.Clear(); 371 list.Clear();
374 ++empty_lists_count; 372 ++empty_lists_count;
375 } 373 }
376 } 374 }
377 375
378 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) { 376 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) {
379 markers_.erase(node); 377 markers_.erase(node);
380 if (markers_.IsEmpty()) 378 if (markers_.IsEmpty())
381 possibly_existing_marker_types_ = 0; 379 possibly_existing_marker_types_ = 0;
382 } 380 }
383 381
384 // repaint the affected node 382 // repaint the affected node
385 if (doc_dirty && node->GetLayoutObject()) { 383 if (doc_dirty && node->GetLayoutObject()) {
386 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 384 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
387 kPaintInvalidationDocumentMarkerChange); 385 kPaintInvalidationDocumentMarkerChange);
388 } 386 }
389 } 387 }
390 388
389 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
390 // TODO(rlanday): this method was created by cutting and pasting code from
391 // DocumentMarkerController::RemoveMarkers(), it should be refactored in a
392 // future CL
393 bool DocumentMarkerListEditor::RemoveMarkers(MarkerList* list,
394 unsigned start_offset,
395 int length) {
396 bool doc_dirty = false;
397 unsigned end_offset = start_offset + length;
398 MarkerList::iterator start_pos =
399 std::upper_bound(list->begin(), list->end(), start_offset, EndsBefore);
400 for (MarkerList::iterator i = start_pos; i != list->end();) {
401 DocumentMarker marker(*i->Get());
402
403 // markers are returned in order, so stop if we are now past the specified
404 // range
405 if (marker.StartOffset() >= end_offset)
406 break;
407
408 list->erase(i - list->begin());
409 doc_dirty = true;
410 }
411
412 return doc_dirty;
413 }
414
391 DocumentMarkerVector DocumentMarkerController::MarkersFor( 415 DocumentMarkerVector DocumentMarkerController::MarkersFor(
392 Node* node, 416 Node* node,
393 DocumentMarker::MarkerTypes marker_types) { 417 DocumentMarker::MarkerTypes marker_types) {
394 DocumentMarkerVector result; 418 DocumentMarkerVector result;
395 419
396 MarkerLists* markers = markers_.at(node); 420 MarkerLists* markers = markers_.at(node);
397 if (!markers) 421 if (!markers)
398 return result; 422 return result;
399 423
400 for (size_t marker_list_index = 0; 424 for (size_t marker_list_index = 0;
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 819
796 MarkerLists* markers = markers_.at(node); 820 MarkerLists* markers = markers_.at(node);
797 if (!markers) 821 if (!markers)
798 return; 822 return;
799 823
800 bool did_shift_marker = false; 824 bool did_shift_marker = false;
801 for (MarkerList* list : *markers) { 825 for (MarkerList* list : *markers) {
802 if (!list) 826 if (!list)
803 continue; 827 continue;
804 828
805 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { 829 if (DocumentMarkerListEditor::ShiftMarkers(list, offset, old_length,
806 RenderedDocumentMarker& marker = **it; 830 new_length))
807 Optional<DocumentMarker::MarkerOffsets> result = 831 did_shift_marker = true;
808 marker.ComputeOffsetsAfterShift(offset, old_length, new_length);
809 if (result == WTF::kNullopt) {
810 list->erase(it - list->begin());
811 --it;
812 did_shift_marker = true;
813 continue;
814 }
815
816 if (marker.StartOffset() != result.value().start_offset ||
817 marker.EndOffset() != result.value().end_offset) {
818 did_shift_marker = true;
819 marker.SetStartOffset(result.value().start_offset);
820 marker.SetEndOffset(result.value().end_offset);
821 }
822 }
823 } 832 }
824 833
825 if (!did_shift_marker) 834 if (!did_shift_marker)
826 return; 835 return;
827 if (!node->GetLayoutObject()) 836 if (!node->GetLayoutObject())
828 return; 837 return;
829 InvalidateRectsForMarkersInNode(*node); 838 InvalidateRectsForMarkersInNode(*node);
830 // repaint the affected node 839 // repaint the affected node
831 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(); 840 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation();
832 } 841 }
833 842
843 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
844 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list,
845 unsigned offset,
846 unsigned old_length,
847 unsigned new_length) {
848 bool did_shift_marker = false;
849 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) {
850 RenderedDocumentMarker& marker = **it;
851 Optional<DocumentMarker::MarkerOffsets> result =
852 marker.ComputeOffsetsAfterShift(offset, old_length, new_length);
853 if (result == WTF::kNullopt) {
854 list->erase(it - list->begin());
855 --it;
856 did_shift_marker = true;
857 continue;
858 }
859
860 if (marker.StartOffset() != result.value().start_offset ||
861 marker.EndOffset() != result.value().end_offset) {
862 did_shift_marker = true;
863 marker.SetStartOffset(result.value().start_offset);
864 marker.SetEndOffset(result.value().end_offset);
865 }
866 }
867
868 return did_shift_marker;
869 }
870
834 } // namespace blink 871 } // namespace blink
835 872
836 #ifndef NDEBUG 873 #ifndef NDEBUG
837 void showDocumentMarkers(const blink::DocumentMarkerController* controller) { 874 void showDocumentMarkers(const blink::DocumentMarkerController* controller) {
838 if (controller) 875 if (controller)
839 controller->ShowMarkers(); 876 controller->ShowMarkers();
840 } 877 }
841 #endif 878 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/markers/DocumentMarkerController.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698