1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 *     * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef InputType_h
34#define InputType_h
35
36#include "core/html/HTMLTextFormControlElement.h"
37#include "core/html/forms/InputTypeView.h"
38#include "core/html/forms/StepRange.h"
39#include "core/frame/UseCounter.h"
40
41namespace WebCore {
42
43class Chrome;
44class DateComponents;
45class DragData;
46class ExceptionState;
47class FileList;
48class FormDataList;
49class HTMLElement;
50class Node;
51
52// An InputType object represents the type-specific part of an HTMLInputElement.
53// Do not expose instances of InputType and classes derived from it to classes
54// other than HTMLInputElement.
55// FIXME: InputType should not inherit InputTypeView. It's conceptually wrong.
56class InputType : public InputTypeView {
57    WTF_MAKE_NONCOPYABLE(InputType);
58    WTF_MAKE_FAST_ALLOCATED;
59
60public:
61    static PassRefPtr<InputType> create(HTMLInputElement&, const AtomicString&);
62    static PassRefPtr<InputType> createText(HTMLInputElement&);
63    static const AtomicString& normalizeTypeName(const AtomicString&);
64    virtual ~InputType();
65
66    static bool canChangeFromAnotherType(const AtomicString& normalizedTypeName);
67    static bool themeSupportsDataListUI(InputType*);
68
69    virtual const AtomicString& formControlType() const = 0;
70
71    // Type query functions
72
73    // Any time we are using one of these functions it's best to refactor
74    // to add a virtual function to allow the input type object to do the
75    // work instead, or at least make a query function that asks a higher
76    // level question. These functions make the HTMLInputElement class
77    // inflexible because it's harder to add new input types if there is
78    // scattered code with special cases for various types.
79
80    virtual bool isCheckbox() const;
81    virtual bool isColorControl() const;
82    virtual bool isDateField() const;
83    virtual bool isDateTimeLocalField() const;
84    virtual bool isEmailField() const;
85    virtual bool isFileUpload() const;
86    virtual bool isHiddenType() const;
87    virtual bool isImageButton() const;
88    virtual bool isInteractiveContent() const;
89    virtual bool isMonthField() const;
90    virtual bool isNumberField() const;
91    virtual bool isPasswordField() const;
92    virtual bool isRadioButton() const;
93    virtual bool isRangeControl() const;
94    virtual bool isSearchField() const;
95    virtual bool isSubmitButton() const;
96    virtual bool isTelephoneField() const;
97    virtual bool isTextButton() const;
98    virtual bool isTextField() const;
99    virtual bool isTextType() const;
100    virtual bool isTimeField() const;
101    virtual bool isURLField() const;
102    virtual bool isWeekField() const;
103
104    // Form value functions
105
106    virtual bool shouldSaveAndRestoreFormControlState() const;
107    virtual FormControlState saveFormControlState() const;
108    virtual void restoreFormControlState(const FormControlState&);
109    virtual bool isFormDataAppendable() const;
110    virtual bool appendFormData(FormDataList&, bool multipart) const;
111    virtual String resultForDialogSubmit() const;
112
113    // DOM property functions
114
115    virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
116    virtual String fallbackValue() const; // Checked last, if both internal storage and value attribute are missing.
117    virtual String defaultValue() const; // Checked after even fallbackValue, only when the valueWithDefault function is called.
118    virtual double valueAsDate() const;
119    virtual void setValueAsDate(double, ExceptionState&) const;
120    virtual double valueAsDouble() const;
121    virtual void setValueAsDouble(double, TextFieldEventBehavior, ExceptionState&) const;
122    virtual void setValueAsDecimal(const Decimal&, TextFieldEventBehavior, ExceptionState&) const;
123
124    // Validation functions
125    virtual String validationMessage() const;
126    virtual bool supportsValidation() const;
127    virtual bool typeMismatchFor(const String&) const;
128    // Type check for the current input value. We do nothing for some types
129    // though typeMismatchFor() does something for them because of value
130    // sanitization.
131    virtual bool typeMismatch() const;
132    virtual bool supportsRequired() const;
133    virtual bool valueMissing(const String&) const;
134    virtual bool hasBadInput() const;
135    virtual bool patternMismatch(const String&) const;
136    bool rangeUnderflow(const String&) const;
137    bool rangeOverflow(const String&) const;
138    bool isInRange(const String&) const;
139    bool isOutOfRange(const String&) const;
140    virtual Decimal defaultValueForStepUp() const;
141    double minimum() const;
142    double maximum() const;
143    bool stepMismatch(const String&) const;
144    virtual bool getAllowedValueStep(Decimal*) const;
145    virtual StepRange createStepRange(AnyStepHandling) const;
146    virtual void stepUp(int, ExceptionState&);
147    virtual void stepUpFromRenderer(int);
148    virtual String badInputText() const;
149    virtual String rangeOverflowText(const Decimal& maximum) const;
150    virtual String rangeUnderflowText(const Decimal& minimum) const;
151    virtual String typeMismatchText() const;
152    virtual String valueMissingText() const;
153    virtual bool canSetStringValue() const;
154    virtual String localizeValue(const String&) const;
155    virtual String visibleValue() const;
156    // Returing the null string means "use the default value."
157    // This function must be called only by HTMLInputElement::sanitizeValue().
158    virtual String sanitizeValue(const String&) const;
159
160    virtual bool isKeyboardFocusable() const;
161    virtual bool shouldShowFocusRingOnMouseFocus() const;
162    virtual bool shouldUseInputMethod() const;
163    virtual void enableSecureTextInput();
164    virtual void disableSecureTextInput();
165    virtual void accessKeyAction(bool sendMouseEvents);
166    virtual bool canBeSuccessfulSubmitButton();
167
168    // Miscellaneous functions
169
170    virtual bool rendererIsNeeded();
171    virtual void countUsage();
172    virtual void sanitizeValueInResponseToMinOrMaxAttributeChange();
173    virtual bool shouldRespectAlignAttribute();
174    virtual FileList* files();
175    virtual void setFiles(PassRefPtr<FileList>);
176    // Should return true if the given DragData has more than one dropped files.
177    virtual bool receiveDroppedFiles(const DragData*);
178    virtual String droppedFileSystemId();
179    // Should return true if the corresponding renderer for a type can display a suggested value.
180    virtual bool canSetSuggestedValue();
181    virtual bool shouldSendChangeEventAfterCheckedChanged();
182    virtual bool canSetValue(const String&);
183    virtual bool storesValueSeparateFromAttribute();
184    virtual void setValue(const String&, bool valueChanged, TextFieldEventBehavior);
185    virtual bool shouldResetOnDocumentActivation();
186    virtual bool shouldRespectListAttribute();
187    virtual bool shouldRespectSpeechAttribute();
188    virtual bool isEnumeratable();
189    virtual bool isCheckable();
190    virtual bool isSteppable() const;
191    virtual bool shouldRespectHeightAndWidthAttributes();
192    virtual bool supportsPlaceholder() const;
193    virtual bool supportsReadOnly() const;
194    virtual void updatePlaceholderText();
195    virtual String defaultToolTip() const;
196    virtual Decimal findClosestTickMarkValue(const Decimal&);
197    virtual void handleDOMActivateEvent(Event*);
198
199    // Parses the specified string for the type, and return
200    // the Decimal value for the parsing result if the parsing
201    // succeeds; Returns defaultValue otherwise. This function can
202    // return NaN or Infinity only if defaultValue is NaN or Infinity.
203    virtual Decimal parseToNumber(const String&, const Decimal& defaultValue) const;
204
205    // Parses the specified string for this InputType, and returns true if it
206    // is successfully parsed. An instance pointed by the DateComponents*
207    // parameter will have parsed values and be modified even if the parsing
208    // fails. The DateComponents* parameter may be 0.
209    virtual bool parseToDateComponents(const String&, DateComponents*) const;
210
211    // Create a string representation of the specified Decimal value for the
212    // input type. If NaN or Infinity is specified, this returns an empty
213    // string. This should not be called for types without valueAsNumber.
214    virtual String serialize(const Decimal&) const;
215
216    virtual bool supportsIndeterminateAppearance() const;
217
218    virtual bool supportsInputModeAttribute() const;
219
220    virtual bool supportsSelectionAPI() const;
221
222    // Gets width and height of the input element if the type of the
223    // element is image. It returns 0 if the element is not image type.
224    virtual unsigned height() const;
225    virtual unsigned width() const;
226
227    void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
228
229    // InputTypeView override
230    virtual bool shouldSubmitImplicitly(Event*) OVERRIDE;
231    virtual bool hasCustomFocusLogic() const OVERRIDE;
232
233protected:
234    InputType(HTMLInputElement& element) : InputTypeView(element) { }
235    Chrome* chrome() const;
236    Locale& locale() const;
237    Decimal parseToNumberOrNaN(const String&) const;
238    void countUsageIfVisible(UseCounter::Feature) const;
239
240    // Derive the step base, following the HTML algorithm steps.
241    Decimal findStepBase(const Decimal&) const;
242
243    StepRange createStepRange(AnyStepHandling, const Decimal& stepBaseDefault, const Decimal& minimumDefault, const Decimal& maximumDefault, const StepRange::StepDescription&) const;
244
245private:
246    // Helper for stepUp()/stepDown(). Adds step value * count to the current value.
247    void applyStep(int count, AnyStepHandling, TextFieldEventBehavior, ExceptionState&);
248};
249
250} // namespace WebCore
251#endif
252