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