Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [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" |
| 32 | #include "WebInputElement.h" |
| 33 | |
| 34 | #include "HTMLNames.h" |
| 35 | #include "RuntimeEnabledFeatures.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 36 | #include "WebElementCollection.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 37 | #include "core/dom/shadow/ElementShadow.h" |
| 38 | #include "core/dom/shadow/ShadowRoot.h" |
| 39 | #include "core/html/HTMLDataListElement.h" |
| 40 | #include "core/html/HTMLInputElement.h" |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 41 | #include "core/html/shadow/ShadowElementNames.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 42 | #include "core/html/shadow/TextControlInnerElements.h" |
| 43 | #include "public/platform/WebString.h" |
| 44 | #include "wtf/PassRefPtr.h" |
| 45 | |
| 46 | using namespace WebCore; |
| 47 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 48 | namespace blink { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 49 | |
| 50 | bool WebInputElement::isTextField() const |
| 51 | { |
| 52 | return constUnwrap<HTMLInputElement>()->isTextField(); |
| 53 | } |
| 54 | |
| 55 | bool WebInputElement::isText() const |
| 56 | { |
| 57 | return constUnwrap<HTMLInputElement>()->isText(); |
| 58 | } |
| 59 | |
| 60 | bool WebInputElement::isPasswordField() const |
| 61 | { |
| 62 | return constUnwrap<HTMLInputElement>()->isPasswordField(); |
| 63 | } |
| 64 | |
| 65 | bool WebInputElement::isImageButton() const |
| 66 | { |
| 67 | return constUnwrap<HTMLInputElement>()->isImageButton(); |
| 68 | } |
| 69 | |
| 70 | bool WebInputElement::isRadioButton() const |
| 71 | { |
| 72 | return constUnwrap<HTMLInputElement>()->isRadioButton(); |
| 73 | } |
| 74 | |
| 75 | bool WebInputElement::isCheckbox() const |
| 76 | { |
| 77 | return constUnwrap<HTMLInputElement>()->isCheckbox(); |
| 78 | } |
| 79 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 80 | int WebInputElement::maxLength() const |
| 81 | { |
| 82 | return constUnwrap<HTMLInputElement>()->maxLength(); |
| 83 | } |
| 84 | |
| 85 | bool WebInputElement::isActivatedSubmit() const |
| 86 | { |
| 87 | return constUnwrap<HTMLInputElement>()->isActivatedSubmit(); |
| 88 | } |
| 89 | |
| 90 | void WebInputElement::setActivatedSubmit(bool activated) |
| 91 | { |
| 92 | unwrap<HTMLInputElement>()->setActivatedSubmit(activated); |
| 93 | } |
| 94 | |
| 95 | int WebInputElement::size() const |
| 96 | { |
| 97 | return constUnwrap<HTMLInputElement>()->size(); |
| 98 | } |
| 99 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 100 | void WebInputElement::setEditingValue(const WebString& value) |
| 101 | { |
| 102 | unwrap<HTMLInputElement>()->setEditingValue(value); |
| 103 | } |
| 104 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 105 | bool WebInputElement::isValidValue(const WebString& value) const |
| 106 | { |
| 107 | return constUnwrap<HTMLInputElement>()->isValidValue(value); |
| 108 | } |
| 109 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 110 | void WebInputElement::setChecked(bool nowChecked, bool sendEvents) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 111 | { |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 112 | unwrap<HTMLInputElement>()->setChecked(nowChecked, sendEvents ? DispatchInputAndChangeEvent : DispatchNoEvent); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool WebInputElement::isChecked() const |
| 116 | { |
| 117 | return constUnwrap<HTMLInputElement>()->checked(); |
| 118 | } |
| 119 | |
| 120 | bool WebInputElement::isMultiple() const |
| 121 | { |
| 122 | return constUnwrap<HTMLInputElement>()->multiple(); |
| 123 | } |
| 124 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 125 | WebElementCollection WebInputElement::dataListOptions() const |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 126 | { |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 127 | if (HTMLDataListElement* dataList = toHTMLDataListElement(constUnwrap<HTMLInputElement>()->list())) |
| 128 | return WebElementCollection(dataList->options()); |
| 129 | return WebElementCollection(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | WebString WebInputElement::localizeValue(const WebString& proposedValue) const |
| 133 | { |
| 134 | return constUnwrap<HTMLInputElement>()->localizeValue(proposedValue); |
| 135 | } |
| 136 | |
| 137 | bool WebInputElement::isSpeechInputEnabled() const |
| 138 | { |
| 139 | #if ENABLE(INPUT_SPEECH) |
| 140 | return constUnwrap<HTMLInputElement>()->isSpeechEnabled(); |
| 141 | #else |
| 142 | return false; |
| 143 | #endif |
| 144 | } |
| 145 | |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 146 | #if ENABLE(INPUT_SPEECH) |
| 147 | static inline InputFieldSpeechButtonElement* speechButtonElement(const WebInputElement* webInput) |
| 148 | { |
| 149 | return toInputFieldSpeechButtonElement(webInput->constUnwrap<HTMLInputElement>()->userAgentShadowRoot()->getElementById(ShadowElementNames::speechButton())); |
| 150 | } |
| 151 | #endif |
| 152 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 153 | WebInputElement::SpeechInputState WebInputElement::getSpeechInputState() const |
| 154 | { |
| 155 | #if ENABLE(INPUT_SPEECH) |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 156 | if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this)) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 157 | return static_cast<WebInputElement::SpeechInputState>(speechButton->state()); |
| 158 | #endif |
| 159 | |
| 160 | return Idle; |
| 161 | } |
| 162 | |
| 163 | void WebInputElement::startSpeechInput() |
| 164 | { |
| 165 | #if ENABLE(INPUT_SPEECH) |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 166 | if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this)) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 167 | speechButton->startSpeechInput(); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | void WebInputElement::stopSpeechInput() |
| 172 | { |
| 173 | #if ENABLE(INPUT_SPEECH) |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 174 | if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this)) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 175 | speechButton->stopSpeechInput(); |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | int WebInputElement::defaultMaxLength() |
| 180 | { |
| 181 | return HTMLInputElement::maximumLength; |
| 182 | } |
| 183 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 184 | // FIXME: Remove this once password_generation_manager.h stops using it. |
| 185 | WebElement WebInputElement::decorationElementFor(void*) |
| 186 | { |
| 187 | return passwordGeneratorButtonElement(); |
| 188 | } |
| 189 | |
| 190 | WebElement WebInputElement::passwordGeneratorButtonElement() const |
| 191 | { |
| 192 | return WebElement(constUnwrap<HTMLInputElement>()->passwordGeneratorButtonElement()); |
| 193 | } |
| 194 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 195 | void WebInputElement::setShouldRevealPassword(bool value) |
| 196 | { |
| 197 | unwrap<HTMLInputElement>()->setShouldRevealPassword(value); |
| 198 | } |
| 199 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 200 | WebInputElement::WebInputElement(const PassRefPtr<HTMLInputElement>& elem) |
| 201 | : WebFormControlElement(elem) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | WebInputElement& WebInputElement::operator=(const PassRefPtr<HTMLInputElement>& elem) |
| 206 | { |
| 207 | m_private = elem; |
| 208 | return *this; |
| 209 | } |
| 210 | |
| 211 | WebInputElement::operator PassRefPtr<HTMLInputElement>() const |
| 212 | { |
| 213 | return toHTMLInputElement(m_private.get()); |
| 214 | } |
| 215 | |
| 216 | WebInputElement* toWebInputElement(WebElement* webElement) |
| 217 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 218 | if (!isHTMLInputElement(*webElement->unwrap<Element>())) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 219 | return 0; |
| 220 | |
| 221 | return static_cast<WebInputElement*>(webElement); |
| 222 | } |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 223 | } // namespace blink |