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 | #pragma once |
26 | |
27 | #include "FloatRect.h" |
28 | #include "IntRect.h" |
29 | #include "RangeBoundaryPoint.h" |
30 | #include <wtf/Forward.h> |
31 | #include <wtf/RefCounted.h> |
32 | #include <wtf/Vector.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | class ContainerNode; |
37 | class DOMRect; |
38 | class DOMRectList; |
39 | class Document; |
40 | class DocumentFragment; |
41 | class FloatQuad; |
42 | class Node; |
43 | class NodeWithIndex; |
44 | class RenderText; |
45 | class SelectionRect; |
46 | class Text; |
47 | class VisiblePosition; |
48 | |
49 | class Range : public RefCounted<Range> { |
50 | public: |
51 | WEBCORE_EXPORT static Ref<Range> create(Document&); |
52 | WEBCORE_EXPORT static Ref<Range> create(Document&, RefPtr<Node>&& startContainer, int startOffset, RefPtr<Node>&& endContainer, int endOffset); |
53 | WEBCORE_EXPORT static Ref<Range> create(Document&, const Position&, const Position&); |
54 | WEBCORE_EXPORT static Ref<Range> create(Document&, const VisiblePosition&, const VisiblePosition&); |
55 | WEBCORE_EXPORT ~Range(); |
56 | |
57 | Document& ownerDocument() const { return m_ownerDocument; } |
58 | |
59 | Node& startContainer() const { ASSERT(m_start.container()); return *m_start.container(); } |
60 | unsigned startOffset() const { return m_start.offset(); } |
61 | Node& endContainer() const { ASSERT(m_end.container()); return *m_end.container(); } |
62 | unsigned endOffset() const { return m_end.offset(); } |
63 | bool collapsed() const { return m_start == m_end; } |
64 | |
65 | Node* commonAncestorContainer() const { return commonAncestorContainer(&startContainer(), &endContainer()); } |
66 | WEBCORE_EXPORT static Node* commonAncestorContainer(Node* containerA, Node* containerB); |
67 | WEBCORE_EXPORT ExceptionOr<void> setStart(Ref<Node>&& container, unsigned offset); |
68 | WEBCORE_EXPORT ExceptionOr<void> setEnd(Ref<Node>&& container, unsigned offset); |
69 | WEBCORE_EXPORT void collapse(bool toStart); |
70 | WEBCORE_EXPORT ExceptionOr<bool> isPointInRange(Node& refNode, unsigned offset); |
71 | WEBCORE_EXPORT ExceptionOr<short> comparePoint(Node& refNode, unsigned offset) const; |
72 | enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE }; |
73 | WEBCORE_EXPORT ExceptionOr<CompareResults> compareNode(Node& refNode) const; |
74 | enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START }; |
75 | WEBCORE_EXPORT ExceptionOr<short> compareBoundaryPoints(CompareHow, const Range& sourceRange) const; |
76 | WEBCORE_EXPORT ExceptionOr<short> compareBoundaryPointsForBindings(unsigned short compareHow, const Range& sourceRange) const; |
77 | static ExceptionOr<short> compareBoundaryPoints(Node* containerA, unsigned offsetA, Node* containerB, unsigned offsetB); |
78 | static ExceptionOr<short> compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB); |
79 | WEBCORE_EXPORT bool boundaryPointsValid() const; |
80 | WEBCORE_EXPORT ExceptionOr<bool> intersectsNode(Node& refNode) const; |
81 | WEBCORE_EXPORT ExceptionOr<void> deleteContents(); |
82 | WEBCORE_EXPORT ExceptionOr<Ref<DocumentFragment>> (); |
83 | WEBCORE_EXPORT ExceptionOr<Ref<DocumentFragment>> cloneContents(); |
84 | WEBCORE_EXPORT ExceptionOr<void> insertNode(Ref<Node>&&); |
85 | WEBCORE_EXPORT String toString() const; |
86 | |
87 | WEBCORE_EXPORT String text() const; |
88 | |
89 | WEBCORE_EXPORT ExceptionOr<Ref<DocumentFragment>> createContextualFragment(const String& html); |
90 | |
91 | WEBCORE_EXPORT void detach(); |
92 | WEBCORE_EXPORT Ref<Range> cloneRange() const; |
93 | |
94 | WEBCORE_EXPORT ExceptionOr<void> setStartAfter(Node&); |
95 | WEBCORE_EXPORT ExceptionOr<void> setEndBefore(Node&); |
96 | WEBCORE_EXPORT ExceptionOr<void> setEndAfter(Node&); |
97 | WEBCORE_EXPORT ExceptionOr<void> selectNode(Node&); |
98 | WEBCORE_EXPORT ExceptionOr<void> selectNodeContents(Node&); |
99 | WEBCORE_EXPORT ExceptionOr<void> surroundContents(Node&); |
100 | WEBCORE_EXPORT ExceptionOr<void> setStartBefore(Node&); |
101 | |
102 | const Position startPosition() const { return m_start.toPosition(); } |
103 | const Position endPosition() const { return m_end.toPosition(); } |
104 | WEBCORE_EXPORT ExceptionOr<void> setStart(const Position&); |
105 | WEBCORE_EXPORT ExceptionOr<void> setEnd(const Position&); |
106 | |
107 | WEBCORE_EXPORT Node* firstNode() const; |
108 | WEBCORE_EXPORT 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 | enum class RespectClippingForTextRects { No, Yes }; |
120 | WEBCORE_EXPORT void absoluteTextRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = nullptr, RespectClippingForTextRects = RespectClippingForTextRects::No) const; |
121 | WEBCORE_EXPORT IntRect absoluteBoundingBox() const; |
122 | |
123 | // Transform-friendly |
124 | WEBCORE_EXPORT void absoluteTextQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = nullptr) const; |
125 | WEBCORE_EXPORT FloatRect absoluteBoundingRect(RespectClippingForTextRects = RespectClippingForTextRects::No) const; |
126 | #if PLATFORM(IOS_FAMILY) |
127 | WEBCORE_EXPORT void collectSelectionRects(Vector<SelectionRect>&) const; |
128 | WEBCORE_EXPORT int collectSelectionRectsWithoutUnionInteriorLines(Vector<SelectionRect>&) const; |
129 | #endif |
130 | |
131 | void nodeChildrenChanged(ContainerNode&); |
132 | void nodeChildrenWillBeRemoved(ContainerNode&); |
133 | void nodeWillBeRemoved(Node&); |
134 | |
135 | void textInserted(Node&, unsigned offset, unsigned length); |
136 | void textRemoved(Node&, unsigned offset, unsigned length); |
137 | void textNodesMerged(NodeWithIndex& oldNode, unsigned offset); |
138 | void textNodeSplit(Text& oldNode); |
139 | |
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 |
142 | // for details. |
143 | WEBCORE_EXPORT ExceptionOr<void> expand(const String&); |
144 | |
145 | Ref<DOMRectList> getClientRects() const; |
146 | Ref<DOMRect> getBoundingClientRect() const; |
147 | |
148 | #if ENABLE(TREE_DEBUGGING) |
149 | void formatForDebugger(char* buffer, unsigned length) const; |
150 | #endif |
151 | |
152 | WEBCORE_EXPORT bool contains(const Range&) const; |
153 | bool contains(const VisiblePosition&) const; |
154 | |
155 | enum ActionType { Delete, , Clone }; |
156 | |
157 | private: |
158 | explicit Range(Document&); |
159 | Range(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset); |
160 | |
161 | void setDocument(Document&); |
162 | ExceptionOr<Node*> checkNodeWOffset(Node&, unsigned offset) const; |
163 | ExceptionOr<RefPtr<DocumentFragment>> processContents(ActionType); |
164 | |
165 | enum class CoordinateSpace { Absolute, Client }; |
166 | Vector<FloatRect> borderAndTextRects(CoordinateSpace, RespectClippingForTextRects = RespectClippingForTextRects::No) const; |
167 | FloatRect boundingRect(CoordinateSpace, RespectClippingForTextRects = RespectClippingForTextRects::No) const; |
168 | |
169 | Vector<FloatRect> absoluteRectsForRangeInText(Node*, RenderText&, bool useSelectionHeight, bool& isFixed, RespectClippingForTextRects) const; |
170 | |
171 | Ref<Document> m_ownerDocument; |
172 | RangeBoundaryPoint m_start; |
173 | RangeBoundaryPoint m_end; |
174 | }; |
175 | |
176 | WEBCORE_EXPORT Ref<Range> rangeOfContents(Node&); |
177 | |
178 | WEBCORE_EXPORT bool areRangesEqual(const Range*, const Range*); |
179 | WEBCORE_EXPORT bool rangesOverlap(const Range*, const Range*); |
180 | |
181 | inline bool documentOrderComparator(const Node* a, const Node* b) |
182 | { |
183 | return Range::compareBoundaryPoints(const_cast<Node*>(a), 0, const_cast<Node*>(b), 0).releaseReturnValue() < 0; |
184 | } |
185 | |
186 | WTF::TextStream& operator<<(WTF::TextStream&, const RangeBoundaryPoint&); |
187 | WTF::TextStream& operator<<(WTF::TextStream&, const Range&); |
188 | |
189 | } // namespace |
190 | |
191 | #if ENABLE(TREE_DEBUGGING) |
192 | // Outside the WebCore namespace for ease of invocation from the debugger. |
193 | void showTree(const WebCore::Range*); |
194 | #endif |
195 | |