1/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WebNode_h
32#define WebNode_h
33
34#include "WebCommon.h"
35#include "WebPrivatePtr.h"
36#include "WebString.h"
37
38namespace WebCore { class Node; }
39
40namespace WebKit {
41class WebDOMEventListener;
42class WebDOMEventListenerPrivate;
43class WebDocument;
44class WebFrame;
45class WebNodeList;
46
47// Provides access to some properties of a DOM node.
48class WebNode {
49public:
50    virtual ~WebNode() { reset(); }
51
52    WebNode() { }
53    WebNode(const WebNode& n) { assign(n); }
54    WebNode& operator=(const WebNode& n)
55    {
56        assign(n);
57        return *this;
58    }
59
60    WEBKIT_API void reset();
61    WEBKIT_API void assign(const WebNode&);
62
63    WEBKIT_API bool equals(const WebNode&) const;
64    // Required for using WebNodes in std maps.  Note the order used is
65    // arbitrary and should not be expected to have any specific meaning.
66    WEBKIT_API bool lessThan(const WebNode&) const;
67
68    bool isNull() const { return m_private.isNull(); }
69
70    enum NodeType {
71        ElementNode = 1,
72        AttributeNode,
73        TextNode,
74        CDataSectionNode,
75        EntityReferenceNode,
76        EntityNode,
77        ProcessingInstructionsNode,
78        CommentNode,
79        DocumentNode,
80        DocumentTypeNode,
81        DocumentFragmentNode,
82        NotationNode,
83        XPathNamespaceNode
84    };
85    WEBKIT_API NodeType nodeType() const;
86    WEBKIT_API WebNode parentNode() const;
87    WEBKIT_API WebString nodeName() const;
88    WEBKIT_API WebString nodeValue() const;
89    WEBKIT_API bool setNodeValue(const WebString&);
90    WEBKIT_API WebDocument document() const;
91    WEBKIT_API WebNode firstChild() const;
92    WEBKIT_API WebNode lastChild() const;
93    WEBKIT_API WebNode previousSibling() const;
94    WEBKIT_API WebNode nextSibling() const;
95    WEBKIT_API bool hasChildNodes() const;
96    WEBKIT_API WebNodeList childNodes();
97    WEBKIT_API WebString createMarkup() const;
98    WEBKIT_API bool isTextNode() const;
99    WEBKIT_API bool isFocusable() const;
100    WEBKIT_API bool isContentEditable() const;
101    WEBKIT_API bool isElementNode() const;
102    WEBKIT_API void addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture);
103    WEBKIT_API void removeEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture);
104    WEBKIT_API void simulateClick();
105    WEBKIT_API WebNodeList getElementsByTagName(const WebString&) const;
106
107    // Returns true if the node has a non-empty bounding box in layout.
108    // This does not 100% guarantee the user can see it, but is pretty close.
109    // Note: This method only works properly after layout has occurred.
110    WEBKIT_API bool hasNonEmptyBoundingBox() const;
111
112    template<typename T> T to()
113    {
114        T res;
115        res.WebNode::assign(*this);
116        return res;
117    }
118
119    template<typename T> const T toConst() const
120    {
121        T res;
122        res.WebNode::assign(*this);
123        return res;
124    }
125
126#if WEBKIT_IMPLEMENTATION
127    WebNode(const WTF::PassRefPtr<WebCore::Node>&);
128    WebNode& operator=(const WTF::PassRefPtr<WebCore::Node>&);
129    operator WTF::PassRefPtr<WebCore::Node>() const;
130#endif
131
132#if WEBKIT_IMPLEMENTATION
133    template<typename T> T* unwrap()
134    {
135        return static_cast<T*>(m_private.get());
136    }
137
138    template<typename T> const T* constUnwrap() const
139    {
140        return static_cast<const T*>(m_private.get());
141    }
142#endif
143
144protected:
145    WebPrivatePtr<WebCore::Node> m_private;
146};
147
148inline bool operator==(const WebNode& a, const WebNode& b)
149{
150    return a.equals(b);
151}
152
153inline bool operator!=(const WebNode& a, const WebNode& b)
154{
155    return !(a == b);
156}
157
158inline bool operator<(const WebNode& a, const WebNode& b)
159{
160    return a.lessThan(b);
161}
162
163} // namespace WebKit
164
165#endif
166