Home | History | Annotate | Download | only in dom
      1 /*
      2  * This file is part of the DOM implementation for WebCore.
      3  *
      4  * Copyright (C) 2006 Apple Computer, Inc.
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Library General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Library General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Library General Public License
     17  * along with this library; see the file COPYING.LIB.  If not, write to
     18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  * Boston, MA 02110-1301, USA.
     20  *
     21  */
     22 
     23 #ifndef DocumentMarker_h
     24 #define DocumentMarker_h
     25 
     26 #include "platform/heap/Handle.h"
     27 #include "wtf/VectorTraits.h"
     28 #include "wtf/text/WTFString.h"
     29 
     30 namespace blink {
     31 
     32 class DocumentMarkerDetails;
     33 
     34 // A range of a node within a document that is "marked", such as the range of a misspelled word.
     35 // It optionally includes a description that could be displayed in the user interface.
     36 // It also optionally includes a flag specifying whether the match is active, which is ignored
     37 // for all types other than type TextMatch.
     38 class DocumentMarker : public NoBaseWillBeGarbageCollected<DocumentMarker> {
     39 public:
     40     enum MarkerTypeIndex {
     41         SpellingMarkerIndex = 0,
     42         GramarMarkerIndex,
     43         TextMatchMarkerIndex,
     44         InvisibleSpellcheckMarkerIndex,
     45         MarkerTypeIndexesCount
     46     };
     47 
     48     enum MarkerType {
     49         Spelling = 1 << SpellingMarkerIndex,
     50         Grammar = 1 << GramarMarkerIndex,
     51         TextMatch = 1 << TextMatchMarkerIndex,
     52         InvisibleSpellcheck = 1 << InvisibleSpellcheckMarkerIndex
     53     };
     54 
     55     class MarkerTypes {
     56     public:
     57         // The constructor is intentionally implicit to allow conversion from the bit-wise sum of above types
     58         MarkerTypes(unsigned mask) : m_mask(mask) { }
     59 
     60         bool contains(MarkerType type) const { return m_mask & type; }
     61         bool intersects(const MarkerTypes& types) const { return (m_mask & types.m_mask); }
     62         bool operator==(const MarkerTypes& other) const { return m_mask == other.m_mask; }
     63 
     64         void add(const MarkerTypes& types) { m_mask |= types.m_mask; }
     65         void remove(const MarkerTypes& types) { m_mask &= ~types.m_mask; }
     66 
     67     private:
     68         unsigned m_mask;
     69     };
     70 
     71     class AllMarkers : public MarkerTypes {
     72     public:
     73         AllMarkers()
     74             : MarkerTypes(Spelling | Grammar | TextMatch | InvisibleSpellcheck)
     75         {
     76         }
     77     };
     78 
     79     class MisspellingMarkers : public MarkerTypes {
     80     public:
     81         MisspellingMarkers()
     82             : MarkerTypes(Spelling | Grammar)
     83         {
     84         }
     85     };
     86 
     87     class SpellCheckClientMarkers : public MarkerTypes {
     88     public:
     89         SpellCheckClientMarkers()
     90             : MarkerTypes(Spelling | Grammar | InvisibleSpellcheck)
     91         {
     92         }
     93     };
     94 
     95     DocumentMarker();
     96     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description, uint32_t hash);
     97     DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch);
     98     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, PassRefPtrWillBeRawPtr<DocumentMarkerDetails>);
     99     DocumentMarker(const DocumentMarker&);
    100 
    101     MarkerType type() const { return m_type; }
    102     unsigned startOffset() const { return m_startOffset; }
    103     unsigned endOffset() const { return m_endOffset; }
    104     uint32_t hash() const { return m_hash; }
    105 
    106     const String& description() const;
    107     bool activeMatch() const;
    108     DocumentMarkerDetails* details() const;
    109 
    110     void setActiveMatch(bool);
    111     void clearDetails() { m_details.clear(); }
    112 
    113     // Offset modifications are done by DocumentMarkerController.
    114     // Other classes should not call following setters.
    115     void setStartOffset(unsigned offset) { m_startOffset = offset; }
    116     void setEndOffset(unsigned offset) { m_endOffset = offset; }
    117     void shiftOffsets(int delta);
    118 
    119     bool operator==(const DocumentMarker& o) const
    120     {
    121         return type() == o.type() && startOffset() == o.startOffset() && endOffset() == o.endOffset();
    122     }
    123 
    124     bool operator!=(const DocumentMarker& o) const
    125     {
    126         return !(*this == o);
    127     }
    128 
    129     void trace(Visitor*);
    130 
    131 private:
    132     MarkerType m_type;
    133     unsigned m_startOffset;
    134     unsigned m_endOffset;
    135     RefPtrWillBeMember<DocumentMarkerDetails> m_details;
    136     uint32_t m_hash;
    137 };
    138 
    139 typedef WillBeHeapVector<RawPtrWillBeMember<DocumentMarker> > DocumentMarkerVector;
    140 
    141 inline DocumentMarkerDetails* DocumentMarker::details() const
    142 {
    143     return m_details.get();
    144 }
    145 
    146 class DocumentMarkerDetails : public RefCountedWillBeGarbageCollectedFinalized<DocumentMarkerDetails>
    147 {
    148 public:
    149     DocumentMarkerDetails() { }
    150     virtual ~DocumentMarkerDetails();
    151     virtual bool isDescription() const { return false; }
    152     virtual bool isTextMatch() const { return false; }
    153 
    154     virtual void trace(Visitor*) { }
    155 };
    156 
    157 } // namespace blink
    158 
    159 #endif // DocumentMarker_h
    160