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

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

Issue 2393403002: Introduce Selection class (Closed)
Patch Set: 2016-10-12T15:57:57 Created 4 years, 2 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
(Empty)
1 // Copyright 2016 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 #include "core/editing/SelectionTemplate.h"
6
7 #include "wtf/Assertions.h"
8 #include <ostream> // NOLINT
9
10 namespace blink {
11
12 template <typename Strategy>
13 SelectionTemplate<Strategy>::SelectionTemplate(const SelectionTemplate& other)
14 : m_base(other.m_base),
15 m_extent(other.m_extent),
16 m_affinity(other.m_affinity),
17 m_granularity(other.m_granularity),
18 m_hasTrailingWhitespace(other.m_hasTrailingWhitespace),
19 m_isDirectional(other.m_isDirectional)
20 #if DCHECK_IS_ON()
21 ,
22 m_domTreeVersion(other.m_domTreeVersion)
23 #endif
24 {
25 DCHECK(other.assertValid());
26 if (!m_hasTrailingWhitespace)
27 return;
28 DCHECK_EQ(m_granularity, WordGranularity) << *this;
29 }
30
31 template <typename Strategy>
32 SelectionTemplate<Strategy>::SelectionTemplate() = default;
33
34 template <typename Strategy>
35 bool SelectionTemplate<Strategy>::operator==(
36 const SelectionTemplate& other) const {
37 DCHECK(assertValid());
38 DCHECK(other.assertValid());
39 if (isNone())
40 return other.isNone();
41 if (other.isNone())
42 return false;
43 DCHECK_EQ(m_base.document(), other.document()) << *this << ' ' << other;
44 return m_base == other.m_base && m_extent == other.m_extent &&
45 m_affinity == other.m_affinity &&
46 m_granularity == other.m_granularity &&
47 m_hasTrailingWhitespace == other.m_hasTrailingWhitespace &&
48 m_isDirectional == other.m_isDirectional;
49 }
50
51 template <typename Strategy>
52 bool SelectionTemplate<Strategy>::operator!=(
53 const SelectionTemplate& other) const {
54 return !operator==(other);
55 }
56
57 template <typename Strategy>
58 const PositionTemplate<Strategy>& SelectionTemplate<Strategy>::base() const {
59 DCHECK(assertValid());
60 DCHECK(!m_base.isOrphan()) << m_base;
61 return m_base;
62 }
63
64 template <typename Strategy>
65 Document* SelectionTemplate<Strategy>::document() const {
66 DCHECK(assertValid());
67 return m_base.document();
68 }
69
70 template <typename Strategy>
71 const PositionTemplate<Strategy>& SelectionTemplate<Strategy>::extent() const {
72 DCHECK(assertValid());
73 DCHECK(!m_extent.isOrphan()) << m_extent;
74 return m_extent;
75 }
76
77 template <typename Strategy>
78 bool SelectionTemplate<Strategy>::assertValidFor(
79 const Document& document) const {
80 if (!assertValid())
81 return false;
82 if (m_base.isNull())
83 return true;
84 DCHECK_EQ(m_base.document(), document) << *this;
85 return true;
86 }
87
88 #if DCHECK_IS_ON()
89 template <typename Strategy>
90 bool SelectionTemplate<Strategy>::assertValid() const {
91 if (m_base.isNull())
92 return true;
93 DCHECK_EQ(m_base.document()->domTreeVersion(), m_domTreeVersion) << *this;
94 DCHECK(!m_base.isOrphan()) << *this;
95 DCHECK(!m_extent.isOrphan()) << *this;
96 DCHECK_EQ(m_base.document(), m_extent.document());
97 return true;
98 }
99 #else
100 template <typename Strategy>
101 bool SelectionTemplate<Strategy>::assertValid() const {
102 return true;
103 }
104 #endif
105
106 template <typename Strategy>
107 void SelectionTemplate<Strategy>::printTo(std::ostream* ostream,
108 const char* type) const {
109 if (isNone()) {
110 *ostream << "()";
111 return;
112 }
113 *ostream << type << '(';
114 #if DCHECK_IS_ON()
115 if (m_domTreeVersion != m_base.document()->domTreeVersion()) {
116 *ostream << "Dirty: " << m_domTreeVersion;
117 *ostream << " != " << m_base.document()->domTreeVersion() << ' ';
118 }
119 #endif
120 *ostream << "base: " << m_base << ", extent: " << m_extent << ')';
121 }
122
123 std::ostream& operator<<(std::ostream& ostream,
124 const SelectionInDOMTree& selection) {
125 selection.printTo(&ostream, "Selection");
126 return ostream;
127 }
128
129 std::ostream& operator<<(std::ostream& ostream,
130 const SelectionInFlatTree& selection) {
131 selection.printTo(&ostream, "SelectionInFlatTree");
132 return ostream;
133 }
134
135 // --
136
137 template <typename Strategy>
138 SelectionTemplate<Strategy>::Builder::Builder(
139 const SelectionTemplate<Strategy>& selection)
140 : m_selection(selection) {}
141
142 template <typename Strategy>
143 SelectionTemplate<Strategy>::Builder::Builder() = default;
144
145 template <typename Strategy>
146 SelectionTemplate<Strategy> SelectionTemplate<Strategy>::Builder::build()
147 const {
148 DCHECK(m_selection.assertValid());
149 return m_selection;
150 }
151
152 template <typename Strategy>
153 typename SelectionTemplate<Strategy>::Builder&
154 SelectionTemplate<Strategy>::Builder::collapse(
155 const PositionTemplate<Strategy>& position) {
156 DCHECK(position.isConnected());
157 m_selection.m_base = position;
158 m_selection.m_extent = position;
159 #if DCHECK_IS_ON()
160 m_selection.m_domTreeVersion = position.document()->domTreeVersion();
161 #endif
162 return *this;
163 }
164
165 template <typename Strategy>
166 typename SelectionTemplate<Strategy>::Builder&
167 SelectionTemplate<Strategy>::Builder::collapse(
168 const PositionWithAffinityTemplate<Strategy>& positionWithAffinity) {
169 collapse(positionWithAffinity.position());
170 setAffinity(positionWithAffinity.affinity());
171 return *this;
172 }
173
174 template <typename Strategy>
175 typename SelectionTemplate<Strategy>::Builder&
176 SelectionTemplate<Strategy>::Builder::extend(
177 const PositionTemplate<Strategy>& position) {
178 DCHECK(position.isConnected());
179 DCHECK_EQ(m_selection.document(), position.document());
180 DCHECK(m_selection.base().isConnected());
181 DCHECK(m_selection.assertValid());
182 m_selection.m_extent = position;
183 return *this;
184 }
185
186 template <typename Strategy>
187 typename SelectionTemplate<Strategy>::Builder&
188 SelectionTemplate<Strategy>::Builder::setAffinity(TextAffinity affinity) {
189 m_selection.m_affinity = affinity;
190 return *this;
191 }
192
193 template <typename Strategy>
194 typename SelectionTemplate<Strategy>::Builder&
195 SelectionTemplate<Strategy>::Builder::setBaseAndExtent(
196 const EphemeralRangeTemplate<Strategy>& range) {
197 if (range.isNull()) {
198 m_selection.m_base = PositionTemplate<Strategy>();
199 m_selection.m_extent = PositionTemplate<Strategy>();
200 #if DCHECK_IS_ON()
201 m_selection.m_domTreeVersion = 0;
202 #endif
203 return *this;
204 }
205 return collapse(range.startPosition()).extend(range.endPosition());
206 }
207
208 template <typename Strategy>
209 typename SelectionTemplate<Strategy>::Builder&
210 SelectionTemplate<Strategy>::Builder::setBaseAndExtent(
211 const PositionTemplate<Strategy>& base,
212 const PositionTemplate<Strategy>& extent) {
213 if (base.isNull()) {
214 DCHECK(extent.isNull()) << extent;
215 return setBaseAndExtent(EphemeralRangeTemplate<Strategy>());
216 }
217 DCHECK(extent.isNotNull());
218 return collapse(base).extend(extent);
219 }
220
221 template <typename Strategy>
222 typename SelectionTemplate<Strategy>::Builder&
223 SelectionTemplate<Strategy>::Builder::setBaseAndExtentDeprecated(
224 const PositionTemplate<Strategy>& base,
225 const PositionTemplate<Strategy>& extent) {
226 if (base.isNotNull() && extent.isNotNull()) {
227 return setBaseAndExtent(base, extent);
228 }
229 if (base.isNotNull())
230 return collapse(base);
231 if (extent.isNotNull())
232 return collapse(extent);
233 return setBaseAndExtent(EphemeralRangeTemplate<Strategy>());
234 }
235
236 template <typename Strategy>
237 typename SelectionTemplate<Strategy>::Builder&
238 SelectionTemplate<Strategy>::Builder::setGranularity(
239 TextGranularity granularity) {
240 m_selection.m_granularity = granularity;
241 return *this;
242 }
243
244 template <typename Strategy>
245 typename SelectionTemplate<Strategy>::Builder&
246 SelectionTemplate<Strategy>::Builder::setHasTrailingWhitespace(
247 bool hasTrailingWhitespace) {
248 m_selection.m_hasTrailingWhitespace = hasTrailingWhitespace;
249 return *this;
250 }
251
252 template <typename Strategy>
253 typename SelectionTemplate<Strategy>::Builder&
254 SelectionTemplate<Strategy>::Builder::setIsDirectional(bool isDirectional) {
255 m_selection.m_isDirectional = isDirectional;
256 return *this;
257 }
258
259 template class CORE_TEMPLATE_EXPORT SelectionTemplate<EditingStrategy>;
260 template class CORE_TEMPLATE_EXPORT
261 SelectionTemplate<EditingInFlatTreeStrategy>;
262
263 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698