1/*
2 * Copyright (C) 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2012 Google Inc.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31#ifndef DOMSelection_h
32#define DOMSelection_h
33
34#include "DOMWindowProperty.h"
35#include <wtf/Forward.h>
36#include <wtf/PassRefPtr.h>
37#include <wtf/RefCounted.h>
38
39namespace WebCore {
40
41    class Frame;
42    class Node;
43    class Position;
44    class Range;
45    class TreeScope;
46    class VisibleSelection;
47
48    typedef int ExceptionCode;
49
50    class DOMSelection : public RefCounted<DOMSelection>, public DOMWindowProperty {
51    public:
52        static PassRefPtr<DOMSelection> create(const TreeScope* treeScope) { return adoptRef(new DOMSelection(treeScope)); }
53
54        void clearTreeScope();
55
56        // Safari Selection Object API
57        // These methods return the valid equivalents of internal editing positions.
58        Node* baseNode() const;
59        Node* extentNode() const;
60        int baseOffset() const;
61        int extentOffset() const;
62        String type() const;
63        void setBaseAndExtent(Node* baseNode, int baseOffset, Node* extentNode, int extentOffset, ExceptionCode&);
64        void setPosition(Node*, int offset, ExceptionCode&);
65        void modify(const String& alter, const String& direction, const String& granularity);
66
67        // Mozilla Selection Object API
68        // In Firefox, anchor/focus are the equal to the start/end of the selection,
69        // but reflect the direction in which the selection was made by the user.  That does
70        // not mean that they are base/extent, since the base/extent don't reflect
71        // expansion.
72        // These methods return the valid equivalents of internal editing positions.
73        Node* anchorNode() const;
74        int anchorOffset() const;
75        Node* focusNode() const;
76        int focusOffset() const;
77        bool isCollapsed() const;
78        int rangeCount() const;
79        void collapse(Node*, int offset, ExceptionCode&);
80        void collapseToEnd(ExceptionCode&);
81        void collapseToStart(ExceptionCode&);
82        void extend(Node*, int offset, ExceptionCode&);
83        PassRefPtr<Range> getRangeAt(int, ExceptionCode&);
84        void removeAllRanges();
85        void addRange(Range*);
86        void deleteFromDocument();
87        bool containsNode(Node*, bool partlyContained) const;
88        void selectAllChildren(Node*, ExceptionCode&);
89
90        String toString();
91
92        // Microsoft Selection Object API
93        void empty();
94
95    private:
96        const TreeScope* m_treeScope;
97
98        explicit DOMSelection(const TreeScope*);
99
100        // Convenience method for accessors, does not NULL check m_frame.
101        const VisibleSelection& visibleSelection() const;
102
103        Node* shadowAdjustedNode(const Position&) const;
104        int shadowAdjustedOffset(const Position&) const;
105
106        bool isValidForPosition(Node*) const;
107    };
108
109} // namespace WebCore
110
111#endif // DOMSelection_h
112