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 #include "config.h"
     32 #include "WebDocument.h"
     33 #include "WebElement.h"
     34 #include "bindings/v8/ExceptionState.h"
     35 #include "core/dom/CustomElementCallbackDispatcher.h"
     36 #include "core/dom/Element.h"
     37 #include "core/dom/NamedNodeMap.h"
     38 #include "core/dom/shadow/ShadowRoot.h"
     39 #include "core/rendering/RenderBoxModelObject.h"
     40 #include "core/rendering/RenderObject.h"
     41 #include "public/platform/WebRect.h"
     42 #include "wtf/PassRefPtr.h"
     43 
     44 
     45 using namespace WebCore;
     46 
     47 namespace WebKit {
     48 
     49 bool WebElement::isFormControlElement() const
     50 {
     51     return constUnwrap<Element>()->isFormControlElement();
     52 }
     53 
     54 bool WebElement::isTextFormControlElement() const
     55 {
     56     return constUnwrap<Element>()->isTextFormControl();
     57 }
     58 
     59 WebString WebElement::tagName() const
     60 {
     61     return constUnwrap<Element>()->tagName();
     62 }
     63 
     64 bool WebElement::hasTagName(const WebString& tagName) const
     65 {
     66     return equalIgnoringCase(constUnwrap<Element>()->tagName(),
     67                              tagName.operator String());
     68 }
     69 
     70 bool WebElement::hasHTMLTagName(const WebString& tagName) const
     71 {
     72     // How to create                     class              nodeName localName
     73     // createElement('input')            HTMLInputElement   INPUT    input
     74     // createElement('INPUT')            HTMLInputElement   INPUT    input
     75     // createElementNS(xhtmlNS, 'input') HTMLInputElement   INPUT    input
     76     // createElementNS(xhtmlNS, 'INPUT') HTMLUnknownElement INPUT    INPUT
     77     const Element* element = constUnwrap<Element>();
     78     return HTMLNames::xhtmlNamespaceURI == element->namespaceURI() && element->localName() == String(tagName).lower();
     79 }
     80 
     81 bool WebElement::hasAttribute(const WebString& attrName) const
     82 {
     83     return constUnwrap<Element>()->hasAttribute(attrName);
     84 }
     85 
     86 void WebElement::removeAttribute(const WebString& attrName)
     87 {
     88     // TODO: Custom element callbacks need to be called on WebKit API methods that
     89     // mutate the DOM in any way.
     90     CustomElementCallbackDispatcher::CallbackDeliveryScope deliverCustomElementCallbacks;
     91     unwrap<Element>()->removeAttribute(attrName);
     92 }
     93 
     94 WebString WebElement::getAttribute(const WebString& attrName) const
     95 {
     96     return constUnwrap<Element>()->getAttribute(attrName);
     97 }
     98 
     99 bool WebElement::setAttribute(const WebString& attrName, const WebString& attrValue)
    100 {
    101     // TODO: Custom element callbacks need to be called on WebKit API methods that
    102     // mutate the DOM in any way.
    103     CustomElementCallbackDispatcher::CallbackDeliveryScope deliverCustomElementCallbacks;
    104     TrackExceptionState es;
    105     unwrap<Element>()->setAttribute(attrName, attrValue, es);
    106     return !es.hadException();
    107 }
    108 
    109 unsigned WebElement::attributeCount() const
    110 {
    111     if (!constUnwrap<Element>()->hasAttributes())
    112         return 0;
    113     return constUnwrap<Element>()->attributeCount();
    114 }
    115 
    116 WebNode WebElement::shadowRoot() const
    117 {
    118     ShadowRoot* shadowRoot = constUnwrap<Element>()->shadowRoot();
    119     if (!shadowRoot)
    120         return WebNode();
    121     return WebNode(shadowRoot->toNode());
    122 }
    123 
    124 WebString WebElement::attributeLocalName(unsigned index) const
    125 {
    126     if (index >= attributeCount())
    127         return WebString();
    128     return constUnwrap<Element>()->attributeItem(index)->localName();
    129 }
    130 
    131 WebString WebElement::attributeValue(unsigned index) const
    132 {
    133     if (index >= attributeCount())
    134         return WebString();
    135     return constUnwrap<Element>()->attributeItem(index)->value();
    136 }
    137 
    138 WebString WebElement::innerText()
    139 {
    140     return unwrap<Element>()->innerText();
    141 }
    142 
    143 WebString WebElement::computeInheritedLanguage() const
    144 {
    145     return WebString(constUnwrap<Element>()->computeInheritedLanguage());
    146 }
    147 
    148 void WebElement::requestFullScreen()
    149 {
    150     unwrap<Element>()->webkitRequestFullScreen(Element::ALLOW_KEYBOARD_INPUT);
    151 }
    152 
    153 WebDocument WebElement::document() const
    154 {
    155     return WebDocument(constUnwrap<Element>()->document());
    156 }
    157 
    158 WebRect WebElement::boundsInViewportSpace()
    159 {
    160     return unwrap<Element>()->boundsInRootViewSpace();
    161 }
    162 
    163 WebImage WebElement::imageContents()
    164 {
    165     if (isNull())
    166         return WebImage();
    167 
    168     WebCore::Image* image = unwrap<Element>()->imageContents();
    169     if (!image)
    170         return WebImage();
    171 
    172     RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame();
    173     if (!bitmap)
    174         return WebImage();
    175 
    176     return bitmap->bitmap();
    177 }
    178 
    179 WebElement::WebElement(const PassRefPtr<Element>& elem)
    180     : WebNode(elem)
    181 {
    182 }
    183 
    184 WebElement& WebElement::operator=(const PassRefPtr<Element>& elem)
    185 {
    186     m_private = elem;
    187     return *this;
    188 }
    189 
    190 WebElement::operator PassRefPtr<Element>() const
    191 {
    192     return toElement(m_private.get());
    193 }
    194 
    195 } // namespace WebKit
    196