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