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

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

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 #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 // |extent| can be in different tree scope of base, but should be in same
46 // document.
47 Builder& extend(const PositionTemplate<Strategy>& extent);
48
49 Builder& setBaseAndExtent(const EphemeralRangeTemplate<Strategy>&);
50
51 // |extent| can not be null if |base| isn't null.
52 Builder& setBaseAndExtent(const PositionTemplate<Strategy>& base,
53 const PositionTemplate<Strategy>& extent);
54
55 // |extent| can be non-null while |base| is null, which is undesired.
56 // Note: This function should be used only in "core/editing/commands".
57 // TODO(yosin): Once all we get rid of all call sites, we remove this.
58 Builder& setBaseAndExtentDeprecated(
59 const PositionTemplate<Strategy>& base,
60 const PositionTemplate<Strategy>& extent);
61
62 Builder& setAffinity(TextAffinity);
63 Builder& setGranularity(TextGranularity);
64 Builder& setHasTrailingWhitespace(bool);
65 Builder& setIsDirectional(bool);
66
67 private:
68 SelectionTemplate m_selection;
69
70 DISALLOW_COPY_AND_ASSIGN(Builder);
71 };
72
73 SelectionTemplate(const SelectionTemplate& other);
74 SelectionTemplate();
75
76 SelectionTemplate& operator=(const SelectionTemplate&) = default;
77
78 bool operator==(const SelectionTemplate&) const;
79 bool operator!=(const SelectionTemplate&) const;
80
81 const PositionTemplate<Strategy>& base() const;
82 const PositionTemplate<Strategy>& extent() const;
83 TextAffinity affinity() const { return m_affinity; }
84 TextGranularity granularity() const { return m_granularity; }
85 bool hasTrailingWhitespace() const { return m_hasTrailingWhitespace; }
86 bool isDirectional() const { return m_isDirectional; }
87 bool isNone() const { return m_base.isNull(); }
88
89 // Returns true if |this| selection holds valid values otherwise it causes
90 // assertion failure.
91 bool assertValid() const;
92 bool assertValidFor(const Document&) const;
93
94 DEFINE_INLINE_TRACE() {
95 visitor->trace(m_base);
96 visitor->trace(m_extent);
97 }
98
99 void printTo(std::ostream*, const char* type) const;
100
101 private:
102 friend class SelectionEditor;
103
104 Document* document() const;
105
106 PositionTemplate<Strategy> m_base;
107 PositionTemplate<Strategy> m_extent;
108 TextAffinity m_affinity = TextAffinity::Downstream;
109 TextGranularity m_granularity = CharacterGranularity;
110 bool m_hasTrailingWhitespace = false;
111 bool m_isDirectional = false;
112 #if DCHECK_IS_ON()
113 uint64_t m_domTreeVersion;
114 #endif
115 };
116
117 extern template class CORE_EXTERN_TEMPLATE_EXPORT
118 SelectionTemplate<EditingStrategy>;
119 extern template class CORE_EXTERN_TEMPLATE_EXPORT
120 SelectionTemplate<EditingInFlatTreeStrategy>;
121
122 using SelectionInDOMTree = SelectionTemplate<EditingStrategy>;
123 using SelectionInFlatTree = SelectionTemplate<EditingInFlatTreeStrategy>;
124
125 CORE_EXPORT std::ostream& operator<<(std::ostream&, const SelectionInDOMTree&);
126 CORE_EXPORT std::ostream& operator<<(std::ostream&, const SelectionInFlatTree&);
127
128 } // namespace blink
129
130 #endif // SelectionTemplate_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/BUILD.gn ('k') | third_party/WebKit/Source/core/editing/SelectionTemplate.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698