Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / Range.h
1 /*
2  * (C) 1999 Lars Knoll (knoll@kde.org)
3  * (C) 2000 Gunnstein Lye (gunnstein@netcom.no)
4  * (C) 2000 Frederik Holljen (frederik.holljen@hig.no)
5  * (C) 2001 Peter Kelly (pmk@post.com)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #ifndef Range_h
26 #define Range_h
27
28 #include "bindings/v8/ExceptionStatePlaceholder.h"
29 #include "bindings/v8/ScriptWrappable.h"
30 #include "core/dom/RangeBoundaryPoint.h"
31 #include "platform/geometry/FloatRect.h"
32 #include "platform/geometry/IntRect.h"
33 #include "platform/heap/Handle.h"
34 #include "wtf/Forward.h"
35 #include "wtf/RefCounted.h"
36
37 namespace WebCore {
38
39 class ClientRect;
40 class ClientRectList;
41 class ContainerNode;
42 class Document;
43 class DocumentFragment;
44 class ExceptionState;
45 class FloatQuad;
46 class Node;
47 class NodeWithIndex;
48 class Text;
49
50 class Range FINAL : public RefCountedWillBeGarbageCollectedFinalized<Range>, public ScriptWrappable {
51 public:
52     static PassRefPtrWillBeRawPtr<Range> create(Document&);
53     static PassRefPtrWillBeRawPtr<Range> create(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
54     static PassRefPtrWillBeRawPtr<Range> create(Document&, const Position&, const Position&);
55     ~Range();
56
57     Document& ownerDocument() const { ASSERT(m_ownerDocument); return *m_ownerDocument.get(); }
58     Node* startContainer() const { return m_start.container(); }
59     int startOffset() const { return m_start.offset(); }
60     Node* endContainer() const { return m_end.container(); }
61     int endOffset() const { return m_end.offset(); }
62
63     bool collapsed() const { return m_start == m_end; }
64
65     Node* commonAncestorContainer() const;
66     static Node* commonAncestorContainer(Node* containerA, Node* containerB);
67     void setStart(PassRefPtrWillBeRawPtr<Node> container, int offset, ExceptionState& = ASSERT_NO_EXCEPTION);
68     void setEnd(PassRefPtrWillBeRawPtr<Node> container, int offset, ExceptionState& = ASSERT_NO_EXCEPTION);
69     void collapse(bool toStart);
70     bool isPointInRange(Node* refNode, int offset, ExceptionState&);
71     short comparePoint(Node* refNode, int offset, ExceptionState&) const;
72     enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE };
73     CompareResults compareNode(Node* refNode, ExceptionState&) const;
74     enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START };
75     short compareBoundaryPoints(CompareHow, const Range* sourceRange, ExceptionState&) const;
76     static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB, ExceptionState&);
77     static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB, ExceptionState&);
78     bool boundaryPointsValid() const;
79     bool intersectsNode(Node* refNode, ExceptionState&);
80     void deleteContents(ExceptionState&);
81     PassRefPtrWillBeRawPtr<DocumentFragment> extractContents(ExceptionState&);
82     PassRefPtrWillBeRawPtr<DocumentFragment> cloneContents(ExceptionState&);
83     void insertNode(PassRefPtrWillBeRawPtr<Node>, ExceptionState&);
84     String toString() const;
85
86     String toHTML() const;
87     String text() const;
88
89     PassRefPtrWillBeRawPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionState&);
90
91     void detach();
92     PassRefPtrWillBeRawPtr<Range> cloneRange() const;
93
94     void setStartAfter(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
95     void setEndBefore(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
96     void setEndAfter(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
97     void selectNode(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
98     void selectNodeContents(Node*, ExceptionState&);
99     void surroundContents(PassRefPtrWillBeRawPtr<Node>, ExceptionState&);
100     void setStartBefore(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
101
102     const Position startPosition() const { return m_start.toPosition(); }
103     const Position endPosition() const { return m_end.toPosition(); }
104     void setStart(const Position&, ExceptionState& = ASSERT_NO_EXCEPTION);
105     void setEnd(const Position&, ExceptionState& = ASSERT_NO_EXCEPTION);
106
107     Node* firstNode() const;
108     Node* pastLastNode() const;
109
110     ShadowRoot* shadowRoot() const;
111
112     enum RangeInFixedPosition {
113         NotFixedPosition,
114         PartiallyFixedPosition,
115         EntirelyFixedPosition
116     };
117
118     // Not transform-friendly
119     void textRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
120     IntRect boundingBox() const;
121
122     // Transform-friendly
123     void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
124     void getBorderAndTextQuads(Vector<FloatQuad>&) const;
125     FloatRect boundingRect() const;
126
127     void nodeChildrenChanged(ContainerNode*);
128     void nodeChildrenWillBeRemoved(ContainerNode&);
129     void nodeWillBeRemoved(Node&);
130
131     void didInsertText(Node*, unsigned offset, unsigned length);
132     void didRemoveText(Node*, unsigned offset, unsigned length);
133     void didMergeTextNodes(const NodeWithIndex& oldNode, unsigned offset);
134     void didSplitTextNode(Text& oldNode);
135     void updateOwnerDocumentIfNeeded();
136
137     // Expand range to a unit (word or sentence or block or document) boundary.
138     // Please refer to https://bugs.webkit.org/show_bug.cgi?id=27632 comment #5
139     // for details.
140     void expand(const String&, ExceptionState&);
141
142     PassRefPtrWillBeRawPtr<ClientRectList> getClientRects() const;
143     PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect() const;
144
145 #ifndef NDEBUG
146     void formatForDebugger(char* buffer, unsigned length) const;
147 #endif
148
149     void trace(Visitor*);
150
151 private:
152     explicit Range(Document&);
153     Range(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
154
155     void setDocument(Document&);
156
157     Node* checkNodeWOffset(Node*, int offset, ExceptionState&) const;
158     void checkNodeBA(Node*, ExceptionState&) const;
159     void checkDeleteExtract(ExceptionState&);
160
161     enum ActionType { DELETE_CONTENTS, EXTRACT_CONTENTS, CLONE_CONTENTS };
162     PassRefPtrWillBeRawPtr<DocumentFragment> processContents(ActionType, ExceptionState&);
163     static PassRefPtrWillBeRawPtr<Node> processContentsBetweenOffsets(ActionType, PassRefPtrWillBeRawPtr<DocumentFragment>, Node*, unsigned startOffset, unsigned endOffset, ExceptionState&);
164     static void processNodes(ActionType, WillBeHeapVector<RefPtrWillBeMember<Node> >&, PassRefPtrWillBeRawPtr<Node> oldContainer, PassRefPtrWillBeRawPtr<Node> newContainer, ExceptionState&);
165     enum ContentsProcessDirection { ProcessContentsForward, ProcessContentsBackward };
166     static PassRefPtrWillBeRawPtr<Node> processAncestorsAndTheirSiblings(ActionType, Node* container, ContentsProcessDirection, PassRefPtrWillBeRawPtr<Node> clonedContainer, Node* commonRoot, ExceptionState&);
167
168     RefPtrWillBeMember<Document> m_ownerDocument; // Cannot be null.
169     RangeBoundaryPoint m_start;
170     RangeBoundaryPoint m_end;
171 };
172
173 PassRefPtrWillBeRawPtr<Range> rangeOfContents(Node*);
174
175 bool areRangesEqual(const Range*, const Range*);
176
177 } // namespace
178
179 #ifndef NDEBUG
180 // Outside the WebCore namespace for ease of invocation from gdb.
181 void showTree(const WebCore::Range*);
182 #endif
183
184 #endif