Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / web / WebNode.h
blob3313073ea3c11b728ddc48e3be24d7e2df47f73b
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #ifndef WebNode_h
32 #define WebNode_h
34 #include "../platform/WebCommon.h"
35 #include "../platform/WebPrivatePtr.h"
36 #include "../platform/WebString.h"
37 #include "../platform/WebVector.h"
38 #include "WebExceptionCode.h"
40 namespace blink {
42 class Node;
43 class WebAXObject;
44 class WebDOMEvent;
45 class WebDocument;
46 class WebElement;
47 class WebElementCollection;
48 class WebNodeList;
49 class WebPluginContainer;
51 // Provides access to some properties of a DOM node.
52 // Note that the class design requires that neither this class nor any of its subclasses have any virtual
53 // methods (other than the destructor), so that it is possible to safely static_cast an instance of one
54 // class to the appropriate subclass based on the actual type of the wrapped blink::Node. For the same
55 // reason, subclasses must not add any additional data members.
56 class WebNode {
57 public:
58 virtual ~WebNode() { reset(); }
60 WebNode() { }
61 WebNode(const WebNode& n) { assign(n); }
62 WebNode& operator=(const WebNode& n)
64 assign(n);
65 return *this;
68 BLINK_EXPORT void reset();
69 BLINK_EXPORT void assign(const WebNode&);
71 BLINK_EXPORT bool equals(const WebNode&) const;
72 // Required for using WebNodes in std maps. Note the order used is
73 // arbitrary and should not be expected to have any specific meaning.
74 BLINK_EXPORT bool lessThan(const WebNode&) const;
76 bool isNull() const { return m_private.isNull(); }
78 enum NodeType {
79 ElementNode = 1,
80 AttributeNode = 2,
81 TextNode = 3,
82 CDataSectionNode = 4,
83 // EntityReferenceNodes are impossible to create in Blink.
84 // EntityNodes are impossible to create in Blink.
85 ProcessingInstructionsNode = 7,
86 CommentNode = 8,
87 DocumentNode = 9,
88 DocumentTypeNode = 10,
89 DocumentFragmentNode = 11,
90 // NotationNodes are impossible to create in Blink.
91 // XPathNamespaceNodes are impossible to create in Blink.
92 ShadowRootNode = 14
95 BLINK_EXPORT NodeType nodeType() const;
96 BLINK_EXPORT WebNode parentNode() const;
97 BLINK_EXPORT WebString nodeValue() const;
98 BLINK_EXPORT WebDocument document() const;
99 BLINK_EXPORT WebNode firstChild() const;
100 BLINK_EXPORT WebNode lastChild() const;
101 BLINK_EXPORT WebNode previousSibling() const;
102 BLINK_EXPORT WebNode nextSibling() const;
103 BLINK_EXPORT bool hasChildNodes() const;
104 BLINK_EXPORT WebNodeList childNodes();
105 BLINK_EXPORT bool isLink() const;
106 BLINK_EXPORT bool isCommentNode() const;
107 BLINK_EXPORT bool isTextNode() const;
108 BLINK_EXPORT bool isFocusable() const;
109 BLINK_EXPORT bool isContentEditable() const;
110 BLINK_EXPORT bool isElementNode() const;
111 BLINK_EXPORT void dispatchEvent(const WebDOMEvent&);
112 BLINK_EXPORT void simulateClick();
113 // The argument should be lower-cased.
114 BLINK_EXPORT WebElementCollection getElementsByHTMLTagName(const WebString&) const;
117 BLINK_EXPORT WebElement querySelector(const WebString& selector, WebExceptionCode&) const;
118 BLINK_EXPORT void querySelectorAll(const WebString& selector, WebVector<WebElement>& results, WebExceptionCode&) const;
120 // Same as querySelector and querySelectorAll, but ASSERT if an exception
121 // code would be generated.
122 BLINK_EXPORT WebElement querySelector(const WebString& selector) const;
123 BLINK_EXPORT void querySelectorAll(const WebString& selector, WebVector<WebElement>& results) const;
125 BLINK_EXPORT bool focused() const;
127 BLINK_EXPORT WebPluginContainer* pluginContainer() const;
129 BLINK_EXPORT bool isInsideFocusableElementOrARIAWidget() const;
130 BLINK_EXPORT WebAXObject accessibilityObject();
132 template<typename T> T to()
134 T res;
135 res.WebNode::assign(*this);
136 return res;
139 template<typename T> const T toConst() const
141 T res;
142 res.WebNode::assign(*this);
143 return res;
146 #if BLINK_IMPLEMENTATION
147 WebNode(const PassRefPtrWillBeRawPtr<Node>&);
148 WebNode& operator=(const PassRefPtrWillBeRawPtr<Node>&);
149 operator PassRefPtrWillBeRawPtr<Node>() const;
150 #endif
152 #if BLINK_IMPLEMENTATION
153 template<typename T> T* unwrap()
155 return static_cast<T*>(m_private.get());
158 template<typename T> const T* constUnwrap() const
160 return static_cast<const T*>(m_private.get());
162 #endif
164 protected:
165 WebPrivatePtr<Node> m_private;
168 inline bool operator==(const WebNode& a, const WebNode& b)
170 return a.equals(b);
173 inline bool operator!=(const WebNode& a, const WebNode& b)
175 return !(a == b);
178 inline bool operator<(const WebNode& a, const WebNode& b)
180 return a.lessThan(b);
183 } // namespace blink
185 #endif