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/TextMatchMarkerList.h" |
| 6 |
| 7 #include "core/editing/markers/DocumentMarkerController.h" |
| 8 #include "core/editing/markers/RenderedTextMatchMarker.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 TextMatchMarkerList::TextMatchMarkerList( |
| 13 DocumentMarkerController* documentMarkerController) |
| 14 : DocumentMarkerList(documentMarkerController) {} |
| 15 |
| 16 DocumentMarker::MarkerType TextMatchMarkerList::allowedMarkerType() const { |
| 17 return DocumentMarker::TextMatch; |
| 18 } |
| 19 |
| 20 static bool doesNotInclude(const Member<DocumentMarker>& marker, |
| 21 size_t startOffset) { |
| 22 return marker->endOffset() < startOffset; |
| 23 } |
| 24 |
| 25 RenderedTextMatchMarker* TextMatchMarkerList::at(size_t index) { |
| 26 return toRenderedTextMatchMarker(m_markers[index].get()); |
| 27 } |
| 28 |
| 29 void TextMatchMarkerList::clear() { |
| 30 m_markers.clear(); |
| 31 } |
| 32 |
| 33 bool TextMatchMarkerList::copyMarkers(unsigned startOffset, |
| 34 int length, |
| 35 Node* dstNode, |
| 36 int delta) const { |
| 37 bool docDirty = false; |
| 38 unsigned endOffset = startOffset + length - 1; |
| 39 auto startPos = std::lower_bound(m_markers.begin(), m_markers.end(), |
| 40 startOffset, doesNotInclude); |
| 41 for (auto i = startPos; i != m_markers.end(); ++i) { |
| 42 DocumentMarker* marker = i->get(); |
| 43 |
| 44 // stop if we are now past the specified range |
| 45 if (marker->startOffset() > endOffset) |
| 46 break; |
| 47 |
| 48 // pin the marker to the specified range and apply the shift delta |
| 49 docDirty = true; |
| 50 if (marker->startOffset() < startOffset) |
| 51 marker->setStartOffset(startOffset); |
| 52 if (marker->endOffset() > endOffset) |
| 53 marker->setEndOffset(endOffset); |
| 54 marker->shiftOffsets(delta); |
| 55 |
| 56 m_documentMarkerController->addMarker(dstNode, marker); |
| 57 } |
| 58 |
| 59 return docDirty; |
| 60 } |
| 61 |
| 62 void TextMatchMarkerList::removeMarkers( |
| 63 unsigned startOffset, |
| 64 int length, |
| 65 bool shouldRemovePartiallyOverlappingMarkers, |
| 66 bool* didRemoveMarker) { |
| 67 unsigned endOffset = startOffset + length; |
| 68 for (auto it = getPosOfFirstMarkerNotEndingBefore(startOffset); |
| 69 it < m_markers.end(); ++it) { |
| 70 RenderedTextMatchMarker& marker = toRenderedTextMatchMarker(**it); |
| 71 |
| 72 if (marker.startOffset() >= endOffset) |
| 73 break; |
| 74 |
| 75 // pitch the old marker |
| 76 m_markers.remove(it - m_markers.begin()); |
| 77 *didRemoveMarker = true; |
| 78 |
| 79 if (shouldRemovePartiallyOverlappingMarkers) { |
| 80 // Stop here. Don't add resulting slices back. |
| 81 continue; |
| 82 } |
| 83 |
| 84 // add either of the resulting slices that are left after removing target |
| 85 if (startOffset > marker.startOffset()) { |
| 86 RenderedTextMatchMarker newLeft = marker; |
| 87 newLeft.setEndOffset(startOffset); |
| 88 size_t insertIndex = it - m_markers.begin(); |
| 89 m_markers.insert(insertIndex, RenderedTextMatchMarker::create(newLeft)); |
| 90 // Move to the marker after the inserted one. |
| 91 it = m_markers.begin() + insertIndex + 1; |
| 92 } |
| 93 if (marker.endOffset() > endOffset) { |
| 94 RenderedTextMatchMarker newRight = marker; |
| 95 newRight.setStartOffset(endOffset); |
| 96 size_t insertIndex = it - m_markers.begin(); |
| 97 m_markers.insert(insertIndex, RenderedTextMatchMarker::create(newRight)); |
| 98 // Move to the marker after the inserted one. |
| 99 it = m_markers.begin() + insertIndex + 1; |
| 100 } |
| 101 } |
| 102 } |
| 103 |
| 104 bool TextMatchMarkerList::shiftMarkers(unsigned offset, |
| 105 unsigned oldLength, |
| 106 unsigned newLength) { |
| 107 bool didShift = false; |
| 108 for (auto it = m_markers.begin(); it < m_markers.end(); ++it) { |
| 109 DocumentMarker& marker = **it; |
| 110 |
| 111 DocumentMarker::ShiftMarkerResult result = |
| 112 marker.getShiftedMarkerPosition(offset, oldLength, newLength); |
| 113 if (result.shouldRemoveMarker) { |
| 114 m_markers.remove(it - m_markers.begin()); |
| 115 --it; |
| 116 |
| 117 didShift = true; |
| 118 } else if (result.newStartOffset != marker.startOffset() || |
| 119 result.newEndOffset != marker.endOffset()) { |
| 120 marker.setStartOffset(result.newStartOffset); |
| 121 marker.setEndOffset(result.newEndOffset); |
| 122 |
| 123 didShift = true; |
| 124 } |
| 125 } |
| 126 |
| 127 return didShift; |
| 128 } |
| 129 |
| 130 void TextMatchMarkerList::push_back(DocumentMarker* marker) { |
| 131 // TextMatch markers must be added in order |
| 132 DCHECK(marker->type() == DocumentMarker::TextMatch); |
| 133 DCHECK(m_markers.isEmpty() || |
| 134 marker->startOffset() >= m_markers.back()->endOffset()); |
| 135 m_markers.push_back(RenderedTextMatchMarker::create(*marker)); |
| 136 } |
| 137 |
| 138 bool TextMatchMarkerList::setTextMatchMarkersActive(unsigned startOffset, |
| 139 unsigned endOffset, |
| 140 bool active) { |
| 141 bool docDirty = false; |
| 142 for (auto it = getPosOfFirstMarkerNotEndingBefore(startOffset); |
| 143 it != m_markers.end(); ++it) { |
| 144 DocumentMarker& marker = **it; |
| 145 // Markers are stored in order, so stop if we are now past the specified |
| 146 // range. |
| 147 if (marker.startOffset() >= endOffset) |
| 148 break; |
| 149 |
| 150 marker.setActiveMatch(active); |
| 151 docDirty = true; |
| 152 } |
| 153 |
| 154 return docDirty; |
| 155 } |
| 156 |
| 157 DEFINE_TRACE(TextMatchMarkerList) { |
| 158 visitor->trace(m_markers); |
| 159 DocumentMarkerList::trace(visitor); |
| 160 } |
| 161 |
| 162 } // namespace blink |
OLD | NEW |