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

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 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::AddMarker(MarkerList* list,
214 const DocumentMarker* marker) {
215 RenderedDocumentMarker* rendered_marker =
216 RenderedDocumentMarker::Create(*marker);
217 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) {
218 list->push_back(rendered_marker);
219 } else {
220 if (marker->GetType() != DocumentMarker::kTextMatch &&
221 marker->GetType() != DocumentMarker::kComposition) {
222 MergeOverlapping(list, rendered_marker);
223 } else {
224 MarkerList::iterator pos =
225 std::lower_bound(list->begin(), list->end(), marker, StartsFurther);
226 list->insert(pos - list->begin(), rendered_marker);
227 }
228 }
229 }
230
212 // Markers are stored in order sorted by their start offset. 231 // Markers are stored in order sorted by their start offset.
213 // Markers of the same type do not overlap each other. 232 // Markers of the same type do not overlap each other.
214 233
215 void DocumentMarkerController::AddMarker(Node* node, 234 void DocumentMarkerController::AddMarker(Node* node,
216 const DocumentMarker& new_marker) { 235 const DocumentMarker& new_marker) {
217 DCHECK_GE(new_marker.EndOffset(), new_marker.StartOffset()); 236 DCHECK_GE(new_marker.EndOffset(), new_marker.StartOffset());
218 if (new_marker.EndOffset() == new_marker.StartOffset()) 237 if (new_marker.EndOffset() == new_marker.StartOffset())
219 return; 238 return;
220 239
221 possibly_existing_marker_types_.Add(new_marker.GetType()); 240 possibly_existing_marker_types_.Add(new_marker.GetType());
222 241
223 Member<MarkerLists>& markers = 242 Member<MarkerLists>& markers =
224 markers_.insert(node, nullptr).stored_value->value; 243 markers_.insert(node, nullptr).stored_value->value;
225 if (!markers) { 244 if (!markers) {
226 markers = new MarkerLists; 245 markers = new MarkerLists;
227 markers->Grow(DocumentMarker::kMarkerTypeIndexesCount); 246 markers->Grow(DocumentMarker::kMarkerTypeIndexesCount);
228 } 247 }
229 248
230 DocumentMarker::MarkerTypeIndex marker_list_index = 249 DocumentMarker::MarkerTypeIndex marker_list_index =
231 MarkerTypeToMarkerIndex(new_marker.GetType()); 250 MarkerTypeToMarkerIndex(new_marker.GetType());
232 if (!markers->at(marker_list_index)) { 251 if (!markers->at(marker_list_index)) {
233 markers->at(marker_list_index) = new MarkerList; 252 markers->at(marker_list_index) = new MarkerList;
234 } 253 }
235 254
236 Member<MarkerList>& list = markers->at(marker_list_index); 255 Member<MarkerList>& list = markers->at(marker_list_index);
237 RenderedDocumentMarker* new_rendered_marker = 256 DocumentMarkerListEditor::AddMarker(list, &new_marker);
yosin_UTC9 2017/04/14 04:14:22 Please implement DocumentMarkerListEditor::AddMark
Xiaocheng 2017/04/14 04:37:54 There are some other code in DMC::AddMarker after
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 257
252 // repaint the affected node 258 // repaint the affected node
253 if (node->GetLayoutObject()) { 259 if (node->GetLayoutObject()) {
254 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 260 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
255 kPaintInvalidationDocumentMarkerChange); 261 kPaintInvalidationDocumentMarkerChange);
256 } 262 }
257 } 263 }
258 264
259 void DocumentMarkerController::MergeOverlapping( 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();) {
270 (*inserted)->SetStartOffset( 277 (*inserted)->SetStartOffset(
271 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); 278 std::min((*inserted)->StartOffset(), (*i)->StartOffset()));
272 (*inserted)->SetEndOffset( 279 (*inserted)->SetEndOffset(
273 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); 280 std::max((*inserted)->EndOffset(), (*i)->EndOffset()));
274 list->erase(i - list->begin()); 281 list->erase(i - list->begin());
275 } 282 }
276 } 283 }
277 284
285 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
286 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list,
287 int length,
288 MarkerList* dst_list) {
289 DCHECK_GT(length, 0);
290 bool didMoveMarker = false;
291 unsigned end_offset = length - 1;
292
293 MarkerList::iterator it;
294 for (it = src_list->begin(); it != src_list->end(); ++it) {
295 DocumentMarker& marker = **it;
296 if (marker.StartOffset() > end_offset)
297 break;
298
299 // pin the marker to the specified range and apply the shift delta
300 if (marker.EndOffset() > end_offset)
301 marker.SetEndOffset(end_offset);
302
303 DocumentMarkerListEditor::AddMarker(dst_list, &marker);
304 didMoveMarker = true;
305 }
306
307 // Remove the range of markers that were moved to dstNode
308 src_list->erase(0, it - src_list->begin());
309
310 return didMoveMarker;
311 }
312
278 // Moves markers from src_node to dst_node. Markers are moved if their start 313 // 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. 314 // offset is less than length. Markers that run past that point are truncated.
280 void DocumentMarkerController::MoveMarkers(Node* src_node, 315 void DocumentMarkerController::MoveMarkers(Node* src_node,
281 int length, 316 int length,
282 Node* dst_node) { 317 Node* dst_node) {
283 if (length <= 0) 318 if (length <= 0)
284 return; 319 return;
285 320
286 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers())) 321 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers()))
287 return; 322 return;
288 DCHECK(!markers_.IsEmpty()); 323 DCHECK(!markers_.IsEmpty());
289 324
290 MarkerLists* markers = markers_.at(src_node); 325 MarkerLists* src_markers = markers_.at(src_node);
291 if (!markers) 326 if (!src_markers)
292 return; 327 return;
293 328
329 if (!markers_.Contains(dst_node)) {
330 markers_.insert(dst_node,
331 new MarkerLists(DocumentMarker::kMarkerTypeIndexesCount));
332 }
333
334 MarkerLists* dst_markers = markers_.at(dst_node);
335
294 bool doc_dirty = false; 336 bool doc_dirty = false;
295 for (Member<MarkerList> list : *markers) { 337 for (size_t marker_list_index = 0; marker_list_index < src_markers->size();
296 if (!list) 338 marker_list_index++) {
339 MarkerList* src_list = src_markers->at(marker_list_index);
340 if (!src_list)
297 continue; 341 continue;
298 342
299 unsigned end_offset = length - 1; 343 if (!dst_markers->at(marker_list_index)) {
300 MarkerList::iterator it; 344 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 } 345 }
315 346
316 // Remove the range of markers that were moved to dstNode 347 if (DocumentMarkerListEditor::MoveMarkers(
317 list->erase(0, it - list->begin()); 348 src_list, length, dst_markers->at(marker_list_index)))
349 doc_dirty = true;
318 } 350 }
319 351
320 // repaint the affected node 352 // repaint the affected node
321 if (doc_dirty && dst_node->GetLayoutObject()) { 353 if (doc_dirty && dst_node->GetLayoutObject()) {
322 dst_node->GetLayoutObject()->SetShouldDoFullPaintInvalidation( 354 dst_node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(
323 kPaintInvalidationDocumentMarkerChange); 355 kPaintInvalidationDocumentMarkerChange);
324 } 356 }
325 } 357 }
326 358
359 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
360 bool DocumentMarkerListEditor::RemoveMarkers(MarkerList* list,
361 unsigned start_offset,
362 int length) {
363 bool doc_dirty = false;
364 unsigned end_offset = start_offset + length;
365 MarkerList::iterator start_pos =
366 std::upper_bound(list->begin(), list->end(), start_offset, EndsBefore);
367 for (MarkerList::iterator i = start_pos; i != list->end();) {
368 DocumentMarker marker(*i->Get());
369
370 // markers are returned in order, so stop if we are now past the specified
371 // range
372 if (marker.StartOffset() >= end_offset)
373 break;
374
375 list->erase(i - list->begin());
376 doc_dirty = true;
377 }
378
379 return doc_dirty;
380 }
381
327 void DocumentMarkerController::RemoveMarkers( 382 void DocumentMarkerController::RemoveMarkers(
328 Node* node, 383 Node* node,
329 unsigned start_offset, 384 unsigned start_offset,
330 int length, 385 int length,
331 DocumentMarker::MarkerTypes marker_types) { 386 DocumentMarker::MarkerTypes marker_types) {
332 if (length <= 0) 387 if (length <= 0)
333 return; 388 return;
334 389
335 if (!PossiblyHasMarkers(marker_types)) 390 if (!PossiblyHasMarkers(marker_types))
336 return; 391 return;
(...skipping 10 matching lines...) Expand all
347 ++marker_list_index) { 402 ++marker_list_index) {
348 Member<MarkerList>& list = (*markers)[marker_list_index]; 403 Member<MarkerList>& list = (*markers)[marker_list_index];
349 if (!list || list->IsEmpty()) { 404 if (!list || list->IsEmpty()) {
350 if (list.Get() && list->IsEmpty()) 405 if (list.Get() && list->IsEmpty())
351 list.Clear(); 406 list.Clear();
352 ++empty_lists_count; 407 ++empty_lists_count;
353 continue; 408 continue;
354 } 409 }
355 if (!marker_types.Contains((*list->begin())->GetType())) 410 if (!marker_types.Contains((*list->begin())->GetType()))
356 continue; 411 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 412
363 // markers are returned in order, so stop if we are now past the specified 413 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; 414 doc_dirty = true;
370 }
371 415
372 if (list->IsEmpty()) { 416 if (list->IsEmpty()) {
373 list.Clear(); 417 list.Clear();
374 ++empty_lists_count; 418 ++empty_lists_count;
375 } 419 }
376 } 420 }
377 421
378 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) { 422 if (empty_lists_count == DocumentMarker::kMarkerTypeIndexesCount) {
379 markers_.erase(node); 423 markers_.erase(node);
380 if (markers_.IsEmpty()) 424 if (markers_.IsEmpty())
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 748
705 const unsigned container_start_offset = 749 const unsigned container_start_offset =
706 range.StartPosition().ComputeOffsetInContainerNode(); 750 range.StartPosition().ComputeOffsetInContainerNode();
707 const unsigned container_end_offset = 751 const unsigned container_end_offset =
708 range.EndPosition().ComputeOffsetInContainerNode(); 752 range.EndPosition().ComputeOffsetInContainerNode();
709 753
710 bool marker_found = false; 754 bool marker_found = false;
711 for (Node& node : range.Nodes()) { 755 for (Node& node : range.Nodes()) {
712 int start_offset = node == start_container ? container_start_offset : 0; 756 int start_offset = node == start_container ? container_start_offset : 0;
713 int end_offset = node == end_container ? container_end_offset : INT_MAX; 757 int end_offset = node == end_container ? container_end_offset : INT_MAX;
714 marker_found |= SetMarkersActive(&node, start_offset, end_offset, active); 758
759 if (SetMarkersActive(&node, start_offset, end_offset, active))
760 marker_found = true;
715 } 761 }
716 return marker_found; 762 return marker_found;
717 } 763 }
718 764
719 bool DocumentMarkerController::SetMarkersActive(Node* node, 765 bool DocumentMarkerController::SetMarkersActive(Node* node,
720 unsigned start_offset, 766 unsigned start_offset,
721 unsigned end_offset, 767 unsigned end_offset,
722 bool active) { 768 bool active) {
723 MarkerLists* markers = markers_.at(node); 769 MarkerLists* markers = markers_.at(node);
724 if (!markers) 770 if (!markers)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 builder.Append(")"); 823 builder.Append(")");
778 } 824 }
779 } 825 }
780 builder.Append("\n"); 826 builder.Append("\n");
781 } 827 }
782 LOG(INFO) << markers_.size() << " nodes have markers:\n" 828 LOG(INFO) << markers_.size() << " nodes have markers:\n"
783 << builder.ToString().Utf8().Data(); 829 << builder.ToString().Utf8().Data();
784 } 830 }
785 #endif 831 #endif
786 832
833 // TODO(rlanday): move DocumentMarkerListEditor into its own .h/.cpp files
834 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list,
835 unsigned offset,
836 unsigned old_length,
837 unsigned new_length) {
838 bool did_shift_marker = false;
839 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) {
840 RenderedDocumentMarker& marker = **it;
841 Optional<DocumentMarker::MarkerOffsets> result =
842 marker.ComputeOffsetsAfterShift(offset, old_length, new_length);
843 if (result == WTF::kNullopt) {
844 list->erase(it - list->begin());
845 --it;
846 did_shift_marker = true;
847 continue;
848 }
849
850 if (marker.StartOffset() != result.value().start_offset ||
851 marker.EndOffset() != result.value().end_offset) {
852 did_shift_marker = true;
853 marker.SetStartOffset(result.value().start_offset);
854 marker.SetEndOffset(result.value().end_offset);
855 }
856 }
857
858 return did_shift_marker;
859 }
860
787 // SynchronousMutationObserver 861 // SynchronousMutationObserver
788 void DocumentMarkerController::DidUpdateCharacterData(CharacterData* node, 862 void DocumentMarkerController::DidUpdateCharacterData(CharacterData* node,
789 unsigned offset, 863 unsigned offset,
790 unsigned old_length, 864 unsigned old_length,
791 unsigned new_length) { 865 unsigned new_length) {
792 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers())) 866 if (!PossiblyHasMarkers(DocumentMarker::AllMarkers()))
793 return; 867 return;
794 DCHECK(!markers_.IsEmpty()); 868 DCHECK(!markers_.IsEmpty());
795 869
796 MarkerLists* markers = markers_.at(node); 870 MarkerLists* markers = markers_.at(node);
797 if (!markers) 871 if (!markers)
798 return; 872 return;
799 873
800 bool did_shift_marker = false; 874 bool did_shift_marker = false;
801 for (MarkerList* list : *markers) { 875 for (MarkerList* list : *markers) {
802 if (!list) 876 if (!list)
803 continue; 877 continue;
804 878
805 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { 879 did_shift_marker |= DocumentMarkerListEditor::ShiftMarkers(
Xiaocheng 2017/04/14 22:32:21 nit: Please change it to if (DMLE::ShitMarkers())
806 RenderedDocumentMarker& marker = **it; 880 list, offset, old_length, new_length);
807 Optional<DocumentMarker::MarkerOffsets> result =
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 } 881 }
824 882
825 if (!did_shift_marker) 883 if (!did_shift_marker)
826 return; 884 return;
827 if (!node->GetLayoutObject()) 885 if (!node->GetLayoutObject())
828 return; 886 return;
829 InvalidateRectsForMarkersInNode(*node); 887 InvalidateRectsForMarkersInNode(*node);
830 // repaint the affected node 888 // repaint the affected node
831 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation(); 889 node->GetLayoutObject()->SetShouldDoFullPaintInvalidation();
832 } 890 }
833 891
834 } // namespace blink 892 } // namespace blink
835 893
836 #ifndef NDEBUG 894 #ifndef NDEBUG
837 void showDocumentMarkers(const blink::DocumentMarkerController* controller) { 895 void showDocumentMarkers(const blink::DocumentMarkerController* controller) {
838 if (controller) 896 if (controller)
839 controller->ShowMarkers(); 897 controller->ShowMarkers();
840 } 898 }
841 #endif 899 #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