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/Selection.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 ENABLE(ASSERT) | |
|
tkent
2016/10/07 00:13:27
ENABLE(ASSERT) -> 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 ENABLE(ASSERT) | |
|
tkent
2016/10/07 00:13:27
ENABLE(ASSERT) -> 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 ENABLE(ASSERT) | |
|
tkent
2016/10/07 00:13:27
ENABLE(ASSERT) -> 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, const Selection& selection) { | |
| 118 selection.printTo(&ostream, "Selection"); | |
| 119 return ostream; | |
| 120 } | |
| 121 | |
| 122 std::ostream& operator<<(std::ostream& ostream, | |
| 123 const SelectionInFlatTree& selection) { | |
| 124 selection.printTo(&ostream, "SelectionInFlatTree"); | |
| 125 return ostream; | |
| 126 } | |
| 127 | |
| 128 // -- | |
| 129 | |
| 130 template <typename Strategy> | |
| 131 SelectionTemplate<Strategy>::Builder::Builder( | |
| 132 const SelectionTemplate<Strategy>& selection) | |
| 133 : m_selection(selection) {} | |
| 134 | |
| 135 template <typename Strategy> | |
| 136 SelectionTemplate<Strategy>::Builder::Builder() = default; | |
| 137 | |
| 138 template <typename Strategy> | |
| 139 SelectionTemplate<Strategy> SelectionTemplate<Strategy>::Builder::build() | |
| 140 const { | |
| 141 return m_selection; | |
| 142 } | |
| 143 | |
| 144 template <typename Strategy> | |
| 145 typename SelectionTemplate<Strategy>::Builder& | |
| 146 SelectionTemplate<Strategy>::Builder::collapse( | |
| 147 const PositionTemplate<Strategy>& position) { | |
| 148 DCHECK(position.isNotNull()); | |
| 149 m_selection.m_base = position; | |
| 150 m_selection.m_extent = position; | |
| 151 #if ENABLE(ASSERT) | |
|
tkent
2016/10/07 00:13:27
ENABLE(ASSERT) -> DCHECK_IS_ON()
| |
| 152 m_selection.m_domTreeVersion = position.document()->domTreeVersion(); | |
| 153 #endif | |
| 154 return *this; | |
| 155 } | |
| 156 | |
| 157 template <typename Strategy> | |
| 158 typename SelectionTemplate<Strategy>::Builder& | |
| 159 SelectionTemplate<Strategy>::Builder::collapse( | |
| 160 const PositionWithAffinityTemplate<Strategy>& positionWithAffinity) { | |
| 161 collapse(positionWithAffinity.position()); | |
| 162 setAffinity(positionWithAffinity.affinity()); | |
| 163 return *this; | |
| 164 } | |
| 165 | |
| 166 template <typename Strategy> | |
| 167 typename SelectionTemplate<Strategy>::Builder& | |
| 168 SelectionTemplate<Strategy>::Builder::extend( | |
|
Xiaocheng
2016/10/06 08:02:33
We should check m_selection's validity to ensure t
| |
| 169 const PositionTemplate<Strategy>& position) { | |
| 170 DCHECK(position.isNotNull()); | |
| 171 DCHECK(m_selection.base().isNotNull()); | |
| 172 m_selection.m_extent = position; | |
| 173 return *this; | |
| 174 } | |
| 175 | |
| 176 template <typename Strategy> | |
| 177 typename SelectionTemplate<Strategy>::Builder& | |
| 178 SelectionTemplate<Strategy>::Builder::setAffinity(TextAffinity affinity) { | |
| 179 m_selection.m_affinity = affinity; | |
| 180 return *this; | |
| 181 } | |
| 182 | |
| 183 template <typename Strategy> | |
| 184 typename SelectionTemplate<Strategy>::Builder& | |
| 185 SelectionTemplate<Strategy>::Builder::setBaseAndExtent( | |
| 186 const EphemeralRangeTemplate<Strategy>& range) { | |
| 187 if (range.isNull()) { | |
| 188 m_selection.m_base = PositionTemplate<Strategy>(); | |
| 189 m_selection.m_extent = PositionTemplate<Strategy>(); | |
| 190 #if ENABLE(ASSERT) | |
|
tkent
2016/10/07 00:13:27
ENABLE(ASSERT) -> DCHECK_IS_ON()
| |
| 191 m_selection.m_domTreeVersion = 0; | |
| 192 #endif | |
| 193 return *this; | |
| 194 } | |
| 195 return collapse(range.startPosition()).extend(range.endPosition()); | |
| 196 } | |
| 197 | |
| 198 template <typename Strategy> | |
| 199 typename SelectionTemplate<Strategy>::Builder& | |
| 200 SelectionTemplate<Strategy>::Builder::setBaseAndExtent( | |
| 201 const PositionTemplate<Strategy>& base, | |
| 202 const PositionTemplate<Strategy>& extent) { | |
| 203 if (base.isNull()) { | |
| 204 DCHECK(extent.isNull()) << extent; | |
| 205 return setBaseAndExtent(EphemeralRangeTemplate<Strategy>()); | |
| 206 } | |
| 207 DCHECK(extent.isNotNull()); | |
| 208 return collapse(base).extend(extent); | |
| 209 } | |
| 210 | |
| 211 template <typename Strategy> | |
| 212 typename SelectionTemplate<Strategy>::Builder& | |
| 213 SelectionTemplate<Strategy>::Builder::setBaseAndExtentDeprecated( | |
| 214 const PositionTemplate<Strategy>& base, | |
| 215 const PositionTemplate<Strategy>& extent) { | |
| 216 if (base.isNotNull() && extent.isNotNull()) { | |
| 217 return setBaseAndExtent(base, extent); | |
| 218 } | |
| 219 if (base.isNotNull()) | |
| 220 return collapse(base); | |
| 221 if (extent.isNotNull()) | |
| 222 return collapse(extent); | |
| 223 m_selection.m_base = PositionTemplate<Strategy>(); | |
| 224 m_selection.m_extent = PositionTemplate<Strategy>(); | |
| 225 return *this; | |
| 226 } | |
| 227 | |
| 228 template <typename Strategy> | |
| 229 typename SelectionTemplate<Strategy>::Builder& | |
| 230 SelectionTemplate<Strategy>::Builder::setGranularity( | |
| 231 TextGranularity granularity) { | |
| 232 m_selection.m_granularity = granularity; | |
| 233 return *this; | |
| 234 } | |
| 235 | |
| 236 template <typename Strategy> | |
| 237 typename SelectionTemplate<Strategy>::Builder& | |
| 238 SelectionTemplate<Strategy>::Builder::setHasTrailingWhitespace( | |
| 239 bool hasTrailingWhitespace) { | |
| 240 m_selection.m_hasTrailingWhitespace = hasTrailingWhitespace; | |
| 241 return *this; | |
| 242 } | |
| 243 | |
| 244 template <typename Strategy> | |
| 245 typename SelectionTemplate<Strategy>::Builder& | |
| 246 SelectionTemplate<Strategy>::Builder::setIsDirectional(bool isDirectional) { | |
| 247 m_selection.m_isDirectional = isDirectional; | |
| 248 return *this; | |
| 249 } | |
| 250 | |
| 251 template class CORE_TEMPLATE_EXPORT SelectionTemplate<EditingStrategy>; | |
| 252 template class CORE_TEMPLATE_EXPORT | |
| 253 SelectionTemplate<EditingInFlatTreeStrategy>; | |
| 254 | |
| 255 } // namespace blink | |
| OLD | NEW |