OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/editing/markers/DocumentMarkerListEditor.h" |
| 6 |
| 7 #include "core/editing/markers/RenderedDocumentMarker.h" |
| 8 |
| 9 namespace blink { |
| 10 |
| 11 // TODO(rlanday): this method was created by cutting and pasting code from |
| 12 // DocumentMarkerController::AddMarker(), it should be refactored in a future CL |
| 13 void DocumentMarkerListEditor::AddMarker(MarkerList* list, |
| 14 const DocumentMarker* marker) { |
| 15 RenderedDocumentMarker* rendered_marker = |
| 16 RenderedDocumentMarker::Create(*marker); |
| 17 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) { |
| 18 list->push_back(rendered_marker); |
| 19 } else { |
| 20 if (marker->GetType() != DocumentMarker::kTextMatch && |
| 21 marker->GetType() != DocumentMarker::kComposition) { |
| 22 MergeOverlapping(list, rendered_marker); |
| 23 } else { |
| 24 MarkerList::iterator pos = std::lower_bound( |
| 25 list->begin(), list->end(), marker, |
| 26 [](const Member<RenderedDocumentMarker>& marker_in_list, |
| 27 const DocumentMarker* marker_to_insert) { |
| 28 return marker_in_list->StartOffset() < |
| 29 marker_to_insert->StartOffset(); |
| 30 }); |
| 31 list->insert(pos - list->begin(), rendered_marker); |
| 32 } |
| 33 } |
| 34 } |
| 35 |
| 36 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list, |
| 37 int length, |
| 38 MarkerList* dst_list) { |
| 39 DCHECK_GT(length, 0); |
| 40 bool didMoveMarker = false; |
| 41 unsigned end_offset = length - 1; |
| 42 |
| 43 MarkerList::iterator it; |
| 44 for (it = src_list->begin(); it != src_list->end(); ++it) { |
| 45 DocumentMarker& marker = **it; |
| 46 if (marker.StartOffset() > end_offset) |
| 47 break; |
| 48 |
| 49 // pin the marker to the specified range and apply the shift delta |
| 50 if (marker.EndOffset() > end_offset) |
| 51 marker.SetEndOffset(end_offset); |
| 52 |
| 53 DocumentMarkerListEditor::AddMarker(dst_list, &marker); |
| 54 didMoveMarker = true; |
| 55 } |
| 56 |
| 57 // Remove the range of markers that were moved to dstNode |
| 58 src_list->erase(0, it - src_list->begin()); |
| 59 |
| 60 return didMoveMarker; |
| 61 } |
| 62 |
| 63 // TODO(rlanday): this method was created by cutting and pasting code from |
| 64 // DocumentMarkerController::RemoveMarkers(), it should be refactored in a |
| 65 // future CL |
| 66 bool DocumentMarkerListEditor::RemoveMarkers(MarkerList* list, |
| 67 unsigned start_offset, |
| 68 int length) { |
| 69 bool doc_dirty = false; |
| 70 const unsigned end_offset = start_offset + length; |
| 71 MarkerList::iterator start_pos = std::upper_bound( |
| 72 list->begin(), list->end(), start_offset, |
| 73 [](size_t start_offset, const Member<RenderedDocumentMarker>& marker) { |
| 74 return start_offset < marker->EndOffset(); |
| 75 }); |
| 76 for (MarkerList::iterator i = start_pos; i != list->end();) { |
| 77 DocumentMarker marker(*i->Get()); |
| 78 |
| 79 // markers are returned in order, so stop if we are now past the specified |
| 80 // range |
| 81 if (marker.StartOffset() >= end_offset) |
| 82 break; |
| 83 |
| 84 list->erase(i - list->begin()); |
| 85 doc_dirty = true; |
| 86 } |
| 87 |
| 88 return doc_dirty; |
| 89 } |
| 90 |
| 91 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list, |
| 92 unsigned offset, |
| 93 unsigned old_length, |
| 94 unsigned new_length) { |
| 95 bool did_shift_marker = false; |
| 96 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { |
| 97 RenderedDocumentMarker& marker = **it; |
| 98 Optional<DocumentMarker::MarkerOffsets> result = |
| 99 marker.ComputeOffsetsAfterShift(offset, old_length, new_length); |
| 100 if (result == WTF::kNullopt) { |
| 101 list->erase(it - list->begin()); |
| 102 --it; |
| 103 did_shift_marker = true; |
| 104 continue; |
| 105 } |
| 106 |
| 107 if (marker.StartOffset() != result.value().start_offset || |
| 108 marker.EndOffset() != result.value().end_offset) { |
| 109 did_shift_marker = true; |
| 110 marker.SetStartOffset(result.value().start_offset); |
| 111 marker.SetEndOffset(result.value().end_offset); |
| 112 } |
| 113 } |
| 114 |
| 115 return did_shift_marker; |
| 116 } |
| 117 |
| 118 void DocumentMarkerListEditor::MergeOverlapping( |
| 119 DocumentMarkerListEditor::MarkerList* list, |
| 120 RenderedDocumentMarker* to_insert) { |
| 121 auto first_overlapping = std::lower_bound( |
| 122 list->begin(), list->end(), to_insert, |
| 123 [](const Member<RenderedDocumentMarker>& marker_in_list, |
| 124 const DocumentMarker* marker_to_insert) { |
| 125 return marker_in_list->EndOffset() < marker_to_insert->StartOffset(); |
| 126 }); |
| 127 size_t index = first_overlapping - list->begin(); |
| 128 list->insert(index, to_insert); |
| 129 const auto inserted = list->begin() + index; |
| 130 first_overlapping = inserted + 1; |
| 131 for (const auto i = first_overlapping; |
| 132 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { |
| 133 (*inserted)->SetStartOffset( |
| 134 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); |
| 135 (*inserted)->SetEndOffset( |
| 136 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); |
| 137 list->erase(i - list->begin()); |
| 138 } |
| 139 } |
| 140 |
| 141 } // namespace blink |
OLD | NEW |