Chromium Code Reviews| Index: third_party/WebKit/Source/core/editing/markers/DocumentMarkerList.cpp |
| diff --git a/third_party/WebKit/Source/core/editing/markers/DocumentMarkerList.cpp b/third_party/WebKit/Source/core/editing/markers/DocumentMarkerList.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a91d4d3d2d99b295a5905689c1c9081d62c610cd |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/editing/markers/DocumentMarkerList.cpp |
| @@ -0,0 +1,155 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/editing/markers/DocumentMarkerList.h" |
| + |
| +namespace blink { |
| + |
| +DocumentMarkerList::DocumentMarkerList() {} |
| + |
| +bool DocumentMarkerList::isEditingMarkerList() const { |
| + return false; |
| +} |
| + |
| +bool DocumentMarkerList::isSpellCheckMarkerList() const { |
| + return false; |
| +} |
| + |
| +DocumentMarker* DocumentMarkerList::at(size_t index) { |
| + return m_markers[index].get(); |
| +} |
| + |
| +void DocumentMarkerList::add(DocumentMarker* marker) { |
| + m_markers.push_back(marker); |
| +} |
| + |
| +void DocumentMarkerList::clear() { |
| + m_markers.clear(); |
| +} |
| + |
| +void DocumentMarkerList::appendMarkersToInputList( |
| + DocumentMarkerVector* list) const { |
| + for (Member<DocumentMarker> marker : m_markers) { |
| + list->push_back(marker); |
| + } |
| +} |
| + |
| +DocumentMarkerList::DidCopyMarkerOrNot DocumentMarkerList::copyMarkers( |
|
Xiaocheng
2017/03/31 18:59:18
Please add |const| to this function to indicate th
rlanday
2017/04/03 20:34:43
Let's fix it here. I will add const and remove the
rlanday
2017/04/03 21:30:14
We actually already have const here so I'll just r
|
| + unsigned startOffset, |
| + int length, |
| + DocumentMarkerList* dstList, |
| + int delta) const { |
| + DidCopyMarkerOrNot didCopyMarker = DidCopyMarkerOrNot::DidNotCopyMarker; |
| + unsigned endOffset = startOffset + length - 1; |
| + |
| + for (Member<DocumentMarker> marker : m_markers) { |
| + // pin the marker to the specified range and apply the shift delta |
| + if (marker->endOffset() >= startOffset && |
| + marker->startOffset() <= endOffset) { |
| + didCopyMarker = DidCopyMarkerOrNot::DidCopyMarker; |
| + |
| + DocumentMarker* copiedMarker = marker->clone(); |
| + if (copiedMarker->startOffset() < startOffset) |
| + copiedMarker->setStartOffset(startOffset); |
| + if (copiedMarker->endOffset() > endOffset) |
| + copiedMarker->setEndOffset(endOffset); |
| + copiedMarker->shiftOffsets(delta); |
| + |
| + dstList->add(copiedMarker); |
| + } |
| + } |
| + |
| + return didCopyMarker; |
| +} |
| + |
| +DocumentMarkerList::DidRemoveMarkerOrNot DocumentMarkerList::removeMarkers( |
| + unsigned startOffset, |
| + int length, |
| + bool shouldRemovePartiallyOverlappingMarkers) { |
| + unsigned endOffset = startOffset + length; |
| + HeapVector<Member<DocumentMarker>> newMarkerList; |
| + DidRemoveMarkerOrNot didRemoveMarker = |
| + DidRemoveMarkerOrNot::DidNotRemoveMarker; |
| + |
| + for (Member<DocumentMarker> marker : m_markers) { |
| + if (marker->endOffset() <= startOffset) { |
| + newMarkerList.push_back(marker); |
| + continue; |
| + } |
| + |
| + if (marker->startOffset() >= endOffset) { |
| + newMarkerList.push_back(marker); |
| + continue; |
| + } |
| + |
| + didRemoveMarker = DidRemoveMarkerOrNot::DidRemoveMarker; |
| + |
| + if (shouldRemovePartiallyOverlappingMarkers) { |
| + // Stop here. Don't add resulting slices back. |
| + continue; |
| + } |
| + |
| + // add either of the resulting slices that are left after removing target |
| + if (startOffset > marker->startOffset()) { |
| + DocumentMarker* newLeft = marker->clone(); |
| + newLeft->setEndOffset(startOffset); |
| + newMarkerList.push_back(newLeft); |
| + } |
| + |
| + if (marker->endOffset() > endOffset) { |
| + DocumentMarker* newRight = marker->clone(); |
| + newRight->setStartOffset(endOffset); |
| + newMarkerList.push_back(newRight); |
| + } |
| + } |
| + |
| + swap(m_markers, newMarkerList); |
| + return didRemoveMarker; |
| +} |
| + |
| +DocumentMarkerList::DidShiftMarkerOrNot DocumentMarkerList::shiftMarkers( |
| + unsigned offset, |
| + unsigned oldLength, |
| + unsigned newLength) { |
| + HeapVector<Member<DocumentMarker>> newMarkerList; |
| + DocumentMarkerList::DidShiftMarkerOrNot didShift = |
| + DidShiftMarkerOrNot::DidNotShiftMarker; |
| + for (Member<DocumentMarker> marker : m_markers) { |
| + Optional<DocumentMarker::MarkerOffsets> result = |
| + marker->computeOffsetsAfterShift(offset, oldLength, newLength); |
| + |
| + if (result == WTF::nullopt) { |
| + didShift = DidShiftMarkerOrNot::DidShiftMarker; |
| + continue; |
| + } |
| + |
| + if (result.value().startOffset != marker->startOffset() || |
| + result.value().endOffset != marker->endOffset()) { |
| + marker->setStartOffset(result.value().startOffset); |
| + marker->setEndOffset(result.value().endOffset); |
| + |
| + didShift = DidShiftMarkerOrNot::DidShiftMarker; |
| + } |
| + newMarkerList.push_back(marker); |
| + } |
| + |
| + swap(m_markers, newMarkerList); |
| + return didShift; |
| +} |
| + |
| +HeapVector<Member<DocumentMarker>>::iterator |
| +DocumentMarkerList::getPosOfFirstMarkerNotEndingBefore(size_t startOffset) { |
| + DCHECK(markerListIsSorted()); |
| + return std::upper_bound( |
| + m_markers.begin(), m_markers.end(), startOffset, |
| + [](size_t startOffset, const Member<DocumentMarker>& rhv) -> bool { |
| + return startOffset < rhv->endOffset(); |
| + }); |
| +} |
| + |
| +DEFINE_TRACE(DocumentMarkerList) { |
| + visitor->trace(m_markers); |
| +} |
| + |
| +} // namespace blink |