initial import
[vuplus_webkit] / Source / WebKit / chromium / public / WebNode.h
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
38 namespace WebCore { class Node; }
39
40 namespace WebKit {
41 class WebDOMEventListener;
42 class WebDOMEventListenerPrivate;
43 class WebDocument;
44 class WebFrame;
45 class WebNodeList;
46
47 // Provides access to some properties of a DOM node.
48 class WebNode {
49 public:
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_EXPORT void reset();
61     WEBKIT_EXPORT void assign(const WebNode&);
62
63     WEBKIT_EXPORT 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_EXPORT 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         ShadowRootNode
85     };
86     WEBKIT_EXPORT NodeType nodeType() const;
87     WEBKIT_EXPORT WebNode parentNode() const;
88     WEBKIT_EXPORT WebString nodeName() const;
89     WEBKIT_EXPORT WebString nodeValue() const;
90     WEBKIT_EXPORT bool setNodeValue(const WebString&);
91     WEBKIT_EXPORT WebDocument document() const;
92     WEBKIT_EXPORT WebNode firstChild() const;
93     WEBKIT_EXPORT WebNode lastChild() const;
94     WEBKIT_EXPORT WebNode previousSibling() const;
95     WEBKIT_EXPORT WebNode nextSibling() const;
96     WEBKIT_EXPORT bool hasChildNodes() const;
97     WEBKIT_EXPORT WebNodeList childNodes();
98     WEBKIT_EXPORT WebString createMarkup() const;
99     WEBKIT_EXPORT bool isLink() const;
100     WEBKIT_EXPORT bool isTextNode() const;
101     WEBKIT_EXPORT bool isFocusable() const;
102     WEBKIT_EXPORT bool isContentEditable() const;
103     WEBKIT_EXPORT bool isElementNode() const;
104     WEBKIT_EXPORT void addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture);
105     WEBKIT_EXPORT void removeEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture);
106     WEBKIT_EXPORT void simulateClick();
107     WEBKIT_EXPORT WebNodeList getElementsByTagName(const WebString&) const;
108
109     // Returns true if the node has a non-empty bounding box in layout.
110     // This does not 100% guarantee the user can see it, but is pretty close.
111     // Note: This method only works properly after layout has occurred.
112     WEBKIT_EXPORT bool hasNonEmptyBoundingBox() const;
113
114     template<typename T> T to()
115     {
116         T res;
117         res.WebNode::assign(*this);
118         return res;
119     }
120
121     template<typename T> const T toConst() const
122     {
123         T res;
124         res.WebNode::assign(*this);
125         return res;
126     }
127
128 #if WEBKIT_IMPLEMENTATION
129     WebNode(const WTF::PassRefPtr<WebCore::Node>&);
130     WebNode& operator=(const WTF::PassRefPtr<WebCore::Node>&);
131     operator WTF::PassRefPtr<WebCore::Node>() const;
132 #endif
133
134 #if WEBKIT_IMPLEMENTATION
135     template<typename T> T* unwrap()
136     {
137         return static_cast<T*>(m_private.get());
138     }
139
140     template<typename T> const T* constUnwrap() const
141     {
142         return static_cast<const T*>(m_private.get());
143     }
144 #endif
145
146 protected:
147     WebPrivatePtr<WebCore::Node> m_private;
148 };
149
150 inline bool operator==(const WebNode& a, const WebNode& b)
151 {
152     return a.equals(b);
153 }
154
155 inline bool operator!=(const WebNode& a, const WebNode& b)
156 {
157     return !(a == b);
158 }
159
160 inline bool operator<(const WebNode& a, const WebNode& b)
161 {
162     return a.lessThan(b);
163 }
164
165 } // namespace WebKit
166
167 #endif