1 | /* |
2 | * Copyright (C) 2004, 2015 Apple 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 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 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 | #pragma once |
27 | |
28 | #include "TextGranularity.h" |
29 | #include "VisiblePosition.h" |
30 | |
31 | namespace WebCore { |
32 | |
33 | class Position; |
34 | |
35 | const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM; |
36 | enum SelectionDirection : uint8_t { DirectionForward, DirectionBackward, DirectionRight, DirectionLeft }; |
37 | |
38 | class VisibleSelection { |
39 | public: |
40 | enum SelectionType { NoSelection, CaretSelection, RangeSelection }; |
41 | |
42 | WEBCORE_EXPORT VisibleSelection(); |
43 | |
44 | VisibleSelection(const Position&, EAffinity, bool isDirectional = false); |
45 | VisibleSelection(const Position&, const Position&, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false); |
46 | |
47 | WEBCORE_EXPORT VisibleSelection(const Range&, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false); |
48 | |
49 | WEBCORE_EXPORT VisibleSelection(const VisiblePosition&, bool isDirectional = false); |
50 | WEBCORE_EXPORT VisibleSelection(const VisiblePosition&, const VisiblePosition&, bool isDirectional = false); |
51 | |
52 | WEBCORE_EXPORT static VisibleSelection selectionFromContentsOfNode(Node*); |
53 | |
54 | SelectionType selectionType() const { return m_selectionType; } |
55 | |
56 | void setAffinity(EAffinity affinity) { m_affinity = affinity; } |
57 | EAffinity affinity() const { return m_affinity; } |
58 | |
59 | void setBase(const Position&); |
60 | void setBase(const VisiblePosition&); |
61 | void setExtent(const Position&); |
62 | void setExtent(const VisiblePosition&); |
63 | |
64 | Position base() const { return m_base; } |
65 | Position extent() const { return m_extent; } |
66 | Position start() const { return m_start; } |
67 | Position end() const { return m_end; } |
68 | |
69 | VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); } |
70 | VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); } |
71 | VisiblePosition visibleBase() const { return VisiblePosition(m_base, isRange() ? (isBaseFirst() ? UPSTREAM : DOWNSTREAM) : affinity()); } |
72 | VisiblePosition visibleExtent() const { return VisiblePosition(m_extent, isRange() ? (isBaseFirst() ? DOWNSTREAM : UPSTREAM) : affinity()); } |
73 | |
74 | bool isNone() const { return selectionType() == NoSelection; } |
75 | bool isCaret() const { return selectionType() == CaretSelection; } |
76 | bool isRange() const { return selectionType() == RangeSelection; } |
77 | bool isCaretOrRange() const { return selectionType() != NoSelection; } |
78 | bool isNonOrphanedRange() const { return isRange() && !start().isOrphan() && !end().isOrphan(); } |
79 | bool isNoneOrOrphaned() const { return isNone() || start().isOrphan() || end().isOrphan(); } |
80 | |
81 | bool isBaseFirst() const { return m_baseIsFirst; } |
82 | bool isDirectional() const { return m_isDirectional; } |
83 | void setIsDirectional(bool isDirectional) { m_isDirectional = isDirectional; } |
84 | |
85 | WEBCORE_EXPORT bool isAll(EditingBoundaryCrossingRule) const; |
86 | |
87 | void appendTrailingWhitespace(); |
88 | |
89 | WEBCORE_EXPORT bool expandUsingGranularity(TextGranularity granularity); |
90 | |
91 | // We don't yet support multi-range selections, so we only ever have one range to return. |
92 | WEBCORE_EXPORT RefPtr<Range> firstRange() const; |
93 | |
94 | // FIXME: Most callers probably don't want this function, but are using it |
95 | // for historical reasons. toNormalizedRange contracts the range around |
96 | // text, and moves the caret upstream before returning the range. |
97 | WEBCORE_EXPORT RefPtr<Range> toNormalizedRange() const; |
98 | |
99 | WEBCORE_EXPORT Element* rootEditableElement() const; |
100 | WEBCORE_EXPORT bool isContentEditable() const; |
101 | bool hasEditableStyle() const; |
102 | WEBCORE_EXPORT bool isContentRichlyEditable() const; |
103 | // Returns a shadow tree node for legacy shadow trees, a child of the |
104 | // ShadowRoot node for new shadow trees, or 0 for non-shadow trees. |
105 | Node* nonBoundaryShadowTreeRootNode() const; |
106 | |
107 | WEBCORE_EXPORT bool isInPasswordField() const; |
108 | |
109 | WEBCORE_EXPORT static Position adjustPositionForEnd(const Position& currentPosition, Node* startContainerNode); |
110 | WEBCORE_EXPORT static Position adjustPositionForStart(const Position& currentPosition, Node* startContainerNode); |
111 | |
112 | #if ENABLE(TREE_DEBUGGING) |
113 | void debugPosition() const; |
114 | void formatForDebugger(char* buffer, unsigned length) const; |
115 | void showTreeForThis() const; |
116 | #endif |
117 | |
118 | void setWithoutValidation(const Position&, const Position&); |
119 | |
120 | private: |
121 | void validate(TextGranularity = CharacterGranularity); |
122 | |
123 | // Support methods for validate() |
124 | void setBaseAndExtentToDeepEquivalents(); |
125 | void setStartAndEndFromBaseAndExtentRespectingGranularity(TextGranularity); |
126 | void adjustSelectionToAvoidCrossingShadowBoundaries(); |
127 | void adjustSelectionToAvoidCrossingEditingBoundaries(); |
128 | void updateSelectionType(); |
129 | |
130 | // We need to store these as Positions because VisibleSelection is |
131 | // used to store values in editing commands for use when |
132 | // undoing the command. We need to be able to create a selection that, while currently |
133 | // invalid, will be valid once the changes are undone. |
134 | |
135 | Position m_base; // Where the first click happened |
136 | Position m_extent; // Where the end click happened |
137 | Position m_start; // Leftmost position when expanded to respect granularity |
138 | Position m_end; // Rightmost position when expanded to respect granularity |
139 | |
140 | EAffinity m_affinity; // the upstream/downstream affinity of the caret |
141 | |
142 | // these are cached, can be recalculated by validate() |
143 | SelectionType m_selectionType; // None, Caret, Range |
144 | bool m_baseIsFirst : 1; // True if base is before the extent |
145 | bool m_isDirectional : 1; // Non-directional ignores m_baseIsFirst and selection always extends on shift + arrow key. |
146 | }; |
147 | |
148 | inline bool operator==(const VisibleSelection& a, const VisibleSelection& b) |
149 | { |
150 | return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.isBaseFirst() == b.isBaseFirst() |
151 | && a.isDirectional() == b.isDirectional(); |
152 | } |
153 | |
154 | inline bool operator!=(const VisibleSelection& a, const VisibleSelection& b) |
155 | { |
156 | return !(a == b); |
157 | } |
158 | |
159 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const VisibleSelection&); |
160 | |
161 | } // namespace WebCore |
162 | |
163 | #if ENABLE(TREE_DEBUGGING) |
164 | // Outside the WebCore namespace for ease of invocation from the debugger. |
165 | void showTree(const WebCore::VisibleSelection&); |
166 | void showTree(const WebCore::VisibleSelection*); |
167 | #endif |
168 | |