blob: 2130461b8a37572ded80a06cccb6f38d7d24e5e5 [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
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"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000032#include "WebDocument.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033#include "WebElement.h"
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010034#include "bindings/v8/ExceptionState.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010035#include "core/dom/Element.h"
36#include "core/dom/NamedNodeMap.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010037#include "core/dom/custom/CustomElementCallbackDispatcher.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010038#include "core/dom/shadow/ShadowRoot.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010039#include "core/rendering/RenderBoxModelObject.h"
40#include "core/rendering/RenderObject.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010041#include "public/platform/WebRect.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010042#include "wtf/PassRefPtr.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000043
44
45using namespace WebCore;
46
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000047namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000048
49bool WebElement::isFormControlElement() const
50{
51 return constUnwrap<Element>()->isFormControlElement();
52}
53
54bool WebElement::isTextFormControlElement() const
55{
56 return constUnwrap<Element>()->isTextFormControl();
57}
58
59WebString WebElement::tagName() const
60{
61 return constUnwrap<Element>()->tagName();
62}
63
64bool WebElement::hasTagName(const WebString& tagName) const
65{
66 return equalIgnoringCase(constUnwrap<Element>()->tagName(),
67 tagName.operator String());
68}
69
70bool 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
81bool WebElement::hasAttribute(const WebString& attrName) const
82{
83 return constUnwrap<Element>()->hasAttribute(attrName);
84}
85
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000086void WebElement::removeAttribute(const WebString& attrName)
87{
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010088 // TODO: Custom element callbacks need to be called on WebKit API methods that
89 // mutate the DOM in any way.
90 CustomElementCallbackDispatcher::CallbackDeliveryScope deliverCustomElementCallbacks;
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000091 unwrap<Element>()->removeAttribute(attrName);
92}
93
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000094WebString WebElement::getAttribute(const WebString& attrName) const
95{
96 return constUnwrap<Element>()->getAttribute(attrName);
97}
98
99bool WebElement::setAttribute(const WebString& attrName, const WebString& attrValue)
100{
Ben Murdoch1fad5ca2013-08-07 11:05:11 +0100101 // TODO: Custom element callbacks need to be called on WebKit API methods that
102 // mutate the DOM in any way.
103 CustomElementCallbackDispatcher::CallbackDeliveryScope deliverCustomElementCallbacks;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000104 TrackExceptionState exceptionState;
105 unwrap<Element>()->setAttribute(attrName, attrValue, exceptionState);
106 return !exceptionState.hadException();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000107}
108
109unsigned WebElement::attributeCount() const
110{
111 if (!constUnwrap<Element>()->hasAttributes())
112 return 0;
113 return constUnwrap<Element>()->attributeCount();
114}
115
Torne (Richard Coles)926b0012013-03-28 15:32:48 +0000116WebNode WebElement::shadowRoot() const
117{
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100118 ShadowRoot* shadowRoot = constUnwrap<Element>()->shadowRoot();
119 if (!shadowRoot)
120 return WebNode();
121 return WebNode(shadowRoot->toNode());
Torne (Richard Coles)926b0012013-03-28 15:32:48 +0000122}
123
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000124WebString WebElement::attributeLocalName(unsigned index) const
125{
126 if (index >= attributeCount())
127 return WebString();
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000128 return constUnwrap<Element>()->attributeItem(index).localName();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000129}
130
131WebString WebElement::attributeValue(unsigned index) const
132{
133 if (index >= attributeCount())
134 return WebString();
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000135 return constUnwrap<Element>()->attributeItem(index).value();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000136}
137
138WebString WebElement::innerText()
139{
140 return unwrap<Element>()->innerText();
141}
142
143WebString WebElement::computeInheritedLanguage() const
144{
145 return WebString(constUnwrap<Element>()->computeInheritedLanguage());
146}
147
148void WebElement::requestFullScreen()
149{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000150 unwrap<Element>()->webkitRequestFullScreen(Element::ALLOW_KEYBOARD_INPUT);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000151}
152
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000153WebRect WebElement::boundsInViewportSpace()
154{
155 return unwrap<Element>()->boundsInRootViewSpace();
156}
157
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100158WebImage WebElement::imageContents()
159{
160 if (isNull())
161 return WebImage();
162
163 WebCore::Image* image = unwrap<Element>()->imageContents();
164 if (!image)
165 return WebImage();
166
167 RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame();
168 if (!bitmap)
169 return WebImage();
170
171 return bitmap->bitmap();
172}
173
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000174WebElement::WebElement(const PassRefPtr<Element>& elem)
175 : WebNode(elem)
176{
177}
178
179WebElement& WebElement::operator=(const PassRefPtr<Element>& elem)
180{
181 m_private = elem;
182 return *this;
183}
184
185WebElement::operator PassRefPtr<Element>() const
186{
Torne (Richard Coles)926b0012013-03-28 15:32:48 +0000187 return toElement(m_private.get());
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000188}
189
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000190} // namespace blink