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
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
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.
32 #include "public/web/WebNode.h"
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"
61 using namespace WebCore;
70 void WebNode::assign(const WebNode& other)
72 m_private = other.m_private;
75 bool WebNode::equals(const WebNode& n) const
77 return m_private.get() == n.m_private.get();
80 bool WebNode::lessThan(const WebNode& n) const
82 return m_private.get() < n.m_private.get();
85 WebNode::NodeType WebNode::nodeType() const
87 return static_cast<NodeType>(m_private->nodeType());
90 WebNode WebNode::parentNode() const
92 return WebNode(const_cast<ContainerNode*>(m_private->parentNode()));
95 WebString WebNode::nodeName() const
97 return m_private->nodeName();
100 WebString WebNode::nodeValue() const
102 return m_private->nodeValue();
105 WebDocument WebNode::document() const
107 return WebDocument(&m_private->document());
110 WebNode WebNode::firstChild() const
112 return WebNode(m_private->firstChild());
115 WebNode WebNode::lastChild() const
117 return WebNode(m_private->lastChild());
120 WebNode WebNode::previousSibling() const
122 return WebNode(m_private->previousSibling());
125 WebNode WebNode::nextSibling() const
127 return WebNode(m_private->nextSibling());
130 bool WebNode::hasChildNodes() const
132 return m_private->hasChildren();
135 WebNodeList WebNode::childNodes()
137 return WebNodeList(m_private->childNodes());
140 WebString WebNode::createMarkup() const
142 return WebCore::createMarkup(m_private.get());
145 bool WebNode::isLink() const
147 return m_private->isLink();
150 bool WebNode::isTextNode() const
152 return m_private->isTextNode();
155 bool WebNode::isFocusable() const
157 if (!m_private->isElementNode())
159 m_private->document().updateLayoutIgnorePendingStylesheets();
160 return toElement(m_private.get())->isFocusable();
163 bool WebNode::isContentEditable() const
165 return m_private->isContentEditable();
168 bool WebNode::isElementNode() const
170 return m_private->isElementNode();
173 void WebNode::addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture)
175 // Please do not add more eventTypes to this list without an API review.
176 RELEASE_ASSERT(eventType == "mousedown");
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
182 m_private->addEventListener(eventType, adoptRef(listenerWrapper), useCapture);
185 bool WebNode::dispatchEvent(const WebDOMEvent& event)
188 return m_private->dispatchEvent(event);
192 void WebNode::simulateClick()
194 m_private->dispatchSimulatedClick(0);
197 WebElementCollection WebNode::getElementsByTagName(const WebString& tag) const
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
203 return WebElementCollection(toContainerNode(m_private.get())->getElementsByTagNameNS(HTMLNames::xhtmlNamespaceURI, tag));
205 return WebElementCollection();
208 WebElement WebNode::querySelector(const WebString& tag, WebExceptionCode& ec) const
210 TrackExceptionState exceptionState;
212 if (m_private->isContainerNode())
213 element = toContainerNode(m_private.get())->querySelector(tag, exceptionState);
214 ec = exceptionState.code();
218 WebElement WebNode::rootEditableElement() const
220 return WebElement(m_private->rootEditableElement());
223 bool WebNode::focused() const
225 return m_private->focused();
228 bool WebNode::remove()
230 TrackExceptionState exceptionState;
231 m_private->remove(exceptionState);
232 return !exceptionState.hadException();
235 bool WebNode::hasNonEmptyBoundingBox() const
237 m_private->document().updateLayoutIgnorePendingStylesheets();
238 return m_private->hasNonEmptyBoundingBox();
241 WebPluginContainer* WebNode::pluginContainer() const
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);
257 WebElement WebNode::shadowHost() const
261 const Node* coreNode = constUnwrap<Node>();
262 return WebElement(coreNode->shadowHost());
265 WebNode::WebNode(const PassRefPtrWillBeRawPtr<Node>& node)
270 WebNode& WebNode::operator=(const PassRefPtrWillBeRawPtr<Node>& node)
276 WebNode::operator PassRefPtrWillBeRawPtr<Node>() const
278 return m_private.get();