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

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

Issue 2780313002: [WIP] Refactor DocumentMarker (Closed)
Patch Set: Move CompositionMarkerList::at() into this CL Created 3 years, 8 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
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "core/editing/markers/DocumentMarker.h" 31 #include "core/editing/markers/DocumentMarker.h"
32 32
33 #include "wtf/StdLibExtras.h" 33 #include "wtf/StdLibExtras.h"
34 34
35 namespace blink { 35 namespace blink {
36 36
37 DocumentMarkerDetails::~DocumentMarkerDetails() {} 37 DocumentMarker::DocumentMarker(const DocumentMarker& marker)
38 : m_startOffset(marker.startOffset()), m_endOffset(marker.endOffset()) {}
38 39
39 class DocumentMarkerDescription final : public DocumentMarkerDetails { 40 DocumentMarker::~DocumentMarker() {}
40 public:
41 static DocumentMarkerDescription* create(const String&);
42 41
43 const String& description() const { return m_description; } 42 DocumentMarker::DocumentMarker(unsigned startOffset, unsigned endOffset)
44 bool isDescription() const override { return true; } 43 : m_startOffset(startOffset), m_endOffset(endOffset) {}
45
46 private:
47 explicit DocumentMarkerDescription(const String& description)
48 : m_description(description) {}
49
50 String m_description;
51 };
52
53 DocumentMarkerDescription* DocumentMarkerDescription::create(
54 const String& description) {
55 return new DocumentMarkerDescription(description);
56 }
57
58 inline DocumentMarkerDescription* toDocumentMarkerDescription(
59 DocumentMarkerDetails* details) {
60 if (details && details->isDescription())
61 return static_cast<DocumentMarkerDescription*>(details);
62 return 0;
63 }
64
65 class DocumentMarkerTextMatch final : public DocumentMarkerDetails {
66 public:
67 static DocumentMarkerTextMatch* create(bool);
68
69 bool activeMatch() const { return m_match; }
70 bool isTextMatch() const override { return true; }
71
72 private:
73 explicit DocumentMarkerTextMatch(bool match) : m_match(match) {}
74
75 bool m_match;
76 };
77
78 DocumentMarkerTextMatch* DocumentMarkerTextMatch::create(bool match) {
79 DEFINE_STATIC_LOCAL(DocumentMarkerTextMatch, trueInstance,
80 (new DocumentMarkerTextMatch(true)));
81 DEFINE_STATIC_LOCAL(DocumentMarkerTextMatch, falseInstance,
82 (new DocumentMarkerTextMatch(false)));
83 return match ? &trueInstance : &falseInstance;
84 }
85
86 inline DocumentMarkerTextMatch* toDocumentMarkerTextMatch(
87 DocumentMarkerDetails* details) {
88 if (details && details->isTextMatch())
89 return static_cast<DocumentMarkerTextMatch*>(details);
90 return 0;
91 }
92
93 class TextCompositionMarkerDetails final : public DocumentMarkerDetails {
94 public:
95 static TextCompositionMarkerDetails* create(Color underlineColor,
96 bool thick,
97 Color backgroundColor);
98
99 bool isComposition() const override { return true; }
100 Color underlineColor() const { return m_underlineColor; }
101 bool thick() const { return m_thick; }
102 Color backgroundColor() const { return m_backgroundColor; }
103
104 private:
105 TextCompositionMarkerDetails(Color underlineColor,
106 bool thick,
107 Color backgroundColor)
108 : m_underlineColor(underlineColor),
109 m_backgroundColor(backgroundColor),
110 m_thick(thick) {}
111
112 Color m_underlineColor;
113 Color m_backgroundColor;
114 bool m_thick;
115 };
116
117 TextCompositionMarkerDetails* TextCompositionMarkerDetails::create(
118 Color underlineColor,
119 bool thick,
120 Color backgroundColor) {
121 return new TextCompositionMarkerDetails(underlineColor, thick,
122 backgroundColor);
123 }
124
125 inline TextCompositionMarkerDetails* toTextCompositionMarkerDetails(
126 DocumentMarkerDetails* details) {
127 if (details && details->isComposition())
128 return static_cast<TextCompositionMarkerDetails*>(details);
129 return nullptr;
130 }
131
132 DocumentMarker* DocumentMarker::clone() const {
133 return new DocumentMarker(*this);
134 }
135
136 DocumentMarker::DocumentMarker(MarkerType type,
137 unsigned startOffset,
138 unsigned endOffset,
139 const String& description)
140 : m_type(type),
141 m_startOffset(startOffset),
142 m_endOffset(endOffset),
143 m_details(description.isEmpty()
144 ? nullptr
145 : DocumentMarkerDescription::create(description)) {
146 DCHECK(type != DocumentMarker::TextMatch);
147 }
148
149 DocumentMarker::DocumentMarker(unsigned startOffset,
150 unsigned endOffset,
151 bool activeMatch)
152 : m_type(DocumentMarker::TextMatch),
153 m_startOffset(startOffset),
154 m_endOffset(endOffset),
155 m_details(DocumentMarkerTextMatch::create(activeMatch)) {}
156
157 DocumentMarker::DocumentMarker(unsigned startOffset,
158 unsigned endOffset,
159 Color underlineColor,
160 bool thick,
161 Color backgroundColor)
162 : m_type(DocumentMarker::Composition),
163 m_startOffset(startOffset),
164 m_endOffset(endOffset),
165 m_details(TextCompositionMarkerDetails::create(underlineColor,
166 thick,
167 backgroundColor)) {}
168
169 DocumentMarker::DocumentMarker(const DocumentMarker& marker)
170 : m_type(marker.type()),
171 m_startOffset(marker.startOffset()),
172 m_endOffset(marker.endOffset()),
173 m_details(marker.details()) {}
174 44
175 Optional<DocumentMarker::MarkerOffsets> 45 Optional<DocumentMarker::MarkerOffsets>
176 DocumentMarker::computeOffsetsAfterShift(unsigned offset, 46 DocumentMarker::computeOffsetsAfterShift(unsigned offset,
177 unsigned oldLength, 47 unsigned oldLength,
178 unsigned newLength) const { 48 unsigned newLength) const {
179 MarkerOffsets result; 49 MarkerOffsets result;
180 result.startOffset = startOffset(); 50 result.startOffset = startOffset();
181 result.endOffset = endOffset(); 51 result.endOffset = endOffset();
182 52
183 // algorithm inspired by https://dom.spec.whatwg.org/#concept-cd-replace 53 // algorithm inspired by https://dom.spec.whatwg.org/#concept-cd-replace
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 return WTF::nullopt; 86 return WTF::nullopt;
217 87
218 return result; 88 return result;
219 } 89 }
220 90
221 void DocumentMarker::shiftOffsets(int delta) { 91 void DocumentMarker::shiftOffsets(int delta) {
222 m_startOffset += delta; 92 m_startOffset += delta;
223 m_endOffset += delta; 93 m_endOffset += delta;
224 } 94 }
225 95
226 void DocumentMarker::setActiveMatch(bool active) {
227 m_details = DocumentMarkerTextMatch::create(active);
228 }
229
230 const String& DocumentMarker::description() const { 96 const String& DocumentMarker::description() const {
231 if (DocumentMarkerDescription* details =
232 toDocumentMarkerDescription(m_details.get()))
233 return details->description();
234 return emptyString; 97 return emptyString;
235 } 98 }
236 99
237 bool DocumentMarker::activeMatch() const { 100 bool DocumentMarker::activeMatch() const {
238 if (DocumentMarkerTextMatch* details =
239 toDocumentMarkerTextMatch(m_details.get()))
240 return details->activeMatch();
241 return false; 101 return false;
242 } 102 }
243 103
244 Color DocumentMarker::underlineColor() const { 104 Color DocumentMarker::underlineColor() const {
245 if (TextCompositionMarkerDetails* details =
246 toTextCompositionMarkerDetails(m_details.get()))
247 return details->underlineColor();
248 return Color::transparent; 105 return Color::transparent;
249 } 106 }
250 107
251 bool DocumentMarker::thick() const { 108 bool DocumentMarker::thick() const {
252 if (TextCompositionMarkerDetails* details =
253 toTextCompositionMarkerDetails(m_details.get()))
254 return details->thick();
255 return false; 109 return false;
256 } 110 }
257 111
258 Color DocumentMarker::backgroundColor() const { 112 Color DocumentMarker::backgroundColor() const {
259 if (TextCompositionMarkerDetails* details =
260 toTextCompositionMarkerDetails(m_details.get()))
261 return details->backgroundColor();
262 return Color::transparent; 113 return Color::transparent;
263 } 114 }
264 115
265 DEFINE_TRACE(DocumentMarker) {
266 visitor->trace(m_details);
267 }
268
269 } // namespace blink 116 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698