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 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_MARKERS_SPELL_CHECK_MARKER_LIST_IMPL_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_MARKERS_SPELL_CHECK_MARKER_LIST_IMPL_H_
7 
8 #include "third_party/blink/renderer/core/editing/markers/document_marker_list.h"
9 #include "third_party/blink/renderer/platform/wtf/casting.h"
10 
11 namespace blink {
12 
13 // Nearly-complete implementation of DocumentMarkerList for Spelling or Grammar
14 // markers (subclassed by SpellingMarkerListImpl and GrammarMarkerListImpl to
15 // implement the MarkerType() method). Markers with touching endpoints are
16 // merged on insert. Markers are kept sorted by start offset in order to be able
17 // to do this efficiently.
18 class CORE_EXPORT SpellCheckMarkerListImpl : public DocumentMarkerList {
19  public:
20   // DocumentMarkerList implementations
21   bool IsEmpty() const final;
22 
23   void Add(DocumentMarker*) final;
24   void Clear() final;
25 
26   const HeapVector<Member<DocumentMarker>>& GetMarkers() const final;
27   DocumentMarker* FirstMarkerIntersectingRange(unsigned start_offset,
28                                                unsigned end_offset) const final;
29   HeapVector<Member<DocumentMarker>> MarkersIntersectingRange(
30       unsigned start_offset,
31       unsigned end_offset) const final;
32 
33   bool MoveMarkers(int length, DocumentMarkerList* dst_list) final;
34   bool RemoveMarkers(unsigned start_offset, int length) final;
35   bool ShiftMarkers(const String& node_text,
36                     unsigned offset,
37                     unsigned old_length,
38                     unsigned new_length) final;
39 
40   void Trace(Visitor*) const override;
41 
42   // SpellCheckMarkerListImpl-specific
43   // Returns true if a marker was removed, false otherwise.
44   bool RemoveMarkersUnderWords(const String& node_text,
45                                const Vector<String>& words);
46 
47  protected:
48   SpellCheckMarkerListImpl() = default;
49 
50  private:
51   HeapVector<Member<DocumentMarker>> markers_;
52 
53   DISALLOW_COPY_AND_ASSIGN(SpellCheckMarkerListImpl);
54 };
55 
56 template <>
57 struct DowncastTraits<SpellCheckMarkerListImpl> {
58   static bool AllowFrom(const DocumentMarkerList& list) {
59     return list.MarkerType() == DocumentMarker::kSpelling ||
60            list.MarkerType() == DocumentMarker::kGrammar;
61   }
62 };
63 
64 }  // namespace blink
65 
66 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_MARKERS_SPELL_CHECK_MARKER_LIST_IMPL_H_
67