Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / InputType.h
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
41 namespace WebCore {
42
43 class Chrome;
44 class DateComponents;
45 class DragData;
46 class ExceptionState;
47 class FileList;
48 class FormDataList;
49 class HTMLElement;
50 class 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.
56 class InputType : public InputTypeView {
57     WTF_MAKE_NONCOPYABLE(InputType);
58     WTF_MAKE_FAST_ALLOCATED;
59
60 public:
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 isTelephoneField() const;
96     virtual bool isTextButton() const;
97     virtual bool isTextField() const;
98     virtual bool isTextType() const;
99     virtual bool isTimeField() const;
100     virtual bool isURLField() const;
101     virtual bool isWeekField() const;
102
103     // Form value functions
104
105     virtual bool shouldSaveAndRestoreFormControlState() const;
106     virtual FormControlState saveFormControlState() const;
107     virtual void restoreFormControlState(const FormControlState&);
108     virtual bool isFormDataAppendable() const;
109     virtual bool appendFormData(FormDataList&, bool multipart) const;
110     virtual String resultForDialogSubmit() const;
111
112     // DOM property functions
113
114     virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
115     virtual String fallbackValue() const; // Checked last, if both internal storage and value attribute are missing.
116     virtual String defaultValue() const; // Checked after even fallbackValue, only when the valueWithDefault function is called.
117     virtual double valueAsDate() const;
118     virtual void setValueAsDate(double, ExceptionState&) const;
119     virtual double valueAsDouble() const;
120     virtual void setValueAsDouble(double, TextFieldEventBehavior, ExceptionState&) const;
121     virtual void setValueAsDecimal(const Decimal&, TextFieldEventBehavior, ExceptionState&) const;
122
123     // Validation functions
124     virtual String validationMessage() const;
125     virtual bool supportsValidation() const;
126     virtual bool typeMismatchFor(const String&) const;
127     // Type check for the current input value. We do nothing for some types
128     // though typeMismatchFor() does something for them because of value
129     // sanitization.
130     virtual bool typeMismatch() const;
131     virtual bool supportsRequired() const;
132     virtual bool valueMissing(const String&) const;
133     virtual bool hasBadInput() const;
134     virtual bool patternMismatch(const String&) const;
135     bool rangeUnderflow(const String&) const;
136     bool rangeOverflow(const String&) const;
137     bool isInRange(const String&) const;
138     bool isOutOfRange(const String&) const;
139     virtual Decimal defaultValueForStepUp() const;
140     double minimum() const;
141     double maximum() const;
142     bool stepMismatch(const String&) const;
143     virtual bool getAllowedValueStep(Decimal*) const;
144     virtual StepRange createStepRange(AnyStepHandling) const;
145     virtual void stepUp(int, ExceptionState&);
146     virtual void stepUpFromRenderer(int);
147     virtual String badInputText() const;
148     virtual String rangeOverflowText(const Decimal& maximum) const;
149     virtual String rangeUnderflowText(const Decimal& minimum) const;
150     virtual String typeMismatchText() const;
151     virtual String valueMissingText() const;
152     virtual bool canSetStringValue() const;
153     virtual String localizeValue(const String&) const;
154     virtual String visibleValue() const;
155     // Returing the null string means "use the default value."
156     // This function must be called only by HTMLInputElement::sanitizeValue().
157     virtual String sanitizeValue(const String&) const;
158
159     virtual bool isKeyboardFocusable() const;
160     virtual bool shouldShowFocusRingOnMouseFocus() const;
161     virtual bool shouldUseInputMethod() const;
162     virtual void enableSecureTextInput();
163     virtual void disableSecureTextInput();
164     virtual void accessKeyAction(bool sendMouseEvents);
165     virtual bool canBeSuccessfulSubmitButton();
166
167     // Miscellaneous functions
168
169     virtual bool rendererIsNeeded();
170     virtual void countUsage();
171     virtual void sanitizeValueInResponseToMinOrMaxAttributeChange();
172     virtual bool shouldRespectAlignAttribute();
173     virtual FileList* files();
174     virtual void setFiles(PassRefPtr<FileList>);
175     // Should return true if the given DragData has more than one dropped files.
176     virtual bool receiveDroppedFiles(const DragData*);
177     virtual String droppedFileSystemId();
178     // Should return true if the corresponding renderer for a type can display a suggested value.
179     virtual bool canSetSuggestedValue();
180     virtual bool shouldSendChangeEventAfterCheckedChanged();
181     virtual bool canSetValue(const String&);
182     virtual bool storesValueSeparateFromAttribute();
183     virtual void setValue(const String&, bool valueChanged, TextFieldEventBehavior);
184     virtual bool shouldRespectListAttribute();
185     virtual bool shouldRespectSpeechAttribute();
186     virtual bool isEnumeratable();
187     virtual bool isCheckable();
188     virtual bool isSteppable() const;
189     virtual bool shouldRespectHeightAndWidthAttributes();
190     virtual bool supportsPlaceholder() const;
191     virtual bool supportsReadOnly() const;
192     virtual String defaultToolTip() const;
193     virtual Decimal findClosestTickMarkValue(const Decimal&);
194     virtual void handleDOMActivateEvent(Event*);
195
196     // Parses the specified string for the type, and return
197     // the Decimal value for the parsing result if the parsing
198     // succeeds; Returns defaultValue otherwise. This function can
199     // return NaN or Infinity only if defaultValue is NaN or Infinity.
200     virtual Decimal parseToNumber(const String&, const Decimal& defaultValue) const;
201
202     // Create a string representation of the specified Decimal value for the
203     // input type. If NaN or Infinity is specified, this returns an empty
204     // string. This should not be called for types without valueAsNumber.
205     virtual String serialize(const Decimal&) const;
206
207     virtual bool supportsIndeterminateAppearance() const;
208
209     virtual bool supportsInputModeAttribute() const;
210
211     virtual bool supportsSelectionAPI() const;
212
213     // Gets width and height of the input element if the type of the
214     // element is image. It returns 0 if the element is not image type.
215     virtual unsigned height() const;
216     virtual unsigned width() const;
217
218     void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
219
220     // InputTypeView override
221     virtual bool shouldSubmitImplicitly(Event*) OVERRIDE;
222     virtual bool hasCustomFocusLogic() const OVERRIDE;
223
224 protected:
225     InputType(HTMLInputElement& element) : InputTypeView(element) { }
226     Chrome* chrome() const;
227     Locale& locale() const;
228     Decimal parseToNumberOrNaN(const String&) const;
229     void countUsageIfVisible(UseCounter::Feature) const;
230
231     // Derive the step base, following the HTML algorithm steps.
232     Decimal findStepBase(const Decimal&) const;
233
234     StepRange createStepRange(AnyStepHandling, const Decimal& stepBaseDefault, const Decimal& minimumDefault, const Decimal& maximumDefault, const StepRange::StepDescription&) const;
235
236 private:
237     // Helper for stepUp()/stepDown(). Adds step value * count to the current value.
238     void applyStep(const Decimal&, int count, AnyStepHandling, TextFieldEventBehavior, ExceptionState&);
239 };
240
241 } // namespace WebCore
242 #endif