Home | History | Annotate | Download | only in dom
      1 /*
      2  * (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  * (C) 2000 Gunnstein Lye (gunnstein (at) netcom.no)
      4  * (C) 2000 Frederik Holljen (frederik.holljen (at) hig.no)
      5  * (C) 2001 Peter Kelly (pmk (at) 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/v8/ExceptionStatePlaceholder.h"
     29 #include "bindings/v8/ScriptWrappable.h"
     30 #include "core/dom/RangeBoundaryPoint.h"
     31 #include "platform/geometry/FloatRect.h"
     32 #include "platform/geometry/IntRect.h"
     33 #include "wtf/Forward.h"
     34 #include "wtf/RefCounted.h"
     35 #include "wtf/Vector.h"
     36 
     37 namespace WebCore {
     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 : public RefCounted<Range>, public ScriptWrappable {
     51 public:
     52     static PassRefPtr<Range> create(Document&);
     53     static PassRefPtr<Range> create(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
     54     static PassRefPtr<Range> create(Document&, const Position&, const Position&);
     55     ~Range();
     56 
     57     Document& ownerDocument() const { ASSERT(m_ownerDocument); return *m_ownerDocument.get(); }
     58     Node* startContainer() const { return m_start.container(); }
     59     int startOffset() const { return m_start.offset(); }
     60     Node* endContainer() const { return m_end.container(); }
     61     int endOffset() const { return m_end.offset(); }
     62 
     63     Node* startContainer(ExceptionState&) const;
     64     int startOffset(ExceptionState&) const;
     65     Node* endContainer(ExceptionState&) const;
     66     int endOffset(ExceptionState&) const;
     67     bool collapsed(ExceptionState&) const;
     68 
     69     Node* commonAncestorContainer(ExceptionState&) const;
     70     static Node* commonAncestorContainer(Node* containerA, Node* containerB);
     71     void setStart(PassRefPtr<Node> container, int offset, ExceptionState& = ASSERT_NO_EXCEPTION);
     72     void setEnd(PassRefPtr<Node> container, int offset, ExceptionState& = ASSERT_NO_EXCEPTION);
     73     void collapse(bool toStart, ExceptionState&);
     74     bool isPointInRange(Node* refNode, int offset, ExceptionState&);
     75     short comparePoint(Node* refNode, int offset, ExceptionState&) const;
     76     enum CompareResults { NODE_BEFORE, NODE_AFTER, NODE_BEFORE_AND_AFTER, NODE_INSIDE };
     77     CompareResults compareNode(Node* refNode, ExceptionState&) const;
     78     enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START };
     79     short compareBoundaryPoints(CompareHow, const Range* sourceRange, ExceptionState&) const;
     80     static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB, ExceptionState&);
     81     static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB, ExceptionState&);
     82     bool boundaryPointsValid() const;
     83     bool intersectsNode(Node* refNode, ExceptionState&);
     84     void deleteContents(ExceptionState&);
     85     PassRefPtr<DocumentFragment> extractContents(ExceptionState&);
     86     PassRefPtr<DocumentFragment> cloneContents(ExceptionState&);
     87     void insertNode(PassRefPtr<Node>, ExceptionState&);
     88     String toString(ExceptionState&) const;
     89 
     90     String toHTML() const;
     91     String text() const;
     92 
     93     PassRefPtr<DocumentFragment> createContextualFragment(const String& html, ExceptionState&);
     94 
     95     void detach(ExceptionState&);
     96     PassRefPtr<Range> cloneRange(ExceptionState&) const;
     97 
     98     void setStartAfter(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
     99     void setEndBefore(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
    100     void setEndAfter(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
    101     void selectNode(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
    102     void selectNodeContents(Node*, ExceptionState&);
    103     void surroundContents(PassRefPtr<Node>, ExceptionState&);
    104     void setStartBefore(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
    105 
    106     const Position startPosition() const { return m_start.toPosition(); }
    107     const Position endPosition() const { return m_end.toPosition(); }
    108     void setStart(const Position&, ExceptionState& = ASSERT_NO_EXCEPTION);
    109     void setEnd(const Position&, ExceptionState& = ASSERT_NO_EXCEPTION);
    110 
    111     Node* firstNode() const;
    112     Node* pastLastNode() const;
    113 
    114     ShadowRoot* shadowRoot() const;
    115 
    116     enum RangeInFixedPosition {
    117         NotFixedPosition,
    118         PartiallyFixedPosition,
    119         EntirelyFixedPosition
    120     };
    121 
    122     // Not transform-friendly
    123     void textRects(Vector<IntRect>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
    124     IntRect boundingBox() const;
    125 
    126     // Transform-friendly
    127     void textQuads(Vector<FloatQuad>&, bool useSelectionHeight = false, RangeInFixedPosition* = 0) const;
    128     void getBorderAndTextQuads(Vector<FloatQuad>&) const;
    129     FloatRect boundingRect() const;
    130 
    131     void nodeChildrenChanged(ContainerNode*);
    132     void nodeChildrenWillBeRemoved(ContainerNode*);
    133     void nodeWillBeRemoved(Node&);
    134 
    135     void didInsertText(Node*, unsigned offset, unsigned length);
    136     void didRemoveText(Node*, unsigned offset, unsigned length);
    137     void didMergeTextNodes(NodeWithIndex& oldNode, unsigned offset);
    138     void didSplitTextNode(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     void expand(const String&, ExceptionState&);
    144 
    145     PassRefPtr<ClientRectList> getClientRects() const;
    146     PassRefPtr<ClientRect> getBoundingClientRect() const;
    147 
    148 #ifndef NDEBUG
    149     void formatForDebugger(char* buffer, unsigned length) const;
    150 #endif
    151 
    152 private:
    153     explicit Range(Document&);
    154     Range(Document&, Node* startContainer, int startOffset, Node* endContainer, int endOffset);
    155 
    156     void setDocument(Document&);
    157 
    158     Node* checkNodeWOffset(Node*, int offset, ExceptionState&) const;
    159     void checkNodeBA(Node*, ExceptionState&) const;
    160     void checkDeleteExtract(ExceptionState&);
    161     int maxStartOffset() const;
    162     int maxEndOffset() const;
    163 
    164     enum ActionType { DELETE_CONTENTS, EXTRACT_CONTENTS, CLONE_CONTENTS };
    165     PassRefPtr<DocumentFragment> processContents(ActionType, ExceptionState&);
    166     static PassRefPtr<Node> processContentsBetweenOffsets(ActionType, PassRefPtr<DocumentFragment>, Node*, unsigned startOffset, unsigned endOffset, ExceptionState&);
    167     static void processNodes(ActionType, Vector<RefPtr<Node> >&, PassRefPtr<Node> oldContainer, PassRefPtr<Node> newContainer, ExceptionState&);
    168     enum ContentsProcessDirection { ProcessContentsForward, ProcessContentsBackward };
    169     static PassRefPtr<Node> processAncestorsAndTheirSiblings(ActionType, Node* container, ContentsProcessDirection, PassRefPtr<Node> clonedContainer, Node* commonRoot, ExceptionState&);
    170 
    171     RefPtr<Document> m_ownerDocument; // Cannot be null.
    172     RangeBoundaryPoint m_start;
    173     RangeBoundaryPoint m_end;
    174 };
    175 
    176 PassRefPtr<Range> rangeOfContents(Node*);
    177 
    178 bool areRangesEqual(const Range*, const Range*);
    179 
    180 } // namespace
    181 
    182 #ifndef NDEBUG
    183 // Outside the WebCore namespace for ease of invocation from gdb.
    184 void showTree(const WebCore::Range*);
    185 #endif
    186 
    187 #endif
    188