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/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 "wtf/PassRefPtr.h"
43
44
45 using namespace WebCore;
46
47 namespace blink {
48
isFormControlElement() const49 bool WebElement::isFormControlElement() const
50 {
51 return constUnwrap<Element>()->isFormControlElement();
52 }
53
isTextFormControlElement() const54 bool WebElement::isTextFormControlElement() const
55 {
56 return constUnwrap<Element>()->isTextFormControl();
57 }
58
tagName() const59 WebString WebElement::tagName() const
60 {
61 return constUnwrap<Element>()->tagName();
62 }
63
hasTagName(const WebString & tagName) const64 bool WebElement::hasTagName(const WebString& tagName) const
65 {
66 return equalIgnoringCase(constUnwrap<Element>()->tagName(),
67 tagName.operator String());
68 }
69
hasHTMLTagName(const WebString & tagName) const70 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
hasAttribute(const WebString & attrName) const81 bool WebElement::hasAttribute(const WebString& attrName) const
82 {
83 return constUnwrap<Element>()->hasAttribute(attrName);
84 }
85
removeAttribute(const WebString & attrName)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
getAttribute(const WebString & attrName) const94 WebString WebElement::getAttribute(const WebString& attrName) const
95 {
96 return constUnwrap<Element>()->getAttribute(attrName);
97 }
98
setAttribute(const WebString & attrName,const WebString & attrValue)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 exceptionState;
105 unwrap<Element>()->setAttribute(attrName, attrValue, exceptionState);
106 return !exceptionState.hadException();
107 }
108
attributeCount() const109 unsigned WebElement::attributeCount() const
110 {
111 if (!constUnwrap<Element>()->hasAttributes())
112 return 0;
113 return constUnwrap<Element>()->attributeCount();
114 }
115
shadowRoot() const116 WebNode WebElement::shadowRoot() const
117 {
118 ShadowRoot* shadowRoot = constUnwrap<Element>()->shadowRoot();
119 if (!shadowRoot)
120 return WebNode();
121 return WebNode(shadowRoot->toNode());
122 }
123
attributeLocalName(unsigned index) const124 WebString WebElement::attributeLocalName(unsigned index) const
125 {
126 if (index >= attributeCount())
127 return WebString();
128 return constUnwrap<Element>()->attributeItem(index)->localName();
129 }
130
attributeValue(unsigned index) const131 WebString WebElement::attributeValue(unsigned index) const
132 {
133 if (index >= attributeCount())
134 return WebString();
135 return constUnwrap<Element>()->attributeItem(index)->value();
136 }
137
innerText()138 WebString WebElement::innerText()
139 {
140 return unwrap<Element>()->innerText();
141 }
142
computeInheritedLanguage() const143 WebString WebElement::computeInheritedLanguage() const
144 {
145 return WebString(constUnwrap<Element>()->computeInheritedLanguage());
146 }
147
requestFullScreen()148 void WebElement::requestFullScreen()
149 {
150 unwrap<Element>()->webkitRequestFullScreen(Element::ALLOW_KEYBOARD_INPUT);
151 }
152
boundsInViewportSpace()153 WebRect WebElement::boundsInViewportSpace()
154 {
155 return unwrap<Element>()->boundsInRootViewSpace();
156 }
157
imageContents()158 WebImage 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
WebElement(const PassRefPtr<Element> & elem)174 WebElement::WebElement(const PassRefPtr<Element>& elem)
175 : WebNode(elem)
176 {
177 }
178
operator =(const PassRefPtr<Element> & elem)179 WebElement& WebElement::operator=(const PassRefPtr<Element>& elem)
180 {
181 m_private = elem;
182 return *this;
183 }
184
operator PassRefPtr<Element>() const185 WebElement::operator PassRefPtr<Element>() const
186 {
187 return toElement(m_private.get());
188 }
189
190 } // namespace blink
191