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