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

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: Add two more TODOs 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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 if (!exception_state.HadException()) { 202 if (!exception_state.HadException()) {
203 // TODO(yosin): Once we have a |EphemeralRange| version of |boundingBox()|, 203 // TODO(yosin): Once we have a |EphemeralRange| version of |boundingBox()|,
204 // we should use it instead of |Range| version. 204 // we should use it instead of |Range| version.
205 marker.SetRenderedRect(LayoutRect(range->BoundingBox())); 205 marker.SetRenderedRect(LayoutRect(range->BoundingBox()));
206 } else { 206 } else {
207 marker.NullifyRenderedRect(); 207 marker.NullifyRenderedRect();
208 } 208 }
209 range->Dispose(); 209 range->Dispose();
210 } 210 }
211 211
212 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
213 void DocumentMarkerListEditor::MergeOverlapping(
Xiaocheng 2017/04/14 00:48:06 Could you put it at the same place as DMC::MergeOv
yosin_UTC9 2017/04/14 01:21:14 Please to follow xiaochengh@'s comment. The purpos
214 MarkerList* list,
215 RenderedDocumentMarker* to_insert) {
216 MarkerList::iterator first_overlapping =
217 std::lower_bound(list->begin(), list->end(), to_insert, DoesNotOverlap);
218 size_t index = first_overlapping - list->begin();
219 list->insert(index, to_insert);
220 MarkerList::iterator inserted = list->begin() + index;
221 first_overlapping = inserted + 1;
222 for (MarkerList::iterator i = first_overlapping;
223 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) {
224 (*inserted)->SetStartOffset(
225 std::min((*inserted)->StartOffset(), (*i)->StartOffset()));
226 (*inserted)->SetEndOffset(
227 std::max((*inserted)->EndOffset(), (*i)->EndOffset()));
228 list->erase(i - list->begin());
229 }
230 }
231
232 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
233 void DocumentMarkerListEditor::AddMarker(MarkerList* list,
234 DocumentMarker* marker) {
235 RenderedDocumentMarker* rendered_marker =
236 RenderedDocumentMarker::Create(*marker);
237 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) {
238 list->push_back(rendered_marker);
239 } else {
240 if (marker->GetType() != DocumentMarker::kTextMatch &&
241 marker->GetType() != DocumentMarker::kComposition) {
242 MergeOverlapping(list, rendered_marker);
243 } else {
244 MarkerList::iterator pos =
245 std::lower_bound(list->begin(), list->end(), marker, StartsFurther);
246 list->insert(pos - list->begin(), rendered_marker);
247 }
248 }
249 }
250
212 // Markers are stored in order sorted by their start offset. 251 // Markers are stored in order sorted by their start offset.
213 // Markers of the same type do not overlap each other. 252 // Markers of the same type do not overlap each other.
214 253
215 void DocumentMarkerController::AddMarker(Node* node, 254 void DocumentMarkerController::AddMarker(Node* node,
216 const DocumentMarker& new_marker) { 255 const DocumentMarker& new_marker) {
217 DCHECK_GE(new_marker.EndOffset(), new_marker.StartOffset()); 256 DCHECK_GE(new_marker.EndOffset(), new_marker.StartOffset());
218 if (new_marker.EndOffset() == new_marker.StartOffset()) 257 if (new_marker.EndOffset() == new_marker.StartOffset())
219 return; 258 return;
220 259
221 possibly_existing_marker_types_.Add(new_marker.GetType()); 260 possibly_existing_marker_types_.Add(new_marker.GetType());
222 261
223 Member<MarkerLists>& markers = 262 Member<MarkerLists>& markers =
224 markers_.insert(node, nullptr).stored_value->value; 263 markers_.insert(node, nullptr).stored_value->value;
225 if (!markers) { 264 if (!markers) {
226 markers = new MarkerLists; 265 markers = new MarkerLists;
227 markers->Grow(DocumentMarker::kMarkerTypeIndexesCount); 266 markers->Grow(DocumentMarker::kMarkerTypeIndexesCount);
228 } 267 }
229 268
230 DocumentMarker::MarkerTypeIndex marker_list_index = 269 DocumentMarker::MarkerTypeIndex marker_list_index =
231 MarkerTypeToMarkerIndex(new_marker.GetType()); 270 MarkerTypeToMarkerIndex(new_marker.GetType());
232 if (!markers->at(marker_list_index)) { 271 if (!markers->at(marker_list_index)) {
233 markers->at(marker_list_index) = new MarkerList; 272 markers->at(marker_list_index) = new MarkerList;
234 } 273 }
235 274
236 Member<MarkerList>& list = markers->at(marker_list_index); 275 Member<MarkerList>& list = markers->at(marker_list_index);
237 RenderedDocumentMarker* new_rendered_marker = 276 RenderedDocumentMarker* new_rendered_marker =
Xiaocheng 2017/04/14 00:48:06 No need to create RDM here. It's already done in D
238 RenderedDocumentMarker::Create(new_marker); 277 RenderedDocumentMarker::Create(new_marker);
239 if (list->IsEmpty() || list->back()->EndOffset() < new_marker.StartOffset()) { 278 DocumentMarkerListEditor::AddMarker(list, new_rendered_marker);
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 279
252 // repaint the affected node 280 // repaint the affected node
253 if (node->GetLayoutObject()) { 281 if (node->GetLayoutObject()) {
254 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 282 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
255 kPaintInvalidationDocumentMarkerChange); 283 kPaintInvalidationDocumentMarkerChange);
256 } 284 }
257 } 285 }
258 286
259 void DocumentMarkerController::MergeOverlapping( 287 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
260 MarkerList* list, 288 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list,
261 RenderedDocumentMarker* to_insert) { 289 int length,
262 MarkerList::iterator first_overlapping = 290 MarkerList* dst_list) {
263 std::lower_bound(list->begin(), list->end(), to_insert, DoesNotOverlap); 291 DCHECK_GT(length, 0);
264 size_t index = first_overlapping - list->begin(); 292 bool didMoveMarker = false;
265 list->insert(index, to_insert); 293 unsigned end_offset = length - 1;
266 MarkerList::iterator inserted = list->begin() + index; 294
267 first_overlapping = inserted + 1; 295 MarkerList::iterator it;
268 for (MarkerList::iterator i = first_overlapping; 296 for (it = src_list->begin(); it != src_list->end(); ++it) {
269 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { 297 DocumentMarker& marker = **it;
270 (*inserted)->SetStartOffset( 298 if (marker.StartOffset() > end_offset)
271 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); 299 break;
272 (*inserted)->SetEndOffset( 300
273 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); 301 // pin the marker to the specified range and apply the shift delta
274 list->erase(i - list->begin()); 302 if (marker.EndOffset() > end_offset)
303 marker.SetEndOffset(end_offset);
304
305 DocumentMarkerListEditor::AddMarker(dst_list, &marker);
306 didMoveMarker = true;
275 } 307 }
308
309 // Remove the range of markers that were moved to dstNode
310 src_list->erase(0, it - src_list->begin());
311
312 return didMoveMarker;
276 } 313 }
277 314
278 // Moves markers from src_node to dst_node. Markers are moved if their start 315 // Moves markers from src_node to dst_node. Markers are moved if their start
279 // offset is less than length. Markers that run past that point are truncated. 316 // offset is less than length. Markers that run past that point are truncated.
280 void DocumentMarkerController::MoveMarkers(Node* src_node, 317 void DocumentMarkerController::MoveMarkers(Node* src_node,
281 int length, 318 int length,
282 Node* dst_node) { 319 Node* dst_node) {
283 if (length <= 0) 320 if (length <= 0)
284 return; 321 return;
285 322
286 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers())) 323 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers()))
287 return; 324 return;
288 DCHECK(!markers_.IsEmpty()); 325 DCHECK(!markers_.IsEmpty());
289 326
290 MarkerLists* markers = markers_.at(src_node); 327 MarkerLists* src_markers = markers_.at(src_node);
291 if (!markers) 328 if (!src_markers)
292 return; 329 return;
293 330
331 Member<MarkerLists>& dst_markers =
Xiaocheng 2017/04/14 00:48:06 Could you make the code less hacky? if (!markers_
332 markers_.insert(dst_node, nullptr).stored_value->value;
333 if (!dst_markers) {
334 dst_markers = new MarkerLists;
335 dst_markers->Grow(DocumentMarker::kMarkerTypeIndexesCount);
336 }
337
294 bool doc_dirty = false; 338 bool doc_dirty = false;
295 for (Member<MarkerList> list : *markers) { 339 for (size_t marker_list_index = 0; marker_list_index < src_markers->size();
296 if (!list) 340 marker_list_index++) {
341 MarkerList* src_list = src_markers->at(marker_list_index);
342 if (!src_list)
297 continue; 343 continue;
298 344
299 unsigned end_offset = length - 1; 345 if (!dst_markers->at(marker_list_index)) {
300 MarkerList::iterator it; 346 dst_markers->at(marker_list_index) = new MarkerList;
301 for (it = list->begin(); it != list->end(); ++it) {
302 DocumentMarker* marker = it->Get();
303
304 // stop if we are now past the specified range
305 if (marker->StartOffset() > end_offset)
306 break;
307
308 // pin the marker to the specified range
309 doc_dirty = true;
310 if (marker->EndOffset() > end_offset)
311 marker->SetEndOffset(end_offset);
312
313 AddMarker(dst_node, *marker);
314 } 347 }
315 348
316 // Remove the range of markers that were moved to dstNode 349 DocumentMarkerListEditor::MoveMarkers(src_list, length,
Xiaocheng 2017/04/14 00:48:06 doc_dirty |= DMLEditor::MoveMarkers(...)
317 list->erase(0, it - list->begin()); 350 dst_markers->at(marker_list_index));
318 } 351 }
319 352
320 // repaint the affected node 353 // repaint the affected node
321 if (doc_dirty && dst_node->GetLayoutObject()) { 354 if (doc_dirty && dst_node->GetLayoutObject()) {
322 dst_node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 355 dst_node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
323 kPaintInvalidationDocumentMarkerChange); 356 kPaintInvalidationDocumentMarkerChange);
324 } 357 }
325 } 358 }
326 359
360 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
361 bool DocumentMarkerListEditor::RemoveMarkers(MarkerList* list,
362 unsigned start_offset,
363 int length) {
364 bool doc_dirty = false;
365 unsigned end_offset = start_offset + length;
366 MarkerList::iterator start_pos =
367 std::upper_bound(list->begin(), list->end(), start_offset, EndsBefore);
368 for (MarkerList::iterator i = start_pos; i != list->end();) {
369 DocumentMarker marker(*i->Get());
370
371 // markers are returned in order, so stop if we are now past the specified
372 // range
373 if (marker.StartOffset() >= end_offset)
374 break;
375
376 list->erase(i - list->begin());
377 doc_dirty = true;
378 }
379
380 return doc_dirty;
381 }
382
327 void DocumentMarkerController::RemoveMarkers( 383 void DocumentMarkerController::RemoveMarkers(
328 Node* node, 384 Node* node,
329 unsigned start_offset, 385 unsigned start_offset,
330 int length, 386 int length,
331 DocumentMarker::MarkerTypes marker_types) { 387 DocumentMarker::MarkerTypes marker_types) {
332 if (length <= 0) 388 if (length <= 0)
333 return; 389 return;
334 390
335 if (!PossiblyHasMarkers(marker_types)) 391 if (!PossiblyHasMarkers(marker_types))
336 return; 392 return;
(...skipping 10 matching lines...) Expand all
347 ++marker_list_index) { 403 ++marker_list_index) {
348 Member<MarkerList>& list = (*markers)[marker_list_index]; 404 Member<MarkerList>& list = (*markers)[marker_list_index];
349 if (!list || list->IsEmpty()) { 405 if (!list || list->IsEmpty()) {
350 if (list.Get() && list->IsEmpty()) 406 if (list.Get() && list->IsEmpty())
351 list.Clear(); 407 list.Clear();
352 ++empty_lists_count; 408 ++empty_lists_count;
353 continue; 409 continue;
354 } 410 }
355 if (!marker_types.Contains((*list->begin())->GetType())) 411 if (!marker_types.Contains((*list->begin())->GetType()))
356 continue; 412 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 413
363 // markers are returned in order, so stop if we are now past the specified 414 doc_dirty =
Xiaocheng 2017/04/14 00:48:06 nit: We can write doc_dirty |= DMLEditor::RemoveMa
rlanday 2017/04/14 01:01:16 I don't think so, I think that would be the same a
yosin_UTC9 2017/04/14 01:21:14 foo |= bar() always call |bar()| regard less value
364 // range 415 DocumentMarkerListEditor::RemoveMarkers(list, start_offset, length) ||
365 if (marker.StartOffset() >= end_offset) 416 doc_dirty;
366 break;
367
368 list->erase(i - list->begin());
369 doc_dirty = true;
370 }
371 417
372 if (list->IsEmpty()) { 418 if (list->IsEmpty()) {
373 list.Clear(); 419 list.Clear();
374 ++empty_lists_count; 420 ++empty_lists_count;
375 } 421 }
376 } 422 }
377 423
378 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) { 424 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) {
379 markers_.erase(node); 425 markers_.erase(node);
380 if (markers_.IsEmpty()) 426 if (markers_.IsEmpty())
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 return; 711 return;
666 DCHECK(!markers_.IsEmpty()); 712 DCHECK(!markers_.IsEmpty());
667 713
668 // outer loop: process each markered node in the document 714 // outer loop: process each markered node in the document
669 MarkerMap::iterator end = markers_.end(); 715 MarkerMap::iterator end = markers_.end();
670 for (MarkerMap::iterator i = markers_.begin(); i != end; ++i) { 716 for (MarkerMap::iterator i = markers_.begin(); i != end; ++i) {
671 const Node* node = i->key; 717 const Node* node = i->key;
672 718
673 // inner loop: process each marker in the current node 719 // inner loop: process each marker in the current node
674 MarkerLists* markers = i->value.Get(); 720 MarkerLists* markers = i->value.Get();
675 for (size_t marker_list_index = 0; 721 for (DocumentMarker::MarkerType type : DocumentMarker::AllMarkers()) {
Xiaocheng 2017/04/14 00:48:06 Irrelevant change. Btw, marker type iterators hav
676 marker_list_index < DocumentMarker::kMarkerTypeIndexesCount; 722 size_t marker_list_index = MarkerTypeToMarkerIndex(type);
677 ++marker_list_index) { 723 MarkerList* list = (*markers)[marker_list_index];
678 Member<MarkerList>& list = (*markers)[marker_list_index]; 724 if (!list || list->IsEmpty() || !marker_types.Contains(type))
679 if (!list || list->IsEmpty() ||
680 !marker_types.Contains((*list->begin())->GetType()))
681 continue; 725 continue;
682 726
683 // cause the node to be redrawn 727 // cause the node to be redrawn
684 if (LayoutObject* layout_object = node->GetLayoutObject()) { 728 if (LayoutObject* layout_object = node->GetLayoutObject()) {
685 layout_object->SetShouldDoFullPaintInvalidation( 729 layout_object->SetShouldDoFullPaintInvalidation(
686 kPaintInvalidationDocumentMarkerChange); 730 kPaintInvalidationDocumentMarkerChange);
687 break; 731 break;
688 } 732 }
689 } 733 }
690 } 734 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 builder.Append(")"); 821 builder.Append(")");
778 } 822 }
779 } 823 }
780 builder.Append("\n"); 824 builder.Append("\n");
781 } 825 }
782 LOG(INFO) << markers_.size() << " nodes have markers:\n" 826 LOG(INFO) << markers_.size() << " nodes have markers:\n"
783 << builder.ToString().Utf8().Data(); 827 << builder.ToString().Utf8().Data();
784 } 828 }
785 #endif 829 #endif
786 830
831 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
832 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list,
833 unsigned offset,
834 unsigned old_length,
835 unsigned new_length) {
836 bool did_shift_marker = false;
837 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) {
838 RenderedDocumentMarker& marker = **it;
839 Optional<DocumentMarker::MarkerOffsets> result =
840 marker.ComputeOffsetsAfterShift(offset, old_length, new_length);
841 if (result == WTF::kNullopt) {
842 list->erase(it - list->begin());
843 --it;
844 did_shift_marker = true;
845 continue;
846 }
847
848 if (marker.StartOffset() != result.value().start_offset ||
849 marker.EndOffset() != result.value().end_offset) {
850 did_shift_marker = true;
851 marker.SetStartOffset(result.value().start_offset);
852 marker.SetEndOffset(result.value().end_offset);
853 }
854 }
855
856 return did_shift_marker;
857 }
858
787 // SynchronousMutationObserver 859 // SynchronousMutationObserver
788 void DocumentMarkerController::DidUpdateCharacterData(CharacterData* node, 860 void DocumentMarkerController::DidUpdateCharacterData(CharacterData* node,
789 unsigned offset, 861 unsigned offset,
790 unsigned old_length, 862 unsigned old_length,
791 unsigned new_length) { 863 unsigned new_length) {
792 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers())) 864 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers()))
793 return; 865 return;
794 DCHECK(!markers_.IsEmpty()); 866 DCHECK(!markers_.IsEmpty());
795 867
796 MarkerLists* markers = markers_.at(node); 868 MarkerLists* markers = markers_.at(node);
797 if (!markers) 869 if (!markers)
798 return; 870 return;
799 871
800 bool did_shift_marker = false; 872 bool did_shift_marker = false;
801 for (MarkerList* list : *markers) { 873 for (MarkerList* list : *markers) {
802 if (!list) 874 if (!list)
803 continue; 875 continue;
804 876
805 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { 877 did_shift_marker = DocumentMarkerListEditor::ShiftMarkers(
yosin_UTC9 2017/04/14 01:21:14 Please put |DMListEditor::ShiftMarkers()| here.
rlanday 2017/04/14 01:29:37 Sorry, I don't understand what you're asking here,
Xiaocheng 2017/04/14 00:48:06 nit: We can write doc_dirty |= DMLEditor::ShiftMar
806 RenderedDocumentMarker& marker = **it; 878 list, offset, old_length, new_length) ||
807 Optional<DocumentMarker::MarkerOffsets> result = 879 did_shift_marker;
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 } 880 }
824 881
825 if (!did_shift_marker) 882 if (!did_shift_marker)
826 return; 883 return;
827 if (!node->GetLayoutObject()) 884 if (!node->GetLayoutObject())
828 return; 885 return;
829 InvalidateRectsForMarkersInNode(*node); 886 InvalidateRectsForMarkersInNode(*node);
830 // repaint the affected node 887 // repaint the affected node
831 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(); 888 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation();
832 } 889 }
833 890
834 } // namespace blink 891 } // namespace blink
835 892
836 #ifndef NDEBUG 893 #ifndef NDEBUG
837 void showDocumentMarkers(const blink::DocumentMarkerController* controller) { 894 void showDocumentMarkers(const blink::DocumentMarkerController* controller) {
838 if (controller) 895 if (controller)
839 controller->ShowMarkers(); 896 controller->ShowMarkers();
840 } 897 }
841 #endif 898 #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