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 "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"
43 class DocumentFragment;
50 class Range FINAL : public RefCountedWillBeGarbageCollectedFinalized<Range>, public ScriptWrappable {
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&);
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(); }
63 bool collapsed() const { return m_start == m_end; }
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;
86 String toHTML() const;
89 PassRefPtrWillBeRawPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionState&);
92 PassRefPtrWillBeRawPtr<Range> cloneRange() const;
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);
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);
107 Node* firstNode() const;
108 Node* pastLastNode() const;
110 ShadowRoot* shadowRoot() const;
112 enum RangeInFixedPosition {
114 PartiallyFixedPosition,
115 EntirelyFixedPosition
118 // Not transform-friendly
119 void textRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
120 IntRect boundingBox() const;
122 // Transform-friendly
123 void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
124 void getBorderAndTextQuads(Vector<FloatQuad>&) const;
125 FloatRect boundingRect() const;
127 void nodeChildrenChanged(ContainerNode*);
128 void nodeChildrenWillBeRemoved(ContainerNode&);
129 void nodeWillBeRemoved(Node&);
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();
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
140 void expand(const String&, ExceptionState&);
142 PassRefPtrWillBeRawPtr<ClientRectList> getClientRects() const;
143 PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect() const;
146 void formatForDebugger(char* buffer, unsigned length) const;
149 void trace(Visitor*);
152 explicit Range(Document&);
153 Range(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
155 void setDocument(Document&);
157 Node* checkNodeWOffset(Node*, int offset, ExceptionState&) const;
158 void checkNodeBA(Node*, ExceptionState&) const;
159 void checkDeleteExtract(ExceptionState&);
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&);
168 RefPtrWillBeMember<Document> m_ownerDocument; // Cannot be null.
169 RangeBoundaryPoint m_start;
170 RangeBoundaryPoint m_end;
173 PassRefPtrWillBeRawPtr<Range> rangeOfContents(Node*);
175 bool areRangesEqual(const Range*, const Range*);
180 // Outside the WebCore namespace for ease of invocation from gdb.
181 void showTree(const WebCore::Range*);