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/core/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/WebDocument.h"
51#include "public/web/WebElement.h"
52#include "public/web/WebElementCollection.h"
53#include "public/web/WebNodeList.h"
54#include "public/web/WebPluginContainer.h"
55#include "web/FrameLoaderClientImpl.h"
56#include "web/WebLocalFrameImpl.h"
57#include "web/WebPluginContainerImpl.h"
58
59namespace blink {
60
61void WebNode::reset()
62{
63    m_private.reset();
64}
65
66void WebNode::assign(const WebNode& other)
67{
68    m_private = other.m_private;
69}
70
71bool WebNode::equals(const WebNode& n) const
72{
73    return m_private.get() == n.m_private.get();
74}
75
76bool WebNode::lessThan(const WebNode& n) const
77{
78    return m_private.get() < n.m_private.get();
79}
80
81WebNode::NodeType WebNode::nodeType() const
82{
83    return static_cast<NodeType>(m_private->nodeType());
84}
85
86WebNode WebNode::parentNode() const
87{
88    return WebNode(const_cast<ContainerNode*>(m_private->parentNode()));
89}
90
91WebString WebNode::nodeName() const
92{
93    return m_private->nodeName();
94}
95
96WebString WebNode::nodeValue() const
97{
98    return m_private->nodeValue();
99}
100
101WebDocument WebNode::document() const
102{
103    return WebDocument(&m_private->document());
104}
105
106WebNode WebNode::firstChild() const
107{
108    return WebNode(m_private->firstChild());
109}
110
111WebNode WebNode::lastChild() const
112{
113    return WebNode(m_private->lastChild());
114}
115
116WebNode WebNode::previousSibling() const
117{
118    return WebNode(m_private->previousSibling());
119}
120
121WebNode WebNode::nextSibling() const
122{
123    return WebNode(m_private->nextSibling());
124}
125
126bool WebNode::hasChildNodes() const
127{
128    return m_private->hasChildren();
129}
130
131WebNodeList WebNode::childNodes()
132{
133    return WebNodeList(m_private->childNodes());
134}
135
136WebString WebNode::createMarkup() const
137{
138    return blink::createMarkup(m_private.get());
139}
140
141bool WebNode::isLink() const
142{
143    return m_private->isLink();
144}
145
146bool WebNode::isTextNode() const
147{
148    return m_private->isTextNode();
149}
150
151bool WebNode::isFocusable() const
152{
153    if (!m_private->isElementNode())
154        return false;
155    m_private->document().updateLayoutIgnorePendingStylesheets();
156    return toElement(m_private.get())->isFocusable();
157}
158
159bool WebNode::isContentEditable() const
160{
161    return m_private->isContentEditable();
162}
163
164bool WebNode::isElementNode() const
165{
166    return m_private->isElementNode();
167}
168
169bool WebNode::dispatchEvent(const WebDOMEvent& event)
170{
171    if (!event.isNull())
172        return m_private->dispatchEvent(event);
173    return false;
174}
175
176void WebNode::simulateClick()
177{
178    m_private->dispatchSimulatedClick(0);
179}
180
181WebElementCollection WebNode::getElementsByHTMLTagName(const WebString& tag) const
182{
183    if (m_private->isContainerNode())
184        return WebElementCollection(toContainerNode(m_private.get())->getElementsByTagNameNS(HTMLNames::xhtmlNamespaceURI, tag));
185    return WebElementCollection();
186}
187
188WebElement WebNode::querySelector(const WebString& tag, WebExceptionCode& ec) const
189{
190    TrackExceptionState exceptionState;
191    WebElement element;
192    if (m_private->isContainerNode())
193        element = toContainerNode(m_private.get())->querySelector(tag, exceptionState);
194    ec = exceptionState.code();
195    return element;
196}
197
198WebElement WebNode::rootEditableElement() const
199{
200    return WebElement(m_private->rootEditableElement());
201}
202
203bool WebNode::focused() const
204{
205    return m_private->focused();
206}
207
208bool WebNode::remove()
209{
210    TrackExceptionState exceptionState;
211    m_private->remove(exceptionState);
212    return !exceptionState.hadException();
213}
214
215bool WebNode::hasNonEmptyBoundingBox() const
216{
217    m_private->document().updateLayoutIgnorePendingStylesheets();
218    return m_private->hasNonEmptyBoundingBox();
219}
220
221bool WebNode::containsIncludingShadowDOM(const WebNode& other) const
222{
223    return m_private->containsIncludingShadowDOM(other.m_private.get());
224}
225
226WebPluginContainer* WebNode::pluginContainer() const
227{
228    if (isNull())
229        return 0;
230    const Node& coreNode = *constUnwrap<Node>();
231    if (isHTMLObjectElement(coreNode) || isHTMLEmbedElement(coreNode)) {
232        RenderObject* object = coreNode.renderer();
233        if (object && object->isWidget()) {
234            Widget* widget = toRenderWidget(object)->widget();
235            if (widget && widget->isPluginContainer())
236                return toWebPluginContainerImpl(widget);
237        }
238    }
239    return 0;
240}
241
242WebElement WebNode::shadowHost() const
243{
244    if (isNull())
245        return WebElement();
246    const Node* coreNode = constUnwrap<Node>();
247    return WebElement(coreNode->shadowHost());
248}
249
250WebNode::WebNode(const PassRefPtrWillBeRawPtr<Node>& node)
251    : m_private(node)
252{
253}
254
255WebNode& WebNode::operator=(const PassRefPtrWillBeRawPtr<Node>& node)
256{
257    m_private = node;
258    return *this;
259}
260
261WebNode::operator PassRefPtrWillBeRawPtr<Node>() const
262{
263    return m_private.get();
264}
265
266} // namespace blink
267