Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebNode.cpp
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 #include "config.h"
32 #include "public/web/WebNode.h"
33
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/Document.h"
36 #include "core/dom/Element.h"
37 #include "core/dom/Node.h"
38 #include "core/dom/NodeList.h"
39 #include "core/dom/TagCollection.h"
40 #include "core/editing/markup.h"
41 #include "core/events/Event.h"
42 #include "core/html/HTMLCollection.h"
43 #include "core/html/HTMLElement.h"
44 #include "core/rendering/RenderObject.h"
45 #include "core/rendering/RenderWidget.h"
46 #include "platform/Widget.h"
47 #include "public/platform/WebString.h"
48 #include "public/platform/WebVector.h"
49 #include "public/web/WebDOMEvent.h"
50 #include "public/web/WebDOMEventListener.h"
51 #include "public/web/WebDocument.h"
52 #include "public/web/WebElement.h"
53 #include "public/web/WebElementCollection.h"
54 #include "public/web/WebNodeList.h"
55 #include "public/web/WebPluginContainer.h"
56 #include "web/EventListenerWrapper.h"
57 #include "web/FrameLoaderClientImpl.h"
58 #include "web/WebLocalFrameImpl.h"
59 #include "web/WebPluginContainerImpl.h"
60
61 using namespace WebCore;
62
63 namespace blink {
64
65 void WebNode::reset()
66 {
67     m_private.reset();
68 }
69
70 void WebNode::assign(const WebNode& other)
71 {
72     m_private = other.m_private;
73 }
74
75 bool WebNode::equals(const WebNode& n) const
76 {
77     return m_private.get() == n.m_private.get();
78 }
79
80 bool WebNode::lessThan(const WebNode& n) const
81 {
82     return m_private.get() < n.m_private.get();
83 }
84
85 WebNode::NodeType WebNode::nodeType() const
86 {
87     return static_cast<NodeType>(m_private->nodeType());
88 }
89
90 WebNode WebNode::parentNode() const
91 {
92     return WebNode(const_cast<ContainerNode*>(m_private->parentNode()));
93 }
94
95 WebString WebNode::nodeName() const
96 {
97     return m_private->nodeName();
98 }
99
100 WebString WebNode::nodeValue() const
101 {
102     return m_private->nodeValue();
103 }
104
105 WebDocument WebNode::document() const
106 {
107     return WebDocument(&m_private->document());
108 }
109
110 WebNode WebNode::firstChild() const
111 {
112     return WebNode(m_private->firstChild());
113 }
114
115 WebNode WebNode::lastChild() const
116 {
117     return WebNode(m_private->lastChild());
118 }
119
120 WebNode WebNode::previousSibling() const
121 {
122     return WebNode(m_private->previousSibling());
123 }
124
125 WebNode WebNode::nextSibling() const
126 {
127     return WebNode(m_private->nextSibling());
128 }
129
130 bool WebNode::hasChildNodes() const
131 {
132     return m_private->hasChildren();
133 }
134
135 WebNodeList WebNode::childNodes()
136 {
137     return WebNodeList(m_private->childNodes());
138 }
139
140 WebString WebNode::createMarkup() const
141 {
142     return WebCore::createMarkup(m_private.get());
143 }
144
145 bool WebNode::isLink() const
146 {
147     return m_private->isLink();
148 }
149
150 bool WebNode::isTextNode() const
151 {
152     return m_private->isTextNode();
153 }
154
155 bool WebNode::isFocusable() const
156 {
157     if (!m_private->isElementNode())
158         return false;
159     m_private->document().updateLayoutIgnorePendingStylesheets();
160     return toElement(m_private.get())->isFocusable();
161 }
162
163 bool WebNode::isContentEditable() const
164 {
165     return m_private->isContentEditable();
166 }
167
168 bool WebNode::isElementNode() const
169 {
170     return m_private->isElementNode();
171 }
172
173 void WebNode::addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture)
174 {
175     // Please do not add more eventTypes to this list without an API review.
176     RELEASE_ASSERT(eventType == "mousedown");
177
178     EventListenerWrapper* listenerWrapper = listener->createEventListenerWrapper(eventType, useCapture, m_private.get());
179     // The listenerWrapper is only referenced by the actual Node.  Once it goes
180     // away, the wrapper notifies the WebEventListener so it can clear its
181     // pointer to it.
182     m_private->addEventListener(eventType, adoptRef(listenerWrapper), useCapture);
183 }
184
185 bool WebNode::dispatchEvent(const WebDOMEvent& event)
186 {
187     if (!event.isNull())
188         return m_private->dispatchEvent(event);
189     return false;
190 }
191
192 void WebNode::simulateClick()
193 {
194     m_private->dispatchSimulatedClick(0);
195 }
196
197 WebElementCollection WebNode::getElementsByTagName(const WebString& tag) const
198 {
199     if (m_private->isContainerNode()) {
200         // FIXME: Calling getElementsByTagNameNS here is inconsistent with the
201         // function name. This is a temporary fix for a serious bug, and should
202         // be reverted soon.
203         return WebElementCollection(toContainerNode(m_private.get())->getElementsByTagNameNS(HTMLNames::xhtmlNamespaceURI, tag));
204     }
205     return WebElementCollection();
206 }
207
208 WebElement WebNode::querySelector(const WebString& tag, WebExceptionCode& ec) const
209 {
210     TrackExceptionState exceptionState;
211     WebElement element;
212     if (m_private->isContainerNode())
213         element = toContainerNode(m_private.get())->querySelector(tag, exceptionState);
214     ec = exceptionState.code();
215     return element;
216 }
217
218 WebElement WebNode::rootEditableElement() const
219 {
220     return WebElement(m_private->rootEditableElement());
221 }
222
223 bool WebNode::focused() const
224 {
225     return m_private->focused();
226 }
227
228 bool WebNode::remove()
229 {
230     TrackExceptionState exceptionState;
231     m_private->remove(exceptionState);
232     return !exceptionState.hadException();
233 }
234
235 bool WebNode::hasNonEmptyBoundingBox() const
236 {
237     m_private->document().updateLayoutIgnorePendingStylesheets();
238     return m_private->hasNonEmptyBoundingBox();
239 }
240
241 WebPluginContainer* WebNode::pluginContainer() const
242 {
243     if (isNull())
244         return 0;
245     const Node& coreNode = *constUnwrap<Node>();
246     if (isHTMLObjectElement(coreNode) || isHTMLEmbedElement(coreNode)) {
247         RenderObject* object = coreNode.renderer();
248         if (object && object->isWidget()) {
249             Widget* widget = WebCore::toRenderWidget(object)->widget();
250             if (widget && widget->isPluginContainer())
251                 return toWebPluginContainerImpl(widget);
252         }
253     }
254     return 0;
255 }
256
257 WebElement WebNode::shadowHost() const
258 {
259     if (isNull())
260         return WebElement();
261     const Node* coreNode = constUnwrap<Node>();
262     return WebElement(coreNode->shadowHost());
263 }
264
265 WebNode::WebNode(const PassRefPtrWillBeRawPtr<Node>& node)
266     : m_private(node)
267 {
268 }
269
270 WebNode& WebNode::operator=(const PassRefPtrWillBeRawPtr<Node>& node)
271 {
272     m_private = node;
273     return *this;
274 }
275
276 WebNode::operator PassRefPtrWillBeRawPtr<Node>() const
277 {
278     return m_private.get();
279 }
280
281 } // namespace blink