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