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/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/StaticNodeList.h"
40 #include "core/dom/TagCollection.h"
41 #include "core/editing/serializers/Serialization.h"
42 #include "core/events/Event.h"
43 #include "core/html/HTMLCollection.h"
44 #include "core/html/HTMLElement.h"
45 #include "core/layout/LayoutObject.h"
46 #include "core/layout/LayoutPart.h"
47 #include "modules/accessibility/AXObject.h"
48 #include "modules/accessibility/AXObjectCacheImpl.h"
49 #include "platform/Task.h"
50 #include "platform/Widget.h"
51 #include "public/platform/WebString.h"
52 #include "public/platform/WebSuspendableTask.h"
53 #include "public/web/WebAXObject.h"
54 #include "public/web/WebDOMEvent.h"
55 #include "public/web/WebDocument.h"
56 #include "public/web/WebElement.h"
57 #include "public/web/WebElementCollection.h"
58 #include "public/web/WebNodeList.h"
59 #include "public/web/WebPluginContainer.h"
60 #include "web/FrameLoaderClientImpl.h"
61 #include "web/WebLocalFrameImpl.h"
62 #include "web/WebPluginContainerImpl.h"
68 class NodeDispatchEventTask
: public SuspendableTask
{
69 WTF_MAKE_NONCOPYABLE(NodeDispatchEventTask
);
71 NodeDispatchEventTask(const WebPrivatePtr
<Node
>& node
, WebDOMEvent event
)
77 ~NodeDispatchEventTask()
84 m_node
->dispatchEvent(m_event
);
87 WebPrivatePtr
<Node
> m_node
;
91 class NodeDispatchSimulatedClickTask
: public SuspendableTask
{
92 WTF_MAKE_NONCOPYABLE(NodeDispatchSimulatedClickTask
);
94 NodeDispatchSimulatedClickTask(const WebPrivatePtr
<Node
>& node
)
99 ~NodeDispatchSimulatedClickTask()
106 m_node
->dispatchSimulatedClick(nullptr);
109 WebPrivatePtr
<Node
> m_node
;
114 void WebNode::reset()
119 void WebNode::assign(const WebNode
& other
)
121 m_private
= other
.m_private
;
124 bool WebNode::equals(const WebNode
& n
) const
126 return m_private
.get() == n
.m_private
.get();
129 bool WebNode::lessThan(const WebNode
& n
) const
131 return m_private
.get() < n
.m_private
.get();
134 WebNode::NodeType
WebNode::nodeType() const
136 return static_cast<NodeType
>(m_private
->nodeType());
139 WebNode
WebNode::parentNode() const
141 return WebNode(const_cast<ContainerNode
*>(m_private
->parentNode()));
144 WebString
WebNode::nodeValue() const
146 return m_private
->nodeValue();
149 WebDocument
WebNode::document() const
151 return WebDocument(&m_private
->document());
154 WebNode
WebNode::firstChild() const
156 return WebNode(m_private
->firstChild());
159 WebNode
WebNode::lastChild() const
161 return WebNode(m_private
->lastChild());
164 WebNode
WebNode::previousSibling() const
166 return WebNode(m_private
->previousSibling());
169 WebNode
WebNode::nextSibling() const
171 return WebNode(m_private
->nextSibling());
174 bool WebNode::hasChildNodes() const
176 return m_private
->hasChildren();
179 WebNodeList
WebNode::childNodes()
181 return WebNodeList(m_private
->childNodes());
184 bool WebNode::isLink() const
186 return m_private
->isLink();
189 bool WebNode::isTextNode() const
191 return m_private
->isTextNode();
194 bool WebNode::isCommentNode() const
196 return m_private
->nodeType() == Node::COMMENT_NODE
;
199 bool WebNode::isFocusable() const
201 if (!m_private
->isElementNode())
203 m_private
->document().updateLayoutIgnorePendingStylesheets();
204 return toElement(m_private
.get())->isFocusable();
207 bool WebNode::isContentEditable() const
209 return m_private
->isContentEditable();
212 bool WebNode::isInsideFocusableElementOrARIAWidget() const
214 return AXObject::isInsideFocusableElementOrARIAWidget(*this->constUnwrap
<Node
>());
217 bool WebNode::isElementNode() const
219 return m_private
->isElementNode();
222 void WebNode::dispatchEvent(const WebDOMEvent
& event
)
225 m_private
->executionContext()->postSuspendableTask(adoptPtr(new NodeDispatchEventTask(m_private
, event
)));
228 void WebNode::simulateClick()
230 m_private
->executionContext()->postSuspendableTask(adoptPtr(new NodeDispatchSimulatedClickTask(m_private
)));
233 WebElementCollection
WebNode::getElementsByHTMLTagName(const WebString
& tag
) const
235 if (m_private
->isContainerNode())
236 return WebElementCollection(toContainerNode(m_private
.get())->getElementsByTagNameNS(HTMLNames::xhtmlNamespaceURI
, tag
));
237 return WebElementCollection();
240 WebElement
WebNode::querySelector(const WebString
& selector
, WebExceptionCode
& ec
) const
242 if (!m_private
->isContainerNode())
244 TrackExceptionState exceptionState
;
245 WebElement element
= toContainerNode(m_private
.get())->querySelector(selector
, exceptionState
);
246 ec
= exceptionState
.code();
250 WebElement
WebNode::querySelector(const WebString
& selector
) const
252 WebExceptionCode ec
= 0;
253 WebElement element
= querySelector(selector
, ec
);
258 void WebNode::querySelectorAll(const WebString
& selector
, WebVector
<WebElement
>& results
, WebExceptionCode
& ec
) const
260 if (!m_private
->isContainerNode())
262 TrackExceptionState exceptionState
;
263 RefPtrWillBeRawPtr
<StaticElementList
> elements
= toContainerNode(m_private
.get())->querySelectorAll(selector
, exceptionState
);
264 ec
= exceptionState
.code();
265 if (exceptionState
.hadException())
267 Vector
<WebElement
> temp
;
268 temp
.reserveCapacity(elements
->length());
269 for (unsigned i
= 0; i
< elements
->length(); ++i
)
270 temp
.append(WebElement(elements
->item(i
)));
271 results
.assign(temp
);
274 void WebNode::querySelectorAll(const WebString
& selector
, WebVector
<WebElement
>& results
) const
276 WebExceptionCode ec
= 0;
277 querySelectorAll(selector
, results
, ec
);
281 bool WebNode::focused() const
283 return m_private
->focused();
286 WebPluginContainer
* WebNode::pluginContainer() const
290 const Node
& coreNode
= *constUnwrap
<Node
>();
291 if (isHTMLObjectElement(coreNode
) || isHTMLEmbedElement(coreNode
)) {
292 LayoutObject
* object
= coreNode
.layoutObject();
293 if (object
&& object
->isLayoutPart()) {
294 Widget
* widget
= toLayoutPart(object
)->widget();
295 if (widget
&& widget
->isPluginContainer())
296 return toWebPluginContainerImpl(widget
);
302 WebAXObject
WebNode::accessibilityObject()
304 WebDocument webDocument
= document();
305 const Document
* doc
= document().constUnwrap
<Document
>();
306 AXObjectCacheImpl
* cache
= toAXObjectCacheImpl(doc
->existingAXObjectCache());
307 Node
* node
= unwrap
<Node
>();
308 return cache
? WebAXObject(cache
->get(node
)) : WebAXObject();
311 WebNode::WebNode(const PassRefPtrWillBeRawPtr
<Node
>& node
)
316 WebNode
& WebNode::operator=(const PassRefPtrWillBeRawPtr
<Node
>& node
)
322 WebNode::operator PassRefPtrWillBeRawPtr
<Node
>() const
324 return m_private
.get();