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