Chromium Code Reviews| 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/DocumentMarkerList.h" | |
| 6 | |
| 7 namespace blink { | |
| 8 | |
| 9 DocumentMarkerList::DocumentMarkerList() {} | |
| 10 | |
| 11 bool DocumentMarkerList::isEditingMarkerList() const { | |
| 12 return false; | |
| 13 } | |
| 14 | |
| 15 bool DocumentMarkerList::isSpellCheckMarkerList() const { | |
| 16 return false; | |
| 17 } | |
| 18 | |
| 19 void DocumentMarkerList::add(DocumentMarker* marker) { | |
| 20 m_markers.push_back(marker); | |
| 21 } | |
| 22 | |
| 23 void DocumentMarkerList::clear() { | |
| 24 m_markers.clear(); | |
| 25 } | |
| 26 | |
| 27 void DocumentMarkerList::appendMarkersToInputList( | |
| 28 DocumentMarkerVector* list) const { | |
| 29 for (Member<DocumentMarker> marker : m_markers) { | |
| 30 list->push_back(marker); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 DocumentMarkerList::DidCopyMarkerOrNot DocumentMarkerList::copyMarkers( | |
| 35 unsigned startOffset, | |
| 36 int length, | |
| 37 DocumentMarkerList* dstList, | |
| 38 int delta) const { | |
| 39 DidCopyMarkerOrNot didCopyMarker = DidCopyMarkerOrNot::DidNotCopyMarker; | |
| 40 unsigned endOffset = startOffset + length - 1; | |
| 41 | |
| 42 for (Member<DocumentMarker> marker : m_markers) { | |
| 43 // pin the marker to the specified range and apply the shift delta | |
| 44 if (marker->endOffset() >= startOffset && | |
| 45 marker->startOffset() <= endOffset) { | |
| 46 didCopyMarker = DidCopyMarkerOrNot::DidCopyMarker; | |
| 47 if (marker->startOffset() < startOffset) | |
| 48 marker->setStartOffset(startOffset); | |
| 49 if (marker->endOffset() > endOffset) | |
| 50 marker->setEndOffset(endOffset); | |
| 51 marker->shiftOffsets(delta); | |
| 52 | |
| 53 dstList->add(marker); | |
|
Xiaocheng
2017/03/30 02:13:04
Just a note that we should better change this to |
| |
| 54 } | |
| 55 } | |
| 56 | |
| 57 return didCopyMarker; | |
| 58 } | |
| 59 | |
| 60 DocumentMarkerList::DidRemoveMarkerOrNot DocumentMarkerList::removeMarkers( | |
| 61 unsigned startOffset, | |
| 62 int length, | |
| 63 bool shouldRemovePartiallyOverlappingMarkers) { | |
| 64 unsigned endOffset = startOffset + length; | |
| 65 HeapVector<Member<DocumentMarker>> newMarkerList; | |
| 66 DidRemoveMarkerOrNot didRemoveMarker = | |
| 67 DidRemoveMarkerOrNot::DidNotRemoveMarker; | |
| 68 | |
| 69 for (Member<DocumentMarker> marker : m_markers) { | |
| 70 if (marker->endOffset() <= startOffset) { | |
| 71 newMarkerList.push_back(marker); | |
| 72 continue; | |
| 73 } | |
| 74 | |
| 75 if (marker->startOffset() >= endOffset) { | |
| 76 newMarkerList.push_back(marker); | |
| 77 continue; | |
| 78 } | |
| 79 | |
| 80 didRemoveMarker = DidRemoveMarkerOrNot::DidRemoveMarker; | |
| 81 | |
| 82 if (shouldRemovePartiallyOverlappingMarkers) { | |
| 83 // Stop here. Don't add resulting slices back. | |
| 84 continue; | |
| 85 } | |
| 86 | |
| 87 // add either of the resulting slices that are left after removing target | |
| 88 if (startOffset > marker->startOffset()) { | |
| 89 DocumentMarker* newLeft = new DocumentMarker(*marker); | |
| 90 newLeft->setEndOffset(startOffset); | |
| 91 newMarkerList.push_back(newLeft); | |
| 92 } | |
| 93 | |
| 94 if (marker->endOffset() > endOffset) { | |
| 95 DocumentMarker* newRight = new DocumentMarker(*marker); | |
| 96 newRight->setStartOffset(endOffset); | |
| 97 newMarkerList.push_back(newRight); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 swap(m_markers, newMarkerList); | |
| 102 return didRemoveMarker; | |
| 103 } | |
| 104 | |
| 105 DocumentMarkerList::DidShiftMarkerOrNot DocumentMarkerList::shiftMarkers( | |
| 106 unsigned offset, | |
| 107 unsigned oldLength, | |
| 108 unsigned newLength) { | |
| 109 HeapVector<Member<DocumentMarker>> newMarkerList; | |
| 110 DocumentMarkerList::DidShiftMarkerOrNot didShift = | |
| 111 DidShiftMarkerOrNot::DidNotShiftMarker; | |
| 112 for (Member<DocumentMarker> marker : m_markers) { | |
| 113 DocumentMarker::ShiftMarkerResult result = | |
| 114 marker->getShiftedMarkerPosition(offset, oldLength, newLength); | |
| 115 | |
| 116 if (result.shouldRemoveMarker) { | |
| 117 didShift = DidShiftMarkerOrNot::DidShiftMarker; | |
| 118 continue; | |
| 119 } | |
| 120 | |
| 121 if (result.newStartOffset != marker->startOffset() || | |
| 122 result.newEndOffset != marker->endOffset()) { | |
| 123 marker->setStartOffset(result.newStartOffset); | |
| 124 marker->setEndOffset(result.newEndOffset); | |
| 125 | |
| 126 didShift = DidShiftMarkerOrNot::DidShiftMarker; | |
| 127 } | |
| 128 newMarkerList.push_back(marker); | |
| 129 } | |
| 130 | |
| 131 swap(m_markers, newMarkerList); | |
| 132 return didShift; | |
| 133 } | |
| 134 | |
| 135 HeapVector<Member<DocumentMarker>>::iterator | |
| 136 DocumentMarkerList::getPosOfFirstMarkerNotEndingBefore(size_t startOffset) { | |
| 137 DCHECK(markerListIsSorted()); | |
| 138 return std::upper_bound( | |
| 139 m_markers.begin(), m_markers.end(), startOffset, | |
| 140 [](size_t startOffset, const Member<DocumentMarker>& rhv) -> bool { | |
| 141 return startOffset < rhv->endOffset(); | |
| 142 }); | |
| 143 } | |
| 144 | |
| 145 DEFINE_TRACE(DocumentMarkerList) { | |
| 146 visitor->trace(m_markers); | |
| 147 } | |
| 148 | |
| 149 } // namespace blink | |
| OLD | NEW |