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 #include "core/editing/markers/DocumentMarkerController.h" | |
| 8 | |
| 9 namespace blink { | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 bool endsBefore(size_t startOffset, const Member<DocumentMarker>& rhv) { | |
| 14 return startOffset < rhv->endOffset(); | |
| 15 } | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 DocumentMarkerList::DocumentMarkerList( | |
| 20 DocumentMarkerController* documentMarkerController) | |
| 21 : m_documentMarkerController(documentMarkerController) {} | |
| 22 | |
| 23 bool DocumentMarkerList::isEditingMarkerList() const { | |
| 24 return false; | |
| 25 } | |
| 26 | |
| 27 bool DocumentMarkerList::isSpellCheckMarkerList() const { | |
| 28 return false; | |
| 29 } | |
| 30 | |
| 31 DocumentMarkerList::iterator DocumentMarkerList::begin() { | |
| 32 return m_markers.begin(); | |
| 33 } | |
| 34 | |
| 35 DocumentMarkerList::iterator DocumentMarkerList::end() { | |
| 36 return m_markers.end(); | |
| 37 } | |
| 38 | |
| 39 DocumentMarkerList::const_iterator DocumentMarkerList::begin() const { | |
| 40 return m_markers.begin(); | |
| 41 } | |
| 42 | |
| 43 DocumentMarkerList::const_iterator DocumentMarkerList::end() const { | |
| 44 return m_markers.end(); | |
| 45 } | |
| 46 | |
| 47 void DocumentMarkerList::appendMarkersToInputList( | |
| 48 DocumentMarkerVector* list) const { | |
| 49 for (Member<DocumentMarker> marker : m_markers) { | |
| 50 list->push_back(marker); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 bool DocumentMarkerList::copyMarkers(unsigned startOffset, | |
|
Xiaocheng
2017/03/24 19:43:49
With a second thought, I think DML should not depe
| |
| 55 int length, | |
| 56 Node* dstNode, | |
| 57 int delta) const { | |
| 58 bool docDirty = false; | |
| 59 unsigned endOffset = startOffset + length - 1; | |
| 60 | |
| 61 for (Member<DocumentMarker> marker : m_markers) { | |
| 62 // pin the marker to the specified range and apply the shift delta | |
| 63 if (marker->endOffset() >= startOffset && | |
| 64 marker->startOffset() <= endOffset) { | |
| 65 docDirty = true; | |
| 66 if (marker->startOffset() < startOffset) | |
| 67 marker->setStartOffset(startOffset); | |
| 68 if (marker->endOffset() > endOffset) | |
| 69 marker->setEndOffset(endOffset); | |
| 70 marker->shiftOffsets(delta); | |
| 71 | |
| 72 m_documentMarkerController->addMarker(dstNode, *marker); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 return docDirty; | |
| 77 } | |
| 78 | |
| 79 void DocumentMarkerList::removeMarkers( | |
| 80 unsigned startOffset, | |
| 81 int length, | |
| 82 bool shouldRemovePartiallyOverlappingMarkers, | |
| 83 bool* didRemoveMarker) { | |
| 84 unsigned endOffset = startOffset + length; | |
| 85 size_t markerIndex = 0; | |
| 86 if (markerListIsSorted()) { | |
| 87 markerIndex = | |
| 88 getPosOfFirstMarkerNotEndingBefore(startOffset) - m_markers.begin(); | |
| 89 } | |
| 90 | |
| 91 for (; markerIndex < m_markers.size();) { | |
|
Xiaocheng
2017/03/24 19:43:49
nit: |while| seems better here.
| |
| 92 DocumentMarker& marker = *m_markers.at(markerIndex); | |
| 93 if (!markerListIsSorted()) { | |
| 94 if (marker.endOffset() <= startOffset) { | |
| 95 ++markerIndex; | |
| 96 continue; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 if (marker.startOffset() >= endOffset) { | |
| 101 if (markerListIsSorted()) | |
| 102 break; | |
| 103 ++markerIndex; | |
| 104 continue; | |
| 105 } | |
| 106 | |
| 107 // pitch the old marker | |
| 108 m_markers.remove(markerIndex); | |
| 109 *didRemoveMarker = true; | |
| 110 | |
| 111 if (shouldRemovePartiallyOverlappingMarkers) { | |
| 112 // Stop here. Don't add resulting slices back. | |
| 113 continue; | |
| 114 } | |
| 115 | |
| 116 // add either of the resulting slices that are left after removing target | |
| 117 if (startOffset > marker.startOffset()) { | |
| 118 DocumentMarker* newLeft = new DocumentMarker(marker); | |
| 119 newLeft->setEndOffset(startOffset); | |
| 120 if (markerListIsSorted()) { | |
| 121 m_markers.insert(markerIndex, *newLeft); | |
| 122 // Move to the marker after the inserted one. | |
| 123 ++markerIndex; | |
| 124 } else { | |
| 125 // For the unsorted case, we just stick the new marker at the end of the | |
| 126 // list. The loop will eventually run on it but that's not a problem | |
| 127 // since it's known to be outside the range being removed. | |
| 128 m_markers.push_back(*newLeft); | |
| 129 } | |
| 130 } | |
| 131 if (marker.endOffset() > endOffset) { | |
| 132 DocumentMarker* newRight = new DocumentMarker(marker); | |
| 133 newRight->setStartOffset(endOffset); | |
| 134 if (markerListIsSorted()) { | |
| 135 m_markers.insert(markerIndex, *newRight); | |
| 136 // Move to the marker after the inserted one. | |
| 137 ++markerIndex; | |
| 138 } else { | |
| 139 m_markers.push_back(*newRight); | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 bool DocumentMarkerList::shiftMarkers(unsigned offset, | |
| 146 unsigned oldLength, | |
| 147 unsigned newLength) { | |
| 148 bool didShift = false; | |
| 149 for (auto it = m_markers.begin(); it != m_markers.end(); ++it) { | |
| 150 DocumentMarker& marker = **it; | |
| 151 | |
| 152 DocumentMarker::ShiftMarkerResult result = | |
| 153 marker.getShiftedMarkerPosition(offset, oldLength, newLength); | |
| 154 if (result.shouldRemoveMarker) { | |
| 155 m_markers.remove(it - m_markers.begin()); | |
| 156 --it; | |
| 157 | |
| 158 didShift = true; | |
| 159 } else if (result.newStartOffset != marker.startOffset() || | |
| 160 result.newEndOffset != marker.endOffset()) { | |
| 161 marker.setStartOffset(result.newStartOffset); | |
| 162 marker.setEndOffset(result.newEndOffset); | |
| 163 | |
| 164 didShift = true; | |
| 165 } | |
| 166 } | |
| 167 return didShift; | |
| 168 } | |
| 169 | |
| 170 HeapVector<Member<DocumentMarker>>::iterator | |
| 171 DocumentMarkerList::getPosOfFirstMarkerNotEndingBefore(size_t startOffset) { | |
| 172 return std::upper_bound(m_markers.begin(), m_markers.end(), startOffset, | |
|
Xiaocheng
2017/03/24 19:43:49
Should DCHECK(markerListIsSorted());
| |
| 173 endsBefore); | |
| 174 } | |
| 175 | |
| 176 DEFINE_TRACE(DocumentMarkerList) { | |
| 177 visitor->trace(m_documentMarkerController); | |
| 178 visitor->trace(m_markers); | |
| 179 } | |
| 180 | |
| 181 } // namespace blink | |
| OLD | NEW |