3549a860823d0add5932e17e1bda724a7ee84a2a
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLInputElement.h
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  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #ifndef HTMLInputElement_h
26 #define HTMLInputElement_h
27
28 #include "core/html/HTMLTextFormControlElement.h"
29 #include "core/html/forms/StepRange.h"
30 #include "platform/FileChooser.h"
31
32 namespace WebCore {
33
34 class CheckedRadioButtons;
35 class DragData;
36 class ExceptionState;
37 class FileList;
38 class HTMLDataListElement;
39 class HTMLImageLoader;
40 class HTMLOptionElement;
41 class InputType;
42 class InputTypeView;
43 class KURL;
44 class ListAttributeTargetObserver;
45 struct DateTimeChooserParameters;
46
47 class HTMLInputElement : public HTMLTextFormControlElement {
48 public:
49     static PassRefPtr<HTMLInputElement> create(Document&, HTMLFormElement*, bool createdByParser);
50     virtual ~HTMLInputElement();
51
52     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitspeechchange);
53
54     virtual bool shouldAutocomplete() const OVERRIDE FINAL;
55
56     // For ValidityState
57     virtual bool hasBadInput() const OVERRIDE FINAL;
58     virtual bool patternMismatch() const OVERRIDE FINAL;
59     virtual bool rangeUnderflow() const OVERRIDE FINAL;
60     virtual bool rangeOverflow() const OVERRIDE FINAL;
61     virtual bool stepMismatch() const OVERRIDE FINAL;
62     virtual bool tooLong() const OVERRIDE FINAL;
63     virtual bool typeMismatch() const OVERRIDE FINAL;
64     virtual bool valueMissing() const OVERRIDE FINAL;
65     virtual String validationMessage() const OVERRIDE FINAL;
66
67     // Returns the minimum value for type=date, number, or range.  Don't call this for other types.
68     double minimum() const;
69     // Returns the maximum value for type=date, number, or range.  Don't call this for other types.
70     // This always returns a value which is >= minimum().
71     double maximum() const;
72     // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
73     // Returns false if there is no "allowed value step."
74     bool getAllowedValueStep(Decimal*) const;
75     StepRange createStepRange(AnyStepHandling) const;
76
77     Decimal findClosestTickMarkValue(const Decimal&);
78
79     // Implementations of HTMLInputElement::stepUp() and stepDown().
80     void stepUp(int, ExceptionState&);
81     void stepDown(int, ExceptionState&);
82     void stepUp(ExceptionState& exceptionState) { stepUp(1, exceptionState); }
83     void stepDown(ExceptionState& exceptionState) { stepDown(1, exceptionState); }
84     // stepUp()/stepDown() for user-interaction.
85     bool isSteppable() const;
86
87     bool isTextButton() const;
88
89     bool isRadioButton() const;
90     bool isTextField() const;
91     bool isSearchField() const;
92     bool isInputTypeHidden() const;
93     bool isPasswordField() const;
94     bool isCheckbox() const;
95     bool isRangeControl() const;
96
97     // FIXME: It's highly likely that any call site calling this function should instead
98     // be using a different one. Many input elements behave like text fields, and in addition
99     // any unknown input type is treated as text. Consider, for example, isTextField or
100     // isTextField && !isPasswordField.
101     bool isText() const;
102
103     bool isEmailField() const;
104     bool isFileUpload() const;
105     bool isImageButton() const;
106     bool isNumberField() const;
107     bool isTelephoneField() const;
108     bool isURLField() const;
109     bool isDateField() const;
110     bool isDateTimeLocalField() const;
111     bool isMonthField() const;
112     bool isTimeField() const;
113     bool isWeekField() const;
114
115 #if ENABLE(INPUT_SPEECH)
116     bool isSpeechEnabled() const;
117 #endif
118
119     HTMLElement* passwordGeneratorButtonElement() const;
120
121     bool checked() const { return m_isChecked; }
122     void setChecked(bool, TextFieldEventBehavior = DispatchNoEvent);
123
124     // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
125     bool indeterminate() const { return m_isIndeterminate; }
126     void setIndeterminate(bool);
127     // shouldAppearChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
128     bool shouldAppearChecked() const;
129     virtual bool shouldAppearIndeterminate() const OVERRIDE;
130
131     int size() const;
132     bool sizeShouldIncludeDecoration(int& preferredSize) const;
133
134     void setType(const AtomicString&);
135
136     virtual String value() const OVERRIDE;
137     void setValue(const String&, ExceptionState&, TextFieldEventBehavior = DispatchNoEvent);
138     void setValue(const String&, TextFieldEventBehavior = DispatchNoEvent);
139     void setValueForUser(const String&);
140     // Checks if the specified string would be a valid value.
141     // We should not call this for types with no string value such as CHECKBOX and RADIO.
142     bool isValidValue(const String&) const;
143     bool hasDirtyValue() const { return !m_valueIfDirty.isNull(); };
144
145     String sanitizeValue(const String&) const;
146
147     String localizeValue(const String&) const;
148
149     const String& suggestedValue() const;
150     void setSuggestedValue(const String&);
151
152     void setEditingValue(const String&);
153
154     double valueAsDate() const;
155     void setValueAsDate(double, ExceptionState&);
156
157     double valueAsNumber() const;
158     void setValueAsNumber(double, ExceptionState&, TextFieldEventBehavior = DispatchNoEvent);
159
160     String valueWithDefault() const;
161
162     void setValueFromRenderer(const String&);
163
164     int selectionStartForBinding(ExceptionState&) const;
165     int selectionEndForBinding(ExceptionState&) const;
166     String selectionDirectionForBinding(ExceptionState&) const;
167     void setSelectionStartForBinding(int, ExceptionState&);
168     void setSelectionEndForBinding(int, ExceptionState&);
169     void setSelectionDirectionForBinding(const String&, ExceptionState&);
170     void setSelectionRangeForBinding(int start, int end, ExceptionState&);
171     void setSelectionRangeForBinding(int start, int end, const String& direction, ExceptionState&);
172
173     virtual bool rendererIsNeeded(const RenderStyle&) OVERRIDE FINAL;
174     virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
175     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE FINAL;
176
177     // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
178     // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
179     // the private virtual method.
180     virtual bool isActivatedSubmit() const OVERRIDE FINAL;
181     virtual void setActivatedSubmit(bool flag) OVERRIDE FINAL;
182
183     String altText() const;
184
185     int maxResults() const { return m_maxResults; }
186
187     const AtomicString& defaultValue() const;
188     void setDefaultValue(const AtomicString&);
189
190     Vector<String> acceptMIMETypes();
191     Vector<String> acceptFileExtensions();
192     const AtomicString& alt() const;
193
194     void setSize(unsigned);
195     void setSize(unsigned, ExceptionState&);
196
197     KURL src() const;
198
199     int maxLength() const;
200     void setMaxLength(int, ExceptionState&);
201
202     bool multiple() const;
203
204     FileList* files();
205     void setFiles(PassRefPtr<FileList>);
206
207     // Returns true if the given DragData has more than one dropped files.
208     bool receiveDroppedFiles(const DragData*);
209
210     String droppedFileSystemId();
211
212     // These functions are used for rendering the input active during a
213     // drag-and-drop operation.
214     bool canReceiveDroppedFiles() const;
215     void setCanReceiveDroppedFiles(bool);
216
217     void onSearch();
218
219     void updateClearButtonVisibility();
220
221     virtual bool willRespondToMouseClickEvents() OVERRIDE;
222
223     HTMLElement* list() const;
224     HTMLDataListElement* dataList() const;
225     bool hasValidDataListOptions() const;
226     void listAttributeTargetChanged();
227
228     HTMLInputElement* checkedRadioButtonForGroup() const;
229     bool isInRequiredRadioButtonGroup();
230
231     // Functions for InputType classes.
232     void setValueInternal(const String&, TextFieldEventBehavior);
233     bool valueAttributeWasUpdatedAfterParsing() const { return m_valueAttributeWasUpdatedAfterParsing; }
234
235     void cacheSelectionInResponseToSetValue(int caretOffset) { cacheSelection(caretOffset, caretOffset, SelectionHasNoDirection); }
236
237     // For test purposes.
238     void selectColorInColorChooser(const Color&);
239
240     String defaultToolTip() const;
241
242 #if ENABLE(MEDIA_CAPTURE)
243     bool capture() const;
244 #endif
245
246     static const int maximumLength;
247
248     unsigned height() const;
249     unsigned width() const;
250     void setHeight(unsigned);
251     void setWidth(unsigned);
252
253     virtual void blur() OVERRIDE FINAL;
254     void defaultBlur();
255
256     virtual const AtomicString& name() const OVERRIDE FINAL;
257
258     void beginEditing();
259     void endEditing();
260
261     static Vector<FileChooserFileInfo> filesFromFileInputFormControlState(const FormControlState&);
262
263     virtual bool matchesReadOnlyPseudoClass() const OVERRIDE FINAL;
264     virtual bool matchesReadWritePseudoClass() const OVERRIDE FINAL;
265     virtual void setRangeText(const String& replacement, ExceptionState&) OVERRIDE FINAL;
266     virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionState&) OVERRIDE FINAL;
267
268     bool hasImageLoader() const { return m_imageLoader; }
269     HTMLImageLoader* imageLoader();
270
271     bool setupDateTimeChooserParameters(DateTimeChooserParameters&);
272
273     bool supportsInputModeAttribute() const;
274
275 protected:
276     HTMLInputElement(Document&, HTMLFormElement*, bool createdByParser);
277
278     virtual void defaultEventHandler(Event*) OVERRIDE;
279
280 private:
281     enum AutoCompleteSetting { Uninitialized, On, Off };
282
283     virtual void didAddUserAgentShadowRoot(ShadowRoot&) OVERRIDE FINAL;
284     virtual void didAddShadowRoot(ShadowRoot&) OVERRIDE FINAL;
285
286     virtual void willChangeForm() OVERRIDE FINAL;
287     virtual void didChangeForm() OVERRIDE FINAL;
288     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
289     virtual void removedFrom(ContainerNode*) OVERRIDE FINAL;
290     virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE FINAL;
291
292     virtual bool hasCustomFocusLogic() const OVERRIDE FINAL;
293     virtual bool isKeyboardFocusable() const OVERRIDE FINAL;
294     virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE FINAL;
295     virtual bool isEnumeratable() const OVERRIDE FINAL;
296     virtual bool isInteractiveContent() const OVERRIDE FINAL;
297     virtual bool supportLabels() const OVERRIDE FINAL;
298     virtual void updateFocusAppearance(bool restorePreviousSelection) OVERRIDE FINAL;
299     virtual bool shouldUseInputMethod() OVERRIDE FINAL;
300
301     virtual bool isTextFormControl() const OVERRIDE FINAL { return isTextField(); }
302
303     virtual bool canTriggerImplicitSubmission() const OVERRIDE FINAL { return isTextField(); }
304
305     virtual const AtomicString& formControlType() const OVERRIDE FINAL;
306
307     virtual bool shouldSaveAndRestoreFormControlState() const OVERRIDE FINAL;
308     virtual FormControlState saveFormControlState() const OVERRIDE FINAL;
309     virtual void restoreFormControlState(const FormControlState&) OVERRIDE FINAL;
310
311     virtual bool canStartSelection() const OVERRIDE FINAL;
312
313     virtual void accessKeyAction(bool sendMouseEvents) OVERRIDE FINAL;
314
315     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
316     virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE FINAL;
317     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) OVERRIDE FINAL;
318     virtual void finishParsingChildren() OVERRIDE FINAL;
319
320     virtual void copyNonAttributePropertiesFromElement(const Element&) OVERRIDE FINAL;
321
322     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE FINAL;
323
324     virtual bool appendFormData(FormDataList&, bool) OVERRIDE FINAL;
325     virtual String resultForDialogSubmit() OVERRIDE FINAL;
326
327     virtual bool canBeSuccessfulSubmitButton() const OVERRIDE FINAL;
328
329     virtual void resetImpl() OVERRIDE FINAL;
330     virtual bool supportsAutofocus() const OVERRIDE FINAL;
331
332     virtual void* preDispatchEventHandler(Event*) OVERRIDE FINAL;
333     virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch) OVERRIDE FINAL;
334
335     virtual bool isURLAttribute(const Attribute&) const OVERRIDE FINAL;
336     virtual bool isInRange() const OVERRIDE FINAL;
337     virtual bool isOutOfRange() const OVERRIDE FINAL;
338
339     bool isTextType() const;
340     bool tooLong(const String&, NeedsToCheckDirtyFlag) const;
341
342     virtual bool supportsPlaceholder() const OVERRIDE FINAL;
343     virtual void updatePlaceholderText() OVERRIDE FINAL;
344     virtual bool isEmptyValue() const OVERRIDE FINAL { return innerTextValue().isEmpty(); }
345     virtual bool isEmptySuggestedValue() const OVERRIDE FINAL { return suggestedValue().isEmpty(); }
346     virtual void handleFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE FINAL;
347     virtual void handleBlurEvent() OVERRIDE FINAL;
348
349     virtual bool isOptionalFormControl() const OVERRIDE FINAL { return !isRequiredFormControl(); }
350     virtual bool isRequiredFormControl() const OVERRIDE FINAL;
351     virtual bool recalcWillValidate() const OVERRIDE FINAL;
352     virtual void requiredAttributeChanged() OVERRIDE FINAL;
353
354     void updateType();
355
356     virtual void subtreeHasChanged() OVERRIDE FINAL;
357
358     void resetListAttributeTargetObserver();
359     void parseMaxLengthAttribute(const AtomicString&);
360     void updateValueIfNeeded();
361
362     // Returns null if this isn't associated with any radio button group.
363     CheckedRadioButtons* checkedRadioButtons() const;
364     void addToRadioButtonGroup();
365     void removeFromRadioButtonGroup();
366 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
367     virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE;
368 #endif
369
370     AtomicString m_name;
371     String m_valueIfDirty;
372     String m_suggestedValue;
373     int m_size;
374     int m_maxLength;
375     short m_maxResults;
376     bool m_isChecked : 1;
377     bool m_reflectsCheckedAttribute : 1;
378     bool m_isIndeterminate : 1;
379     bool m_hasType : 1;
380     bool m_isActivatedSubmit : 1;
381     unsigned m_autocomplete : 2; // AutoCompleteSetting
382     bool m_hasNonEmptyList : 1;
383     bool m_stateRestored : 1;
384     bool m_parsingInProgress : 1;
385     bool m_valueAttributeWasUpdatedAfterParsing : 1;
386     bool m_canReceiveDroppedFiles : 1;
387     bool m_hasTouchEventHandler : 1;
388     RefPtr<InputType> m_inputType;
389     RefPtr<InputTypeView> m_inputTypeView;
390     // The ImageLoader must be owned by this element because the loader code assumes
391     // that it lives as long as its owning element lives. If we move the loader into
392     // the ImageInput object we may delete the loader while this element lives on.
393     OwnPtr<HTMLImageLoader> m_imageLoader;
394     OwnPtr<ListAttributeTargetObserver> m_listAttributeTargetObserver;
395 };
396
397 DEFINE_NODE_TYPE_CASTS(HTMLInputElement, hasTagName(HTMLNames::inputTag));
398
399 } //namespace
400 #endif