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 "FileChooser.h"
29#include "HTMLTextFormControlElement.h"
30#include "StepRange.h"
31
32namespace WebCore {
33
34class CheckedRadioButtons;
35class DragData;
36class FileList;
37class HTMLDataListElement;
38class HTMLImageLoader;
39class HTMLOptionElement;
40class Icon;
41class InputType;
42class KURL;
43class ListAttributeTargetObserver;
44struct DateTimeChooserParameters;
45
46class HTMLInputElement : public HTMLTextFormControlElement {
47public:
48    static PassRefPtr<HTMLInputElement> create(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
49    virtual ~HTMLInputElement();
50
51    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitspeechchange);
52
53    virtual HTMLInputElement* toInputElement() { return this; }
54
55    virtual bool shouldAutocomplete() const;
56
57    // For ValidityState
58    virtual bool hasBadInput() const OVERRIDE;
59    virtual bool patternMismatch() const OVERRIDE;
60    virtual bool rangeUnderflow() const OVERRIDE;
61    virtual bool rangeOverflow() const;
62    virtual bool stepMismatch() const OVERRIDE;
63    virtual bool tooLong() const OVERRIDE;
64    virtual bool typeMismatch() const OVERRIDE;
65    virtual bool valueMissing() const OVERRIDE;
66    virtual String validationMessage() const OVERRIDE;
67
68    // Returns the minimum value for type=date, number, or range.  Don't call this for other types.
69    double minimum() const;
70    // Returns the maximum value for type=date, number, or range.  Don't call this for other types.
71    // This always returns a value which is >= minimum().
72    double maximum() const;
73    // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
74    // Returns false if there is no "allowed value step."
75    bool getAllowedValueStep(Decimal*) const;
76    StepRange createStepRange(AnyStepHandling) const;
77
78#if ENABLE(DATALIST_ELEMENT)
79    Decimal findClosestTickMarkValue(const Decimal&);
80#endif
81
82    // Implementations of HTMLInputElement::stepUp() and stepDown().
83    void stepUp(int, ExceptionCode&);
84    void stepDown(int, ExceptionCode&);
85    void stepUp(ExceptionCode& ec) { stepUp(1, ec); }
86    void stepDown(ExceptionCode& ec) { stepDown(1, ec); }
87    // stepUp()/stepDown() for user-interaction.
88    bool isSteppable() const;
89
90    bool isTextButton() const;
91
92    bool isRadioButton() const;
93    bool isTextField() const;
94    bool isSearchField() const;
95    bool isInputTypeHidden() const;
96    bool isPasswordField() const;
97    bool isCheckbox() const;
98    bool isRangeControl() const;
99
100#if ENABLE(INPUT_TYPE_COLOR)
101    bool isColorControl() const;
102#endif
103
104    // FIXME: It's highly likely that any call site calling this function should instead
105    // be using a different one. Many input elements behave like text fields, and in addition
106    // any unknown input type is treated as text. Consider, for example, isTextField or
107    // isTextField && !isPasswordField.
108    bool isText() const;
109
110    bool isEmailField() const;
111    bool isFileUpload() const;
112    bool isImageButton() const;
113    bool isNumberField() const;
114    bool isSubmitButton() const;
115    bool isTelephoneField() const;
116    bool isURLField() const;
117    bool isDateField() const;
118    bool isDateTimeField() const;
119    bool isDateTimeLocalField() const;
120    bool isMonthField() const;
121    bool isTimeField() const;
122    bool isWeekField() const;
123
124#if ENABLE(INPUT_SPEECH)
125    bool isSpeechEnabled() const;
126#endif
127
128    HTMLElement* containerElement() const;
129    virtual HTMLElement* innerTextElement() const;
130    HTMLElement* innerBlockElement() const;
131    HTMLElement* innerSpinButtonElement() const;
132    HTMLElement* resultsButtonElement() const;
133    HTMLElement* cancelButtonElement() const;
134#if ENABLE(INPUT_SPEECH)
135    HTMLElement* speechButtonElement() const;
136#endif
137    HTMLElement* sliderThumbElement() const;
138    HTMLElement* sliderTrackElement() const;
139    virtual HTMLElement* placeholderElement() const;
140
141    bool checked() const { return m_isChecked; }
142    void setChecked(bool, TextFieldEventBehavior = DispatchNoEvent);
143
144    // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
145    bool indeterminate() const { return m_isIndeterminate; }
146    void setIndeterminate(bool);
147    // shouldAppearChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
148    bool shouldAppearChecked() const;
149    virtual bool shouldAppearIndeterminate() const OVERRIDE;
150
151    int size() const;
152    bool sizeShouldIncludeDecoration(int& preferredSize) const;
153
154    void setType(const String&);
155
156    String value() const;
157    void setValue(const String&, ExceptionCode&, TextFieldEventBehavior = DispatchNoEvent);
158    void setValue(const String&, TextFieldEventBehavior = DispatchNoEvent);
159    void setValueForUser(const String&);
160    // Checks if the specified string would be a valid value.
161    // We should not call this for types with no string value such as CHECKBOX and RADIO.
162    bool isValidValue(const String&) const;
163    bool hasDirtyValue() const { return !m_valueIfDirty.isNull(); };
164
165    String sanitizeValue(const String&) const;
166
167    String localizeValue(const String&) const;
168
169    // The value which is drawn by a renderer.
170    String visibleValue() const;
171
172    const String& suggestedValue() const;
173    void setSuggestedValue(const String&);
174
175    void setEditingValue(const String&);
176
177    double valueAsDate() const;
178    void setValueAsDate(double, ExceptionCode&);
179
180    double valueAsNumber() const;
181    void setValueAsNumber(double, ExceptionCode&, TextFieldEventBehavior = DispatchNoEvent);
182
183    String valueWithDefault() const;
184
185    void setValueFromRenderer(const String&);
186
187    bool canHaveSelection() const;
188
189    virtual bool rendererIsNeeded(const NodeRenderingContext&);
190    virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
191    virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
192
193    // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
194    // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
195    // the private virtual method.
196    virtual bool isActivatedSubmit() const;
197    virtual void setActivatedSubmit(bool flag);
198
199    String altText() const;
200
201    int maxResults() const { return m_maxResults; }
202
203    String defaultValue() const;
204    void setDefaultValue(const String&);
205
206    Vector<String> acceptMIMETypes();
207    Vector<String> acceptFileExtensions();
208    String accept() const;
209    String alt() const;
210
211    void setSize(unsigned);
212    void setSize(unsigned, ExceptionCode&);
213
214    KURL src() const;
215
216    virtual int maxLength() const;
217    void setMaxLength(int, ExceptionCode&);
218
219    bool multiple() const;
220
221    bool isAutofilled() const { return m_isAutofilled; }
222    void setAutofilled(bool = true);
223
224    FileList* files();
225    void setFiles(PassRefPtr<FileList>);
226
227    // Returns true if the given DragData has more than one dropped files.
228    bool receiveDroppedFiles(const DragData*);
229
230#if ENABLE(FILE_SYSTEM)
231    String droppedFileSystemId();
232#endif
233
234    Icon* icon() const;
235    // These functions are used for rendering the input active during a
236    // drag-and-drop operation.
237    bool canReceiveDroppedFiles() const;
238    void setCanReceiveDroppedFiles(bool);
239
240    void addSearchResult();
241    void onSearch();
242
243    void updateClearButtonVisibility();
244
245    virtual bool willRespondToMouseClickEvents() OVERRIDE;
246
247#if ENABLE(DATALIST_ELEMENT)
248    HTMLElement* list() const;
249    HTMLDataListElement* dataList() const;
250    void listAttributeTargetChanged();
251#endif
252
253    HTMLInputElement* checkedRadioButtonForGroup() const;
254    bool isInRequiredRadioButtonGroup();
255
256    // Functions for InputType classes.
257    void setValueInternal(const String&, TextFieldEventBehavior);
258    bool isTextFormControlFocusable() const;
259    bool isTextFormControlKeyboardFocusable(KeyboardEvent*) const;
260    bool isTextFormControlMouseFocusable() const;
261    bool valueAttributeWasUpdatedAfterParsing() const { return m_valueAttributeWasUpdatedAfterParsing; }
262
263    void cacheSelectionInResponseToSetValue(int caretOffset) { cacheSelection(caretOffset, caretOffset, SelectionHasNoDirection); }
264
265#if ENABLE(INPUT_TYPE_COLOR)
266    // For test purposes.
267    void selectColorInColorChooser(const Color&);
268#endif
269
270    String defaultToolTip() const;
271
272#if ENABLE(MEDIA_CAPTURE)
273    String capture() const;
274    void setCapture(const String& value);
275#endif
276
277    static const int maximumLength;
278
279    unsigned height() const;
280    unsigned width() const;
281    void setHeight(unsigned);
282    void setWidth(unsigned);
283
284    virtual void blur() OVERRIDE;
285    void defaultBlur();
286
287    virtual const AtomicString& name() const OVERRIDE;
288
289    void endEditing();
290
291    static Vector<FileChooserFileInfo> filesFromFileInputFormControlState(const FormControlState&);
292
293    virtual bool matchesReadOnlyPseudoClass() const OVERRIDE;
294    virtual bool matchesReadWritePseudoClass() const OVERRIDE;
295    virtual void setRangeText(const String& replacement, ExceptionCode&) OVERRIDE;
296    virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionCode&) OVERRIDE;
297
298    bool hasImageLoader() const { return m_imageLoader; }
299    HTMLImageLoader* imageLoader();
300
301#if ENABLE(DATE_AND_TIME_INPUT_TYPES)
302    bool setupDateTimeChooserParameters(DateTimeChooserParameters&);
303#endif
304
305protected:
306    HTMLInputElement(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
307
308    virtual void defaultEventHandler(Event*);
309
310private:
311    enum AutoCompleteSetting { Uninitialized, On, Off };
312
313    // FIXME: Author shadows should be allowed
314    // https://bugs.webkit.org/show_bug.cgi?id=92608
315    virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
316
317    virtual void didAddUserAgentShadowRoot(ShadowRoot*) OVERRIDE;
318
319    virtual void willChangeForm() OVERRIDE;
320    virtual void didChangeForm() OVERRIDE;
321    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
322    virtual void removedFrom(ContainerNode*) OVERRIDE;
323    virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
324
325    virtual bool hasCustomFocusLogic() const OVERRIDE;
326    virtual bool isKeyboardFocusable(KeyboardEvent*) const OVERRIDE;
327    virtual bool isMouseFocusable() const OVERRIDE;
328    virtual bool isEnumeratable() const;
329    virtual bool supportLabels() const OVERRIDE;
330    virtual void updateFocusAppearance(bool restorePreviousSelection);
331    virtual bool shouldUseInputMethod() OVERRIDE FINAL;
332
333    virtual bool isTextFormControl() const { return isTextField(); }
334
335    virtual bool canTriggerImplicitSubmission() const { return isTextField(); }
336
337    virtual const AtomicString& formControlType() const;
338
339    virtual bool shouldSaveAndRestoreFormControlState() const OVERRIDE;
340    virtual FormControlState saveFormControlState() const OVERRIDE;
341    virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
342
343    virtual bool canStartSelection() const;
344
345    virtual void accessKeyAction(bool sendMouseEvents);
346
347    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
348    virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
349    virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) OVERRIDE;
350    virtual void finishParsingChildren();
351
352    virtual void copyNonAttributePropertiesFromElement(const Element&);
353
354    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
355
356    virtual bool appendFormData(FormDataList&, bool);
357
358    virtual bool isSuccessfulSubmitButton() const;
359
360    virtual void reset();
361
362    virtual void* preDispatchEventHandler(Event*);
363    virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch);
364
365    virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
366    virtual bool isInRange() const;
367    virtual bool isOutOfRange() const;
368
369    virtual void documentDidResumeFromPageCache();
370
371    virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;
372
373    bool needsSuspensionCallback();
374    void registerForSuspensionCallbackIfNeeded();
375    void unregisterForSuspensionCallbackIfNeeded();
376
377    bool supportsMaxLength() const { return isTextType(); }
378    bool isTextType() const;
379    bool tooLong(const String&, NeedsToCheckDirtyFlag) const;
380
381    virtual bool supportsPlaceholder() const;
382    virtual void updatePlaceholderText();
383    virtual bool isEmptyValue() const OVERRIDE { return innerTextValue().isEmpty(); }
384    virtual bool isEmptySuggestedValue() const { return suggestedValue().isEmpty(); }
385    virtual void handleFocusEvent(Node* oldFocusedNode, FocusDirection) OVERRIDE;
386    virtual void handleBlurEvent();
387
388    virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
389    virtual bool isRequiredFormControl() const;
390    virtual bool recalcWillValidate() const;
391    virtual void requiredAttributeChanged() OVERRIDE;
392
393    void updateType();
394
395    virtual void subtreeHasChanged();
396
397#if ENABLE(DATALIST_ELEMENT)
398    void resetListAttributeTargetObserver();
399#endif
400    void parseMaxLengthAttribute(const AtomicString&);
401    void updateValueIfNeeded();
402
403    // Returns null if this isn't associated with any radio button group.
404    CheckedRadioButtons* checkedRadioButtons() const;
405    void addToRadioButtonGroup();
406    void removeFromRadioButtonGroup();
407
408    AtomicString m_name;
409    String m_valueIfDirty;
410    String m_suggestedValue;
411    int m_size;
412    int m_maxLength;
413    short m_maxResults;
414    bool m_isChecked : 1;
415    bool m_reflectsCheckedAttribute : 1;
416    bool m_isIndeterminate : 1;
417    bool m_hasType : 1;
418    bool m_isActivatedSubmit : 1;
419    unsigned m_autocomplete : 2; // AutoCompleteSetting
420    bool m_isAutofilled : 1;
421#if ENABLE(DATALIST_ELEMENT)
422    bool m_hasNonEmptyList : 1;
423#endif
424    bool m_stateRestored : 1;
425    bool m_parsingInProgress : 1;
426    bool m_valueAttributeWasUpdatedAfterParsing : 1;
427    bool m_wasModifiedByUser : 1;
428    bool m_canReceiveDroppedFiles : 1;
429#if ENABLE(TOUCH_EVENTS)
430    bool m_hasTouchEventHandler : 1;
431#endif
432    OwnPtr<InputType> m_inputType;
433    // The ImageLoader must be owned by this element because the loader code assumes
434    // that it lives as long as its owning element lives. If we move the loader into
435    // the ImageInput object we may delete the loader while this element lives on.
436    OwnPtr<HTMLImageLoader> m_imageLoader;
437#if ENABLE(DATALIST_ELEMENT)
438    OwnPtr<ListAttributeTargetObserver> m_listAttributeTargetObserver;
439#endif
440};
441
442} //namespace
443#endif
444