1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef HTMLInputElement_h
25#define HTMLInputElement_h
26
27#include "HTMLFormControlElement.h"
28#include "InputElement.h"
29
30namespace WebCore {
31
32class FileList;
33class HTMLDataListElement;
34class HTMLOptionElement;
35class InputType;
36class KURL;
37
38class HTMLInputElement : public HTMLTextFormControlElement, public InputElement {
39public:
40    static PassRefPtr<HTMLInputElement> create(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
41    virtual ~HTMLInputElement();
42
43    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitspeechchange);
44
45    virtual InputElement* toInputElement() { return this; }
46
47    bool autoComplete() const;
48
49    // For ValidityState
50    bool typeMismatch() const;
51    // valueMissing() ignores the specified string value for CHECKBOX and RADIO.
52    bool valueMissing(const String&) const;
53    bool patternMismatch(const String&) const;
54    bool tooLong(const String&, NeedsToCheckDirtyFlag) const;
55    bool rangeUnderflow(const String&) const;
56    bool rangeOverflow(const String&) const;
57    // Returns the minimum value for type=date, number, or range.  Don't call this for other types.
58    double minimum() const;
59    // Returns the maximum value for type=date, number, or range.  Don't call this for other types.
60    // This always returns a value which is >= minimum().
61    double maximum() const;
62    // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
63    // Returns false if there is no "allowed value step."
64    bool getAllowedValueStep(double*) const;
65    bool getAllowedValueStepWithDecimalPlaces(double*, unsigned*) const;
66    // For ValidityState.
67    bool stepMismatch(const String&) const;
68    String minimumString() const;
69    String maximumString() const;
70    String stepBaseString() const;
71    String stepString() const;
72    String typeMismatchText() const;
73    String valueMissingText() const;
74
75    // Implementations of HTMLInputElement::stepUp() and stepDown().
76    void stepUp(int, ExceptionCode&);
77    void stepDown(int, ExceptionCode&);
78    void stepUp(ExceptionCode& ec) { stepUp(1, ec); }
79    void stepDown(ExceptionCode& ec) { stepDown(1, ec); }
80    // stepUp()/stepDown() for user-interaction.
81    void stepUpFromRenderer(int);
82
83    bool isTextButton() const;
84
85    virtual bool isRadioButton() const;
86    virtual bool isTextField() const;
87    virtual bool isSearchField() const;
88    virtual bool isInputTypeHidden() const;
89    virtual bool isPasswordField() const;
90    virtual bool isCheckbox() const;
91
92    // FIXME: It's highly likely that any call site calling this function should instead
93    // be using a different one. Many input elements behave like text fields, and in addition
94    // any unknown input type is treated as text. Consider, for example, isTextField or
95    // isTextField && !isPasswordField.
96    bool isText() const;
97
98    bool isEmailField() const;
99    bool isFileUpload() const;
100    bool isImageButton() const;
101    bool isNumberField() const;
102    bool isSubmitButton() const;
103    bool isTelephoneField() const;
104    bool isURLField() const;
105
106#if ENABLE(INPUT_SPEECH)
107    virtual bool isSpeechEnabled() const;
108#endif
109
110    bool checked() const { return m_isChecked; }
111    void setChecked(bool, bool sendChangeEvent = false);
112
113    // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
114    bool indeterminate() const { return m_isIndeterminate; }
115    void setIndeterminate(bool);
116
117    virtual int size() const;
118
119    void setType(const String&);
120
121    virtual String value() const;
122    virtual void setValue(const String&, bool sendChangeEvent = false);
123    virtual void setValueForUser(const String&);
124    // Checks if the specified string would be a valid value.
125    // We should not call this for types with no string value such as CHECKBOX and RADIO.
126    bool isValidValue(const String&) const;
127
128    virtual const String& suggestedValue() const;
129    void setSuggestedValue(const String&);
130
131    double valueAsDate() const;
132    void setValueAsDate(double, ExceptionCode&);
133
134    double valueAsNumber() const;
135    void setValueAsNumber(double, ExceptionCode&);
136
137    virtual String placeholder() const;
138    virtual void setPlaceholder(const String&);
139
140    String valueWithDefault() const;
141
142    virtual void setValueFromRenderer(const String&);
143    void setFileListFromRenderer(const Vector<String>&);
144
145    bool canHaveSelection() const;
146    virtual void select() { HTMLTextFormControlElement::select(); }
147
148    virtual bool rendererIsNeeded(RenderStyle*);
149    virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
150    virtual void detach();
151
152    // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
153    // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
154    // the private virtual method.
155    virtual bool isActivatedSubmit() const;
156    virtual void setActivatedSubmit(bool flag);
157
158    String altText() const;
159
160    int maxResults() const { return m_maxResults; }
161
162    String defaultValue() const;
163    void setDefaultValue(const String&);
164
165    void setDefaultName(const AtomicString&);
166
167    String accept() const;
168    String alt() const;
169
170    void setSize(unsigned);
171
172    KURL src() const;
173
174    virtual int maxLength() const;
175    void setMaxLength(int, ExceptionCode&);
176
177    bool multiple() const;
178
179    virtual bool isAutofilled() const { return m_isAutofilled; }
180    void setAutofilled(bool = true);
181
182    FileList* files();
183
184    void addSearchResult();
185    void onSearch();
186
187#if ENABLE(DATALIST)
188    HTMLElement* list() const;
189    HTMLOptionElement* selectedOption() const;
190#endif
191
192#if ENABLE(WCSS)
193    void setWapInputFormat(String& mask);
194#endif
195
196    // These functions are public so they can be used in InputType classes.
197    // Otherwise, they would be private.
198    CheckedRadioButtons& checkedRadioButtons() const;
199    void handleBeforeTextInsertedEvent(Event*);
200    void updateCheckedRadioButtons();
201
202    bool lastChangeWasUserEdit() const;
203
204#if PLATFORM(ANDROID) && ENABLE(MEDIA_CAPTURE)
205    String capture() const;
206#endif
207
208protected:
209    HTMLInputElement(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
210
211    virtual void defaultEventHandler(Event*);
212
213private:
214    enum AutoCompleteSetting { Uninitialized, On, Off };
215
216    virtual void willMoveToNewOwnerDocument();
217    virtual void didMoveToNewOwnerDocument();
218
219    virtual bool isKeyboardFocusable(KeyboardEvent*) const;
220    virtual bool isMouseFocusable() const;
221    virtual bool isEnumeratable() const;
222    virtual void updateFocusAppearance(bool restorePreviousSelection);
223    virtual void aboutToUnload();
224    virtual bool shouldUseInputMethod() const;
225
226    virtual const AtomicString& formControlName() const;
227
228    // isChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
229    virtual bool isChecked() const;
230    virtual bool isIndeterminate() const { return indeterminate(); }
231
232    virtual bool isTextFormControl() const { return isTextField(); }
233
234    virtual bool hasSpinButton() const;
235    virtual bool canTriggerImplicitSubmission() const { return isTextField(); }
236
237    virtual const AtomicString& formControlType() const;
238
239    virtual bool searchEventsShouldBeDispatched() const;
240
241    virtual bool saveFormControlState(String& value) const;
242    virtual void restoreFormControlState(const String&);
243
244    virtual bool canStartSelection() const;
245
246    virtual void accessKeyAction(bool sendToAnyElement);
247
248    virtual bool mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const;
249    virtual void parseMappedAttribute(Attribute*);
250    virtual void finishParsingChildren();
251
252    virtual void copyNonAttributeProperties(const Element* source);
253
254    virtual void attach();
255
256    virtual bool appendFormData(FormDataList&, bool);
257
258    virtual bool isSuccessfulSubmitButton() const;
259
260    virtual void reset();
261
262    virtual void* preDispatchEventHandler(Event*);
263    virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch);
264
265    virtual bool isURLAttribute(Attribute*) const;
266
267    virtual void cacheSelection(int start, int end);
268
269    virtual String visibleValue() const;
270    virtual String convertFromVisibleValue(const String&) const;
271    virtual bool isAcceptableValue(const String&) const;
272    virtual String sanitizeValue(const String&) const;
273    virtual bool hasUnacceptableValue() const;
274
275    virtual bool isInRange() const;
276    virtual bool isOutOfRange() const;
277
278    virtual void documentDidBecomeActive();
279
280    virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;
281
282    bool needsActivationCallback();
283    void registerForActivationCallbackIfNeeded();
284    void unregisterForActivationCallbackIfNeeded();
285
286    virtual bool supportsMaxLength() const { return isTextType(); }
287    bool isTextType() const;
288
289    virtual bool supportsPlaceholder() const;
290    virtual bool isEmptyValue() const { return value().isEmpty(); }
291    virtual bool isEmptySuggestedValue() const { return suggestedValue().isEmpty(); }
292    virtual void handleFocusEvent();
293    virtual void handleBlurEvent();
294    virtual int cachedSelectionStart() const { return m_data.cachedSelectionStart(); }
295    virtual int cachedSelectionEnd() const { return m_data.cachedSelectionEnd(); }
296
297    virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
298    virtual bool isRequiredFormControl() const;
299    virtual bool recalcWillValidate() const;
300
301    void updateType();
302
303    // Helper for stepUp()/stepDown().  Adds step value * count to the current value.
304    void applyStep(double count, ExceptionCode&);
305
306#if ENABLE(DATALIST)
307    HTMLDataListElement* dataList() const;
308#endif
309
310#if ENABLE(WCSS)
311    virtual InputElementData data() const { return m_data; }
312#endif
313
314    InputElementData m_data;
315    short m_maxResults;
316    bool m_isChecked : 1;
317    bool m_reflectsCheckedAttribute : 1;
318    bool m_isIndeterminate : 1;
319    bool m_hasType : 1;
320    bool m_isActivatedSubmit : 1;
321    unsigned m_autocomplete : 2; // AutoCompleteSetting
322    bool m_isAutofilled : 1;
323#if ENABLE(DATALIST)
324    bool m_hasNonEmptyList : 1;
325#endif
326    bool m_stateRestored : 1;
327    bool m_parsingInProgress : 1;
328    OwnPtr<InputType> m_inputType;
329};
330
331} //namespace
332
333#endif
334