Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/dom/DocumentMarker.h"
     33 
     34 namespace blink {
     35 
     36 DocumentMarkerDetails::~DocumentMarkerDetails()
     37 {
     38 }
     39 
     40 class DocumentMarkerDescription FINAL : public DocumentMarkerDetails {
     41 public:
     42     static PassRefPtrWillBeRawPtr<DocumentMarkerDescription> create(const String&);
     43 
     44     const String& description() const { return m_description; }
     45     virtual bool isDescription() const OVERRIDE { return true; }
     46 
     47 private:
     48     explicit DocumentMarkerDescription(const String& description)
     49         : m_description(description)
     50     {
     51     }
     52 
     53     String m_description;
     54 };
     55 
     56 PassRefPtrWillBeRawPtr<DocumentMarkerDescription> DocumentMarkerDescription::create(const String& description)
     57 {
     58     return adoptRefWillBeNoop(new DocumentMarkerDescription(description));
     59 }
     60 
     61 inline DocumentMarkerDescription* toDocumentMarkerDescription(DocumentMarkerDetails* details)
     62 {
     63     if (details && details->isDescription())
     64         return static_cast<DocumentMarkerDescription*>(details);
     65     return 0;
     66 }
     67 
     68 
     69 class DocumentMarkerTextMatch FINAL : public DocumentMarkerDetails {
     70 public:
     71     static PassRefPtrWillBeRawPtr<DocumentMarkerTextMatch> instanceFor(bool);
     72 
     73     bool activeMatch() const { return m_match; }
     74     virtual bool isTextMatch() const OVERRIDE { return true; }
     75 
     76 private:
     77     explicit DocumentMarkerTextMatch(bool match)
     78         : m_match(match)
     79     {
     80     }
     81 
     82     bool m_match;
     83 };
     84 
     85 PassRefPtrWillBeRawPtr<DocumentMarkerTextMatch> DocumentMarkerTextMatch::instanceFor(bool match)
     86 {
     87     DEFINE_STATIC_REF_WILL_BE_PERSISTENT(DocumentMarkerTextMatch, trueInstance, (adoptRefWillBeNoop(new DocumentMarkerTextMatch(true))));
     88     DEFINE_STATIC_REF_WILL_BE_PERSISTENT(DocumentMarkerTextMatch, falseInstance, (adoptRefWillBeNoop(new DocumentMarkerTextMatch(false))));
     89     return match ? trueInstance : falseInstance;
     90 }
     91 
     92 inline DocumentMarkerTextMatch* toDocumentMarkerTextMatch(DocumentMarkerDetails* details)
     93 {
     94     if (details && details->isTextMatch())
     95         return static_cast<DocumentMarkerTextMatch*>(details);
     96     return 0;
     97 }
     98 
     99 
    100 DocumentMarker::DocumentMarker()
    101     : m_type(Spelling)
    102     , m_startOffset(0)
    103     , m_endOffset(0)
    104     , m_hash(0)
    105 {
    106 }
    107 
    108 DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset, const String& description, uint32_t hash)
    109     : m_type(type)
    110     , m_startOffset(startOffset)
    111     , m_endOffset(endOffset)
    112     , m_details(description.isEmpty() ? nullptr : DocumentMarkerDescription::create(description))
    113     , m_hash(hash)
    114 {
    115 }
    116 
    117 DocumentMarker::DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch)
    118     : m_type(DocumentMarker::TextMatch)
    119     , m_startOffset(startOffset)
    120     , m_endOffset(endOffset)
    121     , m_details(DocumentMarkerTextMatch::instanceFor(activeMatch))
    122     , m_hash(0)
    123 {
    124 }
    125 
    126 DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset, PassRefPtrWillBeRawPtr<DocumentMarkerDetails> details)
    127     : m_type(type)
    128     , m_startOffset(startOffset)
    129     , m_endOffset(endOffset)
    130     , m_details(details)
    131     , m_hash(0)
    132 {
    133 }
    134 
    135 DocumentMarker::DocumentMarker(const DocumentMarker& marker)
    136     : m_type(marker.type())
    137     , m_startOffset(marker.startOffset())
    138     , m_endOffset(marker.endOffset())
    139     , m_details(marker.details())
    140     , m_hash(marker.hash())
    141 {
    142 }
    143 
    144 void DocumentMarker::shiftOffsets(int delta)
    145 {
    146     m_startOffset += delta;
    147     m_endOffset +=  delta;
    148 }
    149 
    150 void DocumentMarker::setActiveMatch(bool active)
    151 {
    152     m_details = DocumentMarkerTextMatch::instanceFor(active);
    153 }
    154 
    155 const String& DocumentMarker::description() const
    156 {
    157     if (DocumentMarkerDescription* details = toDocumentMarkerDescription(m_details.get()))
    158         return details->description();
    159     return emptyString();
    160 }
    161 
    162 bool DocumentMarker::activeMatch() const
    163 {
    164     if (DocumentMarkerTextMatch* details = toDocumentMarkerTextMatch(m_details.get()))
    165         return details->activeMatch();
    166     return false;
    167 }
    168 
    169 void DocumentMarker::trace(Visitor* visitor)
    170 {
    171     visitor->trace(m_details);
    172 }
    173 
    174 } // namespace blink
    175