Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 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 "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) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 40 | #include "WebElementCollection.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 41 | #include "WebFrameImpl.h" |
| 42 | #include "WebNodeList.h" |
| 43 | #include "WebPluginContainer.h" |
| 44 | #include "WebPluginContainerImpl.h" |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 45 | #include "bindings/v8/ExceptionState.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 46 | #include "core/dom/Document.h" |
| 47 | #include "core/dom/Element.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 48 | #include "core/dom/Node.h" |
| 49 | #include "core/dom/NodeList.h" |
| 50 | #include "core/editing/markup.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 51 | #include "core/events/Event.h" |
| 52 | #include "core/html/HTMLCollection.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 53 | #include "core/html/HTMLElement.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 54 | #include "core/rendering/RenderObject.h" |
| 55 | #include "core/rendering/RenderWidget.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 56 | #include "platform/Widget.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 57 | #include "public/platform/WebString.h" |
| 58 | #include "public/platform/WebVector.h" |
| 59 | |
| 60 | using namespace WebCore; |
| 61 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 62 | namespace blink { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 63 | |
| 64 | void WebNode::reset() |
| 65 | { |
| 66 | m_private.reset(); |
| 67 | } |
| 68 | |
| 69 | void WebNode::assign(const WebNode& other) |
| 70 | { |
| 71 | m_private = other.m_private; |
| 72 | } |
| 73 | |
| 74 | bool WebNode::equals(const WebNode& n) const |
| 75 | { |
| 76 | return m_private.get() == n.m_private.get(); |
| 77 | } |
| 78 | |
| 79 | bool WebNode::lessThan(const WebNode& n) const |
| 80 | { |
| 81 | return m_private.get() < n.m_private.get(); |
| 82 | } |
| 83 | |
| 84 | WebNode::NodeType WebNode::nodeType() const |
| 85 | { |
| 86 | return static_cast<NodeType>(m_private->nodeType()); |
| 87 | } |
| 88 | |
| 89 | WebNode WebNode::parentNode() const |
| 90 | { |
| 91 | return WebNode(const_cast<ContainerNode*>(m_private->parentNode())); |
| 92 | } |
| 93 | |
| 94 | WebString WebNode::nodeName() const |
| 95 | { |
| 96 | return m_private->nodeName(); |
| 97 | } |
| 98 | |
| 99 | WebString WebNode::nodeValue() const |
| 100 | { |
| 101 | return m_private->nodeValue(); |
| 102 | } |
| 103 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 104 | WebDocument WebNode::document() const |
| 105 | { |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 106 | return WebDocument(&m_private->document()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | WebNode WebNode::firstChild() const |
| 110 | { |
| 111 | return WebNode(m_private->firstChild()); |
| 112 | } |
| 113 | |
| 114 | WebNode WebNode::lastChild() const |
| 115 | { |
| 116 | return WebNode(m_private->lastChild()); |
| 117 | } |
| 118 | |
| 119 | WebNode WebNode::previousSibling() const |
| 120 | { |
| 121 | return WebNode(m_private->previousSibling()); |
| 122 | } |
| 123 | |
| 124 | WebNode WebNode::nextSibling() const |
| 125 | { |
| 126 | return WebNode(m_private->nextSibling()); |
| 127 | } |
| 128 | |
| 129 | bool WebNode::hasChildNodes() const |
| 130 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 131 | return m_private->hasChildren(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | WebNodeList WebNode::childNodes() |
| 135 | { |
| 136 | return WebNodeList(m_private->childNodes()); |
| 137 | } |
| 138 | |
| 139 | WebString WebNode::createMarkup() const |
| 140 | { |
| 141 | return WebCore::createMarkup(m_private.get()); |
| 142 | } |
| 143 | |
| 144 | bool WebNode::isLink() const |
| 145 | { |
| 146 | return m_private->isLink(); |
| 147 | } |
| 148 | |
| 149 | bool WebNode::isTextNode() const |
| 150 | { |
| 151 | return m_private->isTextNode(); |
| 152 | } |
| 153 | |
| 154 | bool WebNode::isFocusable() const |
| 155 | { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 156 | if (!m_private->isElementNode()) |
| 157 | return false; |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 158 | m_private->document().updateLayoutIgnorePendingStylesheets(); |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 159 | return toElement(m_private.get())->isFocusable(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool WebNode::isContentEditable() const |
| 163 | { |
| 164 | return m_private->isContentEditable(); |
| 165 | } |
| 166 | |
| 167 | bool WebNode::isElementNode() const |
| 168 | { |
| 169 | return m_private->isElementNode(); |
| 170 | } |
| 171 | |
| 172 | void 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 | |
| 184 | bool WebNode::dispatchEvent(const WebDOMEvent& event) |
| 185 | { |
| 186 | if (!event.isNull()) |
| 187 | return m_private->dispatchEvent(event); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | void WebNode::simulateClick() |
| 192 | { |
| 193 | m_private->dispatchSimulatedClick(0); |
| 194 | } |
| 195 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 196 | WebElementCollection WebNode::getElementsByTagName(const WebString& tag) const |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 197 | { |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 198 | if (m_private->isContainerNode()) |
| 199 | return WebElementCollection(toContainerNode(m_private.get())->getElementsByTagName(tag)); |
| 200 | return WebElementCollection(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | WebElement WebNode::querySelector(const WebString& tag, WebExceptionCode& ec) const |
| 204 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 205 | TrackExceptionState exceptionState; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 206 | WebElement element; |
| 207 | if (m_private->isContainerNode()) |
| 208 | element = toContainerNode(m_private.get())->querySelector(tag, exceptionState); |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 209 | ec = exceptionState.code(); |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 210 | return element; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | WebElement WebNode::rootEditableElement() const |
| 214 | { |
| 215 | return WebElement(m_private->rootEditableElement()); |
| 216 | } |
| 217 | |
| 218 | bool WebNode::focused() const |
| 219 | { |
| 220 | return m_private->focused(); |
| 221 | } |
| 222 | |
| 223 | bool WebNode::remove() |
| 224 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 225 | TrackExceptionState exceptionState; |
| 226 | m_private->remove(exceptionState); |
| 227 | return !exceptionState.hadException(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool WebNode::hasNonEmptyBoundingBox() const |
| 231 | { |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 232 | m_private->document().updateLayoutIgnorePendingStylesheets(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 233 | return m_private->hasNonEmptyBoundingBox(); |
| 234 | } |
| 235 | |
| 236 | WebPluginContainer* WebNode::pluginContainer() const |
| 237 | { |
| 238 | if (isNull()) |
| 239 | return 0; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 240 | const Node& coreNode = *constUnwrap<Node>(); |
| 241 | if (isHTMLObjectElement(coreNode) || isHTMLEmbedElement(coreNode)) { |
| 242 | RenderObject* object = coreNode.renderer(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 243 | if (object && object->isWidget()) { |
| 244 | Widget* widget = WebCore::toRenderWidget(object)->widget(); |
| 245 | if (widget && widget->isPluginContainer()) |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 246 | return toWebPluginContainerImpl(widget); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | WebElement WebNode::shadowHost() const |
| 253 | { |
| 254 | if (isNull()) |
| 255 | return WebElement(); |
| 256 | const Node* coreNode = constUnwrap<Node>(); |
| 257 | return WebElement(coreNode->shadowHost()); |
| 258 | } |
| 259 | |
| 260 | WebNode::WebNode(const PassRefPtr<Node>& node) |
| 261 | : m_private(node) |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | WebNode& WebNode::operator=(const PassRefPtr<Node>& node) |
| 266 | { |
| 267 | m_private = node; |
| 268 | return *this; |
| 269 | } |
| 270 | |
| 271 | WebNode::operator PassRefPtr<Node>() const |
| 272 | { |
| 273 | return m_private.get(); |
| 274 | } |
| 275 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 276 | } // namespace blink |