Chromium Code Reviews| OLD | NEW |
|---|---|
| (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; | |
|
yoichio
2016/10/12 01:37:00
DCHECK(m_selection.assertValid()) ?
yosin_UTC9
2016/10/12 03:39:02
Good catch! Done.
| |
| 143 } | |
| 144 | |
| 145 template <typename Strategy> | |
| 146 typename SelectionTemplate<Strategy>::Builder& | |
| 147 SelectionTemplate<Strategy>::Builder::collapse( | |
| 148 const PositionTemplate<Strategy>& position) { | |
| 149 DCHECK(position.isConnected()); | |
| 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( | |
| 170 const PositionTemplate<Strategy>& position) { | |
| 171 DCHECK(position.isConnected()); | |
| 172 DCHECK(m_selection.base().isConnected()); | |
| 173 m_selection.assertValid(); | |
| 174 m_selection.m_extent = position; | |
| 175 return *this; | |
| 176 } | |
| 177 | |
| 178 template <typename Strategy> | |
| 179 typename SelectionTemplate<Strategy>::Builder& | |
| 180 SelectionTemplate<Strategy>::Builder::setAffinity(TextAffinity affinity) { | |
| 181 m_selection.m_affinity = affinity; | |
| 182 return *this; | |
| 183 } | |
| 184 | |
| 185 template <typename Strategy> | |
| 186 typename SelectionTemplate<Strategy>::Builder& | |
| 187 SelectionTemplate<Strategy>::Builder::setBaseAndExtent( | |
| 188 const EphemeralRangeTemplate<Strategy>& range) { | |
| 189 if (range.isNull()) { | |
|
yoichio
2016/10/12 01:37:00
Do we need this kind of resetting?
yosin_UTC9
2016/10/12 03:39:02
Yes. There are usages in SelectionEditor::pdateCac
| |
| 190 m_selection.m_base = PositionTemplate<Strategy>(); | |
| 191 m_selection.m_extent = PositionTemplate<Strategy>(); | |
| 192 #if DCHECK_IS_ON() | |
| 193 m_selection.m_domTreeVersion = 0; | |
| 194 #endif | |
| 195 return *this; | |
| 196 } | |
| 197 return collapse(range.startPosition()).extend(range.endPosition()); | |
| 198 } | |
| 199 | |
| 200 template <typename Strategy> | |
| 201 typename SelectionTemplate<Strategy>::Builder& | |
| 202 SelectionTemplate<Strategy>::Builder::setBaseAndExtent( | |
| 203 const PositionTemplate<Strategy>& base, | |
| 204 const PositionTemplate<Strategy>& extent) { | |
| 205 if (base.isNull()) { | |
|
yoichio
2016/10/12 01:37:00
ditto
yosin_UTC9
2016/10/12 03:39:02
See a reply in EphemeralRange version of setBaseAn
| |
| 206 DCHECK(extent.isNull()) << extent; | |
| 207 return setBaseAndExtent(EphemeralRangeTemplate<Strategy>()); | |
| 208 } | |
| 209 DCHECK(extent.isNotNull()); | |
| 210 return collapse(base).extend(extent); | |
|
yosin_UTC9
2016/10/12 03:39:02
Note: |base| can be following to |extent|
| |
| 211 } | |
| 212 | |
| 213 template <typename Strategy> | |
| 214 typename SelectionTemplate<Strategy>::Builder& | |
| 215 SelectionTemplate<Strategy>::Builder::setBaseAndExtentDeprecated( | |
| 216 const PositionTemplate<Strategy>& base, | |
| 217 const PositionTemplate<Strategy>& extent) { | |
| 218 if (base.isNotNull() && extent.isNotNull()) { | |
| 219 return setBaseAndExtent(base, extent); | |
| 220 } | |
| 221 if (base.isNotNull()) | |
| 222 return collapse(base); | |
| 223 if (extent.isNotNull()) | |
| 224 return collapse(extent); | |
| 225 m_selection.m_base = PositionTemplate<Strategy>(); | |
| 226 m_selection.m_extent = PositionTemplate<Strategy>(); | |
| 227 return *this; | |
| 228 } | |
| 229 | |
| 230 template <typename Strategy> | |
| 231 typename SelectionTemplate<Strategy>::Builder& | |
| 232 SelectionTemplate<Strategy>::Builder::setGranularity( | |
| 233 TextGranularity granularity) { | |
| 234 m_selection.m_granularity = granularity; | |
| 235 return *this; | |
| 236 } | |
| 237 | |
| 238 template <typename Strategy> | |
| 239 typename SelectionTemplate<Strategy>::Builder& | |
| 240 SelectionTemplate<Strategy>::Builder::setHasTrailingWhitespace( | |
| 241 bool hasTrailingWhitespace) { | |
| 242 m_selection.m_hasTrailingWhitespace = hasTrailingWhitespace; | |
| 243 return *this; | |
| 244 } | |
| 245 | |
| 246 template <typename Strategy> | |
| 247 typename SelectionTemplate<Strategy>::Builder& | |
| 248 SelectionTemplate<Strategy>::Builder::setIsDirectional(bool isDirectional) { | |
| 249 m_selection.m_isDirectional = isDirectional; | |
| 250 return *this; | |
| 251 } | |
| 252 | |
| 253 template class CORE_TEMPLATE_EXPORT SelectionTemplate<EditingStrategy>; | |
| 254 template class CORE_TEMPLATE_EXPORT | |
| 255 SelectionTemplate<EditingInFlatTreeStrategy>; | |
| 256 | |
| 257 } // namespace blink | |
| OLD | NEW |