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 blink {
     40 
     41 class Node;
     42 class WebDOMEvent;
     43 class WebDocument;
     44 class WebElement;
     45 class WebElementCollection;
     46 class WebNodeList;
     47 class WebPluginContainer;
     48 
     49 // Provides access to some properties of a DOM node.
     50 // Note that the class design requires that neither this class nor any of its subclasses have any virtual
     51 // methods (other than the destructor), so that it is possible to safely static_cast an instance of one
     52 // class to the appropriate subclass based on the actual type of the wrapped blink::Node. For the same
     53 // reason, subclasses must not add any additional data members.
     54 class WebNode {
     55 public:
     56     virtual ~WebNode() { reset(); }
     57 
     58     WebNode() { }
     59     WebNode(const WebNode& n) { assign(n); }
     60     WebNode& operator=(const WebNode& n)
     61     {
     62         assign(n);
     63         return *this;
     64     }
     65 
     66     BLINK_EXPORT void reset();
     67     BLINK_EXPORT void assign(const WebNode&);
     68 
     69     BLINK_EXPORT bool equals(const WebNode&) const;
     70     // Required for using WebNodes in std maps.  Note the order used is
     71     // arbitrary and should not be expected to have any specific meaning.
     72     BLINK_EXPORT bool lessThan(const WebNode&) const;
     73 
     74     bool isNull() const { return m_private.isNull(); }
     75 
     76     enum NodeType {
     77         ElementNode = 1,
     78         AttributeNode = 2,
     79         TextNode = 3,
     80         CDataSectionNode = 4,
     81         // EntityReferenceNodes are impossible to create in Blink.
     82         // EntityNodes are impossible to create in Blink.
     83         ProcessingInstructionsNode = 7,
     84         CommentNode = 8,
     85         DocumentNode = 9,
     86         DocumentTypeNode = 10,
     87         DocumentFragmentNode = 11,
     88         // NotationNodes are impossible to create in Blink.
     89         // XPathNamespaceNodes are impossible to create in Blink.
     90         ShadowRootNode = 14
     91     };
     92 
     93     BLINK_EXPORT NodeType nodeType() const;
     94     BLINK_EXPORT WebNode parentNode() const;
     95     BLINK_EXPORT WebString nodeName() const;
     96     BLINK_EXPORT WebString nodeValue() const;
     97     BLINK_EXPORT WebDocument document() const;
     98     BLINK_EXPORT WebNode firstChild() const;
     99     BLINK_EXPORT WebNode lastChild() const;
    100     BLINK_EXPORT WebNode previousSibling() const;
    101     BLINK_EXPORT WebNode nextSibling() const;
    102     BLINK_EXPORT bool hasChildNodes() const;
    103     BLINK_EXPORT WebNodeList childNodes();
    104     BLINK_EXPORT WebString createMarkup() const;
    105     BLINK_EXPORT bool isLink() const;
    106     BLINK_EXPORT bool isTextNode() const;
    107     BLINK_EXPORT bool isFocusable() const;
    108     BLINK_EXPORT bool isContentEditable() const;
    109     BLINK_EXPORT bool isElementNode() const;
    110     BLINK_EXPORT bool dispatchEvent(const WebDOMEvent&);
    111     BLINK_EXPORT void simulateClick();
    112     // The argument should be lower-cased.
    113     BLINK_EXPORT WebElementCollection getElementsByHTMLTagName(const WebString&) const;
    114     BLINK_EXPORT WebElement querySelector(const WebString&, WebExceptionCode&) const;
    115     BLINK_EXPORT WebElement rootEditableElement() const;
    116     BLINK_EXPORT bool focused() const;
    117     BLINK_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     BLINK_EXPORT bool hasNonEmptyBoundingBox() const;
    123 
    124     BLINK_EXPORT bool containsIncludingShadowDOM(const WebNode&) const;
    125     BLINK_EXPORT WebPluginContainer* pluginContainer() const;
    126     BLINK_EXPORT WebElement shadowHost() const;
    127 
    128     template<typename T> T to()
    129     {
    130         T res;
    131         res.WebNode::assign(*this);
    132         return res;
    133     }
    134 
    135     template<typename T> const T toConst() const
    136     {
    137         T res;
    138         res.WebNode::assign(*this);
    139         return res;
    140     }
    141 
    142 #if BLINK_IMPLEMENTATION
    143     WebNode(const PassRefPtrWillBeRawPtr<Node>&);
    144     WebNode& operator=(const PassRefPtrWillBeRawPtr<Node>&);
    145     operator PassRefPtrWillBeRawPtr<Node>() const;
    146 #endif
    147 
    148 #if BLINK_IMPLEMENTATION
    149     template<typename T> T* unwrap()
    150     {
    151         return static_cast<T*>(m_private.get());
    152     }
    153 
    154     template<typename T> const T* constUnwrap() const
    155     {
    156         return static_cast<const T*>(m_private.get());
    157     }
    158 #endif
    159 
    160 protected:
    161     WebPrivatePtr<Node> m_private;
    162 };
    163 
    164 inline bool operator==(const WebNode& a, const WebNode& b)
    165 {
    166     return a.equals(b);
    167 }
    168 
    169 inline bool operator!=(const WebNode& a, const WebNode& b)
    170 {
    171     return !(a == b);
    172 }
    173 
    174 inline bool operator<(const WebNode& a, const WebNode& b)
    175 {
    176     return a.lessThan(b);
    177 }
    178 
    179 } // namespace blink
    180 
    181 #endif
    182