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/core/v8/ExceptionStatePlaceholder.h"
29 #include "bindings/core/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 {
51 DEFINE_WRAPPERTYPEINFO();
53 static PassRefPtrWillBeRawPtr<Range> create(Document&);
54 static PassRefPtrWillBeRawPtr<Range> create(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
55 static PassRefPtrWillBeRawPtr<Range> create(Document&, const Position&, const Position&);
58 Document& ownerDocument() const { ASSERT(m_ownerDocument); return *m_ownerDocument.get(); }
59 Node* startContainer() const { return m_start.container(); }
60 int startOffset() const { return m_start.offset(); }
61 Node* endContainer() const { return m_end.container(); }
62 int endOffset() const { return m_end.offset(); }
64 bool collapsed() const { return m_start == m_end; }
66 Node* commonAncestorContainer() const;
67 static Node* commonAncestorContainer(Node* containerA, Node* containerB);
68 void setStart(PassRefPtrWillBeRawPtr<Node> container, int offset, ExceptionState& = ASSERT_NO_EXCEPTION);
69 void setEnd(PassRefPtrWillBeRawPtr<Node> container, int offset, ExceptionState& = ASSERT_NO_EXCEPTION);
70 void collapse(bool toStart);
71 bool isPointInRange(Node* refNode, int offset, ExceptionState&);
72 short comparePoint(Node* refNode, int offset, ExceptionState&) const;
73 enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE };
74 CompareResults compareNode(Node* refNode, ExceptionState&) const;
75 enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START };
76 short compareBoundaryPoints(unsigned how, const Range* sourceRange, ExceptionState&) const;
77 static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB, ExceptionState&);
78 static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB, ExceptionState&);
79 bool boundaryPointsValid() const;
80 bool intersectsNode(Node* refNode, ExceptionState&);
81 static bool intersectsNode(Node* refNode, const Position& start, const Position& end, ExceptionState&);
82 void deleteContents(ExceptionState&);
83 PassRefPtrWillBeRawPtr<DocumentFragment> extractContents(ExceptionState&);
84 PassRefPtrWillBeRawPtr<DocumentFragment> cloneContents(ExceptionState&);
85 void insertNode(PassRefPtrWillBeRawPtr<Node>, ExceptionState&);
86 String toString() const;
88 String toHTML() const;
91 PassRefPtrWillBeRawPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionState&);
94 PassRefPtrWillBeRawPtr<Range> cloneRange() const;
96 void setStartAfter(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
97 void setEndBefore(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
98 void setEndAfter(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
99 void selectNode(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
100 void selectNodeContents(Node*, ExceptionState&);
101 static bool selectNodeContents(Node*, Position&, Position&);
102 void surroundContents(PassRefPtrWillBeRawPtr<Node>, ExceptionState&);
103 void setStartBefore(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
105 const Position startPosition() const { return m_start.toPosition(); }
106 const Position endPosition() const { return m_end.toPosition(); }
107 void setStart(const Position&, ExceptionState& = ASSERT_NO_EXCEPTION);
108 void setEnd(const Position&, ExceptionState& = ASSERT_NO_EXCEPTION);
110 Node* firstNode() const;
111 Node* pastLastNode() const;
113 ShadowRoot* shadowRoot() const;
115 enum RangeInFixedPosition {
117 PartiallyFixedPosition,
118 EntirelyFixedPosition
121 // Not transform-friendly
122 void textRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
123 IntRect boundingBox() const;
125 // Transform-friendly
126 void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
127 void getBorderAndTextQuads(Vector<FloatQuad>&) const;
128 FloatRect boundingRect() const;
130 void nodeChildrenChanged(ContainerNode*);
131 void nodeChildrenWillBeRemoved(ContainerNode&);
132 void nodeWillBeRemoved(Node&);
134 void didInsertText(Node*, unsigned offset, unsigned length);
135 void didRemoveText(Node*, unsigned offset, unsigned length);
136 void didMergeTextNodes(const NodeWithIndex& oldNode, unsigned offset);
137 void didSplitTextNode(Text& oldNode);
138 void updateOwnerDocumentIfNeeded();
140 // Expand range to a unit (word or sentence or block or document) boundary.
141 // Please refer to https://bugs.webkit.org/show_bug.cgi?id=27632 comment #5
143 void expand(const String&, ExceptionState&);
145 PassRefPtrWillBeRawPtr<ClientRectList> getClientRects() const;
146 PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect() const;
149 void formatForDebugger(char* buffer, unsigned length) const;
152 void trace(Visitor*);
155 explicit Range(Document&);
156 Range(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
158 void setDocument(Document&);
160 Node* checkNodeWOffset(Node*, int offset, ExceptionState&) const;
161 void checkNodeBA(Node*, ExceptionState&) const;
162 void checkExtractPrecondition(ExceptionState&);
164 enum ActionType { DELETE_CONTENTS, EXTRACT_CONTENTS, CLONE_CONTENTS };
165 PassRefPtrWillBeRawPtr<DocumentFragment> processContents(ActionType, ExceptionState&);
166 static PassRefPtrWillBeRawPtr<Node> processContentsBetweenOffsets(ActionType, PassRefPtrWillBeRawPtr<DocumentFragment>, Node*, unsigned startOffset, unsigned endOffset, ExceptionState&);
167 static void processNodes(ActionType, WillBeHeapVector<RefPtrWillBeMember<Node> >&, PassRefPtrWillBeRawPtr<Node> oldContainer, PassRefPtrWillBeRawPtr<Node> newContainer, ExceptionState&);
168 enum ContentsProcessDirection { ProcessContentsForward, ProcessContentsBackward };
169 static PassRefPtrWillBeRawPtr<Node> processAncestorsAndTheirSiblings(ActionType, Node* container, ContentsProcessDirection, PassRefPtrWillBeRawPtr<Node> clonedContainer, Node* commonRoot, ExceptionState&);
171 RefPtrWillBeMember<Document> m_ownerDocument; // Cannot be null.
172 RangeBoundaryPoint m_start;
173 RangeBoundaryPoint m_end;
176 PassRefPtrWillBeRawPtr<Range> rangeOfContents(Node*);
178 bool areRangesEqual(const Range*, const Range*);
183 // Outside the WebCore namespace for ease of invocation from gdb.
184 void showTree(const blink::Range*);