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