Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / WebElement.cpp
blob0a9668ee9266eace187c508674462091c4cc31b9
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "public/web/WebElement.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/HTMLNames.h"
36 #include "core/dom/Element.h"
37 #include "core/dom/Fullscreen.h"
38 #include "core/dom/custom/CustomElementProcessingStack.h"
39 #include "core/html/HTMLTextFormControlElement.h"
40 #include "platform/graphics/Image.h"
41 #include "public/platform/WebRect.h"
42 #include "wtf/PassRefPtr.h"
43 #include "wtf/text/AtomicString.h"
44 #include "wtf/text/WTFString.h"
46 namespace blink {
48 using namespace HTMLNames;
50 bool WebElement::isFormControlElement() const
52 return constUnwrap<Element>()->isFormControlElement();
55 bool WebElement::isTextFormControlElement() const
57 return constUnwrap<Element>()->isTextFormControl();
60 bool WebElement::isEditable() const
62 const Element* element = constUnwrap<Element>();
64 if (element->isContentEditable())
65 return true;
67 if (element->isTextFormControl()) {
68 const HTMLTextFormControlElement* input = toHTMLTextFormControlElement(element);
69 if (!input->isDisabledOrReadOnly())
70 return true;
73 return equalIgnoringCase(element->getAttribute(roleAttr), "textbox");
76 WebString WebElement::tagName() const
78 return constUnwrap<Element>()->tagName();
81 bool WebElement::hasHTMLTagName(const WebString& tagName) const
83 // How to create class nodeName localName
84 // createElement('input') HTMLInputElement INPUT input
85 // createElement('INPUT') HTMLInputElement INPUT input
86 // createElementNS(xhtmlNS, 'input') HTMLInputElement INPUT input
87 // createElementNS(xhtmlNS, 'INPUT') HTMLUnknownElement INPUT INPUT
88 const Element* element = constUnwrap<Element>();
89 return HTMLNames::xhtmlNamespaceURI == element->namespaceURI() && element->localName() == String(tagName).lower();
92 bool WebElement::hasAttribute(const WebString& attrName) const
94 return constUnwrap<Element>()->hasAttribute(attrName);
97 void WebElement::removeAttribute(const WebString& attrName)
99 // TODO: Custom element callbacks need to be called on WebKit API methods that
100 // mutate the DOM in any way.
101 CustomElementProcessingStack::CallbackDeliveryScope deliverCustomElementCallbacks;
102 unwrap<Element>()->removeAttribute(attrName);
105 WebString WebElement::getAttribute(const WebString& attrName) const
107 return constUnwrap<Element>()->getAttribute(attrName);
110 bool WebElement::setAttribute(const WebString& attrName, const WebString& attrValue)
112 // TODO: Custom element callbacks need to be called on WebKit API methods that
113 // mutate the DOM in any way.
114 CustomElementProcessingStack::CallbackDeliveryScope deliverCustomElementCallbacks;
115 TrackExceptionState exceptionState;
116 unwrap<Element>()->setAttribute(attrName, attrValue, exceptionState);
117 return !exceptionState.hadException();
120 unsigned WebElement::attributeCount() const
122 if (!constUnwrap<Element>()->hasAttributes())
123 return 0;
124 return constUnwrap<Element>()->attributes().size();
127 WebString WebElement::attributeLocalName(unsigned index) const
129 if (index >= attributeCount())
130 return WebString();
131 return constUnwrap<Element>()->attributes().at(index).localName();
134 WebString WebElement::attributeValue(unsigned index) const
136 if (index >= attributeCount())
137 return WebString();
138 return constUnwrap<Element>()->attributes().at(index).value();
141 WebString WebElement::textContent() const
143 return constUnwrap<Element>()->textContent();
146 void WebElement::requestFullScreen()
148 Element* element = unwrap<Element>();
149 Fullscreen::from(element->document()).requestFullscreen(*element, Fullscreen::PrefixedRequest);
152 bool WebElement::hasNonEmptyLayoutSize() const
154 return constUnwrap<Element>()->hasNonEmptyLayoutSize();
157 WebRect WebElement::boundsInViewportSpace()
159 return unwrap<Element>()->boundsInViewportSpace();
162 WebImage WebElement::imageContents()
164 if (isNull())
165 return WebImage();
167 Image* image = unwrap<Element>()->imageContents();
168 if (!image)
169 return WebImage();
171 SkBitmap bitmap;
172 if (!image->deprecatedBitmapForCurrentFrame(&bitmap))
173 return WebImage();
175 return WebImage(bitmap);
178 WebElement::WebElement(const PassRefPtrWillBeRawPtr<Element>& elem)
179 : WebNode(elem)
183 WebElement& WebElement::operator=(const PassRefPtrWillBeRawPtr<Element>& elem)
185 m_private = elem;
186 return *this;
189 WebElement::operator PassRefPtrWillBeRawPtr<Element>() const
191 return toElement(m_private.get());
194 } // namespace blink