Home | History | Annotate | Download | only in src
      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 "Document.h"
     35 #include "Frame.h"
     36 #include "FrameLoaderClientImpl.h"
     37 #include "Node.h"
     38 #include "NodeList.h"
     39 
     40 #include "EventListenerWrapper.h"
     41 #include "WebDocument.h"
     42 #include "WebEvent.h"
     43 #include "WebEventListener.h"
     44 #include "WebFrameImpl.h"
     45 #include "WebNodeList.h"
     46 #include "WebString.h"
     47 #include "WebVector.h"
     48 
     49 #include "markup.h"
     50 
     51 #include <wtf/PassRefPtr.h>
     52 
     53 using namespace WebCore;
     54 
     55 namespace WebKit {
     56 
     57 class WebNodePrivate : public Node {
     58 };
     59 
     60 void WebNode::reset()
     61 {
     62     assign(0);
     63 }
     64 
     65 void WebNode::assign(const WebNode& other)
     66 {
     67     WebNodePrivate* p = const_cast<WebNodePrivate*>(other.m_private);
     68     if (p)
     69         p->ref();
     70     assign(p);
     71 }
     72 
     73 WebNode::NodeType WebNode::nodeType() const
     74 {
     75     return static_cast<NodeType>(m_private->nodeType());
     76 }
     77 
     78 WebNode WebNode::parentNode() const
     79 {
     80     return PassRefPtr<Node>(const_cast<Node*>(m_private->parentNode()));
     81 }
     82 
     83 WebString WebNode::nodeName() const
     84 {
     85     return m_private->nodeName();
     86 }
     87 
     88 WebString WebNode::nodeValue() const
     89 {
     90     return m_private->nodeValue();
     91 }
     92 
     93 bool WebNode::setNodeValue(const WebString& value)
     94 {
     95     ExceptionCode exceptionCode = 0;
     96     m_private->setNodeValue(value, exceptionCode);
     97     return !exceptionCode;
     98 }
     99 
    100 WebNode::WebNode(const PassRefPtr<Node>& node)
    101     : m_private(static_cast<WebNodePrivate*>(node.releaseRef()))
    102 {
    103 }
    104 
    105 WebNode& WebNode::operator=(const PassRefPtr<Node>& node)
    106 {
    107     assign(static_cast<WebNodePrivate*>(node.releaseRef()));
    108     return *this;
    109 }
    110 
    111 WebNode::operator PassRefPtr<Node>() const
    112 {
    113     return PassRefPtr<Node>(const_cast<WebNodePrivate*>(m_private));
    114 }
    115 
    116 void WebNode::assign(WebNodePrivate* p)
    117 {
    118     // p is already ref'd for us by the caller
    119     if (m_private)
    120         m_private->deref();
    121     m_private = p;
    122 }
    123 
    124 WebFrame* WebNode::frame() const
    125 {
    126     return WebFrameImpl::fromFrame(m_private->document()->frame());
    127 }
    128 
    129 WebDocument WebNode::document() const
    130 {
    131     return WebDocument(m_private->document());
    132 }
    133 
    134 WebNode WebNode::firstChild() const
    135 {
    136     return WebNode(m_private->firstChild());
    137 }
    138 
    139 WebNode WebNode::lastChild() const
    140 {
    141     return WebNode(m_private->lastChild());
    142 }
    143 
    144 WebNode WebNode::previousSibling() const
    145 {
    146     return WebNode(m_private->previousSibling());
    147 }
    148 
    149 WebNode WebNode::nextSibling() const
    150 {
    151     return WebNode(m_private->nextSibling());
    152 }
    153 
    154 bool WebNode::hasChildNodes() const
    155 {
    156     return m_private->hasChildNodes();
    157 }
    158 
    159 WebNodeList WebNode::childNodes()
    160 {
    161     return WebNodeList(m_private->childNodes());
    162 }
    163 
    164 WebString WebNode::createMarkup() const
    165 {
    166     return WebCore::createMarkup(m_private);
    167 }
    168 
    169 bool WebNode::isTextNode() const
    170 {
    171     return m_private->isTextNode();
    172 }
    173 
    174 bool WebNode::isElementNode() const
    175 {
    176     return m_private->isElementNode();
    177 }
    178 
    179 void WebNode::addEventListener(const WebString& eventType, WebEventListener* listener, bool useCapture)
    180 {
    181     EventListenerWrapper* listenerWrapper =
    182         listener->createEventListenerWrapper(eventType, useCapture, m_private);
    183     // The listenerWrapper is only referenced by the actual Node.  Once it goes
    184     // away, the wrapper notifies the WebEventListener so it can clear its
    185     // pointer to it.
    186     m_private->addEventListener(eventType, adoptRef(listenerWrapper), useCapture);
    187 }
    188 
    189 void WebNode::removeEventListener(const WebString& eventType, WebEventListener* listener, bool useCapture)
    190 {
    191     EventListenerWrapper* listenerWrapper =
    192         listener->getEventListenerWrapper(eventType, useCapture, m_private);
    193     m_private->removeEventListener(eventType, listenerWrapper, useCapture);
    194     // listenerWrapper is now deleted.
    195 }
    196 
    197 } // namespace WebKit
    198