| Index: third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp
|
| diff --git a/third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp b/third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5853852526811fc4d1de5ffd392e6e4f87cf0852
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp
|
| @@ -0,0 +1,73 @@
|
| +// 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/SpellCheckMarkerList.h"
|
| +
|
| +#include <algorithm>
|
| +
|
| +namespace blink {
|
| +
|
| +SpellCheckMarkerList::SpellCheckMarkerList(DocumentMarker::MarkerType type)
|
| + : m_type(type) {}
|
| +
|
| +DocumentMarker::MarkerType SpellCheckMarkerList::allowedMarkerType() const {
|
| + return m_type;
|
| +}
|
| +
|
| +bool SpellCheckMarkerList::isSpellCheckMarkerList() const {
|
| + return true;
|
| +}
|
| +
|
| +void SpellCheckMarkerList::add(DocumentMarker* marker) {
|
| + DCHECK(marker->type() == allowedMarkerType());
|
| + // Optimize case where (non-merging) markers are being added in order so we
|
| + // can add n markers in O(n) time instead of O(n log n)
|
| + if (m_markers.isEmpty() ||
|
| + m_markers.back()->endOffset() < marker->startOffset()) {
|
| + m_markers.push_back(marker);
|
| + return;
|
| + }
|
| +
|
| + auto firstOverlappingIt = std::lower_bound(
|
| + m_markers.begin(), m_markers.end(), marker,
|
| + [](const Member<DocumentMarker>& lhv, const DocumentMarker* rhv) {
|
| + return lhv->endOffset() < rhv->startOffset();
|
| + });
|
| +
|
| + size_t index = firstOverlappingIt - m_markers.begin();
|
| + m_markers.insert(index, marker);
|
| + auto insertedIt = m_markers.begin() + index;
|
| +
|
| + unsigned newStart = marker->startOffset();
|
| + unsigned newEnd = marker->endOffset();
|
| +
|
| + iterator it;
|
| + for (it = insertedIt + 1;
|
| + it != m_markers.end() && (*it)->startOffset() <= newEnd; ++it) {
|
| + newStart = std::min(newStart, (*it)->startOffset());
|
| + newEnd = std::max(newEnd, (*it)->endOffset());
|
| + }
|
| + m_markers.erase(index + 1, it - (insertedIt + 1));
|
| +
|
| + (*insertedIt)->setStartOffset(newStart);
|
| + (*insertedIt)->setEndOffset(newEnd);
|
| +}
|
| +
|
| +void SpellCheckMarkerList::removeMarkersForWords(const String& nodeText,
|
| + const Vector<String>& words) {
|
| + // Build a second vector and swap with m_markers to avoid O(n^2) performance
|
| + HeapVector<Member<DocumentMarker>> newMarkerList;
|
| +
|
| + for (Member<DocumentMarker> marker : m_markers) {
|
| + unsigned start = marker->startOffset();
|
| + unsigned length = marker->endOffset() - marker->startOffset();
|
| + String markerText = nodeText.substring(start, length);
|
| + if (!words.contains(markerText))
|
| + newMarkerList.push_back(marker);
|
| + }
|
| +
|
| + std::swap(m_markers, newMarkerList);
|
| +}
|
| +
|
| +} // namespace blink
|
|
|