Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / public / web / 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 "../platform/WebCommon.h"
35 #include "../platform/WebPrivatePtr.h"
36 #include "../platform/WebString.h"
37 #include "WebExceptionCode.h"
38
39 namespace WebCore { class Node; }
40
41 namespace blink {
42 class WebDOMEvent;
43 class WebDOMEventListener;
44 class WebDOMEventListenerPrivate;
45 class WebDocument;
46 class WebElement;
47 class WebElementCollection;
48 class WebFrame;
49 class WebNodeList;
50 class WebPluginContainer;
51
52 // Provides access to some properties of a DOM node.
53 // Note that the class design requires that neither this class nor any of its subclasses have any virtual
54 // methods (other than the destructor), so that it is possible to safely static_cast an instance of one
55 // class to the appropriate subclass based on the actual type of the wrapped WebCore::Node. For the same
56 // reason, subclasses must not add any additional data members.
57 class WebNode {
58 public:
59     virtual ~WebNode() { reset(); }
60
61     WebNode() { }
62     WebNode(const WebNode& n) { assign(n); }
63     WebNode& operator=(const WebNode& n)
64     {
65         assign(n);
66         return *this;
67     }
68
69     BLINK_EXPORT void reset();
70     BLINK_EXPORT void assign(const WebNode&);
71
72     BLINK_EXPORT bool equals(const WebNode&) const;
73     // Required for using WebNodes in std maps.  Note the order used is
74     // arbitrary and should not be expected to have any specific meaning.
75     BLINK_EXPORT bool lessThan(const WebNode&) const;
76
77     bool isNull() const { return m_private.isNull(); }
78
79     enum NodeType {
80         ElementNode = 1,
81         AttributeNode = 2,
82         TextNode = 3,
83         CDataSectionNode = 4,
84         // EntityReferenceNodes are impossible to create in Blink.
85         // EntityNodes are impossible to create in Blink.
86         ProcessingInstructionsNode = 7,
87         CommentNode = 8,
88         DocumentNode = 9,
89         DocumentTypeNode = 10,
90         DocumentFragmentNode = 11,
91         // NotationNodes are impossible to create in Blink.
92         // XPathNamespaceNodes are impossible to create in Blink.
93         ShadowRootNode = 14
94     };
95
96     BLINK_EXPORT NodeType nodeType() const;
97     BLINK_EXPORT WebNode parentNode() const;
98     BLINK_EXPORT WebString nodeName() const;
99     BLINK_EXPORT WebString nodeValue() const;
100     BLINK_EXPORT WebDocument document() const;
101     BLINK_EXPORT WebNode firstChild() const;
102     BLINK_EXPORT WebNode lastChild() const;
103     BLINK_EXPORT WebNode previousSibling() const;
104     BLINK_EXPORT WebNode nextSibling() const;
105     BLINK_EXPORT bool hasChildNodes() const;
106     BLINK_EXPORT WebNodeList childNodes();
107     BLINK_EXPORT WebString createMarkup() const;
108     BLINK_EXPORT bool isLink() const;
109     BLINK_EXPORT bool isTextNode() const;
110     BLINK_EXPORT bool isFocusable() const;
111     BLINK_EXPORT bool isContentEditable() const;
112     BLINK_EXPORT bool isElementNode() const;
113     // addEventListener only works with a small set of eventTypes.
114     BLINK_EXPORT void addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture);
115     BLINK_EXPORT bool dispatchEvent(const WebDOMEvent&);
116     BLINK_EXPORT void simulateClick();
117     BLINK_EXPORT WebElementCollection getElementsByTagName(const WebString&) const;
118     BLINK_EXPORT WebElement querySelector(const WebString&, WebExceptionCode&) const;
119     BLINK_EXPORT WebElement rootEditableElement() const;
120     BLINK_EXPORT bool focused() const;
121     BLINK_EXPORT bool remove();
122
123     // Returns true if the node has a non-empty bounding box in layout.
124     // This does not 100% guarantee the user can see it, but is pretty close.
125     // Note: This method only works properly after layout has occurred.
126     BLINK_EXPORT bool hasNonEmptyBoundingBox() const;
127     BLINK_EXPORT WebPluginContainer* pluginContainer() const;
128     BLINK_EXPORT WebElement shadowHost() const;
129
130     template<typename T> T to()
131     {
132         T res;
133         res.WebNode::assign(*this);
134         return res;
135     }
136
137     template<typename T> const T toConst() const
138     {
139         T res;
140         res.WebNode::assign(*this);
141         return res;
142     }
143
144 #if BLINK_IMPLEMENTATION
145     WebNode(const WTF::PassRefPtr<WebCore::Node>&);
146     WebNode& operator=(const WTF::PassRefPtr<WebCore::Node>&);
147     operator WTF::PassRefPtr<WebCore::Node>() const;
148 #endif
149
150 #if BLINK_IMPLEMENTATION
151     template<typename T> T* unwrap()
152     {
153         return static_cast<T*>(m_private.get());
154     }
155
156     template<typename T> const T* constUnwrap() const
157     {
158         return static_cast<const T*>(m_private.get());
159     }
160 #endif
161
162 protected:
163     WebPrivatePtr<WebCore::Node> m_private;
164 };
165
166 inline bool operator==(const WebNode& a, const WebNode& b)
167 {
168     return a.equals(b);
169 }
170
171 inline bool operator!=(const WebNode& a, const WebNode& b)
172 {
173     return !(a == b);
174 }
175
176 inline bool operator<(const WebNode& a, const WebNode& b)
177 {
178     return a.lessThan(b);
179 }
180
181 } // namespace blink
182
183 #endif