Range.h 6.81 KB
Newer Older
1
/*
kocienda's avatar
kocienda committed
2
3
4
5
 * (C) 1999 Lars Knoll (knoll@kde.org)
 * (C) 2000 Gunnstein Lye (gunnstein@netcom.no)
 * (C) 2000 Frederik Holljen (frederik.holljen@hig.no)
 * (C) 2001 Peter Kelly (pmk@post.com)
6
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
kocienda's avatar
kocienda committed
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
20
21
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
kocienda's avatar
kocienda committed
22
23
24
 *
 */

25
26
#ifndef Range_h
#define Range_h
kocienda's avatar
kocienda committed
27

28
#include "FloatQuad.h"
29
#include "RangeBoundaryPoint.h"
30
#include <wtf/Forward.h>
ggaren@apple.com's avatar
ggaren@apple.com committed
31
#include <wtf/RefCounted.h>
darin's avatar
darin committed
32

darin's avatar
darin committed
33
34
namespace WebCore {

35
36
class ClientRect;
class ClientRectList;
darin's avatar
darin committed
37
class DocumentFragment;
darin@apple.com's avatar
darin@apple.com committed
38
39
class NodeWithIndex;
class Text;
darin's avatar
darin committed
40

darin@apple.com's avatar
darin@apple.com committed
41
class Range : public RefCounted<Range> {
kocienda's avatar
kocienda committed
42
public:
darin@apple.com's avatar
darin@apple.com committed
43
44
45
    static PassRefPtr<Range> create(PassRefPtr<Document>);
    static PassRefPtr<Range> create(PassRefPtr<Document>, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset);
    static PassRefPtr<Range> create(PassRefPtr<Document>, const Position&, const Position&);
mjs's avatar
mjs committed
46
    ~Range();
darin's avatar
darin committed
47

ggaren's avatar
ggaren committed
48
    Document* ownerDocument() const { return m_ownerDocument.get(); }
49
50
51
52
    Node* startContainer() const { return m_start.container(); }
    int startOffset() const { return m_start.offset(); }
    Node* endContainer() const { return m_end.container(); }
    int endOffset() const { return m_end.offset(); }
ggaren's avatar
ggaren committed
53

darin's avatar
darin committed
54
    Node* startContainer(ExceptionCode&) const;
darin's avatar
darin committed
55
    int startOffset(ExceptionCode&) const;
darin's avatar
darin committed
56
    Node* endContainer(ExceptionCode&) const;
darin's avatar
darin committed
57
58
59
    int endOffset(ExceptionCode&) const;
    bool collapsed(ExceptionCode&) const;

darin's avatar
darin committed
60
61
    Node* commonAncestorContainer(ExceptionCode&) const;
    static Node* commonAncestorContainer(Node* containerA, Node* containerB);
darin@apple.com's avatar
darin@apple.com committed
62
63
    void setStart(PassRefPtr<Node> container, int offset, ExceptionCode&);
    void setEnd(PassRefPtr<Node> container, int offset, ExceptionCode&);
darin's avatar
darin committed
64
    void collapse(bool toStart, ExceptionCode&);
65
66
    bool isPointInRange(Node* refNode, int offset, ExceptionCode&);
    short comparePoint(Node* refNode, int offset, ExceptionCode&) const;
kmccullo's avatar
kmccullo committed
67
    enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE };
68
    CompareResults compareNode(Node* refNode, ExceptionCode&) const;
darin's avatar
darin committed
69
    enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START };
darin's avatar
darin committed
70
71
    short compareBoundaryPoints(CompareHow, const Range* sourceRange, ExceptionCode&) const;
    static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB);
72
    static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB);
darin's avatar
darin committed
73
    bool boundaryPointsValid() const;
kmccullo's avatar
kmccullo committed
74
    bool intersectsNode(Node* refNode, ExceptionCode&);
darin's avatar
darin committed
75
    void deleteContents(ExceptionCode&);
darin's avatar
darin committed
76
77
78
    PassRefPtr<DocumentFragment> extractContents(ExceptionCode&);
    PassRefPtr<DocumentFragment> cloneContents(ExceptionCode&);
    void insertNode(PassRefPtr<Node>, ExceptionCode&);
darin's avatar
darin committed
79
    String toString(ExceptionCode&) const;
adele's avatar
adele committed
80

darin's avatar
darin committed
81
82
83
    String toHTML() const;
    String text() const;

darin's avatar
darin committed
84
    PassRefPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionCode&) const;
darin's avatar
darin committed
85
86

    void detach(ExceptionCode&);
darin's avatar
darin committed
87
    PassRefPtr<Range> cloneRange(ExceptionCode&) const;
kocienda's avatar
kocienda committed
88

darin's avatar
darin committed
89
90
91
92
93
94
95
    void setStartAfter(Node*, ExceptionCode&);
    void setEndBefore(Node*, ExceptionCode&);
    void setEndAfter(Node*, ExceptionCode&);
    void selectNode(Node*, ExceptionCode&);
    void selectNodeContents(Node*, ExceptionCode&);
    void surroundContents(PassRefPtr<Node>, ExceptionCode&);
    void setStartBefore(Node*, ExceptionCode&);
kocienda's avatar
kocienda committed
96

eric@webkit.org's avatar
eric@webkit.org committed
97
98
    const Position startPosition() const { return m_start.toPosition(); }
    const Position endPosition() const { return m_end.toPosition(); }
kocienda's avatar
kocienda committed
99

darin@apple.com's avatar
darin@apple.com committed
100
101
    Node* firstNode() const;
    Node* pastLastNode() const;
darin's avatar
darin committed
102

kocienda's avatar
kocienda committed
103
104
    Position editingStartPosition() const;

adele@apple.com's avatar
adele@apple.com committed
105
106
    Node* shadowTreeRootNode() const;

thatcher's avatar
thatcher committed
107
    IntRect boundingBox();
108
    // Not transform-friendly
109
    void textRects(Vector<IntRect>&, bool useSelectionHeight = false);
110
111
    // Transform-friendly
    void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false);
112
    void getBorderAndTextQuads(Vector<FloatQuad>&) const;
thatcher's avatar
thatcher committed
113

114
    void nodeChildrenChanged(ContainerNode*);
ap@apple.com's avatar
ap@apple.com committed
115
    void nodeChildrenWillBeRemoved(ContainerNode*);
116
    void nodeWillBeRemoved(Node*);
darin@apple.com's avatar
darin@apple.com committed
117
118
119
120
121
122

    void textInserted(Node*, unsigned offset, unsigned length);
    void textRemoved(Node*, unsigned offset, unsigned length);
    void textNodesMerged(NodeWithIndex& oldNode, unsigned offset);
    void textNodeSplit(Text* oldNode);

123
124
125
126
127
    // Expand range to a unit (word or sentence or block or document) boundary.
    // Please refer to https://bugs.webkit.org/show_bug.cgi?id=27632 comment #5 
    // for details.
    void expand(const String&, ExceptionCode&);

128
129
130
    PassRefPtr<ClientRectList> getClientRects() const;
    PassRefPtr<ClientRect> getBoundingClientRect() const;

weinig's avatar
weinig committed
131
132
#ifndef NDEBUG
    void formatForDebugger(char* buffer, unsigned length) const;
kocienda's avatar
kocienda committed
133
134
#endif

darin's avatar
darin committed
135
private:
darin@apple.com's avatar
darin@apple.com committed
136
137
    Range(PassRefPtr<Document>);
    Range(PassRefPtr<Document>, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset);
kocienda's avatar
kocienda committed
138

139
140
    void setDocument(Document*);

141
    Node* checkNodeWOffset(Node*, int offset, ExceptionCode&) const;
darin's avatar
darin committed
142
    void checkNodeBA(Node*, ExceptionCode&) const;
darin's avatar
darin committed
143
    void checkDeleteExtract(ExceptionCode&);
darin's avatar
darin committed
144
    bool containedByReadOnly() const;
darin@apple.com's avatar
darin@apple.com committed
145
146
147
148
149
    int maxStartOffset() const;
    int maxEndOffset() const;

    enum ActionType { DELETE_CONTENTS, EXTRACT_CONTENTS, CLONE_CONTENTS };
    PassRefPtr<DocumentFragment> processContents(ActionType, ExceptionCode&);
150
151
152
    static PassRefPtr<Node> processContentsBetweenOffsets(ActionType, PassRefPtr<DocumentFragment>, Node*, unsigned startOffset, unsigned endOffset, ExceptionCode&);
    enum ContentsProcessDirection { ProcessContentsForward, ProcessContentsBackward };
    static PassRefPtr<Node> processAncestorsAndTheirSiblings(ActionType, Node* container, ContentsProcessDirection, PassRefPtr<Node> clonedContainer, Node* commonRoot, ExceptionCode&);
darin@apple.com's avatar
darin@apple.com committed
153
154

    RefPtr<Document> m_ownerDocument;
155
156
    RangeBoundaryPoint m_start;
    RangeBoundaryPoint m_end;
kocienda's avatar
kocienda committed
157
158
};

darin's avatar
darin committed
159
PassRefPtr<Range> rangeOfContents(Node*);
darin's avatar
darin committed
160

161
bool areRangesEqual(const Range*, const Range*);
darin's avatar
darin committed
162

darin's avatar
darin committed
163
} // namespace
kocienda's avatar
kocienda committed
164

165
166
167
168
169
#ifndef NDEBUG
// Outside the WebCore namespace for ease of invocation from gdb.
void showTree(const WebCore::Range*);
#endif

kocienda's avatar
kocienda committed
170
#endif