Position.h 8.73 KB
Newer Older
kocienda's avatar
kocienda committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
kocienda's avatar
kocienda committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

26
27
#ifndef Position_h
#define Position_h
kocienda's avatar
kocienda committed
28

darin's avatar
darin committed
29
#include "TextAffinity.h"
mitz@apple.com's avatar
mitz@apple.com committed
30
#include "TextDirection.h"
31
#include "Node.h" // for position creation functions
eric@webkit.org's avatar
eric@webkit.org committed
32
#include <wtf/Assertions.h>
darin@apple.com's avatar
darin@apple.com committed
33
34
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
kocienda's avatar
kocienda committed
35

darin's avatar
darin committed
36
namespace WebCore {
kocienda's avatar
kocienda committed
37

darin's avatar
darin committed
38
39
class CSSComputedStyleDeclaration;
class Element;
mitz@apple.com's avatar
mitz@apple.com committed
40
class InlineBox;
darin@apple.com's avatar
darin@apple.com committed
41
class Node;
darin's avatar
darin committed
42
class Range;
darin@apple.com's avatar
darin@apple.com committed
43
class RenderObject;
kocienda's avatar
kocienda committed
44

ap@webkit.org's avatar
ap@webkit.org committed
45
46
47
48
49
enum PositionMoveType {
    CodePoint,       // Move by a single code point.
    Character,       // Move to the next Unicode character break.
    BackwardDeletion // Subject to platform conventions.
};
kocienda's avatar
kocienda committed
50

darin@apple.com's avatar
darin@apple.com committed
51
class Position {
kocienda's avatar
kocienda committed
52
public:
eric@webkit.org's avatar
eric@webkit.org committed
53
54
55
56
57
    enum AnchorType {
        PositionIsOffsetInAnchor,
        PositionIsAfterAnchor,
        PositionIsBeforeAnchor
    };
darin's avatar
darin committed
58

eric@webkit.org's avatar
eric@webkit.org committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    Position()
        : m_offset(0)
        , m_anchorType(PositionIsOffsetInAnchor)
        , m_isLegacyEditingPosition(false)
    {
    }

    // For creating legacy editing positions: (Anchor type will be determined from editingIgnoresContent(node))
    Position(PassRefPtr<Node> anchorNode, int offset);

    // For creating before/after positions:
    Position(PassRefPtr<Node> anchorNode, AnchorType);
    // For creating offset positions:
    Position(PassRefPtr<Node> anchorNode, int offset, AnchorType);
eric@webkit.org's avatar
eric@webkit.org committed
73

74
    AnchorType anchorType() const { return static_cast<AnchorType>(m_anchorType); }
eric@webkit.org's avatar
eric@webkit.org committed
75
76

    void clear() { m_anchorNode.clear(); m_offset = 0; m_anchorType = PositionIsOffsetInAnchor; m_isLegacyEditingPosition = false; }
eric@webkit.org's avatar
eric@webkit.org committed
77

eric@webkit.org's avatar
eric@webkit.org committed
78
79
    // These are always DOM compliant values.  Editing positions like [img, 0] (aka [img, before])
    // will return img->parentNode() and img->nodeIndex() from these functions.
eric@webkit.org's avatar
eric@webkit.org committed
80
81
82
83
84
85
86
87
88
    Node* containerNode() const; // NULL for a before/after position anchored to a node with no parent
    int computeOffsetInContainerNode() const;  // O(n) for before/after-anchored positions, O(1) for parent-anchored positions

    // Inline O(1) access for Positions which callers know to be parent-anchored
    int offsetInContainerNode() const
    {
        ASSERT(anchorType() == PositionIsOffsetInAnchor);
        return m_offset;
    }
eric@webkit.org's avatar
eric@webkit.org committed
89

eric@webkit.org's avatar
eric@webkit.org committed
90
91
92
    // New code should not use this function.
    int deprecatedEditingOffset() const
    {
eric@webkit.org's avatar
eric@webkit.org committed
93
        // This should probably ASSERT(m_isLegacyEditingPosition);
eric@webkit.org's avatar
eric@webkit.org committed
94
95
96
        return m_offset;
    }

eric@webkit.org's avatar
eric@webkit.org committed
97
98
99
100
    // These are convenience methods which are smart about whether the position is neighbor anchored or parent anchored
    Node* computeNodeBeforePosition() const;
    Node* computeNodeAfterPosition() const;

eric@webkit.org's avatar
eric@webkit.org committed
101
    Node* anchorNode() const { return m_anchorNode.get(); }
darin's avatar
darin committed
102

eric@webkit.org's avatar
eric@webkit.org committed
103
104
105
106
    // FIXME: Callers should be moved off of node(), node() is not always the container for this position.
    // For nodes which editingIgnoresContent(node()) returns true, positions like [ignoredNode, 0]
    // will be treated as before ignoredNode (thus node() is really after the position, not containing it).
    Node* node() const { return m_anchorNode.get(); }
kocienda's avatar
kocienda committed
107

eric@webkit.org's avatar
eric@webkit.org committed
108
109
110
111
    // These should only be used for PositionIsOffsetInAnchor positions, unless
    // the position is a legacy editing position.
    void moveToPosition(PassRefPtr<Node> anchorNode, int offset);
    void moveToOffset(int offset);
eric@webkit.org's avatar
eric@webkit.org committed
112
113
114

    bool isNull() const { return !m_anchorNode; }
    bool isNotNull() const { return m_anchorNode; }
darin's avatar
darin committed
115

darin's avatar
darin committed
116
117
    Element* element() const;
    PassRefPtr<CSSComputedStyleDeclaration> computedStyle() const;
kocienda's avatar
kocienda committed
118

ap@webkit.org's avatar
ap@webkit.org committed
119
120
121
    // Move up or down the DOM by one position.
    // Offsets are computed using render text for nodes that have renderers - but note that even when
    // using composed characters, the result may be inside a single user-visible character if a ligature is formed.
ap@webkit.org's avatar
ap@webkit.org committed
122
123
    Position previous(PositionMoveType = CodePoint) const;
    Position next(PositionMoveType = CodePoint) const;
ap@webkit.org's avatar
ap@webkit.org committed
124
    static int uncheckedPreviousOffset(const Node*, int current);
ap@webkit.org's avatar
ap@webkit.org committed
125
    static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current);
ap@webkit.org's avatar
ap@webkit.org committed
126
127
    static int uncheckedNextOffset(const Node*, int current);

128
129
130
131
132
133
134
    // These can be either inside or just before/after the node, depending on
    // if the node is ignored by editing or not.
    bool atFirstEditingPositionForNode() const;
    bool atLastEditingPositionForNode() const;

    bool atStartOfTree() const;
    bool atEndOfTree() const;
ap@webkit.org's avatar
ap@webkit.org committed
135

darin's avatar
darin committed
136
137
    // FIXME: Make these non-member functions and put them somewhere in the editing directory.
    // These aren't really basic "position" operations. More high level editing helper functions.
darin's avatar
darin committed
138
139
    Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
    Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
140
141
    
    // These return useful visually equivalent positions.
mjs's avatar
mjs committed
142
143
    Position upstream() const;
    Position downstream() const;
kocienda's avatar
kocienda committed
144
    
justing's avatar
justing committed
145
    bool isCandidate() const;
weinig's avatar
weinig committed
146
    bool inRenderedText() const;
kocienda's avatar
kocienda committed
147
    bool isRenderedCharacter() const;
darin@apple.com's avatar
darin@apple.com committed
148
    bool rendersInDifferentPosition(const Position&) const;
mitz@apple.com's avatar
mitz@apple.com committed
149
150
151
152

    void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
    void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;

weinig's avatar
weinig committed
153
154
155
    static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
    static bool nodeIsUserSelectNone(Node*);
    
darin's avatar
darin committed
156
    void debugPosition(const char* msg = "") const;
kocienda's avatar
kocienda committed
157
158

#ifndef NDEBUG
darin's avatar
darin committed
159
    void formatForDebugger(char* buffer, unsigned length) const;
darin's avatar
darin committed
160
    void showTreeForThis() const;
kocienda's avatar
kocienda committed
161
#endif
kocienda's avatar
kocienda committed
162
    
kocienda's avatar
kocienda committed
163
private:
adele's avatar
adele committed
164
    int renderedOffset() const;
darin's avatar
darin committed
165

darin's avatar
darin committed
166
167
    Position previousCharacterPosition(EAffinity) const;
    Position nextCharacterPosition(EAffinity) const;
eric@webkit.org's avatar
eric@webkit.org committed
168

eric@webkit.org's avatar
eric@webkit.org committed
169
170
    static AnchorType anchorTypeForLegacyEditingPosition(Node* anchorNode, int offset);

eric@webkit.org's avatar
eric@webkit.org committed
171
172
173
174
    RefPtr<Node> m_anchorNode;
    // m_offset can be the offset inside m_anchorNode, or if editingIgnoresContent(m_anchorNode)
    // returns true, then other places in editing will treat m_offset == 0 as "before the anchor"
    // and m_offset > 0 as "after the anchor node".  See rangeCompliantEquivalent for more info.
eric@webkit.org's avatar
eric@webkit.org committed
175
    int m_offset;
176
    unsigned m_anchorType : 2;
eric@webkit.org's avatar
eric@webkit.org committed
177
    bool m_isLegacyEditingPosition : 1;
kocienda's avatar
kocienda committed
178
179
};

darin@apple.com's avatar
darin@apple.com committed
180
inline bool operator==(const Position& a, const Position& b)
kocienda's avatar
kocienda committed
181
{
eric@webkit.org's avatar
eric@webkit.org committed
182
183
    // FIXME: In <div><img></div> [div, 0] != [img, 0] even though most of the
    // editing code will treat them as identical.
eric@webkit.org's avatar
eric@webkit.org committed
184
    return a.anchorNode() == b.anchorNode() && a.deprecatedEditingOffset() == b.deprecatedEditingOffset();
kocienda's avatar
kocienda committed
185
186
}

darin@apple.com's avatar
darin@apple.com committed
187
inline bool operator!=(const Position& a, const Position& b)
kocienda's avatar
kocienda committed
188
189
190
191
{
    return !(a == b);
}

192
193
194
// We define position creation functions to make callsites more readable.
// These are inline to prevent ref-churn when returning a Position object.
// If we ever add a PassPosition we can make these non-inline.
darin's avatar
darin committed
195

196
197
198
199
200
201
202
203
204
205
206
207
208
209
inline Position positionBeforeNode(const Node* node)
{
    // FIXME: This should ASSERT(node->parentNode())
    // At least one caller currently hits this ASSERT though, which indicates
    // that the caller is trying to make a position relative to a disconnected node (which is likely an error)
    // Specifically, editing/deleting/delete-ligature-001.html crashes with ASSERT(node->parentNode())
    return Position(node->parentNode(), node->nodeIndex(), Position::PositionIsOffsetInAnchor);
}

inline Position positionAfterNode(const Node* node)
{
    ASSERT(node->parentNode());
    return Position(node->parentNode(), node->nodeIndex() + 1, Position::PositionIsOffsetInAnchor);
}
210

darin's avatar
darin committed
211
212
} // namespace WebCore

harrison's avatar
harrison committed
213
#ifndef NDEBUG
darin's avatar
darin committed
214
215
216
// Outside the WebCore namespace for ease of invocation from gdb.
void showTree(const WebCore::Position&);
void showTree(const WebCore::Position*);
harrison's avatar
harrison committed
217
218
#endif

219
#endif // Position_h