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.
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.
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.
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.
28 #include "ExceptionCodePlaceholder.h"
29 #include "FloatRect.h"
30 #include "FragmentScriptingPermission.h"
32 #include "RangeBoundaryPoint.h"
33 #include <wtf/Forward.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/Vector.h>
43 class DocumentFragment;
50 class VisiblePosition;
53 class Range : public RefCounted<Range> {
55 static PassRefPtr<Range> create(Document&);
56 static PassRefPtr<Range> create(Document&, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset);
57 static PassRefPtr<Range> create(Document&, const Position&, const Position&);
58 static PassRefPtr<Range> create(ScriptExecutionContext&);
60 // FIXME: Consider making this a static non-member, non-friend function.
61 static PassRefPtr<Range> create(Document&, const VisiblePosition&, const VisiblePosition&);
65 Document& ownerDocument() const { return const_cast<Document&>(m_ownerDocument.get()); }
67 Node* startContainer() const { return m_start.container(); }
68 int startOffset() const { return m_start.offset(); }
69 Node* endContainer() const { return m_end.container(); }
70 int endOffset() const { return m_end.offset(); }
72 Node* startContainer(ExceptionCode&) const;
73 int startOffset(ExceptionCode&) const;
74 Node* endContainer(ExceptionCode&) const;
75 int endOffset(ExceptionCode&) const;
76 bool collapsed(ExceptionCode&) const;
78 Node* commonAncestorContainer(ExceptionCode&) const;
79 static Node* commonAncestorContainer(Node* containerA, Node* containerB);
80 void setStart(PassRefPtr<Node> container, int offset, ExceptionCode& = ASSERT_NO_EXCEPTION);
81 void setEnd(PassRefPtr<Node> container, int offset, ExceptionCode& = ASSERT_NO_EXCEPTION);
82 void collapse(bool toStart, ExceptionCode&);
83 bool isPointInRange(Node* refNode, int offset, ExceptionCode&);
84 short comparePoint(Node* refNode, int offset, ExceptionCode&) const;
85 enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE };
86 CompareResults compareNode(Node* refNode, ExceptionCode&) const;
87 enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START };
88 short compareBoundaryPoints(CompareHow, const Range* sourceRange, ExceptionCode&) const;
89 static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB, ExceptionCode&);
90 static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB, ExceptionCode&);
91 bool boundaryPointsValid() const;
92 bool intersectsNode(Node* refNode, ExceptionCode&);
93 void deleteContents(ExceptionCode&);
94 PassRefPtr<DocumentFragment> extractContents(ExceptionCode&);
95 PassRefPtr<DocumentFragment> cloneContents(ExceptionCode&);
96 void insertNode(PassRefPtr<Node>, ExceptionCode&);
97 String toString(ExceptionCode&) const;
99 String toHTML() const;
102 PassRefPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionCode&);
104 void detach(ExceptionCode&);
105 PassRefPtr<Range> cloneRange(ExceptionCode&) const;
107 void setStartAfter(Node*, ExceptionCode& = ASSERT_NO_EXCEPTION);
108 void setEndBefore(Node*, ExceptionCode& = ASSERT_NO_EXCEPTION);
109 void setEndAfter(Node*, ExceptionCode& = ASSERT_NO_EXCEPTION);
110 void selectNode(Node*, ExceptionCode& = ASSERT_NO_EXCEPTION);
111 void selectNodeContents(Node*, ExceptionCode&);
112 void surroundContents(PassRefPtr<Node>, ExceptionCode&);
113 void setStartBefore(Node*, ExceptionCode&);
115 const Position startPosition() const { return m_start.toPosition(); }
116 const Position endPosition() const { return m_end.toPosition(); }
117 void setStart(const Position&, ExceptionCode& = ASSERT_NO_EXCEPTION);
118 void setEnd(const Position&, ExceptionCode& = ASSERT_NO_EXCEPTION);
120 Node* firstNode() const;
121 Node* pastLastNode() const;
123 ShadowRoot* shadowRoot() const;
125 enum RangeInFixedPosition {
127 PartiallyFixedPosition,
128 EntirelyFixedPosition
131 // Not transform-friendly
132 void textRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
133 IntRect boundingBox() const;
135 // Transform-friendly
136 void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
137 void getBorderAndTextQuads(Vector<FloatQuad>&) const;
138 FloatRect boundingRect() const;
140 void collectSelectionRects(Vector<SelectionRect>&);
143 void nodeChildrenChanged(ContainerNode&);
144 void nodeChildrenWillBeRemoved(ContainerNode&);
145 void nodeWillBeRemoved(Node*);
147 void textInserted(Node*, unsigned offset, unsigned length);
148 void textRemoved(Node*, unsigned offset, unsigned length);
149 void textNodesMerged(NodeWithIndex& oldNode, unsigned offset);
150 void textNodeSplit(Text* oldNode);
152 // Expand range to a unit (word or sentence or block or document) boundary.
153 // Please refer to https://bugs.webkit.org/show_bug.cgi?id=27632 comment #5
155 void expand(const String&, ExceptionCode&);
157 PassRefPtr<ClientRectList> getClientRects() const;
158 PassRefPtr<ClientRect> getBoundingClientRect() const;
161 void formatForDebugger(char* buffer, unsigned length) const;
164 bool contains(const Range&) const;
167 explicit Range(Document&);
168 Range(Document&, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset);
170 void setDocument(Document&);
172 Node* checkNodeWOffset(Node*, int offset, ExceptionCode&) const;
173 void checkNodeBA(Node*, ExceptionCode&) const;
174 void checkDeleteExtract(ExceptionCode&);
175 bool containedByReadOnly() const;
177 enum ActionType { Delete, Extract, Clone };
178 PassRefPtr<DocumentFragment> processContents(ActionType, ExceptionCode&);
179 static PassRefPtr<Node> processContentsBetweenOffsets(ActionType, PassRefPtr<DocumentFragment>, Node*, unsigned startOffset, unsigned endOffset, ExceptionCode&);
180 static void processNodes(ActionType, Vector<RefPtr<Node>>&, PassRefPtr<Node> oldContainer, PassRefPtr<Node> newContainer, ExceptionCode&);
181 enum ContentsProcessDirection { ProcessContentsForward, ProcessContentsBackward };
182 static PassRefPtr<Node> processAncestorsAndTheirSiblings(ActionType, Node* container, ContentsProcessDirection, PassRefPtr<Node> clonedContainer, Node* commonRoot, ExceptionCode&);
184 Ref<Document> m_ownerDocument;
185 RangeBoundaryPoint m_start;
186 RangeBoundaryPoint m_end;
189 PassRefPtr<Range> rangeOfContents(Node&);
191 bool areRangesEqual(const Range*, const Range*);
192 bool rangesOverlap(const Range*, const Range*);
197 // Outside the WebCore namespace for ease of invocation from gdb.
198 void showTree(const WebCore::Range*);