Home | History | Annotate | Download | only in web
      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 #ifndef WebNode_h
     32 #define WebNode_h
     33 
     34 #include "../platform/WebCommon.h"
     35 #include "../platform/WebPrivatePtr.h"
     36 #include "../platform/WebString.h"
     37 #include "WebExceptionCode.h"
     38 
     39 namespace WebCore { class Node; }
     40 
     41 namespace WebKit {
     42 class WebDOMEvent;
     43 class WebDOMEventListener;
     44 class WebDOMEventListenerPrivate;
     45 class WebDocument;
     46 class WebElement;
     47 class WebFrame;
     48 class WebNodeList;
     49 class WebPluginContainer;
     50 
     51 // Provides access to some properties of a DOM node.
     52 class WebNode {
     53 public:
     54     virtual ~WebNode() { reset(); }
     55 
     56     WebNode() { }
     57     WebNode(const WebNode& n) { assign(n); }
     58     WebNode& operator=(const WebNode& n)
     59     {
     60         assign(n);
     61         return *this;
     62     }
     63 
     64     WEBKIT_EXPORT void reset();
     65     WEBKIT_EXPORT void assign(const WebNode&);
     66 
     67     WEBKIT_EXPORT bool equals(const WebNode&) const;
     68     // Required for using WebNodes in std maps.  Note the order used is
     69     // arbitrary and should not be expected to have any specific meaning.
     70     WEBKIT_EXPORT bool lessThan(const WebNode&) const;
     71 
     72     bool isNull() const { return m_private.isNull(); }
     73 
     74     enum NodeType {
     75         ElementNode = 1,
     76         AttributeNode = 2,
     77         TextNode = 3,
     78         CDataSectionNode = 4,
     79         // EntityReferenceNodes are deprecated and impossible to create in WebKit.
     80         EntityNode = 6,
     81         ProcessingInstructionsNode = 7,
     82         CommentNode = 8,
     83         DocumentNode = 9,
     84         DocumentTypeNode = 10,
     85         DocumentFragmentNode = 11,
     86         NotationNode = 12,
     87         XPathNamespaceNode = 13,
     88         ShadowRootNode = 14
     89     };
     90 
     91     WEBKIT_EXPORT NodeType nodeType() const;
     92     WEBKIT_EXPORT WebNode parentNode() const;
     93     WEBKIT_EXPORT WebString nodeName() const;
     94     WEBKIT_EXPORT WebString nodeValue() const;
     95     WEBKIT_EXPORT bool setNodeValue(const WebString&);
     96     WEBKIT_EXPORT WebDocument document() const;
     97     WEBKIT_EXPORT WebNode firstChild() const;
     98     WEBKIT_EXPORT WebNode lastChild() const;
     99     WEBKIT_EXPORT WebNode previousSibling() const;
    100     WEBKIT_EXPORT WebNode nextSibling() const;
    101     WEBKIT_EXPORT bool hasChildNodes() const;
    102     WEBKIT_EXPORT WebNodeList childNodes();
    103     WEBKIT_EXPORT WebString createMarkup() const;
    104     WEBKIT_EXPORT bool isLink() const;
    105     WEBKIT_EXPORT bool isTextNode() const;
    106     WEBKIT_EXPORT bool isFocusable() const;
    107     WEBKIT_EXPORT bool isContentEditable() const;
    108     WEBKIT_EXPORT bool isElementNode() const;
    109     // addEventListener only works with a small set of eventTypes.
    110     WEBKIT_EXPORT void addEventListener(const WebString& eventType, WebDOMEventListener* listener, bool useCapture);
    111     WEBKIT_EXPORT bool dispatchEvent(const WebDOMEvent&);
    112     WEBKIT_EXPORT void simulateClick();
    113     WEBKIT_EXPORT WebNodeList getElementsByTagName(const WebString&) const;
    114     WEBKIT_EXPORT WebElement querySelector(const WebString&, WebExceptionCode&) const;
    115     WEBKIT_EXPORT WebElement rootEditableElement() const;
    116     WEBKIT_EXPORT bool focused() const;
    117     WEBKIT_EXPORT bool remove();
    118 
    119     // Returns true if the node has a non-empty bounding box in layout.
    120     // This does not 100% guarantee the user can see it, but is pretty close.
    121     // Note: This method only works properly after layout has occurred.
    122     WEBKIT_EXPORT bool hasNonEmptyBoundingBox() const;
    123     WEBKIT_EXPORT WebPluginContainer* pluginContainer() const;
    124     WEBKIT_EXPORT WebElement shadowHost() const;
    125 
    126     template<typename T> T to()
    127     {
    128         T res;
    129         res.WebNode::assign(*this);
    130         return res;
    131     }
    132 
    133     template<typename T> const T toConst() const
    134     {
    135         T res;
    136         res.WebNode::assign(*this);
    137         return res;
    138     }
    139 
    140 #if WEBKIT_IMPLEMENTATION
    141     WebNode(const WTF::PassRefPtr<WebCore::Node>&);
    142     WebNode& operator=(const WTF::PassRefPtr<WebCore::Node>&);
    143     operator WTF::PassRefPtr<WebCore::Node>() const;
    144 #endif
    145 
    146 #if WEBKIT_IMPLEMENTATION
    147     template<typename T> T* unwrap()
    148     {
    149         return static_cast<T*>(m_private.get());
    150     }
    151 
    152     template<typename T> const T* constUnwrap() const
    153     {
    154         return static_cast<const T*>(m_private.get());
    155     }
    156 #endif
    157 
    158 protected:
    159     WebPrivatePtr<WebCore::Node> m_private;
    160 };
    161 
    162 inline bool operator==(const WebNode& a, const WebNode& b)
    163 {
    164     return a.equals(b);
    165 }
    166 
    167 inline bool operator!=(const WebNode& a, const WebNode& b)
    168 {
    169     return !(a == b);
    170 }
    171 
    172 inline bool operator<(const WebNode& a, const WebNode& b)
    173 {
    174     return a.lessThan(b);
    175 }
    176 
    177 } // namespace WebKit
    178 
    179 #endif
    180