| Index: third_party/WebKit/Source/core/editing/markers/TextMatchMarkerList.cpp
|
| diff --git a/third_party/WebKit/Source/core/editing/markers/TextMatchMarkerList.cpp b/third_party/WebKit/Source/core/editing/markers/TextMatchMarkerList.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8c2e607b384a8dd79c6e467958cf1fdfd6d9ea8c
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/editing/markers/TextMatchMarkerList.cpp
|
| @@ -0,0 +1,162 @@
|
| +// 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/TextMatchMarkerList.h"
|
| +
|
| +#include "core/editing/markers/DocumentMarkerController.h"
|
| +#include "core/editing/markers/RenderedTextMatchMarker.h"
|
| +
|
| +namespace blink {
|
| +
|
| +TextMatchMarkerList::TextMatchMarkerList(
|
| + DocumentMarkerController* documentMarkerController)
|
| + : DocumentMarkerList(documentMarkerController) {}
|
| +
|
| +DocumentMarker::MarkerType TextMatchMarkerList::allowedMarkerType() const {
|
| + return DocumentMarker::TextMatch;
|
| +}
|
| +
|
| +static bool doesNotInclude(const Member<DocumentMarker>& marker,
|
| + size_t startOffset) {
|
| + return marker->endOffset() < startOffset;
|
| +}
|
| +
|
| +RenderedTextMatchMarker* TextMatchMarkerList::at(size_t index) {
|
| + return toRenderedTextMatchMarker(m_markers[index].get());
|
| +}
|
| +
|
| +void TextMatchMarkerList::clear() {
|
| + m_markers.clear();
|
| +}
|
| +
|
| +bool TextMatchMarkerList::copyMarkers(unsigned startOffset,
|
| + int length,
|
| + Node* dstNode,
|
| + int delta) const {
|
| + bool docDirty = false;
|
| + unsigned endOffset = startOffset + length - 1;
|
| + auto startPos = std::lower_bound(m_markers.begin(), m_markers.end(),
|
| + startOffset, doesNotInclude);
|
| + for (auto i = startPos; i != m_markers.end(); ++i) {
|
| + DocumentMarker* marker = i->get();
|
| +
|
| + // stop if we are now past the specified range
|
| + if (marker->startOffset() > endOffset)
|
| + break;
|
| +
|
| + // pin the marker to the specified range and apply the shift delta
|
| + 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 TextMatchMarkerList::removeMarkers(
|
| + unsigned startOffset,
|
| + int length,
|
| + bool shouldRemovePartiallyOverlappingMarkers,
|
| + bool* didRemoveMarker) {
|
| + unsigned endOffset = startOffset + length;
|
| + for (auto it = getPosOfFirstMarkerNotEndingBefore(startOffset);
|
| + it < m_markers.end(); ++it) {
|
| + RenderedTextMatchMarker& marker = toRenderedTextMatchMarker(**it);
|
| +
|
| + if (marker.startOffset() >= endOffset)
|
| + break;
|
| +
|
| + // pitch the old marker
|
| + m_markers.remove(it - m_markers.begin());
|
| + *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()) {
|
| + RenderedTextMatchMarker newLeft = marker;
|
| + newLeft.setEndOffset(startOffset);
|
| + size_t insertIndex = it - m_markers.begin();
|
| + m_markers.insert(insertIndex, RenderedTextMatchMarker::create(newLeft));
|
| + // Move to the marker after the inserted one.
|
| + it = m_markers.begin() + insertIndex + 1;
|
| + }
|
| + if (marker.endOffset() > endOffset) {
|
| + RenderedTextMatchMarker newRight = marker;
|
| + newRight.setStartOffset(endOffset);
|
| + size_t insertIndex = it - m_markers.begin();
|
| + m_markers.insert(insertIndex, RenderedTextMatchMarker::create(newRight));
|
| + // Move to the marker after the inserted one.
|
| + it = m_markers.begin() + insertIndex + 1;
|
| + }
|
| + }
|
| +}
|
| +
|
| +bool TextMatchMarkerList::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 TextMatchMarkerList::push_back(DocumentMarker* marker) {
|
| + // TextMatch markers must be added in order
|
| + DCHECK(marker->type() == DocumentMarker::TextMatch);
|
| + DCHECK(m_markers.isEmpty() ||
|
| + marker->startOffset() >= m_markers.back()->endOffset());
|
| + m_markers.push_back(RenderedTextMatchMarker::create(*marker));
|
| +}
|
| +
|
| +bool TextMatchMarkerList::setTextMatchMarkersActive(unsigned startOffset,
|
| + unsigned endOffset,
|
| + bool active) {
|
| + bool docDirty = false;
|
| + for (auto it = getPosOfFirstMarkerNotEndingBefore(startOffset);
|
| + it != m_markers.end(); ++it) {
|
| + DocumentMarker& marker = **it;
|
| + // Markers are stored in order, so stop if we are now past the specified
|
| + // range.
|
| + if (marker.startOffset() >= endOffset)
|
| + break;
|
| +
|
| + marker.setActiveMatch(active);
|
| + docDirty = true;
|
| + }
|
| +
|
| + return docDirty;
|
| +}
|
| +
|
| +DEFINE_TRACE(TextMatchMarkerList) {
|
| + visitor->trace(m_markers);
|
| + DocumentMarkerList::trace(visitor);
|
| +}
|
| +
|
| +} // namespace blink
|
|
|