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" 40 #include "WebFrameImpl.h" 41 #include "WebNodeList.h" 42 #include "WebPluginContainer.h" 43 #include "WebPluginContainerImpl.h" 44 #include "bindings/v8/ExceptionState.h" 45 #include "core/dom/Document.h" 46 #include "core/dom/Element.h" 47 #include "core/dom/Event.h" 48 #include "core/dom/Node.h" 49 #include "core/dom/NodeList.h" 50 #include "core/editing/markup.h" 51 #include "core/page/Frame.h" 52 #include "core/platform/Widget.h" 53 #include "core/rendering/RenderObject.h" 54 #include "core/rendering/RenderWidget.h" 55 #include "public/platform/WebString.h" 56 #include "public/platform/WebVector.h" 57 58 using namespace WebCore; 59 60 namespace WebKit { 61 62 void WebNode::reset() 63 { 64 m_private.reset(); 65 } 66 67 void WebNode::assign(const WebNode& other) 68 { 69 m_private = other.m_private; 70 } 71 72 bool WebNode::equals(const WebNode& n) const 73 { 74 return m_private.get() == n.m_private.get(); 75 } 76 77 bool WebNode::lessThan(const WebNode& n) const 78 { 79 return m_private.get() < n.m_private.get(); 80 } 81 82 WebNode::NodeType WebNode::nodeType() const 83 { 84 return static_cast<NodeType>(m_private->nodeType()); 85 } 86 87 WebNode WebNode::parentNode() const 88 { 89 return WebNode(const_cast<ContainerNode*>(m_private->parentNode())); 90 } 91 92 WebString WebNode::nodeName() const 93 { 94 return m_private->nodeName(); 95 } 96 97 WebString WebNode::nodeValue() const 98 { 99 return m_private->nodeValue(); 100 } 101 102 bool WebNode::setNodeValue(const WebString& value) 103 { 104 m_private->setNodeValue(value); 105 return true; 106 } 107 108 WebDocument WebNode::document() const 109 { 110 return WebDocument(m_private->document()); 111 } 112 113 WebNode WebNode::firstChild() const 114 { 115 return WebNode(m_private->firstChild()); 116 } 117 118 WebNode WebNode::lastChild() const 119 { 120 return WebNode(m_private->lastChild()); 121 } 122 123 WebNode WebNode::previousSibling() const 124 { 125 return WebNode(m_private->previousSibling()); 126 } 127 128 WebNode WebNode::nextSibling() const 129 { 130 return WebNode(m_private->nextSibling()); 131 } 132 133 bool WebNode::hasChildNodes() const 134 { 135 return m_private->hasChildNodes(); 136 } 137 138 WebNodeList WebNode::childNodes() 139 { 140 return WebNodeList(m_private->childNodes()); 141 } 142 143 WebString WebNode::createMarkup() const 144 { 145 return WebCore::createMarkup(m_private.get()); 146 } 147 148 bool WebNode::isLink() const 149 { 150 return m_private->isLink(); 151 } 152 153 bool WebNode::isTextNode() const 154 { 155 return m_private->isTextNode(); 156 } 157 158 bool WebNode::isFocusable() const 159 { 160 if (!m_private->isElementNode()) 161 return false; 162 m_private->document()->updateLayoutIgnorePendingStylesheets(); 163 return toElement(m_private.get())->isFocusable(); 164 } 165 166 bool WebNode::isContentEditable() const 167 { 168 return m_private->isContentEditable(); 169 } 170 171 bool WebNode::isElementNode() const 172 { 173 return m_private->isElementNode(); 174 } 175 176 void WebNode::addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture) 177 { 178 // Please do not add more eventTypes to this list without an API review. 179 RELEASE_ASSERT(eventType == "mousedown"); 180 181 EventListenerWrapper* listenerWrapper = listener->createEventListenerWrapper(eventType, useCapture, m_private.get()); 182 // The listenerWrapper is only referenced by the actual Node. Once it goes 183 // away, the wrapper notifies the WebEventListener so it can clear its 184 // pointer to it. 185 m_private->addEventListener(eventType, adoptRef(listenerWrapper), useCapture); 186 } 187 188 bool WebNode::dispatchEvent(const WebDOMEvent& event) 189 { 190 if (!event.isNull()) 191 return m_private->dispatchEvent(event); 192 return false; 193 } 194 195 void WebNode::simulateClick() 196 { 197 m_private->dispatchSimulatedClick(0); 198 } 199 200 WebNodeList WebNode::getElementsByTagName(const WebString& tag) const 201 { 202 return WebNodeList(m_private->getElementsByTagName(tag)); 203 } 204 205 WebElement WebNode::querySelector(const WebString& tag, WebExceptionCode& ec) const 206 { 207 TrackExceptionState es; 208 WebElement element(m_private->querySelector(tag, es)); 209 ec = es.code(); 210 return element; 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 { 225 TrackExceptionState es; 226 m_private->remove(es); 227 return !es.hadException(); 228 } 229 230 bool WebNode::hasNonEmptyBoundingBox() const 231 { 232 m_private->document()->updateLayoutIgnorePendingStylesheets(); 233 return m_private->hasNonEmptyBoundingBox(); 234 } 235 236 WebPluginContainer* WebNode::pluginContainer() const 237 { 238 if (isNull()) 239 return 0; 240 const Node* coreNode = constUnwrap<Node>(); 241 if (coreNode->hasTagName(HTMLNames::objectTag) || coreNode->hasTagName(HTMLNames::embedTag)) { 242 RenderObject* object = coreNode->renderer(); 243 if (object && object->isWidget()) { 244 Widget* widget = WebCore::toRenderWidget(object)->widget(); 245 if (widget && widget->isPluginContainer()) 246 return static_cast<WebPluginContainerImpl*>(widget); 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 276 } // namespace WebKit 277