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

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: Remove MoveMarkers() changes, move Editor method calls below methods that call them 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
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 void DocumentMarkerListEditor::AddMarker(MarkerList* list,
yosin_UTC9 2017/04/17 06:59:01 Note: Since we can't introduce this function in mi
248 const DocumentMarker* marker) {
249 RenderedDocumentMarker* rendered_marker =
250 RenderedDocumentMarker::Create(*marker);
251 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) {
252 list->push_back(rendered_marker);
253 } else {
254 if (marker->GetType() != DocumentMarker::kTextMatch &&
255 marker->GetType() != DocumentMarker::kComposition) {
256 MergeOverlapping(list, rendered_marker);
257 } else {
258 MarkerList::iterator pos =
259 std::lower_bound(list->begin(), list->end(), marker, StartsFurther);
260 list->insert(pos - list->begin(), rendered_marker);
261 }
262 }
263 }
264
265 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
266 void DocumentMarkerListEditor::MergeOverlapping(
260 MarkerList* list, 267 MarkerList* list,
261 RenderedDocumentMarker* to_insert) { 268 RenderedDocumentMarker* to_insert) {
262 MarkerList::iterator first_overlapping = 269 MarkerList::iterator first_overlapping =
263 std::lower_bound(list->begin(), list->end(), to_insert, DoesNotOverlap); 270 std::lower_bound(list->begin(), list->end(), to_insert, DoesNotOverlap);
264 size_t index = first_overlapping - list->begin(); 271 size_t index = first_overlapping - list->begin();
265 list->insert(index, to_insert); 272 list->insert(index, to_insert);
266 MarkerList::iterator inserted = list->begin() + index; 273 MarkerList::iterator inserted = list->begin() + index;
267 first_overlapping = inserted + 1; 274 first_overlapping = inserted + 1;
268 for (MarkerList::iterator i = first_overlapping; 275 for (MarkerList::iterator i = first_overlapping;
269 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { 276 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 ++marker_list_index) { 354 ++marker_list_index) {
348 Member<MarkerList>& list = (*markers)[marker_list_index]; 355 Member<MarkerList>& list = (*markers)[marker_list_index];
349 if (!list || list->IsEmpty()) { 356 if (!list || list->IsEmpty()) {
350 if (list.Get() && list->IsEmpty()) 357 if (list.Get() && list->IsEmpty())
351 list.Clear(); 358 list.Clear();
352 ++empty_lists_count; 359 ++empty_lists_count;
353 continue; 360 continue;
354 } 361 }
355 if (!marker_types.Contains((*list->begin())->GetType())) 362 if (!marker_types.Contains((*list->begin())->GetType()))
356 continue; 363 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 364
363 // markers are returned in order, so stop if we are now past the specified 365 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; 366 doc_dirty = true;
370 }
371 367
372 if (list->IsEmpty()) { 368 if (list->IsEmpty()) {
373 list.Clear(); 369 list.Clear();
374 ++empty_lists_count; 370 ++empty_lists_count;
375 } 371 }
376 } 372 }
377 373
378 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) { 374 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) {
379 markers_.erase(node); 375 markers_.erase(node);
380 if (markers_.IsEmpty()) 376 if (markers_.IsEmpty())
381 possibly_existing_marker_types_ = 0; 377 possibly_existing_marker_types_ = 0;
382 } 378 }
383 379
384 // repaint the affected node 380 // repaint the affected node
385 if (doc_dirty && node->GetLayoutObject()) { 381 if (doc_dirty && node->GetLayoutObject()) {
386 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 382 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
387 kPaintInvalidationDocumentMarkerChange); 383 kPaintInvalidationDocumentMarkerChange);
388 } 384 }
389 } 385 }
390 386
387 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
388 bool DocumentMarkerListEditor::RemoveMarkers(MarkerList* list,
yosin_UTC9 2017/04/17 06:59:01 Note: Since we can't introduce this function in mi
389 unsigned start_offset,
390 int length) {
391 bool doc_dirty = false;
392 unsigned end_offset = start_offset + length;
393 MarkerList::iterator start_pos =
394 std::upper_bound(list->begin(), list->end(), start_offset, EndsBefore);
395 for (MarkerList::iterator i = start_pos; i != list->end();) {
396 DocumentMarker marker(*i->Get());
397
398 // markers are returned in order, so stop if we are now past the specified
399 // range
400 if (marker.StartOffset() >= end_offset)
401 break;
402
403 list->erase(i - list->begin());
404 doc_dirty = true;
405 }
406
407 return doc_dirty;
408 }
409
391 DocumentMarkerVector DocumentMarkerController::MarkersFor( 410 DocumentMarkerVector DocumentMarkerController::MarkersFor(
392 Node* node, 411 Node* node,
393 DocumentMarker::MarkerTypes marker_types) { 412 DocumentMarker::MarkerTypes marker_types) {
394 DocumentMarkerVector result; 413 DocumentMarkerVector result;
395 414
396 MarkerLists* markers = markers_.at(node); 415 MarkerLists* markers = markers_.at(node);
397 if (!markers) 416 if (!markers)
398 return result; 417 return result;
399 418
400 for (size_t marker_list_index = 0; 419 for (size_t marker_list_index = 0;
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 723
705 const unsigned container_start_offset = 724 const unsigned container_start_offset =
706 range.StartPosition().ComputeOffsetInContainerNode(); 725 range.StartPosition().ComputeOffsetInContainerNode();
707 const unsigned container_end_offset = 726 const unsigned container_end_offset =
708 range.EndPosition().ComputeOffsetInContainerNode(); 727 range.EndPosition().ComputeOffsetInContainerNode();
709 728
710 bool marker_found = false; 729 bool marker_found = false;
711 for (Node& node : range.Nodes()) { 730 for (Node& node : range.Nodes()) {
712 int start_offset = node == start_container ? container_start_offset : 0; 731 int start_offset = node == start_container ? container_start_offset : 0;
713 int end_offset = node == end_container ? container_end_offset : INT_MAX; 732 int end_offset = node == end_container ? container_end_offset : INT_MAX;
733
Xiaocheng 2017/04/17 06:49:29 nit: remove this extra blank line
yosin_UTC9 2017/04/17 06:59:01 nit: We don't need to have this extra blank line.
714 marker_found |= SetMarkersActive(&node, start_offset, end_offset, active); 734 marker_found |= SetMarkersActive(&node, start_offset, end_offset, active);
715 } 735 }
716 return marker_found; 736 return marker_found;
717 } 737 }
718 738
719 bool DocumentMarkerController::SetMarkersActive(Node* node, 739 bool DocumentMarkerController::SetMarkersActive(Node* node,
720 unsigned start_offset, 740 unsigned start_offset,
721 unsigned end_offset, 741 unsigned end_offset,
722 bool active) { 742 bool active) {
723 MarkerLists* markers = markers_.at(node); 743 MarkerLists* markers = markers_.at(node);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 815
796 MarkerLists* markers = markers_.at(node); 816 MarkerLists* markers = markers_.at(node);
797 if (!markers) 817 if (!markers)
798 return; 818 return;
799 819
800 bool did_shift_marker = false; 820 bool did_shift_marker = false;
801 for (MarkerList* list : *markers) { 821 for (MarkerList* list : *markers) {
802 if (!list) 822 if (!list)
803 continue; 823 continue;
804 824
805 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { 825 if (DocumentMarkerListEditor::ShiftMarkers(list, offset, old_length,
806 RenderedDocumentMarker& marker = **it; 826 new_length))
807 Optional<DocumentMarker::MarkerOffsets> result = 827 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 } 828 }
824 829
825 if (!did_shift_marker) 830 if (!did_shift_marker)
826 return; 831 return;
827 if (!node->GetLayoutObject()) 832 if (!node->GetLayoutObject())
828 return; 833 return;
829 InvalidateRectsForMarkersInNode(*node); 834 InvalidateRectsForMarkersInNode(*node);
830 // repaint the affected node 835 // repaint the affected node
831 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(); 836 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation();
832 } 837 }
833 838
839 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
840 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list,
841 unsigned offset,
842 unsigned old_length,
843 unsigned new_length) {
844 bool did_shift_marker = false;
845 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) {
846 RenderedDocumentMarker& marker = **it;
847 Optional<DocumentMarker::MarkerOffsets> result =
848 marker.ComputeOffsetsAfterShift(offset, old_length, new_length);
849 if (result == WTF::kNullopt) {
850 list->erase(it - list->begin());
851 --it;
852 did_shift_marker = true;
853 continue;
854 }
855
856 if (marker.StartOffset() != result.value().start_offset ||
857 marker.EndOffset() != result.value().end_offset) {
858 did_shift_marker = true;
859 marker.SetStartOffset(result.value().start_offset);
860 marker.SetEndOffset(result.value().end_offset);
861 }
862 }
863
864 return did_shift_marker;
865 }
866
834 } // namespace blink 867 } // namespace blink
835 868
836 #ifndef NDEBUG 869 #ifndef NDEBUG
837 void showDocumentMarkers(const blink::DocumentMarkerController* controller) { 870 void showDocumentMarkers(const blink::DocumentMarkerController* controller) {
838 if (controller) 871 if (controller)
839 controller->ShowMarkers(); 872 controller->ShowMarkers();
840 } 873 }
841 #endif 874 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698