1 /*
2 * Copyright (C) 2004, 2006, 2008 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 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 Position_h
27 #define Position_h
28
29 #include "ContainerNode.h"
30 #include "EditingBoundary.h"
31 #include "TextAffinity.h"
32 #include "TextDirection.h"
33 #include <wtf/Assertions.h>
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefPtr.h>
36
37 namespace WebCore {
38
39 class CSSComputedStyleDeclaration;
40 class Element;
41 class InlineBox;
42 class Node;
43 class Range;
44 class RenderObject;
45
46 enum PositionMoveType {
47 CodePoint, // Move by a single code point.
48 Character, // Move to the next Unicode character break.
49 BackwardDeletion // Subject to platform conventions.
50 };
51
52 class Position {
53 public:
54 enum AnchorType {
55 PositionIsOffsetInAnchor,
56 PositionIsAfterAnchor,
57 PositionIsBeforeAnchor
58 };
59
Position()60 Position()
61 : m_offset(0)
62 , m_anchorType(PositionIsOffsetInAnchor)
63 , m_isLegacyEditingPosition(false)
64 {
65 }
66
67 // For creating legacy editing positions: (Anchor type will be determined from editingIgnoresContent(node))
68 Position(PassRefPtr<Node> anchorNode, int offset);
69
70 // For creating before/after positions:
71 Position(PassRefPtr<Node> anchorNode, AnchorType);
72 // For creating offset positions:
73 Position(PassRefPtr<Node> anchorNode, int offset, AnchorType);
74
anchorType()75 AnchorType anchorType() const { return static_cast<AnchorType>(m_anchorType); }
76
clear()77 void clear() { m_anchorNode.clear(); m_offset = 0; m_anchorType = PositionIsOffsetInAnchor; m_isLegacyEditingPosition = false; }
78
79 // These are always DOM compliant values. Editing positions like [img, 0] (aka [img, before])
80 // will return img->parentNode() and img->nodeIndex() from these functions.
81 Node* containerNode() const; // NULL for a before/after position anchored to a node with no parent
82 int computeOffsetInContainerNode() const; // O(n) for before/after-anchored positions, O(1) for parent-anchored positions
83 Position parentAnchoredEquivalent() const; // Convenience method for DOM positions that also fixes up some positions for editing
84
85 // Inline O(1) access for Positions which callers know to be parent-anchored
offsetInContainerNode()86 int offsetInContainerNode() const
87 {
88 ASSERT(anchorType() == PositionIsOffsetInAnchor);
89 return m_offset;
90 }
91
92 // New code should not use this function.
deprecatedEditingOffset()93 int deprecatedEditingOffset() const
94 {
95 if (m_isLegacyEditingPosition || m_anchorType != PositionIsAfterAnchor)
96 return m_offset;
97 return offsetForPositionAfterAnchor();
98 }
99
100 // These are convenience methods which are smart about whether the position is neighbor anchored or parent anchored
101 Node* computeNodeBeforePosition() const;
102 Node* computeNodeAfterPosition() const;
103
anchorNode()104 Node* anchorNode() const { return m_anchorNode.get(); }
105
106 // FIXME: Callers should be moved off of node(), node() is not always the container for this position.
107 // For nodes which editingIgnoresContent(node()) returns true, positions like [ignoredNode, 0]
108 // will be treated as before ignoredNode (thus node() is really after the position, not containing it).
deprecatedNode()109 Node* deprecatedNode() const { return m_anchorNode.get(); }
110
document()111 Document* document() const { return m_anchorNode ? m_anchorNode->document() : 0; }
112
113 // These should only be used for PositionIsOffsetInAnchor positions, unless
114 // the position is a legacy editing position.
115 void moveToPosition(PassRefPtr<Node> anchorNode, int offset);
116 void moveToOffset(int offset);
117
isNull()118 bool isNull() const { return !m_anchorNode; }
isNotNull()119 bool isNotNull() const { return m_anchorNode; }
isOrphan()120 bool isOrphan() const { return m_anchorNode && !m_anchorNode->inDocument(); }
121
122 Element* element() const;
123 PassRefPtr<CSSComputedStyleDeclaration> computedStyle() const;
124
125 // Move up or down the DOM by one position.
126 // Offsets are computed using render text for nodes that have renderers - but note that even when
127 // using composed characters, the result may be inside a single user-visible character if a ligature is formed.
128 Position previous(PositionMoveType = CodePoint) const;
129 Position next(PositionMoveType = CodePoint) const;
130 static int uncheckedPreviousOffset(const Node*, int current);
131 static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current);
132 static int uncheckedNextOffset(const Node*, int current);
133
134 // These can be either inside or just before/after the node, depending on
135 // if the node is ignored by editing or not.
136 // FIXME: These should go away. They only make sense for legacy positions.
137 bool atFirstEditingPositionForNode() const;
138 bool atLastEditingPositionForNode() const;
139
140 // Returns true if the visually equivalent positions around have different editability
141 bool atEditingBoundary() const;
142 Node* parentEditingBoundary() const;
143
144 bool atStartOfTree() const;
145 bool atEndOfTree() const;
146
147 // FIXME: Make these non-member functions and put them somewhere in the editing directory.
148 // These aren't really basic "position" operations. More high level editing helper functions.
149 Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
150 Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
151
152 // These return useful visually equivalent positions.
153 Position upstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const;
154 Position downstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const;
155
156 bool isCandidate() const;
157 bool inRenderedText() const;
158 bool isRenderedCharacter() const;
159 bool rendersInDifferentPosition(const Position&) const;
160
161 void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
162 void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;
163
164 TextDirection primaryDirection() const;
165
166 static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
167 static bool nodeIsUserSelectNone(Node*);
168
169 void debugPosition(const char* msg = "") const;
170
171 #ifndef NDEBUG
172 void formatForDebugger(char* buffer, unsigned length) const;
173 void showAnchorTypeAndOffset() const;
174 void showTreeForThis() const;
175 #endif
176
177 private:
178 int offsetForPositionAfterAnchor() const;
179
180 int renderedOffset() const;
181
182 Position previousCharacterPosition(EAffinity) const;
183 Position nextCharacterPosition(EAffinity) const;
184
185 static AnchorType anchorTypeForLegacyEditingPosition(Node* anchorNode, int offset);
186
187 RefPtr<Node> m_anchorNode;
188 // m_offset can be the offset inside m_anchorNode, or if editingIgnoresContent(m_anchorNode)
189 // returns true, then other places in editing will treat m_offset == 0 as "before the anchor"
190 // and m_offset > 0 as "after the anchor node". See parentAnchoredEquivalent for more info.
191 int m_offset;
192 unsigned m_anchorType : 2;
193 bool m_isLegacyEditingPosition : 1;
194 };
195
196 inline bool operator==(const Position& a, const Position& b)
197 {
198 // FIXME: In <div><img></div> [div, 0] != [img, 0] even though most of the
199 // editing code will treat them as identical.
200 return a.anchorNode() == b.anchorNode() && a.deprecatedEditingOffset() == b.deprecatedEditingOffset() && a.anchorType() == b.anchorType();
201 }
202
203 inline bool operator!=(const Position& a, const Position& b)
204 {
205 return !(a == b);
206 }
207
208 // We define position creation functions to make callsites more readable.
209 // These are inline to prevent ref-churn when returning a Position object.
210 // If we ever add a PassPosition we can make these non-inline.
211
positionInParentBeforeNode(const Node * node)212 inline Position positionInParentBeforeNode(const Node* node)
213 {
214 // FIXME: This should ASSERT(node->parentNode())
215 // At least one caller currently hits this ASSERT though, which indicates
216 // that the caller is trying to make a position relative to a disconnected node (which is likely an error)
217 // Specifically, editing/deleting/delete-ligature-001.html crashes with ASSERT(node->parentNode())
218 return Position(node->parentNode(), node->nodeIndex(), Position::PositionIsOffsetInAnchor);
219 }
220
positionInParentAfterNode(const Node * node)221 inline Position positionInParentAfterNode(const Node* node)
222 {
223 ASSERT(node->parentNode());
224 return Position(node->parentNode(), node->nodeIndex() + 1, Position::PositionIsOffsetInAnchor);
225 }
226
227 // positionBeforeNode and positionAfterNode return neighbor-anchored positions, construction is O(1)
positionBeforeNode(Node * anchorNode)228 inline Position positionBeforeNode(Node* anchorNode)
229 {
230 ASSERT(anchorNode);
231 return Position(anchorNode, Position::PositionIsBeforeAnchor);
232 }
233
positionAfterNode(Node * anchorNode)234 inline Position positionAfterNode(Node* anchorNode)
235 {
236 ASSERT(anchorNode);
237 return Position(anchorNode, Position::PositionIsAfterAnchor);
238 }
239
lastOffsetInNode(Node * node)240 inline int lastOffsetInNode(Node* node)
241 {
242 return node->offsetInCharacters() ? node->maxCharacterOffset() : static_cast<int>(node->childNodeCount());
243 }
244
245 // firstPositionInNode and lastPositionInNode return parent-anchored positions, lastPositionInNode construction is O(n) due to childNodeCount()
firstPositionInNode(Node * anchorNode)246 inline Position firstPositionInNode(Node* anchorNode)
247 {
248 return Position(anchorNode, 0, Position::PositionIsOffsetInAnchor);
249 }
250
lastPositionInNode(Node * anchorNode)251 inline Position lastPositionInNode(Node* anchorNode)
252 {
253 return Position(anchorNode, lastOffsetInNode(anchorNode), Position::PositionIsOffsetInAnchor);
254 }
255
256 } // namespace WebCore
257
258 #ifndef NDEBUG
259 // Outside the WebCore namespace for ease of invocation from gdb.
260 void showTree(const WebCore::Position&);
261 void showTree(const WebCore::Position*);
262 #endif
263
264 #endif // Position_h
265