Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebFormControlElement.cpp
1 /*
2  * Copyright (C) 2010 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 "public/web/WebFormControlElement.h"
33
34 #include "core/dom/NodeRenderStyle.h"
35 #include "core/html/HTMLFormControlElement.h"
36 #include "core/html/HTMLFormElement.h"
37 #include "core/html/HTMLInputElement.h"
38 #include "core/html/HTMLSelectElement.h"
39 #include "core/html/HTMLTextAreaElement.h"
40
41 #include "wtf/PassRefPtr.h"
42
43 using namespace blink;
44
45 namespace blink {
46
47 bool WebFormControlElement::isEnabled() const
48 {
49     return !constUnwrap<HTMLFormControlElement>()->isDisabledFormControl();
50 }
51
52 bool WebFormControlElement::isReadOnly() const
53 {
54     return constUnwrap<HTMLFormControlElement>()->isReadOnly();
55 }
56
57 WebString WebFormControlElement::formControlName() const
58 {
59     return constUnwrap<HTMLFormControlElement>()->name();
60 }
61
62 WebString WebFormControlElement::formControlType() const
63 {
64     return constUnwrap<HTMLFormControlElement>()->type();
65 }
66
67 void WebFormControlElement::dispatchFormControlChangeEvent()
68 {
69     unwrap<HTMLFormControlElement>()->dispatchFormControlChangeEvent();
70 }
71
72 bool WebFormControlElement::isAutofilled() const
73 {
74     return constUnwrap<HTMLFormControlElement>()->isAutofilled();
75 }
76
77 void WebFormControlElement::setAutofilled(bool autofilled)
78 {
79     unwrap<HTMLFormControlElement>()->setAutofilled(autofilled);
80 }
81
82 WebString WebFormControlElement::nameForAutofill() const
83 {
84     return constUnwrap<HTMLFormControlElement>()->nameForAutofill();
85 }
86
87 bool WebFormControlElement::autoComplete() const
88 {
89     if (isHTMLInputElement(*m_private))
90         return constUnwrap<HTMLInputElement>()->shouldAutocomplete();
91     if (isHTMLTextAreaElement(*m_private))
92         return constUnwrap<HTMLTextAreaElement>()->shouldAutocomplete();
93     return false;
94 }
95
96 void WebFormControlElement::setValue(const WebString& value, bool sendEvents)
97 {
98     if (isHTMLInputElement(*m_private))
99         unwrap<HTMLInputElement>()->setValue(value, sendEvents ? DispatchInputAndChangeEvent : DispatchNoEvent);
100     else if (isHTMLTextAreaElement(*m_private))
101         unwrap<HTMLTextAreaElement>()->setValue(value, sendEvents ? DispatchInputAndChangeEvent : DispatchNoEvent);
102     else if (isHTMLSelectElement(*m_private))
103         unwrap<HTMLSelectElement>()->setValue(value, sendEvents);
104 }
105
106 WebString WebFormControlElement::value() const
107 {
108     if (isHTMLInputElement(*m_private))
109         return constUnwrap<HTMLInputElement>()->value();
110     if (isHTMLTextAreaElement(*m_private))
111         return constUnwrap<HTMLTextAreaElement>()->value();
112     if (isHTMLSelectElement(*m_private))
113         return constUnwrap<HTMLSelectElement>()->value();
114     return WebString();
115 }
116
117 void WebFormControlElement::setSuggestedValue(const WebString& value)
118 {
119     if (isHTMLInputElement(*m_private))
120         unwrap<HTMLInputElement>()->setSuggestedValue(value);
121     else if (isHTMLTextAreaElement(*m_private))
122         unwrap<HTMLTextAreaElement>()->setSuggestedValue(value);
123     else if (isHTMLSelectElement(*m_private))
124         unwrap<HTMLSelectElement>()->setSuggestedValue(value);
125 }
126
127 WebString WebFormControlElement::suggestedValue() const
128 {
129     if (isHTMLInputElement(*m_private))
130         return constUnwrap<HTMLInputElement>()->suggestedValue();
131     if (isHTMLTextAreaElement(*m_private))
132         return constUnwrap<HTMLTextAreaElement>()->suggestedValue();
133     if (isHTMLSelectElement(*m_private))
134         return constUnwrap<HTMLSelectElement>()->suggestedValue();
135     return WebString();
136 }
137
138 WebString WebFormControlElement::editingValue() const
139 {
140     if (isHTMLInputElement(*m_private))
141         return constUnwrap<HTMLInputElement>()->innerEditorValue();
142     if (isHTMLTextAreaElement(*m_private))
143         return constUnwrap<HTMLTextAreaElement>()->innerEditorValue();
144     return WebString();
145 }
146
147 void WebFormControlElement::setSelectionRange(int start, int end)
148 {
149     if (isHTMLInputElement(*m_private))
150         unwrap<HTMLInputElement>()->setSelectionRange(start, end);
151     else if (isHTMLTextAreaElement(*m_private))
152         unwrap<HTMLTextAreaElement>()->setSelectionRange(start, end);
153 }
154
155 int WebFormControlElement::selectionStart() const
156 {
157     if (isHTMLInputElement(*m_private))
158         return constUnwrap<HTMLInputElement>()->selectionStart();
159     if (isHTMLTextAreaElement(*m_private))
160         return constUnwrap<HTMLTextAreaElement>()->selectionStart();
161     return 0;
162 }
163
164 int WebFormControlElement::selectionEnd() const
165 {
166     if (isHTMLInputElement(*m_private))
167         return constUnwrap<HTMLInputElement>()->selectionEnd();
168     if (isHTMLTextAreaElement(*m_private))
169         return constUnwrap<HTMLTextAreaElement>()->selectionEnd();
170     return 0;
171 }
172
173 WebString WebFormControlElement::directionForFormData() const
174 {
175     if (RenderStyle* style = constUnwrap<HTMLFormControlElement>()->renderStyle())
176         return style->isLeftToRightDirection() ? WebString::fromUTF8("ltr") : WebString::fromUTF8("rtl");
177     return WebString::fromUTF8("ltr");
178 }
179
180 bool WebFormControlElement::isActivatedSubmit() const
181 {
182     return constUnwrap<HTMLFormControlElement>()->isActivatedSubmit();
183 }
184
185 WebFormElement WebFormControlElement::form() const
186 {
187     return WebFormElement(constUnwrap<HTMLFormControlElement>()->form());
188 }
189
190 WebFormControlElement::WebFormControlElement(const PassRefPtrWillBeRawPtr<HTMLFormControlElement>& elem)
191     : WebElement(elem)
192 {
193 }
194
195 WebFormControlElement& WebFormControlElement::operator=(const PassRefPtrWillBeRawPtr<HTMLFormControlElement>& elem)
196 {
197     m_private = elem;
198     return *this;
199 }
200
201 WebFormControlElement::operator PassRefPtrWillBeRawPtr<HTMLFormControlElement>() const
202 {
203     return toHTMLFormControlElement(m_private.get());
204 }
205
206 } // namespace blink