2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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>
39 class CSSComputedStyleDeclaration;
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.
56 PositionIsOffsetInAnchor,
57 PositionIsBeforeAnchor,
58 PositionIsAfterAnchor,
59 PositionIsBeforeChildren,
60 PositionIsAfterChildren,
65 , m_anchorType(PositionIsOffsetInAnchor)
66 , m_isLegacyEditingPosition(false)
70 // For creating legacy editing positions: (Anchor type will be determined from editingIgnoresContent(node))
71 class LegacyEditingOffset {
73 int value() const { return m_offset; }
76 explicit LegacyEditingOffset(int offset) : m_offset(offset) { }
78 friend Position createLegacyEditingPosition(PassRefPtr<Node>, int offset);
82 Position(PassRefPtr<Node> anchorNode, LegacyEditingOffset);
84 // For creating before/after positions:
85 Position(PassRefPtr<Node> anchorNode, AnchorType);
86 Position(PassRefPtr<Text> textNode, unsigned offset);
88 // For creating offset positions:
89 // FIXME: This constructor should eventually go away. See bug 63040.
90 Position(PassRefPtr<Node> anchorNode, int offset, AnchorType);
92 AnchorType anchorType() const { return static_cast<AnchorType>(m_anchorType); }
94 void clear() { m_anchorNode.clear(); m_offset = 0; m_anchorType = PositionIsOffsetInAnchor; m_isLegacyEditingPosition = false; }
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;
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
104 // Inline O(1) access for Positions which callers know to be parent-anchored
105 int offsetInContainerNode() const
107 ASSERT(anchorType() == PositionIsOffsetInAnchor);
111 // New code should not use this function.
112 int deprecatedEditingOffset() const
114 if (m_isLegacyEditingPosition || (m_anchorType != PositionIsAfterAnchor && m_anchorType != PositionIsAfterChildren))
116 return offsetForPositionAfterAnchor();
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;
123 Node* anchorNode() const { return m_anchorNode.get(); }
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).
128 Node* deprecatedNode() const { return m_anchorNode.get(); }
130 Document* document() const { return m_anchorNode ? m_anchorNode->document() : 0; }
131 Element* rootEditableElement() const
133 Node* container = containerNode();
134 return container ? container->rootEditableElement() : 0;
137 // These should only be used for PositionIsOffsetInAnchor positions, unless
138 // the position is a legacy editing position.
139 void moveToPosition(PassRefPtr<Node> anchorNode, int offset);
140 void moveToOffset(int offset);
142 bool isNull() const { return !m_anchorNode; }
143 bool isNotNull() const { return m_anchorNode; }
144 bool isOrphan() const { return m_anchorNode && !m_anchorNode->inDocument(); }
146 Element* element() const;
148 // Move up or down the DOM by one position.
149 // Offsets are computed using render text for nodes that have renderers - but note that even when
150 // using composed characters, the result may be inside a single user-visible character if a ligature is formed.
151 Position previous(PositionMoveType = CodePoint) const;
152 Position next(PositionMoveType = CodePoint) const;
153 static int uncheckedPreviousOffset(const Node*, int current);
154 static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current);
155 static int uncheckedNextOffset(const Node*, int current);
157 // These can be either inside or just before/after the node, depending on
158 // if the node is ignored by editing or not.
159 // FIXME: These should go away. They only make sense for legacy positions.
160 bool atFirstEditingPositionForNode() const;
161 bool atLastEditingPositionForNode() const;
163 // Returns true if the visually equivalent positions around have different editability
164 bool atEditingBoundary() const;
165 Node* parentEditingBoundary() const;
167 bool atStartOfTree() const;
168 bool atEndOfTree() const;
170 // FIXME: Make these non-member functions and put them somewhere in the editing directory.
171 // These aren't really basic "position" operations. More high level editing helper functions.
172 Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
173 Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
175 // These return useful visually equivalent positions.
176 Position upstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const;
177 Position downstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const;
179 bool isCandidate() const;
180 bool inRenderedText() const;
181 bool isRenderedCharacter() const;
182 bool rendersInDifferentPosition(const Position&) const;
184 void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
185 void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;
187 TextDirection primaryDirection() const;
189 static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
190 static bool nodeIsUserSelectNone(Node*);
191 #if ENABLE(USERSELECT_ALL)
192 static bool nodeIsUserSelectAll(const Node*);
193 static Node* rootUserSelectAllForNode(Node*);
195 static ContainerNode* findParent(const Node*);
197 void debugPosition(const char* msg = "") const;
200 void formatForDebugger(char* buffer, unsigned length) const;
201 void showAnchorTypeAndOffset() const;
202 void showTreeForThis() const;
206 int offsetForPositionAfterAnchor() const;
208 int renderedOffset() const;
211 Position previousCharacterPosition(EAffinity) const;
212 Position nextCharacterPosition(EAffinity) const;
214 static AnchorType anchorTypeForLegacyEditingPosition(Node* anchorNode, int offset);
216 RefPtr<Node> m_anchorNode;
217 // m_offset can be the offset inside m_anchorNode, or if editingIgnoresContent(m_anchorNode)
218 // returns true, then other places in editing will treat m_offset == 0 as "before the anchor"
219 // and m_offset > 0 as "after the anchor node". See parentAnchoredEquivalent for more info.
221 unsigned m_anchorType : 3;
222 bool m_isLegacyEditingPosition : 1;
225 inline Position createLegacyEditingPosition(PassRefPtr<Node> node, int offset)
227 return Position(node, Position::LegacyEditingOffset(offset));
230 inline bool operator==(const Position& a, const Position& b)
232 // FIXME: In <div><img></div> [div, 0] != [img, 0] even though most of the
233 // editing code will treat them as identical.
234 return a.anchorNode() == b.anchorNode() && a.deprecatedEditingOffset() == b.deprecatedEditingOffset() && a.anchorType() == b.anchorType();
237 inline bool operator!=(const Position& a, const Position& b)
242 // We define position creation functions to make callsites more readable.
243 // These are inline to prevent ref-churn when returning a Position object.
244 // If we ever add a PassPosition we can make these non-inline.
246 inline Position positionInParentBeforeNode(const Node* node)
248 // FIXME: This should ASSERT(node->parentNode())
249 // At least one caller currently hits this ASSERT though, which indicates
250 // that the caller is trying to make a position relative to a disconnected node (which is likely an error)
251 // Specifically, editing/deleting/delete-ligature-001.html crashes with ASSERT(node->parentNode())
252 return Position(Position::findParent(node), node->nodeIndex(), Position::PositionIsOffsetInAnchor);
255 inline Position positionInParentAfterNode(const Node* node)
257 ASSERT(Position::findParent(node));
258 return Position(Position::findParent(node), node->nodeIndex() + 1, Position::PositionIsOffsetInAnchor);
261 // positionBeforeNode and positionAfterNode return neighbor-anchored positions, construction is O(1)
262 inline Position positionBeforeNode(Node* anchorNode)
265 return Position(anchorNode, Position::PositionIsBeforeAnchor);
268 inline Position positionAfterNode(Node* anchorNode)
271 return Position(anchorNode, Position::PositionIsAfterAnchor);
274 inline int lastOffsetInNode(Node* node)
276 return node->offsetInCharacters() ? node->maxCharacterOffset() : static_cast<int>(node->childNodeCount());
279 // firstPositionInNode and lastPositionInNode return parent-anchored positions, lastPositionInNode construction is O(n) due to childNodeCount()
280 inline Position firstPositionInNode(Node* anchorNode)
282 if (anchorNode->isTextNode())
283 return Position(anchorNode, 0, Position::PositionIsOffsetInAnchor);
284 return Position(anchorNode, Position::PositionIsBeforeChildren);
287 inline Position lastPositionInNode(Node* anchorNode)
289 if (anchorNode->isTextNode())
290 return Position(anchorNode, lastOffsetInNode(anchorNode), Position::PositionIsOffsetInAnchor);
291 return Position(anchorNode, Position::PositionIsAfterChildren);
294 inline int minOffsetForNode(Node* anchorNode, int offset)
296 if (anchorNode->offsetInCharacters())
297 return std::min(offset, anchorNode->maxCharacterOffset());
300 for (Node* node = anchorNode->firstChild(); node && newOffset < offset; node = node->nextSibling())
306 inline bool offsetIsBeforeLastNodeOffset(int offset, Node* anchorNode)
308 if (anchorNode->offsetInCharacters())
309 return offset < anchorNode->maxCharacterOffset();
311 int currentOffset = 0;
312 for (Node* node = anchorNode->firstChild(); node && currentOffset < offset; node = node->nextSibling())
316 return offset < currentOffset;
319 } // namespace WebCore
322 // Outside the WebCore namespace for ease of invocation from gdb.
323 void showTree(const WebCore::Position&);
324 void showTree(const WebCore::Position*);