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 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 "RangeBoundaryPoint.h" 29 #include <wtf/RefCounted.h> 30 #include <wtf/Vector.h> 31 32 namespace WebCore { 33 34 class DocumentFragment; 35 class NodeWithIndex; 36 class Text; 37 38 class Range : public RefCounted<Range> { 39 public: 40 static PassRefPtr<Range> create(PassRefPtr<Document>); 41 static PassRefPtr<Range> create(PassRefPtr<Document>, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset); 42 static PassRefPtr<Range> create(PassRefPtr<Document>, const Position&, const Position&); 43 ~Range(); 44 ownerDocument()45 Document* ownerDocument() const { return m_ownerDocument.get(); } startContainer()46 Node* startContainer() const { return m_start.container(); } startOffset()47 int startOffset() const { return m_start.offset(); } endContainer()48 Node* endContainer() const { return m_end.container(); } endOffset()49 int endOffset() const { return m_end.offset(); } 50 51 Node* startContainer(ExceptionCode&) const; 52 int startOffset(ExceptionCode&) const; 53 Node* endContainer(ExceptionCode&) const; 54 int endOffset(ExceptionCode&) const; 55 bool collapsed(ExceptionCode&) const; 56 57 Node* commonAncestorContainer(ExceptionCode&) const; 58 static Node* commonAncestorContainer(Node* containerA, Node* containerB); 59 void setStart(PassRefPtr<Node> container, int offset, ExceptionCode&); 60 void setEnd(PassRefPtr<Node> container, int offset, ExceptionCode&); 61 void collapse(bool toStart, ExceptionCode&); 62 bool isPointInRange(Node* refNode, int offset, ExceptionCode&); 63 short comparePoint(Node* refNode, int offset, ExceptionCode&) const; 64 enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE }; 65 CompareResults compareNode(Node* refNode, ExceptionCode&) const; 66 enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START }; 67 short compareBoundaryPoints(CompareHow, const Range* sourceRange, ExceptionCode&) const; 68 static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB); 69 static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB); 70 bool boundaryPointsValid() const; 71 bool intersectsNode(Node* refNode, ExceptionCode&); 72 void deleteContents(ExceptionCode&); 73 PassRefPtr<DocumentFragment> extractContents(ExceptionCode&); 74 PassRefPtr<DocumentFragment> cloneContents(ExceptionCode&); 75 void insertNode(PassRefPtr<Node>, ExceptionCode&); 76 String toString(ExceptionCode&) const; 77 78 String toHTML() const; 79 String text() const; 80 81 PassRefPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionCode&) const; 82 83 void detach(ExceptionCode&); 84 PassRefPtr<Range> cloneRange(ExceptionCode&) const; 85 86 void setStartAfter(Node*, ExceptionCode&); 87 void setEndBefore(Node*, ExceptionCode&); 88 void setEndAfter(Node*, ExceptionCode&); 89 void selectNode(Node*, ExceptionCode&); 90 void selectNodeContents(Node*, ExceptionCode&); 91 void surroundContents(PassRefPtr<Node>, ExceptionCode&); 92 void setStartBefore(Node*, ExceptionCode&); 93 startPosition()94 const Position startPosition() const { return m_start.toPosition(); } endPosition()95 const Position endPosition() const { return m_end.toPosition(); } 96 97 Node* firstNode() const; 98 Node* pastLastNode() const; 99 100 Position editingStartPosition() const; 101 102 Node* shadowTreeRootNode() const; 103 104 IntRect boundingBox(); 105 void textRects(Vector<IntRect>&, bool useSelectionHeight = false); 106 107 void nodeChildrenChanged(ContainerNode*); 108 void nodeWillBeRemoved(Node*); 109 110 void textInserted(Node*, unsigned offset, unsigned length); 111 void textRemoved(Node*, unsigned offset, unsigned length); 112 void textNodesMerged(NodeWithIndex& oldNode, unsigned offset); 113 void textNodeSplit(Text* oldNode); 114 115 #ifndef NDEBUG 116 void formatForDebugger(char* buffer, unsigned length) const; 117 #endif 118 119 private: 120 Range(PassRefPtr<Document>); 121 Range(PassRefPtr<Document>, PassRefPtr<Node> startContainer, int startOffset, PassRefPtr<Node> endContainer, int endOffset); 122 123 Node* checkNodeWOffset(Node*, int offset, ExceptionCode&) const; 124 void checkNodeBA(Node*, ExceptionCode&) const; 125 void checkDeleteExtract(ExceptionCode&); 126 bool containedByReadOnly() const; 127 int maxStartOffset() const; 128 int maxEndOffset() const; 129 130 enum ActionType { DELETE_CONTENTS, EXTRACT_CONTENTS, CLONE_CONTENTS }; 131 PassRefPtr<DocumentFragment> processContents(ActionType, ExceptionCode&); 132 133 RefPtr<Document> m_ownerDocument; 134 RangeBoundaryPoint m_start; 135 RangeBoundaryPoint m_end; 136 }; 137 138 PassRefPtr<Range> rangeOfContents(Node*); 139 140 bool operator==(const Range&, const Range&); 141 inline bool operator!=(const Range& a, const Range& b) { return !(a == b); } 142 143 } // namespace 144 145 #endif 146