blob: eb3f9e9b6c07cfd9b93754349f610af24d333a70 [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
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 "WebNode.h"
33
34#include "EventListenerWrapper.h"
35#include "FrameLoaderClientImpl.h"
36#include "WebDOMEvent.h"
37#include "WebDOMEventListener.h"
38#include "WebDocument.h"
39#include "WebElement.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000040#include "WebElementCollection.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010041#include "WebFrameImpl.h"
42#include "WebNodeList.h"
43#include "WebPluginContainer.h"
44#include "WebPluginContainerImpl.h"
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010045#include "bindings/v8/ExceptionState.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010046#include "core/dom/Document.h"
47#include "core/dom/Element.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010048#include "core/dom/Node.h"
49#include "core/dom/NodeList.h"
50#include "core/editing/markup.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000051#include "core/events/Event.h"
52#include "core/html/HTMLCollection.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000053#include "core/html/HTMLElement.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010054#include "core/rendering/RenderObject.h"
55#include "core/rendering/RenderWidget.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010056#include "platform/Widget.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010057#include "public/platform/WebString.h"
58#include "public/platform/WebVector.h"
59
60using namespace WebCore;
61
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000062namespace blink {
Ben Murdoche69819b2013-07-17 14:56:49 +010063
64void WebNode::reset()
65{
66 m_private.reset();
67}
68
69void WebNode::assign(const WebNode& other)
70{
71 m_private = other.m_private;
72}
73
74bool WebNode::equals(const WebNode& n) const
75{
76 return m_private.get() == n.m_private.get();
77}
78
79bool WebNode::lessThan(const WebNode& n) const
80{
81 return m_private.get() < n.m_private.get();
82}
83
84WebNode::NodeType WebNode::nodeType() const
85{
86 return static_cast<NodeType>(m_private->nodeType());
87}
88
89WebNode WebNode::parentNode() const
90{
91 return WebNode(const_cast<ContainerNode*>(m_private->parentNode()));
92}
93
94WebString WebNode::nodeName() const
95{
96 return m_private->nodeName();
97}
98
99WebString WebNode::nodeValue() const
100{
101 return m_private->nodeValue();
102}
103
Ben Murdoche69819b2013-07-17 14:56:49 +0100104WebDocument WebNode::document() const
105{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100106 return WebDocument(&m_private->document());
Ben Murdoche69819b2013-07-17 14:56:49 +0100107}
108
109WebNode WebNode::firstChild() const
110{
111 return WebNode(m_private->firstChild());
112}
113
114WebNode WebNode::lastChild() const
115{
116 return WebNode(m_private->lastChild());
117}
118
119WebNode WebNode::previousSibling() const
120{
121 return WebNode(m_private->previousSibling());
122}
123
124WebNode WebNode::nextSibling() const
125{
126 return WebNode(m_private->nextSibling());
127}
128
129bool WebNode::hasChildNodes() const
130{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000131 return m_private->hasChildren();
Ben Murdoche69819b2013-07-17 14:56:49 +0100132}
133
134WebNodeList WebNode::childNodes()
135{
136 return WebNodeList(m_private->childNodes());
137}
138
139WebString WebNode::createMarkup() const
140{
141 return WebCore::createMarkup(m_private.get());
142}
143
144bool WebNode::isLink() const
145{
146 return m_private->isLink();
147}
148
149bool WebNode::isTextNode() const
150{
151 return m_private->isTextNode();
152}
153
154bool WebNode::isFocusable() const
155{
Ben Murdochdf957042013-08-06 11:01:27 +0100156 if (!m_private->isElementNode())
157 return false;
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100158 m_private->document().updateLayoutIgnorePendingStylesheets();
Ben Murdochdf957042013-08-06 11:01:27 +0100159 return toElement(m_private.get())->isFocusable();
Ben Murdoche69819b2013-07-17 14:56:49 +0100160}
161
162bool WebNode::isContentEditable() const
163{
164 return m_private->isContentEditable();
165}
166
167bool WebNode::isElementNode() const
168{
169 return m_private->isElementNode();
170}
171
172void WebNode::addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture)
173{
174 // Please do not add more eventTypes to this list without an API review.
175 RELEASE_ASSERT(eventType == "mousedown");
176
177 EventListenerWrapper* listenerWrapper = listener->createEventListenerWrapper(eventType, useCapture, m_private.get());
178 // The listenerWrapper is only referenced by the actual Node. Once it goes
179 // away, the wrapper notifies the WebEventListener so it can clear its
180 // pointer to it.
181 m_private->addEventListener(eventType, adoptRef(listenerWrapper), useCapture);
182}
183
184bool WebNode::dispatchEvent(const WebDOMEvent& event)
185{
186 if (!event.isNull())
187 return m_private->dispatchEvent(event);
188 return false;
189}
190
191void WebNode::simulateClick()
192{
193 m_private->dispatchSimulatedClick(0);
194}
195
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000196WebElementCollection WebNode::getElementsByTagName(const WebString& tag) const
Ben Murdoche69819b2013-07-17 14:56:49 +0100197{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000198 if (m_private->isContainerNode())
199 return WebElementCollection(toContainerNode(m_private.get())->getElementsByTagName(tag));
200 return WebElementCollection();
Ben Murdoche69819b2013-07-17 14:56:49 +0100201}
202
203WebElement WebNode::querySelector(const WebString& tag, WebExceptionCode& ec) const
204{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000205 TrackExceptionState exceptionState;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000206 WebElement element;
207 if (m_private->isContainerNode())
208 element = toContainerNode(m_private.get())->querySelector(tag, exceptionState);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000209 ec = exceptionState.code();
Ben Murdochdf957042013-08-06 11:01:27 +0100210 return element;
Ben Murdoche69819b2013-07-17 14:56:49 +0100211}
212
213WebElement WebNode::rootEditableElement() const
214{
215 return WebElement(m_private->rootEditableElement());
216}
217
218bool WebNode::focused() const
219{
220 return m_private->focused();
221}
222
223bool WebNode::remove()
224{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000225 TrackExceptionState exceptionState;
226 m_private->remove(exceptionState);
227 return !exceptionState.hadException();
Ben Murdoche69819b2013-07-17 14:56:49 +0100228}
229
230bool WebNode::hasNonEmptyBoundingBox() const
231{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100232 m_private->document().updateLayoutIgnorePendingStylesheets();
Ben Murdoche69819b2013-07-17 14:56:49 +0100233 return m_private->hasNonEmptyBoundingBox();
234}
235
236WebPluginContainer* WebNode::pluginContainer() const
237{
238 if (isNull())
239 return 0;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000240 const Node& coreNode = *constUnwrap<Node>();
241 if (isHTMLObjectElement(coreNode) || isHTMLEmbedElement(coreNode)) {
242 RenderObject* object = coreNode.renderer();
Ben Murdoche69819b2013-07-17 14:56:49 +0100243 if (object && object->isWidget()) {
244 Widget* widget = WebCore::toRenderWidget(object)->widget();
245 if (widget && widget->isPluginContainer())
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000246 return toWebPluginContainerImpl(widget);
Ben Murdoche69819b2013-07-17 14:56:49 +0100247 }
248 }
249 return 0;
250}
251
252WebElement WebNode::shadowHost() const
253{
254 if (isNull())
255 return WebElement();
256 const Node* coreNode = constUnwrap<Node>();
257 return WebElement(coreNode->shadowHost());
258}
259
260WebNode::WebNode(const PassRefPtr<Node>& node)
261 : m_private(node)
262{
263}
264
265WebNode& WebNode::operator=(const PassRefPtr<Node>& node)
266{
267 m_private = node;
268 return *this;
269}
270
271WebNode::operator PassRefPtr<Node>() const
272{
273 return m_private.get();
274}
275
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000276} // namespace blink