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 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#include <wtf/OwnPtr.h>
30
31namespace WebCore {
32
33class DateComponents;
34class FileList;
35class HTMLDataListElement;
36class HTMLImageLoader;
37class HTMLOptionElement;
38class KURL;
39class VisibleSelection;
40
41class HTMLInputElement : public HTMLTextFormControlElement, public InputElement {
42public:
43    enum InputType {
44        TEXT = 0, // TEXT must be 0.
45        PASSWORD,
46        ISINDEX,
47        CHECKBOX,
48        RADIO,
49        SUBMIT,
50        RESET,
51        FILE,
52        HIDDEN,
53        IMAGE,
54        BUTTON,
55        SEARCH,
56        RANGE,
57        EMAIL,
58        NUMBER,
59        TELEPHONE,
60        URL,
61        COLOR,
62        DATE,
63        DATETIME,
64        DATETIMELOCAL,
65        MONTH,
66        TIME,
67        WEEK,
68        // If you add new types or change the order of enum values, update numberOfTypes below.
69    };
70    static const int numberOfTypes = WEEK + 1;
71
72    enum AutoCompleteSetting {
73        Uninitialized,
74        On,
75        Off
76    };
77
78    HTMLInputElement(const QualifiedName&, Document*, HTMLFormElement* = 0);
79    virtual ~HTMLInputElement();
80
81    virtual HTMLTagStatus endTagRequirement() const { return TagStatusForbidden; }
82    virtual int tagPriority() const { return 0; }
83
84    virtual bool isKeyboardFocusable(KeyboardEvent*) const;
85    virtual bool isMouseFocusable() const;
86    virtual bool isEnumeratable() const { return inputType() != IMAGE; }
87    virtual void updateFocusAppearance(bool restorePreviousSelection);
88    virtual void aboutToUnload();
89    virtual bool shouldUseInputMethod() const;
90
91    virtual const AtomicString& formControlName() const;
92
93    bool autoComplete() const;
94
95    // isChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
96    virtual bool isChecked() const { return checked() && (inputType() == CHECKBOX || inputType() == RADIO); }
97    virtual bool isIndeterminate() const { return indeterminate(); }
98
99    bool readOnly() const { return isReadOnlyFormControl(); }
100
101    virtual bool isTextFormControl() const { return isTextField(); }
102
103    virtual bool valueMissing() const;
104    virtual bool patternMismatch() const;
105    virtual bool tooLong() const;
106    // For ValidityState
107    bool rangeUnderflow() const;
108    bool rangeOverflow() const;
109    // Returns the minimum value for type=date, number, or range.  Don't call this for other types.
110    double minimum() const;
111    // Returns the maximum value for type=date, number, or range.  Don't call this for other types.
112    // This always returns a value which is >= minimum().
113    double maximum() const;
114    // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
115    // Returns false if there is no "allowed value step."
116    bool getAllowedValueStep(double*) const;
117    // For ValidityState.
118    bool stepMismatch() const;
119    // Implementations of HTMLInputElement::stepUp() and stepDown().
120    void stepUp(int, ExceptionCode&);
121    void stepDown(int, ExceptionCode&);
122    void stepUp(ExceptionCode& ec) { stepUp(1, ec); }
123    void stepDown(ExceptionCode& ec) { stepDown(1, ec); }
124
125    bool isTextButton() const { return m_type == SUBMIT || m_type == RESET || m_type == BUTTON; }
126    virtual bool isRadioButton() const { return m_type == RADIO; }
127    virtual bool isTextField() const;
128    virtual bool isSearchField() const { return m_type == SEARCH; }
129    virtual bool isInputTypeHidden() const { return m_type == HIDDEN; }
130    virtual bool isPasswordField() const { return m_type == PASSWORD; }
131
132    bool checked() const { return m_checked; }
133    void setChecked(bool, bool sendChangeEvent = false);
134    bool indeterminate() const { return m_indeterminate; }
135    void setIndeterminate(bool);
136    virtual int size() const;
137    virtual const AtomicString& formControlType() const;
138    void setType(const String&);
139
140    virtual const String& suggestedValue() const;
141    void setSuggestedValue(const String&);
142
143    virtual String value() const;
144    virtual void setValue(const String&, bool sendChangeEvent = false);
145    virtual void setValueForUser(const String&);
146
147    double valueAsDate() const;
148    void setValueAsDate(double, ExceptionCode&);
149
150    double valueAsNumber() const;
151    void setValueAsNumber(double, ExceptionCode&);
152
153    virtual String placeholder() const;
154    virtual void setPlaceholder(const String&);
155
156    virtual bool searchEventsShouldBeDispatched() const;
157
158    String valueWithDefault() const;
159
160    virtual void setValueFromRenderer(const String&);
161    void setFileListFromRenderer(const Vector<String>&);
162
163    virtual bool saveFormControlState(String& value) const;
164    virtual void restoreFormControlState(const String&);
165
166    virtual bool canStartSelection() const;
167
168    bool canHaveSelection() const;
169    virtual void select() { HTMLTextFormControlElement::select(); }
170
171    virtual void accessKeyAction(bool sendToAnyElement);
172
173    virtual bool mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const;
174    virtual void parseMappedAttribute(MappedAttribute*);
175
176    virtual void copyNonAttributeProperties(const Element* source);
177
178    virtual void attach();
179    virtual bool rendererIsNeeded(RenderStyle*);
180    virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
181    virtual void detach();
182    virtual bool appendFormData(FormDataList&, bool);
183
184    virtual bool isSuccessfulSubmitButton() const;
185    virtual bool isActivatedSubmit() const;
186    virtual void setActivatedSubmit(bool flag);
187
188    InputType inputType() const { return static_cast<InputType>(m_type); }
189    void setInputType(const String&);
190
191    // Report if this input type uses height & width attributes
192    bool respectHeightAndWidthAttrs() const { return inputType() == IMAGE || inputType() == HIDDEN; }
193
194    virtual void reset();
195
196    virtual void* preDispatchEventHandler(Event*);
197    virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch);
198    virtual void defaultEventHandler(Event*);
199
200    String altText() const;
201
202    virtual bool isURLAttribute(Attribute*) const;
203
204    int maxResults() const { return m_maxResults; }
205
206    String defaultValue() const;
207    void setDefaultValue(const String&);
208
209    bool defaultChecked() const;
210    void setDefaultChecked(bool);
211
212    void setDefaultName(const AtomicString&);
213
214    String accept() const;
215    void setAccept(const String&);
216
217    String accessKey() const;
218    void setAccessKey(const String&);
219
220    String align() const;
221    void setAlign(const String&);
222
223    String alt() const;
224    void setAlt(const String&);
225
226    void setSize(unsigned);
227
228    KURL src() const;
229    void setSrc(const String&);
230
231#if ENABLE(DATALIST)
232    HTMLElement* list() const;
233    HTMLOptionElement* selectedOption() const;
234#endif
235
236    int maxLength() const;
237    void setMaxLength(int, ExceptionCode&);
238
239    bool multiple() const;
240    void setMultiple(bool);
241
242    String useMap() const;
243    void setUseMap(const String&);
244
245    virtual bool isAutofilled() const { return m_autofilled; }
246    void setAutofilled(bool value = true);
247
248    FileList* files();
249
250    virtual void cacheSelection(int start, int end);
251    void addSearchResult();
252    void onSearch();
253
254    virtual String sanitizeValue(const String&) const;
255
256    virtual void documentDidBecomeActive();
257
258    virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;
259
260    virtual bool willValidate() const;
261
262    // Converts the specified string to a floating number.
263    // If the conversion fails, the return value is false. Take care that leading or trailing unnecessary characters make failures.  This returns false for an empty string input.
264    // The double* parameter may be 0.
265    static bool formStringToDouble(const String&, double*);
266    // Converts the specified number to a string. This is an implementation of
267    // HTML5's "algorithm to convert a number to a string" for NUMBER/RANGE types.
268    static String formStringFromDouble(double);
269    // Parses the specified string as the InputType, and returns true if it is successfully parsed.
270    // An instance pointed by the DateComponents* parameter will have parsed values and be
271    // modified even if the parsing fails.  The DateComponents* parameter may be 0.
272    static bool formStringToDateComponents(InputType, const String&, DateComponents*);
273
274protected:
275    virtual void willMoveToNewOwnerDocument();
276    virtual void didMoveToNewOwnerDocument();
277
278private:
279    bool storesValueSeparateFromAttribute() const;
280
281    bool needsActivationCallback();
282    void registerForActivationCallbackIfNeeded();
283    void unregisterForActivationCallbackIfNeeded();
284
285    virtual bool supportsPlaceholder() const { return isTextField(); }
286    virtual bool isEmptyValue() const { return value().isEmpty(); }
287    virtual void handleFocusEvent();
288    virtual void handleBlurEvent();
289    virtual int cachedSelectionStart() const { return m_data.cachedSelectionStart(); }
290    virtual int cachedSelectionEnd() const { return m_data.cachedSelectionEnd(); }
291
292    virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
293    virtual bool isRequiredFormControl() const;
294
295    PassRefPtr<HTMLFormElement> createTemporaryFormForIsIndex();
296    // Helper for getAllowedValueStep();
297    bool getStepParameters(double* defaultStep, double* stepScaleFactor) const;
298    // Helper for stepUp()/stepDown().  Adds step value * count to the current value.
299    void applyStep(double count, ExceptionCode&);
300    // Helper for applyStepForNumberOrRange().
301    double stepBase() const;
302
303    // Parses the specified string for the current type, and return
304    // the double value for the parsing result if the parsing
305    // succeeds; Returns defaultValue otherwise. This function can
306    // return NaN or Infinity only if defaultValue is NaN or Infinity.
307    double parseToDouble(const String&, double defaultValue) const;
308
309    // Generates a suitable string for the specified DateComponents and the
310    // step value, and calls setValue() with it.
311    void setDateValue(const DateComponents&);
312
313
314#if ENABLE(DATALIST)
315    HTMLDataListElement* dataList() const;
316#endif
317
318    InputElementData m_data;
319    int m_xPos;
320    int m_yPos;
321    short m_maxResults;
322    OwnPtr<HTMLImageLoader> m_imageLoader;
323    RefPtr<FileList> m_fileList;
324    unsigned m_type : 5; // InputType
325    bool m_checked : 1;
326    bool m_defaultChecked : 1;
327    bool m_useDefaultChecked : 1;
328    bool m_indeterminate : 1;
329    bool m_haveType : 1;
330    bool m_activeSubmit : 1;
331    unsigned m_autocomplete : 2; // AutoCompleteSetting
332    bool m_autofilled : 1;
333    bool m_inited : 1;
334#if ENABLE(DATALIST)
335    bool m_hasNonEmptyList : 1;
336#endif
337};
338
339} //namespace
340
341#endif
342