2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "DocumentMarker.h"
36 DocumentMarkerDetails::~DocumentMarkerDetails()
40 class DocumentMarkerDescription final : public DocumentMarkerDetails {
42 static Ref<DocumentMarkerDescription> create(const String&);
44 const String& description() const { return m_description; }
45 bool isDescription() const override { return true; }
48 DocumentMarkerDescription(const String& description)
49 : m_description(description)
56 Ref<DocumentMarkerDescription> DocumentMarkerDescription::create(const String& description)
58 return adoptRef(*new DocumentMarkerDescription(description));
61 inline DocumentMarkerDescription* toDocumentMarkerDescription(DocumentMarkerDetails* details)
63 if (details && details->isDescription())
64 return static_cast<DocumentMarkerDescription*>(details);
69 class DocumentMarkerTextMatch : public DocumentMarkerDetails {
71 static PassRefPtr<DocumentMarkerTextMatch> instanceFor(bool);
73 bool activeMatch() const { return m_match; }
74 bool isTextMatch() const override { return true; }
77 explicit DocumentMarkerTextMatch(bool match)
85 PassRefPtr<DocumentMarkerTextMatch> DocumentMarkerTextMatch::instanceFor(bool match)
87 static DocumentMarkerTextMatch* trueInstance = adoptRef(new DocumentMarkerTextMatch(true)).leakRef();
88 static DocumentMarkerTextMatch* falseInstance = adoptRef(new DocumentMarkerTextMatch(false)).leakRef();
89 return match ? trueInstance : falseInstance;
92 inline DocumentMarkerTextMatch* toDocumentMarkerTextMatch(DocumentMarkerDetails* details)
94 if (details && details->isTextMatch())
95 return static_cast<DocumentMarkerTextMatch*>(details);
100 DocumentMarker::DocumentMarker()
101 : m_type(Spelling), m_startOffset(0), m_endOffset(0)
105 DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset)
106 : m_type(type), m_startOffset(startOffset), m_endOffset(endOffset)
110 DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset, const String& description)
112 , m_startOffset(startOffset)
113 , m_endOffset(endOffset)
114 , m_details(DocumentMarkerDescription::create(description))
119 DocumentMarker::DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch)
120 : m_type(DocumentMarker::TextMatch)
121 , m_startOffset(startOffset)
122 , m_endOffset(endOffset)
123 , m_details(DocumentMarkerTextMatch::instanceFor(activeMatch))
128 DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset, RefPtr<DocumentMarkerDetails>&& details)
130 , m_startOffset(startOffset)
131 , m_endOffset(endOffset)
137 void DocumentMarker::shiftOffsets(int delta)
139 m_startOffset += delta;
140 m_endOffset += delta;
143 void DocumentMarker::setActiveMatch(bool active)
145 m_details = DocumentMarkerTextMatch::instanceFor(active);
148 const String& DocumentMarker::description() const
150 if (DocumentMarkerDescription* details = toDocumentMarkerDescription(m_details.get()))
151 return details->description();
152 return emptyString();
155 bool DocumentMarker::activeMatch() const
157 if (DocumentMarkerTextMatch* details = toDocumentMarkerTextMatch(m_details.get()))
158 return details->activeMatch();
162 } // namespace WebCore