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