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 = 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 = 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/28 00:02:06
This might have some side-effect...
Now the same
| |
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 size_t markerIndex = 0; | |
66 DidRemoveMarkerOrNot didRemoveMarker = DidNotRemoveMarker; | |
67 | |
68 if (markerListIsSorted()) { | |
69 markerIndex = | |
70 getPosOfFirstMarkerNotEndingBefore(startOffset) - m_markers.begin(); | |
71 } | |
72 | |
73 while (markerIndex < m_markers.size()) { | |
74 DocumentMarker& marker = *m_markers.at(markerIndex); | |
75 if (!markerListIsSorted()) { | |
76 if (marker.endOffset() <= startOffset) { | |
77 ++markerIndex; | |
78 continue; | |
79 } | |
80 } | |
81 | |
82 if (marker.startOffset() >= endOffset) { | |
83 if (markerListIsSorted()) | |
84 break; | |
85 ++markerIndex; | |
86 continue; | |
87 } | |
88 | |
89 // pitch the old marker | |
90 m_markers.remove(markerIndex); | |
91 didRemoveMarker = DidRemoveMarker; | |
92 | |
93 if (shouldRemovePartiallyOverlappingMarkers) { | |
94 // Stop here. Don't add resulting slices back. | |
95 continue; | |
96 } | |
97 | |
98 // add either of the resulting slices that are left after removing target | |
99 if (startOffset > marker.startOffset()) { | |
100 DocumentMarker* newLeft = new DocumentMarker(marker); | |
101 newLeft->setEndOffset(startOffset); | |
102 if (markerListIsSorted()) { | |
103 m_markers.insert(markerIndex, *newLeft); | |
104 // Move to the marker after the inserted one. | |
105 ++markerIndex; | |
106 } else { | |
107 // For the unsorted case, we just stick the new marker at the end of the | |
108 // list. The loop will eventually run on it but that's not a problem | |
109 // since it's known to be outside the range being removed. | |
110 m_markers.push_back(*newLeft); | |
111 } | |
112 } | |
113 if (marker.endOffset() > endOffset) { | |
114 DocumentMarker* newRight = new DocumentMarker(marker); | |
115 newRight->setStartOffset(endOffset); | |
116 if (markerListIsSorted()) { | |
117 m_markers.insert(markerIndex, *newRight); | |
118 // Move to the marker after the inserted one. | |
119 ++markerIndex; | |
120 } else { | |
121 m_markers.push_back(*newRight); | |
122 } | |
123 } | |
124 } | |
125 | |
126 return didRemoveMarker; | |
127 } | |
128 | |
129 DocumentMarkerList::DidShiftMarkerOrNot DocumentMarkerList::shiftMarkers( | |
130 unsigned offset, | |
131 unsigned oldLength, | |
132 unsigned newLength) { | |
133 DocumentMarkerList::DidShiftMarkerOrNot didShift = DidNotShiftMarker; | |
134 for (auto it = m_markers.begin(); it != m_markers.end(); ++it) { | |
135 DocumentMarker& marker = **it; | |
136 | |
137 DocumentMarker::ShiftMarkerResult result = | |
138 marker.getShiftedMarkerPosition(offset, oldLength, newLength); | |
139 if (result.shouldRemoveMarker) { | |
140 m_markers.remove(it - m_markers.begin()); | |
141 --it; | |
142 | |
143 didShift = DidShiftMarker; | |
144 } else if (result.newStartOffset != marker.startOffset() || | |
145 result.newEndOffset != marker.endOffset()) { | |
146 marker.setStartOffset(result.newStartOffset); | |
147 marker.setEndOffset(result.newEndOffset); | |
148 | |
149 didShift = DidShiftMarker; | |
150 } | |
151 } | |
152 return didShift; | |
153 } | |
154 | |
155 HeapVector<Member<DocumentMarker>>::iterator | |
156 DocumentMarkerList::getPosOfFirstMarkerNotEndingBefore(size_t startOffset) { | |
157 DCHECK(markerListIsSorted()); | |
158 return std::upper_bound( | |
159 m_markers.begin(), m_markers.end(), startOffset, | |
160 [](size_t startOffset, const Member<DocumentMarker>& rhv) -> bool { | |
161 return startOffset < rhv->endOffset(); | |
162 }); | |
163 } | |
164 | |
165 DEFINE_TRACE(DocumentMarkerList) { | |
166 visitor->trace(m_markers); | |
167 } | |
168 | |
169 } // namespace blink | |
OLD | NEW |