Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebInputElement.cpp
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"
36 #include "WebElementCollection.h"
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"
41 #include "core/html/shadow/ShadowElementNames.h"
42 #include "core/html/shadow/TextControlInnerElements.h"
43 #include "public/platform/WebString.h"
44 #include "wtf/PassRefPtr.h"
45
46 using namespace WebCore;
47
48 namespace blink {
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
80 bool WebInputElement::autoComplete() const
81 {
82     return constUnwrap<HTMLInputElement>()->shouldAutocomplete();
83 }
84
85 int WebInputElement::maxLength() const
86 {
87     return constUnwrap<HTMLInputElement>()->maxLength();
88 }
89
90 bool WebInputElement::isActivatedSubmit() const
91 {
92     return constUnwrap<HTMLInputElement>()->isActivatedSubmit();
93 }
94
95 void WebInputElement::setActivatedSubmit(bool activated)
96 {
97     unwrap<HTMLInputElement>()->setActivatedSubmit(activated);
98 }
99
100 int WebInputElement::size() const
101 {
102     return constUnwrap<HTMLInputElement>()->size();
103 }
104
105 void WebInputElement::setValue(const WebString& value, bool sendChangeEvent)
106 {
107     unwrap<HTMLInputElement>()->setValue(value, sendChangeEvent ? DispatchChangeEvent : DispatchNoEvent);
108 }
109
110 WebString WebInputElement::value() const
111 {
112     return constUnwrap<HTMLInputElement>()->value();
113 }
114
115 WebString WebInputElement::editingValue() const
116 {
117     return constUnwrap<HTMLInputElement>()->innerTextValue();
118 }
119
120 void WebInputElement::setEditingValue(const WebString& value)
121 {
122     unwrap<HTMLInputElement>()->setEditingValue(value);
123 }
124
125 void WebInputElement::setSuggestedValue(const WebString& value)
126 {
127     unwrap<HTMLInputElement>()->setSuggestedValue(value);
128 }
129
130 WebString WebInputElement::suggestedValue() const
131 {
132     return constUnwrap<HTMLInputElement>()->suggestedValue();
133 }
134
135 void WebInputElement::setSelectionRange(int start, int end)
136 {
137     unwrap<HTMLInputElement>()->setSelectionRange(start, end);
138 }
139
140 int WebInputElement::selectionStart() const
141 {
142     return constUnwrap<HTMLInputElement>()->selectionStart();
143 }
144
145 int WebInputElement::selectionEnd() const
146 {
147     return constUnwrap<HTMLInputElement>()->selectionEnd();
148 }
149
150 bool WebInputElement::isValidValue(const WebString& value) const
151 {
152     return constUnwrap<HTMLInputElement>()->isValidValue(value);
153 }
154
155 void WebInputElement::setChecked(bool nowChecked, bool sendChangeEvent)
156 {
157     unwrap<HTMLInputElement>()->setChecked(nowChecked, sendChangeEvent ? DispatchChangeEvent : DispatchNoEvent);
158 }
159
160 bool WebInputElement::isChecked() const
161 {
162     return constUnwrap<HTMLInputElement>()->checked();
163 }
164
165 bool WebInputElement::isMultiple() const
166 {
167     return constUnwrap<HTMLInputElement>()->multiple();
168 }
169
170 WebElementCollection WebInputElement::dataListOptions() const
171 {
172     if (HTMLDataListElement* dataList = toHTMLDataListElement(constUnwrap<HTMLInputElement>()->list()))
173         return WebElementCollection(dataList->options());
174     return WebElementCollection();
175 }
176
177 WebString WebInputElement::localizeValue(const WebString& proposedValue) const
178 {
179     return constUnwrap<HTMLInputElement>()->localizeValue(proposedValue);
180 }
181
182 bool WebInputElement::isSpeechInputEnabled() const
183 {
184 #if ENABLE(INPUT_SPEECH)
185     return constUnwrap<HTMLInputElement>()->isSpeechEnabled();
186 #else
187     return false;
188 #endif
189 }
190
191 #if ENABLE(INPUT_SPEECH)
192 static inline InputFieldSpeechButtonElement* speechButtonElement(const WebInputElement* webInput)
193 {
194     return toInputFieldSpeechButtonElement(webInput->constUnwrap<HTMLInputElement>()->userAgentShadowRoot()->getElementById(ShadowElementNames::speechButton()));
195 }
196 #endif
197
198 WebInputElement::SpeechInputState WebInputElement::getSpeechInputState() const
199 {
200 #if ENABLE(INPUT_SPEECH)
201     if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this))
202         return static_cast<WebInputElement::SpeechInputState>(speechButton->state());
203 #endif
204
205     return Idle;
206 }
207
208 void WebInputElement::startSpeechInput()
209 {
210 #if ENABLE(INPUT_SPEECH)
211     if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this))
212         speechButton->startSpeechInput();
213 #endif
214 }
215
216 void WebInputElement::stopSpeechInput()
217 {
218 #if ENABLE(INPUT_SPEECH)
219     if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this))
220         speechButton->stopSpeechInput();
221 #endif
222 }
223
224 int WebInputElement::defaultMaxLength()
225 {
226     return HTMLInputElement::maximumLength;
227 }
228
229 WebString WebInputElement::directionForFormData() const
230 {
231     return constUnwrap<HTMLInputElement>()->directionForFormData();
232 }
233
234 // FIXME: Remove this once password_generation_manager.h stops using it.
235 WebElement WebInputElement::decorationElementFor(void*)
236 {
237     return passwordGeneratorButtonElement();
238 }
239
240 WebElement WebInputElement::passwordGeneratorButtonElement() const
241 {
242     return WebElement(constUnwrap<HTMLInputElement>()->passwordGeneratorButtonElement());
243 }
244
245 void WebInputElement::setShouldRevealPassword(bool value)
246 {
247     unwrap<HTMLInputElement>()->setShouldRevealPassword(value);
248 }
249
250 WebInputElement::WebInputElement(const PassRefPtr<HTMLInputElement>& elem)
251     : WebFormControlElement(elem)
252 {
253 }
254
255 WebInputElement& WebInputElement::operator=(const PassRefPtr<HTMLInputElement>& elem)
256 {
257     m_private = elem;
258     return *this;
259 }
260
261 WebInputElement::operator PassRefPtr<HTMLInputElement>() const
262 {
263     return toHTMLInputElement(m_private.get());
264 }
265
266 WebInputElement* toWebInputElement(WebElement* webElement)
267 {
268     if (!webElement->unwrap<Element>()->hasTagName(HTMLNames::inputTag))
269         return 0;
270
271     return static_cast<WebInputElement*>(webElement);
272 }
273 } // namespace blink