Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Unified Diff: third_party/WebKit/Source/core/editing/markers/EditingMarkerList.cpp

Issue 2723663002: Refactor DocumentMarkerController (Closed)
Patch Set: Use correct base commit Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/markers/EditingMarkerList.cpp
diff --git a/third_party/WebKit/Source/core/editing/markers/EditingMarkerList.cpp b/third_party/WebKit/Source/core/editing/markers/EditingMarkerList.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..b975aec7cfe4e8287650ba875ea95c67e1ed2eec
--- /dev/null
+++ b/third_party/WebKit/Source/core/editing/markers/EditingMarkerList.cpp
@@ -0,0 +1,146 @@
+// 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/EditingMarkerList.h"
+
+#include <algorithm>
+#include "core/editing/markers/DocumentMarkerController.h"
+
+namespace blink {
+
+EditingMarkerList::EditingMarkerList(
+ DocumentMarkerController* documentMarkerController)
+ : DocumentMarkerList(documentMarkerController), m_markersAreSorted(false) {}
+
+void EditingMarkerList::clear() {
+ m_markers.clear();
+ m_markersAreSorted = true;
+}
+
+bool EditingMarkerList::copyMarkers(unsigned startOffset,
+ int length,
+ Node* dstNode,
+ int delta) const {
+ bool docDirty = false;
+ 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) {
+ docDirty = true;
+ if (marker->startOffset() < startOffset)
+ marker->setStartOffset(startOffset);
+ if (marker->endOffset() > endOffset)
+ marker->setEndOffset(endOffset);
+ marker->shiftOffsets(delta);
+
+ m_documentMarkerController->addMarker(dstNode, marker);
+ }
+ }
+
+ return docDirty;
+}
+
+void EditingMarkerList::removeMarkers(
+ unsigned startOffset,
+ int length,
+ bool shouldRemovePartiallyOverlappingMarkers,
+ bool* didRemoveMarker) {
+ unsigned endOffset = startOffset + length;
+ size_t markerIndex = 0;
+ if (m_markersAreSorted) {
+ markerIndex =
+ getPosOfFirstMarkerNotEndingBefore(markerIndex) - m_markers.begin();
+ }
+
+ for (; markerIndex < m_markers.size(); ++markerIndex) {
+ DocumentMarker& marker = *m_markers.at(markerIndex);
+
+ if (!m_markersAreSorted) {
+ if (marker.endOffset() <= startOffset)
+ continue;
+ }
+
+ if (marker.startOffset() >= endOffset) {
+ if (m_markersAreSorted)
+ break;
+ continue;
+ }
+
+ // pitch the old marker
+ m_markers.remove(markerIndex);
+ *didRemoveMarker = true;
+
+ 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 = new DocumentMarker(marker);
+ newLeft->setEndOffset(startOffset);
+ m_markers.insert(markerIndex, *newLeft);
+ // Move to the marker after the inserted one.
+ ++markerIndex;
+ }
+ if (marker.endOffset() > endOffset) {
+ DocumentMarker* newRight = new DocumentMarker(marker);
+ newRight->setStartOffset(endOffset);
+ m_markers.insert(markerIndex, *newRight);
+ // Move to the marker after the inserted one.
+ ++markerIndex;
+ }
+ }
+}
+
+bool EditingMarkerList::shiftMarkers(unsigned offset,
+ unsigned oldLength,
+ unsigned newLength) {
+ bool didShift = false;
+ for (auto it = m_markers.begin(); it != m_markers.end(); ++it) {
+ DocumentMarker& marker = **it;
+
+ DocumentMarker::ShiftMarkerResult result =
+ marker.getShiftedMarkerPosition(offset, oldLength, newLength);
+ if (result.shouldRemoveMarker) {
+ m_markers.remove(it - m_markers.begin());
+ --it;
+
+ didShift = true;
+ } else if (result.newStartOffset != marker.startOffset() ||
+ result.newEndOffset != marker.endOffset()) {
+ marker.setStartOffset(result.newStartOffset);
+ marker.setEndOffset(result.newEndOffset);
+
+ didShift = true;
+ }
+ }
+
+ return didShift;
+}
+
+void EditingMarkerList::insert(DocumentMarker* marker) {
+ DCHECK(marker->type() == allowedMarkerType());
+ if (m_markersAreSorted && !m_markers.isEmpty() &&
+ m_markers.back()->endOffset() > marker->startOffset())
+ m_markersAreSorted = false;
+ m_markers.push_back(marker);
+}
+
+void EditingMarkerList::sortMarkerList() {
+ std::sort(m_markers.begin(), m_markers.end(), compareByStart);
+}
+
+bool EditingMarkerList::compareByStart(const Member<DocumentMarker>& lhv,
+ const Member<DocumentMarker>& rhv) {
+ return lhv->startOffset() < rhv->startOffset();
+}
+
+DEFINE_TRACE(EditingMarkerList) {
+ visitor->trace(m_markers);
+ DocumentMarkerList::trace(visitor);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698