Chromium Code Reviews| Index: third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp |
| diff --git a/third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp b/third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4af19acea22a748643744af48f6b7e33bcc9515f |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp |
| @@ -0,0 +1,206 @@ |
| +// 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/DocumentMarkerListEditor.h" |
| + |
| +namespace blink { |
| + |
| +DocumentMarkerList::iterator |
| +DocumentMarkerListEditor::getPosOfFirstMarkerNotEndingBeforeInSortedList( |
| + DocumentMarkerVector* markerList, |
| + size_t startOffset) { |
| + return std::upper_bound( |
| + markerList->begin(), markerList->end(), startOffset, |
| + [](size_t startOffset, const Member<DocumentMarker>& rhv) -> bool { |
| + return startOffset < rhv->endOffset(); |
| + }); |
| +} |
| + |
| +void DocumentMarkerListEditor::appendMarkersToInputList( |
| + const DocumentMarkerVector& markerList, |
| + DocumentMarkerVector* inputList) { |
| + for (Member<DocumentMarker> marker : markerList) { |
| + inputList->push_back(marker); |
| + } |
| +} |
| + |
| +DocumentMarkerList::DidCopyMarkerOrNot |
| +DocumentMarkerListEditor::copyUnsortedMarkers(DocumentMarkerVector* markerList, |
| + unsigned startOffset, |
| + int length, |
| + DocumentMarkerList* dstList, |
| + int delta) { |
| + return copyMarkersHelper(markerList, markerList->begin(), markerList->end(), |
| + startOffset, length, dstList, delta); |
| +} |
| + |
| +DocumentMarkerList::DidCopyMarkerOrNot |
| +DocumentMarkerListEditor::copySortedMarkers(DocumentMarkerVector* markerList, |
| + unsigned startOffset, |
| + int length, |
| + DocumentMarkerList* dstList, |
| + int delta) { |
| + return copyMarkersHelper( |
| + markerList, |
| + std::lower_bound( |
| + markerList->begin(), markerList->end(), startOffset, |
| + [](const Member<DocumentMarker>& marker, size_t startOffset) { |
| + return marker->endOffset() < startOffset; |
| + }), |
| + markerList->end(), startOffset, length, dstList, delta); |
| +} |
| + |
| +DocumentMarkerList::DidRemoveMarkerOrNot |
| +DocumentMarkerListEditor::removeUnsortedMarkers( |
| + DocumentMarkerVector* markerList, |
| + unsigned startOffset, |
| + int length, |
| + bool shouldRemovePartiallyOverlappingMarkers) { |
| + unsigned endOffset = startOffset + length; |
| + DocumentMarkerList::DidRemoveMarkerOrNot didRemoveMarker = |
| + DocumentMarkerList::DidRemoveMarkerOrNot::kDidNotRemoveMarker; |
| + |
| + for (auto it = markerList->begin(); it != markerList->end(); ++it) { |
| + const DocumentMarker& marker = **it; |
| + |
| + // markers have non-empty overlap |
| + if (marker.startOffset() < endOffset && marker.endOffset() > startOffset) { |
| + // Erase by doing a swap-and-shrink to get O(1) performance |
| + std::swap(*it, markerList->back()); |
| + markerList->shrink(markerList->size() - 1); |
| + --it; |
| + |
| + didRemoveMarker = |
| + DocumentMarkerList::DidRemoveMarkerOrNot::kDidRemoveMarker; |
| + } |
| + } |
| + |
| + return didRemoveMarker; |
| +} |
| + |
| +DocumentMarkerList::DocumentMarkerList::DidRemoveMarkerOrNot |
| +DocumentMarkerListEditor::removeSortedMarkers( |
| + DocumentMarkerVector* markerList, |
| + unsigned startOffset, |
| + int length, |
| + bool shouldRemovePartiallyOverlappingMarkers) { |
| + unsigned endOffset = startOffset + length; |
| + |
| + const auto startPos = |
| + getPosOfFirstMarkerNotEndingBeforeInSortedList(markerList, startOffset); |
| + auto it = startPos; |
| + for (; it != markerList->end(); ++it) { |
| + if ((*it)->startOffset() >= endOffset) |
| + break; |
| + } |
| + |
| + // it should now point at the first marker *not* to be removed (or at |
| + // markerList->end()) |
| + markerList->erase(startPos - markerList->begin(), it - startPos); |
| + |
| + if (it == startPos) |
| + return DocumentMarkerList::DidRemoveMarkerOrNot::kDidNotRemoveMarker; |
| + |
| + return DocumentMarkerList::DidRemoveMarkerOrNot::kDidRemoveMarker; |
| +} |
| + |
| +DocumentMarkerList::DidShiftMarkerOrNot |
| +DocumentMarkerListEditor::shiftUnsortedMarkers(DocumentMarkerVector* markerList, |
| + unsigned offset, |
| + unsigned oldLength, |
| + unsigned newLength) { |
| + DocumentMarkerList::DidShiftMarkerOrNot didShift = |
| + DocumentMarkerList::DidShiftMarkerOrNot::kDidNotShiftMarker; |
| + for (auto it = markerList->begin(); it != markerList->end(); ++it) { |
| + DocumentMarker& marker = **it; |
| + Optional<DocumentMarker::MarkerOffsets> result = |
| + marker.computeOffsetsAfterShift(offset, oldLength, newLength); |
| + |
| + if (result == WTF::nullopt) { |
| + didShift = DocumentMarkerList::DidShiftMarkerOrNot::kDidShiftMarker; |
| + // Erase by doing a swap-and-shrink to get O(1) performance |
| + std::swap(*it, markerList->back()); |
| + markerList->shrink(markerList->size() - 1); |
| + --it; |
| + continue; |
| + } |
| + |
| + if (result.value().startOffset != marker.startOffset() || |
| + result.value().endOffset != marker.endOffset()) { |
| + marker.setStartOffset(result.value().startOffset); |
| + marker.setEndOffset(result.value().endOffset); |
| + |
| + didShift = DocumentMarkerList::DidShiftMarkerOrNot::kDidShiftMarker; |
| + } |
| + } |
| + |
| + return didShift; |
| +} |
| + |
| +DocumentMarkerList::DidShiftMarkerOrNot |
| +DocumentMarkerListEditor::shiftSortedMarkers(DocumentMarkerVector* markerList, |
| + unsigned offset, |
| + unsigned oldLength, |
| + unsigned newLength) { |
| + DocumentMarkerVector newMarkerList; |
| + DocumentMarkerList::DidShiftMarkerOrNot didShift = |
| + DocumentMarkerList::DidShiftMarkerOrNot::kDidNotShiftMarker; |
| + for (Member<DocumentMarker> marker : *markerList) { |
| + Optional<DocumentMarker::MarkerOffsets> result = |
| + marker->computeOffsetsAfterShift(offset, oldLength, newLength); |
| + |
| + if (result == WTF::nullopt) { |
| + didShift = DocumentMarkerList::DidShiftMarkerOrNot::kDidShiftMarker; |
| + continue; |
| + } |
| + |
| + if (result.value().startOffset != marker->startOffset() || |
| + result.value().endOffset != marker->endOffset()) { |
| + marker->setStartOffset(result.value().startOffset); |
| + marker->setEndOffset(result.value().endOffset); |
| + |
| + didShift = DocumentMarkerList::DidShiftMarkerOrNot::kDidShiftMarker; |
| + } |
| + |
| + newMarkerList.push_back(marker); |
| + } |
| + |
| + swap(*markerList, newMarkerList); |
| + return didShift; |
| +} |
| + |
| +DocumentMarkerList::DidCopyMarkerOrNot |
| +DocumentMarkerListEditor::copyMarkersHelper( |
| + DocumentMarkerVector* markerList, |
| + DocumentMarkerList::iterator beginIt, |
| + DocumentMarkerList::iterator endIt, |
| + unsigned startOffset, |
| + int length, |
| + DocumentMarkerList* dstList, |
| + int delta) { |
| + DocumentMarkerList::DidCopyMarkerOrNot didCopyMarker = |
|
yosin_UTC9
2017/04/11 01:39:37
Let's add |DCHECK_GT(length, 0)|
|
| + DocumentMarkerList::DidCopyMarkerOrNot::kDidNotCopyMarker; |
| + unsigned endOffset = startOffset + length - 1; |
| + |
| + for (auto it = beginIt; it != endIt; ++it) { |
| + DocumentMarker& marker = **it; |
| + // pin the marker to the specified range and apply the shift delta |
| + if (marker.endOffset() >= startOffset && |
|
yosin_UTC9
2017/04/11 01:39:37
Let's use early-continue to reduce indentation.
i
|
| + marker.startOffset() <= endOffset) { |
| + didCopyMarker = DocumentMarkerList::DidCopyMarkerOrNot::kDidCopyMarker; |
| + |
| + if (marker.startOffset() < startOffset) |
| + marker.setStartOffset(startOffset); |
| + if (marker.endOffset() > endOffset) |
| + marker.setEndOffset(endOffset); |
| + marker.shiftOffsets(delta); |
| + |
| + dstList->add(&marker); |
| + } |
| + } |
| + |
| + return didCopyMarker; |
| +} |
| + |
| +} // namespace blink |