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

Side by Side Diff: third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp

Issue 2770413003: Add SpellCheckMarkerList in preparation for DocumentMarkerController refactor (Closed)
Patch Set: Add DCHECK that markers being inserted are of correct type 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/editing/markers/SpellCheckMarkerList.h"
6
7 #include <algorithm>
8 #include "core/dom/Text.h"
9
10 namespace blink {
11
12 SpellCheckMarkerList::SpellCheckMarkerList(DocumentMarker::MarkerType type)
13 : m_type(type) {}
14
15 static bool doesNotOverlap(const Member<DocumentMarker>& lhv,
Xiaocheng 2017/03/25 18:55:52 The name doesn't match what it does. Btw, I just r
16 const DocumentMarker* rhv) {
17 return lhv->endOffset() < rhv->startOffset();
18 }
19
20 DocumentMarker::MarkerType SpellCheckMarkerList::allowedMarkerType() const {
21 return m_type;
22 }
23
24 bool SpellCheckMarkerList::isSpellCheckMarkerList() const {
25 return true;
26 }
27
28 void SpellCheckMarkerList::push_back(DocumentMarker* marker) {
Xiaocheng 2017/03/25 18:55:52 Hmm... We need to be more careful about the functi
29 DCHECK(marker->type() == allowedMarkerType());
30 auto firstOverlappingIt = std::lower_bound(m_markers.begin(), m_markers.end(),
31 marker, doesNotOverlap);
32 size_t index = firstOverlappingIt - m_markers.begin();
33 m_markers.insert(index, marker);
34 auto insertedIt = m_markers.begin() + index;
35 for (auto it = insertedIt + 1;
36 it != m_markers.end() &&
37 (*it)->startOffset() <= (*insertedIt)->endOffset();) {
38 (*insertedIt)
39 ->setStartOffset(
40 std::min((*insertedIt)->startOffset(), (*it)->startOffset()));
41 (*insertedIt)
42 ->setEndOffset(
43 std::max((*insertedIt)->endOffset(), (*it)->endOffset()));
44 m_markers.remove(it - m_markers.begin());
Xiaocheng 2017/03/25 18:55:52 This leads to quadratic running time. Could you re
rlanday 2017/03/25 19:33:00 Ah, I did notice this can be improved, I must've b
45 }
46 }
47
48 void SpellCheckMarkerList::removeMarkersForWords(const Text& textNode,
Xiaocheng 2017/03/25 18:55:52 Let's take a String parameter to remove the depend
49 const Vector<String>& words) {
50 // Build a second vector and swap with m_markers to avoid O(n^2) performance
51 HeapVector<Member<DocumentMarker>> newMarkerList;
52
53 for (Member<DocumentMarker> marker : m_markers) {
54 unsigned start = marker->startOffset();
55 unsigned length = marker->endOffset() - marker->startOffset();
56 String markerText = textNode.data().substring(start, length);
57 if (!words.contains(markerText))
58 newMarkerList.push_back(marker);
59 }
60
61 std::swap(m_markers, newMarkerList);
62 }
63
64 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698