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/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" 36 37 namespace blink { 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 DEFINE_WRAPPERTYPEINFO(); 52 public: 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&); 56 ~Range(); 57 ownerDocument()58 Document& ownerDocument() const { ASSERT(m_ownerDocument); return *m_ownerDocument.get(); } startContainer()59 Node* startContainer() const { return m_start.container(); } startOffset()60 int startOffset() const { return m_start.offset(); } endContainer()61 Node* endContainer() const { return m_end.container(); } endOffset()62 int endOffset() const { return m_end.offset(); } 63 collapsed()64 bool collapsed() const { return m_start == m_end; } 65 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; 87 88 String toHTML() const; 89 String text() const; 90 91 PassRefPtrWillBeRawPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionState&); 92 93 void detach(); 94 PassRefPtrWillBeRawPtr<Range> cloneRange() const; 95 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); 104 startPosition()105 const Position startPosition() const { return m_start.toPosition(); } endPosition()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); 109 110 Node* firstNode() const; 111 Node* pastLastNode() const; 112 113 ShadowRoot* shadowRoot() const; 114 115 enum RangeInFixedPosition { 116 NotFixedPosition, 117 PartiallyFixedPosition, 118 EntirelyFixedPosition 119 }; 120 121 // Not transform-friendly 122 void textRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const; 123 IntRect boundingBox() const; 124 125 // Transform-friendly 126 void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const; 127 void getBorderAndTextQuads(Vector<FloatQuad>&) const; 128 FloatRect boundingRect() const; 129 130 void nodeChildrenChanged(ContainerNode*); 131 void nodeChildrenWillBeRemoved(ContainerNode&); 132 void nodeWillBeRemoved(Node&); 133 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(); 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 void expand(const String&, ExceptionState&); 144 145 PassRefPtrWillBeRawPtr<ClientRectList> getClientRects() const; 146 PassRefPtrWillBeRawPtr<ClientRect> getBoundingClientRect() const; 147 148 #ifndef NDEBUG 149 void formatForDebugger(char* buffer, unsigned length) const; 150 #endif 151 152 void trace(Visitor*); 153 154 private: 155 explicit Range(Document&); 156 Range(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset); 157 158 void setDocument(Document&); 159 160 Node* checkNodeWOffset(Node*, int offset, ExceptionState&) const; 161 void checkNodeBA(Node*, ExceptionState&) const; 162 void checkExtractPrecondition(ExceptionState&); 163 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&); 170 171 RefPtrWillBeMember<Document> m_ownerDocument; // Cannot be null. 172 RangeBoundaryPoint m_start; 173 RangeBoundaryPoint m_end; 174 }; 175 176 PassRefPtrWillBeRawPtr<Range> rangeOfContents(Node*); 177 178 bool areRangesEqual(const Range*, const Range*); 179 180 } // namespace blink 181 182 #ifndef NDEBUG 183 // Outside the WebCore namespace for ease of invocation from gdb. 184 void showTree(const blink::Range*); 185 #endif 186 187 #endif // Range_h 188