Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 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" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 32 | #include "WebDocument.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 33 | #include "WebElement.h" |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 34 | #include "bindings/v8/ExceptionState.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 35 | #include "core/dom/Element.h" |
| 36 | #include "core/dom/NamedNodeMap.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 37 | #include "core/dom/custom/CustomElementCallbackDispatcher.h" |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 38 | #include "core/dom/shadow/ShadowRoot.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 39 | #include "core/rendering/RenderBoxModelObject.h" |
| 40 | #include "core/rendering/RenderObject.h" |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 41 | #include "public/platform/WebRect.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 42 | #include "wtf/PassRefPtr.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | using namespace WebCore; |
| 46 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 47 | namespace blink { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 48 | |
| 49 | bool WebElement::isFormControlElement() const |
| 50 | { |
| 51 | return constUnwrap<Element>()->isFormControlElement(); |
| 52 | } |
| 53 | |
| 54 | bool WebElement::isTextFormControlElement() const |
| 55 | { |
| 56 | return constUnwrap<Element>()->isTextFormControl(); |
| 57 | } |
| 58 | |
| 59 | WebString WebElement::tagName() const |
| 60 | { |
| 61 | return constUnwrap<Element>()->tagName(); |
| 62 | } |
| 63 | |
| 64 | bool WebElement::hasTagName(const WebString& tagName) const |
| 65 | { |
| 66 | return equalIgnoringCase(constUnwrap<Element>()->tagName(), |
| 67 | tagName.operator String()); |
| 68 | } |
| 69 | |
| 70 | 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 | |
| 81 | bool WebElement::hasAttribute(const WebString& attrName) const |
| 82 | { |
| 83 | return constUnwrap<Element>()->hasAttribute(attrName); |
| 84 | } |
| 85 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 86 | void WebElement::removeAttribute(const WebString& attrName) |
| 87 | { |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 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; |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 91 | unwrap<Element>()->removeAttribute(attrName); |
| 92 | } |
| 93 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 94 | WebString WebElement::getAttribute(const WebString& attrName) const |
| 95 | { |
| 96 | return constUnwrap<Element>()->getAttribute(attrName); |
| 97 | } |
| 98 | |
| 99 | bool WebElement::setAttribute(const WebString& attrName, const WebString& attrValue) |
| 100 | { |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 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; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 104 | TrackExceptionState exceptionState; |
| 105 | unwrap<Element>()->setAttribute(attrName, attrValue, exceptionState); |
| 106 | return !exceptionState.hadException(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | unsigned WebElement::attributeCount() const |
| 110 | { |
| 111 | if (!constUnwrap<Element>()->hasAttributes()) |
| 112 | return 0; |
| 113 | return constUnwrap<Element>()->attributeCount(); |
| 114 | } |
| 115 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 116 | WebNode WebElement::shadowRoot() const |
| 117 | { |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 118 | ShadowRoot* shadowRoot = constUnwrap<Element>()->shadowRoot(); |
| 119 | if (!shadowRoot) |
| 120 | return WebNode(); |
| 121 | return WebNode(shadowRoot->toNode()); |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 124 | WebString WebElement::attributeLocalName(unsigned index) const |
| 125 | { |
| 126 | if (index >= attributeCount()) |
| 127 | return WebString(); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 128 | return constUnwrap<Element>()->attributeItem(index).localName(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | WebString WebElement::attributeValue(unsigned index) const |
| 132 | { |
| 133 | if (index >= attributeCount()) |
| 134 | return WebString(); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 135 | return constUnwrap<Element>()->attributeItem(index).value(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | WebString WebElement::innerText() |
| 139 | { |
| 140 | return unwrap<Element>()->innerText(); |
| 141 | } |
| 142 | |
| 143 | WebString WebElement::computeInheritedLanguage() const |
| 144 | { |
| 145 | return WebString(constUnwrap<Element>()->computeInheritedLanguage()); |
| 146 | } |
| 147 | |
| 148 | void WebElement::requestFullScreen() |
| 149 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 150 | unwrap<Element>()->webkitRequestFullScreen(Element::ALLOW_KEYBOARD_INPUT); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 153 | WebRect WebElement::boundsInViewportSpace() |
| 154 | { |
| 155 | return unwrap<Element>()->boundsInRootViewSpace(); |
| 156 | } |
| 157 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 174 | WebElement::WebElement(const PassRefPtr<Element>& elem) |
| 175 | : WebNode(elem) |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | WebElement& WebElement::operator=(const PassRefPtr<Element>& elem) |
| 180 | { |
| 181 | m_private = elem; |
| 182 | return *this; |
| 183 | } |
| 184 | |
| 185 | WebElement::operator PassRefPtr<Element>() const |
| 186 | { |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 187 | return toElement(m_private.get()); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 190 | } // namespace blink |