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

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

Issue 2393403002: Introduce Selection class (Closed)
Patch Set: 2016-10-07T17:20:15 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 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 assertValid();
38 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 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 assertValid();
67 return m_base.document();
68 }
69
70 template <typename Strategy>
71 const PositionTemplate<Strategy>& SelectionTemplate<Strategy>::extent() const {
72 assertValid();
73 DCHECK(!m_extent.isOrphan()) << m_extent;
74 return m_extent;
75 }
76
77 template <typename Strategy>
78 void SelectionTemplate<Strategy>::assertValidFor(
79 const Document& document) const {
80 assertValid();
81 if (m_base.isNull())
82 return;
83 DCHECK_EQ(m_base.document(), document) << *this;
84 }
85
86 #if DCHECK_IS_ON()
87 template <typename Strategy>
88 void SelectionTemplate<Strategy>::assertValid() const {
89 if (m_base.isNull())
90 return;
91 DCHECK_EQ(m_base.document()->domTreeVersion(), m_domTreeVersion) << *this;
92 DCHECK(!m_base.isOrphan()) << *this;
93 DCHECK(!m_extent.isOrphan()) << *this;
94 }
95 #else
96 template <typename Strategy>
97 void SelectionTemplate<Strategy>::assertValid() const {}
98 #endif
99
100 template <typename Strategy>
101 void SelectionTemplate<Strategy>::printTo(std::ostream* ostream,
102 const char* type) const {
103 if (isNone()) {
104 *ostream << "()";
105 return;
106 }
107 *ostream << type << '(';
108 #if DCHECK_IS_ON()
109 if (m_domTreeVersion != m_base.document()->domTreeVersion()) {
110 *ostream << "Dirty: " << m_domTreeVersion;
111 *ostream << " != " << m_base.document()->domTreeVersion() << ' ';
112 }
113 #endif
114 *ostream << "base: " << m_base << ", extent: " << m_extent << ')';
115 }
116
117 std::ostream& operator<<(std::ostream& ostream,
118 const SelectionInDOMTree& selection) {
119 selection.printTo(&ostream, "Selection");
120 return ostream;
121 }
122
123 std::ostream& operator<<(std::ostream& ostream,
124 const SelectionInFlatTree& selection) {
125 selection.printTo(&ostream, "SelectionInFlatTree");
126 return ostream;
127 }
128
129 // --
130
131 template <typename Strategy>
132 SelectionTemplate<Strategy>::Builder::Builder(
133 const SelectionTemplate<Strategy>& selection)
134 : m_selection(selection) {}
135
136 template <typename Strategy>
137 SelectionTemplate<Strategy>::Builder::Builder() = default;
138
139 template <typename Strategy>
140 SelectionTemplate<Strategy> SelectionTemplate<Strategy>::Builder::build()
141 const {
142 return m_selection;
143 }
144
145 template <typename Strategy>
146 typename SelectionTemplate<Strategy>::Builder&
147 SelectionTemplate<Strategy>::Builder::collapse(
148 const PositionTemplate<Strategy>& position) {
149 DCHECK(position.isNotNull());
150 m_selection.m_base = position;
151 m_selection.m_extent = position;
152 #if DCHECK_IS_ON()
153 m_selection.m_domTreeVersion = position.document()->domTreeVersion();
154 #endif
155 return *this;
156 }
157
158 template <typename Strategy>
159 typename SelectionTemplate<Strategy>::Builder&
160 SelectionTemplate<Strategy>::Builder::collapse(
161 const PositionWithAffinityTemplate<Strategy>& positionWithAffinity) {
162 collapse(positionWithAffinity.position());
163 setAffinity(positionWithAffinity.affinity());
164 return *this;
165 }
166
167 template <typename Strategy>
168 typename SelectionTemplate<Strategy>::Builder&
169 SelectionTemplate<Strategy>::Builder::extend(
Xiaocheng 2016/10/07 09:00:52 We should call |m_selection.assertValid()| in this
yosin_UTC9 2016/10/07 09:22:55 Good catch! I forgot to do so. Done.
170 const PositionTemplate<Strategy>& position) {
171 DCHECK(position.isNotNull());
172 DCHECK(m_selection.base().isNotNull());
173 m_selection.m_extent = position;
174 return *this;
175 }
176
177 template <typename Strategy>
178 typename SelectionTemplate<Strategy>::Builder&
179 SelectionTemplate<Strategy>::Builder::setAffinity(TextAffinity affinity) {
180 m_selection.m_affinity = affinity;
181 return *this;
182 }
183
184 template <typename Strategy>
185 typename SelectionTemplate<Strategy>::Builder&
186 SelectionTemplate<Strategy>::Builder::setBaseAndExtent(
187 const EphemeralRangeTemplate<Strategy>& range) {
188 if (range.isNull()) {
189 m_selection.m_base = PositionTemplate<Strategy>();
190 m_selection.m_extent = PositionTemplate<Strategy>();
191 #if DCHECK_IS_ON()
192 m_selection.m_domTreeVersion = 0;
193 #endif
194 return *this;
195 }
196 return collapse(range.startPosition()).extend(range.endPosition());
197 }
198
199 template <typename Strategy>
200 typename SelectionTemplate<Strategy>::Builder&
201 SelectionTemplate<Strategy>::Builder::setBaseAndExtent(
202 const PositionTemplate<Strategy>& base,
203 const PositionTemplate<Strategy>& extent) {
204 if (base.isNull()) {
205 DCHECK(extent.isNull()) << extent;
206 return setBaseAndExtent(EphemeralRangeTemplate<Strategy>());
207 }
208 DCHECK(extent.isNotNull());
209 return collapse(base).extend(extent);
210 }
211
212 template <typename Strategy>
213 typename SelectionTemplate<Strategy>::Builder&
214 SelectionTemplate<Strategy>::Builder::setBaseAndExtentDeprecated(
215 const PositionTemplate<Strategy>& base,
216 const PositionTemplate<Strategy>& extent) {
217 if (base.isNotNull() && extent.isNotNull()) {
218 return setBaseAndExtent(base, extent);
219 }
220 if (base.isNotNull())
221 return collapse(base);
222 if (extent.isNotNull())
223 return collapse(extent);
224 m_selection.m_base = PositionTemplate<Strategy>();
225 m_selection.m_extent = PositionTemplate<Strategy>();
226 return *this;
227 }
228
229 template <typename Strategy>
230 typename SelectionTemplate<Strategy>::Builder&
231 SelectionTemplate<Strategy>::Builder::setGranularity(
232 TextGranularity granularity) {
233 m_selection.m_granularity = granularity;
234 return *this;
235 }
236
237 template <typename Strategy>
238 typename SelectionTemplate<Strategy>::Builder&
239 SelectionTemplate<Strategy>::Builder::setHasTrailingWhitespace(
240 bool hasTrailingWhitespace) {
241 m_selection.m_hasTrailingWhitespace = hasTrailingWhitespace;
242 return *this;
243 }
244
245 template <typename Strategy>
246 typename SelectionTemplate<Strategy>::Builder&
247 SelectionTemplate<Strategy>::Builder::setIsDirectional(bool isDirectional) {
248 m_selection.m_isDirectional = isDirectional;
249 return *this;
250 }
251
252 template class CORE_TEMPLATE_EXPORT SelectionTemplate<EditingStrategy>;
253 template class CORE_TEMPLATE_EXPORT
254 SelectionTemplate<EditingInFlatTreeStrategy>;
255
256 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698