Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / VisibleSelection.h
1 /*
2  * Copyright (C) 2004 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef VisibleSelection_h
27 #define VisibleSelection_h
28
29 #include "core/editing/SelectionType.h"
30 #include "core/editing/TextGranularity.h"
31 #include "core/editing/VisiblePosition.h"
32
33 namespace blink {
34
35 class LayoutPoint;
36 class Position;
37
38 const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM;
39 enum SelectionDirection { DirectionForward, DirectionBackward, DirectionRight, DirectionLeft };
40
41 class VisibleSelection {
42     DISALLOW_ALLOCATION();
43 public:
44     VisibleSelection();
45
46     VisibleSelection(const Position&, EAffinity, bool isDirectional = false);
47     VisibleSelection(const Position&, const Position&, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false);
48
49     explicit VisibleSelection(const Range*, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false);
50
51     explicit VisibleSelection(const VisiblePosition&, bool isDirectional = false);
52     VisibleSelection(const VisiblePosition&, const VisiblePosition&, bool isDirectional = false);
53
54     VisibleSelection(const VisibleSelection&);
55     VisibleSelection& operator=(const VisibleSelection&);
56
57     ~VisibleSelection();
58
59     static VisibleSelection selectionFromContentsOfNode(Node*);
60
61     SelectionType selectionType() const { return m_selectionType; }
62
63     void setAffinity(EAffinity affinity) { m_affinity = affinity; }
64     EAffinity affinity() const { return m_affinity; }
65
66     void setBase(const Position&);
67     void setBase(const VisiblePosition&);
68     void setExtent(const Position&);
69     void setExtent(const VisiblePosition&);
70
71     Position base() const { return m_base; }
72     Position extent() const { return m_extent; }
73     Position start() const { return m_start; }
74     Position end() const { return m_end; }
75
76     VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); }
77     VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
78     VisiblePosition visibleBase() const { return VisiblePosition(m_base, isRange() ? (isBaseFirst() ? UPSTREAM : DOWNSTREAM) : affinity()); }
79     VisiblePosition visibleExtent() const { return VisiblePosition(m_extent, isRange() ? (isBaseFirst() ? DOWNSTREAM : UPSTREAM) : affinity()); }
80
81     bool isNone() const { return selectionType() == NoSelection; }
82     bool isCaret() const { return selectionType() == CaretSelection; }
83     bool isRange() const { return selectionType() == RangeSelection; }
84     bool isCaretOrRange() const { return selectionType() != NoSelection; }
85     bool isNonOrphanedRange() const { return isRange() && !start().isOrphan() && !end().isOrphan(); }
86     bool isNonOrphanedCaretOrRange() const { return isCaretOrRange() && !start().isOrphan() && !end().isOrphan(); }
87
88     bool isBaseFirst() const { return m_baseIsFirst; }
89     bool isDirectional() const { return m_isDirectional; }
90     void setIsDirectional(bool isDirectional) { m_isDirectional = isDirectional; }
91
92     void appendTrailingWhitespace();
93
94     bool expandUsingGranularity(TextGranularity granularity);
95
96     // We don't yet support multi-range selections, so we only ever have one range to return.
97     PassRefPtrWillBeRawPtr<Range> firstRange() const;
98
99     bool intersectsNode(Node*) const;
100
101     // FIXME: Most callers probably don't want these functions, but
102     // are using them for historical reasons. toNormalizedRange and
103     // toNormalizedPositions contracts the range around text, and
104     // moves the caret upstream before returning the range/positions.
105     PassRefPtrWillBeRawPtr<Range> toNormalizedRange() const;
106     bool toNormalizedPositions(Position& start, Position& end) const;
107
108     Element* rootEditableElement() const;
109     bool isContentEditable() const;
110     bool hasEditableStyle() const;
111     bool isContentRichlyEditable() const;
112     // Returns a shadow tree node for legacy shadow trees, a child of the
113     // ShadowRoot node for new shadow trees, or 0 for non-shadow trees.
114     Node* nonBoundaryShadowTreeRootNode() const;
115
116     VisiblePosition visiblePositionRespectingEditingBoundary(const LayoutPoint& localPoint, Node* targetNode) const;
117
118     void setWithoutValidation(const Position&, const Position&);
119
120     // Listener of VisibleSelection modification. didChangeVisibleSelection() will be invoked when base, extent, start
121     // or end is moved to a different position.
122     //
123     // Objects implementing |ChangeObserver| interface must outlive the VisibleSelection object.
124     class ChangeObserver : public WillBeGarbageCollectedMixin {
125         WTF_MAKE_NONCOPYABLE(ChangeObserver);
126     public:
127         ChangeObserver();
128         virtual ~ChangeObserver();
129         virtual void didChangeVisibleSelection() = 0;
130         virtual void trace(Visitor*) { }
131     };
132
133     void setChangeObserver(ChangeObserver&);
134     void clearChangeObserver();
135     void didChange(); // Fire the change observer, if any.
136
137     void trace(Visitor*);
138
139     void validatePositionsIfNeeded();
140
141 #ifndef NDEBUG
142     void debugPosition() const;
143     void formatForDebugger(char* buffer, unsigned length) const;
144     void showTreeForThis() const;
145 #endif
146
147 private:
148     void validate(TextGranularity = CharacterGranularity);
149
150     // Support methods for validate()
151     void setBaseAndExtentToDeepEquivalents();
152     void setStartAndEndFromBaseAndExtentRespectingGranularity(TextGranularity);
153     void adjustSelectionToAvoidCrossingShadowBoundaries();
154     void adjustSelectionToAvoidCrossingEditingBoundaries();
155     void updateSelectionType();
156
157     // We need to store these as Positions because VisibleSelection is
158     // used to store values in editing commands for use when
159     // undoing the command. We need to be able to create a selection that, while currently
160     // invalid, will be valid once the changes are undone.
161
162     Position m_base;   // Where the first click happened
163     Position m_extent; // Where the end click happened
164     Position m_start;  // Leftmost position when expanded to respect granularity
165     Position m_end;    // Rightmost position when expanded to respect granularity
166
167     EAffinity m_affinity;           // the upstream/downstream affinity of the caret
168
169     // Oilpan: this reference has a lifetime that is at least as long
170     // as this object.
171     RawPtrWillBeMember<ChangeObserver> m_changeObserver;
172
173     // these are cached, can be recalculated by validate()
174     SelectionType m_selectionType; // None, Caret, Range
175     bool m_baseIsFirst : 1; // True if base is before the extent
176     bool m_isDirectional : 1; // Non-directional ignores m_baseIsFirst and selection always extends on shift + arrow key.
177 };
178
179 inline bool operator==(const VisibleSelection& a, const VisibleSelection& b)
180 {
181     return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.isBaseFirst() == b.isBaseFirst()
182         && a.isDirectional() == b.isDirectional();
183 }
184
185 inline bool operator!=(const VisibleSelection& a, const VisibleSelection& b)
186 {
187     return !(a == b);
188 }
189
190 } // namespace blink
191
192 #ifndef NDEBUG
193 // Outside the WebCore namespace for ease of invocation from gdb.
194 void showTree(const blink::VisibleSelection&);
195 void showTree(const blink::VisibleSelection*);
196 #endif
197
198 #endif // VisibleSelection_h