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 #ifndef SelectionTemplate_h | |
| 6 #define SelectionTemplate_h | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "core/CoreExport.h" | |
| 10 #include "core/editing/EphemeralRange.h" | |
| 11 #include "core/editing/Position.h" | |
| 12 #include "core/editing/PositionWithAffinity.h" | |
| 13 #include "core/editing/TextAffinity.h" | |
| 14 #include "core/editing/TextGranularity.h" | |
| 15 #include "wtf/Allocator.h" | |
| 16 #include <iosfwd> | |
| 17 | |
| 18 namespace blink { | |
| 19 | |
| 20 // SelectionTemplate is used for representing a selection in DOM tree or Flat | |
| 21 // tree with template parameter |Strategy|. Instances of |SelectionTemplate| | |
| 22 // are immutable objects, you can't change them once constructed. | |
| 23 // | |
| 24 // To construct |SelectionTemplate| object, please use |Builder| class. | |
| 25 template <typename Strategy> | |
| 26 class CORE_EXPORT SelectionTemplate final { | |
| 27 DISALLOW_NEW(); | |
| 28 | |
| 29 public: | |
| 30 // |Builder| is a helper class for constructing |SelectionTemplate| object. | |
| 31 class CORE_EXPORT Builder final { | |
| 32 DISALLOW_NEW(); | |
| 33 | |
| 34 public: | |
| 35 explicit Builder(const SelectionTemplate&); | |
| 36 Builder(); | |
| 37 | |
| 38 SelectionTemplate build() const; | |
| 39 | |
| 40 // Move selection to |base|. |base| can't be null. | |
| 41 Builder& collapse(const PositionTemplate<Strategy>& base); | |
| 42 Builder& collapse(const PositionWithAffinityTemplate<Strategy>& base); | |
| 43 | |
| 44 // Extend selection to |extent|. It is error if selection is none. | |
| 45 Builder& extend(const PositionTemplate<Strategy>& extent); | |
| 46 | |
| 47 Builder& setBaseAndExtent(const EphemeralRangeTemplate<Strategy>&); | |
| 48 | |
| 49 // |extent| can not be null if |base| isn't null. | |
| 50 Builder& setBaseAndExtent(const PositionTemplate<Strategy>& base, | |
| 51 const PositionTemplate<Strategy>& extent); | |
| 52 | |
| 53 // |extent| can be non-null while |base| is null, which is undesired. | |
| 54 // Note: This function should be used only in "core/editing/commands". | |
| 55 Builder& setBaseAndExtentDeprecated( | |
| 56 const PositionTemplate<Strategy>& base, | |
| 57 const PositionTemplate<Strategy>& extent); | |
| 58 | |
| 59 Builder& setAffinity(TextAffinity); | |
| 60 Builder& setGranularity(TextGranularity); | |
| 61 Builder& setHasTrailingWhitespace(bool); | |
| 62 Builder& setIsDirectional(bool); | |
| 63 | |
| 64 private: | |
| 65 SelectionTemplate m_selection; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(Builder); | |
| 68 }; | |
| 69 | |
| 70 SelectionTemplate(const SelectionTemplate& other); | |
| 71 SelectionTemplate(); | |
| 72 | |
| 73 SelectionTemplate& operator=(const SelectionTemplate&) = default; | |
| 74 | |
| 75 bool operator==(const SelectionTemplate&) const; | |
| 76 bool operator!=(const SelectionTemplate&) const; | |
| 77 | |
| 78 const PositionTemplate<Strategy>& base() const; | |
| 79 const PositionTemplate<Strategy>& extent() const; | |
| 80 TextAffinity affinity() const { return m_affinity; } | |
| 81 TextGranularity granularity() const { return m_granularity; } | |
| 82 bool hasTrailingWhitespace() const { return m_hasTrailingWhitespace; } | |
| 83 bool isDirectional() const { return m_isDirectional; } | |
| 84 bool isNone() const { return m_base.isNull(); } | |
| 85 | |
| 86 // Returns true if |this| selection holds valid values otherwise it causes, | |
|
Xiaocheng
2016/10/12 05:09:42
nit: weird position of the comma.
yosin_UTC9
2016/10/12 06:22:54
Done.
| |
| 87 // assertion failure. | |
| 88 bool assertValid() const; | |
| 89 bool assertValidFor(const Document&) const; | |
| 90 | |
| 91 DEFINE_INLINE_TRACE() { | |
| 92 visitor->trace(m_base); | |
| 93 visitor->trace(m_extent); | |
| 94 } | |
| 95 | |
| 96 void printTo(std::ostream*, const char* type) const; | |
| 97 | |
| 98 private: | |
| 99 friend class SelectionEditor; | |
| 100 | |
| 101 Document* document() const; | |
| 102 | |
| 103 PositionTemplate<Strategy> m_base; | |
| 104 PositionTemplate<Strategy> m_extent; | |
| 105 TextAffinity m_affinity = TextAffinity::Downstream; | |
| 106 TextGranularity m_granularity = CharacterGranularity; | |
| 107 bool m_hasTrailingWhitespace = false; | |
| 108 bool m_isDirectional = false; | |
| 109 #if DCHECK_IS_ON() | |
| 110 uint64_t m_domTreeVersion; | |
| 111 #endif | |
| 112 }; | |
| 113 | |
| 114 extern template class CORE_EXTERN_TEMPLATE_EXPORT | |
| 115 SelectionTemplate<EditingStrategy>; | |
| 116 extern template class CORE_EXTERN_TEMPLATE_EXPORT | |
| 117 SelectionTemplate<EditingInFlatTreeStrategy>; | |
| 118 | |
| 119 using SelectionInDOMTree = SelectionTemplate<EditingStrategy>; | |
| 120 using SelectionInFlatTree = SelectionTemplate<EditingInFlatTreeStrategy>; | |
| 121 | |
| 122 CORE_EXPORT std::ostream& operator<<(std::ostream&, const SelectionInDOMTree&); | |
| 123 CORE_EXPORT std::ostream& operator<<(std::ostream&, const SelectionInFlatTree&); | |
| 124 | |
| 125 } // namespace blink | |
| 126 | |
| 127 #endif // SelectionTemplate_h | |
| OLD | NEW |