Upstream version 9.38.198.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 blink {
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_WILL_BE_REMOVED;
59
60 public:
61     static PassRefPtrWillBeRawPtr<InputType> create(HTMLInputElement&, const AtomicString&);
62     static PassRefPtrWillBeRawPtr<InputType> createText(HTMLInputElement&);
63     static const AtomicString& normalizeTypeName(const AtomicString&);
64     virtual ~InputType();
65
66     virtual const AtomicString& formControlType() const = 0;
67
68     // Type query functions
69
70     // Any time we are using one of these functions it's best to refactor
71     // to add a virtual function to allow the input type object to do the
72     // work instead, or at least make a query function that asks a higher
73     // level question. These functions make the HTMLInputElement class
74     // inflexible because it's harder to add new input types if there is
75     // scattered code with special cases for various types.
76
77     virtual bool isCheckbox() const;
78     virtual bool isColorControl() const;
79     virtual bool isDateField() const;
80     virtual bool isDateTimeLocalField() const;
81     virtual bool isEmailField() const;
82     virtual bool isFileUpload() const;
83     virtual bool isHiddenType() const;
84     virtual bool isImageButton() const;
85     virtual bool isInteractiveContent() const;
86     virtual bool isMonthField() const;
87     virtual bool isNumberField() const;
88     virtual bool isPasswordField() const;
89     virtual bool isRadioButton() const;
90     virtual bool isRangeControl() const;
91     virtual bool isSearchField() const;
92     virtual bool isTelephoneField() const;
93     virtual bool isTextButton() const;
94     virtual bool isTextField() const;
95     virtual bool isTextType() const;
96     virtual bool isTimeField() const;
97     virtual bool isURLField() const;
98     virtual bool isWeekField() const;
99
100     // Form value functions
101
102     virtual bool shouldSaveAndRestoreFormControlState() const;
103     virtual FormControlState saveFormControlState() const;
104     virtual void restoreFormControlState(const FormControlState&);
105     virtual bool isFormDataAppendable() const;
106     virtual bool appendFormData(FormDataList&, bool multipart) const;
107     virtual String resultForDialogSubmit() const;
108
109     // DOM property functions
110
111     virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
112     virtual String fallbackValue() const; // Checked last, if both internal storage and value attribute are missing.
113     virtual String defaultValue() const; // Checked after even fallbackValue, only when the valueWithDefault function is called.
114     virtual double valueAsDate() const;
115     virtual void setValueAsDate(double, ExceptionState&) const;
116     virtual double valueAsDouble() const;
117     virtual void setValueAsDouble(double, TextFieldEventBehavior, ExceptionState&) const;
118     virtual void setValueAsDecimal(const Decimal&, TextFieldEventBehavior, ExceptionState&) const;
119
120     // Validation functions
121     virtual String validationMessage() const;
122     virtual bool supportsValidation() const;
123     virtual bool typeMismatchFor(const String&) const;
124     // Type check for the current input value. We do nothing for some types
125     // though typeMismatchFor() does something for them because of value
126     // sanitization.
127     virtual bool typeMismatch() const;
128     virtual bool supportsRequired() const;
129     virtual bool valueMissing(const String&) const;
130     virtual bool hasBadInput() const;
131     virtual bool patternMismatch(const String&) const;
132     bool rangeUnderflow(const String&) const;
133     bool rangeOverflow(const String&) const;
134     bool isInRange(const String&) const;
135     bool isOutOfRange(const String&) const;
136     virtual Decimal defaultValueForStepUp() const;
137     double minimum() const;
138     double maximum() const;
139     bool stepMismatch(const String&) const;
140     virtual bool getAllowedValueStep(Decimal*) const;
141     virtual StepRange createStepRange(AnyStepHandling) const;
142     virtual void stepUp(int, ExceptionState&);
143     virtual void stepUpFromRenderer(int);
144     virtual String badInputText() const;
145     virtual String rangeOverflowText(const Decimal& maximum) const;
146     virtual String rangeUnderflowText(const Decimal& minimum) const;
147     virtual String typeMismatchText() const;
148     virtual String valueMissingText() const;
149     virtual bool canSetStringValue() const;
150     virtual String localizeValue(const String&) const;
151     virtual String visibleValue() const;
152     // Returing the null string means "use the default value."
153     // This function must be called only by HTMLInputElement::sanitizeValue().
154     virtual String sanitizeValue(const String&) const;
155
156     virtual bool isKeyboardFocusable() const;
157     virtual bool shouldShowFocusRingOnMouseFocus() const;
158     virtual void enableSecureTextInput();
159     virtual void disableSecureTextInput();
160     virtual void accessKeyAction(bool sendMouseEvents);
161     virtual bool canBeSuccessfulSubmitButton();
162
163     // Miscellaneous functions
164
165     virtual bool rendererIsNeeded();
166     virtual void countUsage();
167     virtual void sanitizeValueInResponseToMinOrMaxAttributeChange();
168     virtual bool shouldRespectAlignAttribute();
169     virtual FileList* files();
170     virtual void setFiles(PassRefPtrWillBeRawPtr<FileList>);
171     // Should return true if the given DragData has more than one dropped files.
172     virtual bool receiveDroppedFiles(const DragData*);
173     virtual String droppedFileSystemId();
174     // Should return true if the corresponding renderer for a type can display a suggested value.
175     virtual bool canSetSuggestedValue();
176     virtual bool shouldSendChangeEventAfterCheckedChanged();
177     virtual bool canSetValue(const String&);
178     virtual bool storesValueSeparateFromAttribute();
179     virtual void setValue(const String&, bool valueChanged, TextFieldEventBehavior);
180     virtual bool shouldRespectListAttribute();
181     virtual bool shouldRespectSpeechAttribute();
182     virtual bool isEnumeratable();
183     virtual bool isCheckable();
184     virtual bool isSteppable() const;
185     virtual bool shouldRespectHeightAndWidthAttributes();
186     virtual bool supportsPlaceholder() const;
187     virtual bool supportsReadOnly() const;
188     virtual String defaultToolTip() const;
189     virtual Decimal findClosestTickMarkValue(const Decimal&);
190     virtual void handleDOMActivateEvent(Event*);
191     virtual bool hasLegalLinkAttribute(const QualifiedName&) const;
192     virtual const QualifiedName& subResourceAttributeName() const;
193
194     // Parses the specified string for the type, and return
195     // the Decimal value for the parsing result if the parsing
196     // succeeds; Returns defaultValue otherwise. This function can
197     // return NaN or Infinity only if defaultValue is NaN or Infinity.
198     virtual Decimal parseToNumber(const String&, const Decimal& defaultValue) const;
199
200     // Create a string representation of the specified Decimal value for the
201     // input type. If NaN or Infinity is specified, this returns an empty
202     // string. This should not be called for types without valueAsNumber.
203     virtual String serialize(const Decimal&) const;
204
205     virtual bool supportsIndeterminateAppearance() const;
206
207     virtual bool supportsInputModeAttribute() const;
208
209     virtual bool supportsSelectionAPI() const;
210
211     // Gets width and height of the input element if the type of the
212     // element is image. It returns 0 if the element is not image type.
213     virtual unsigned height() const;
214     virtual unsigned width() const;
215
216     virtual TextDirection computedTextDirection();
217
218     void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
219
220     // InputTypeView override
221     virtual bool shouldSubmitImplicitly(Event*) OVERRIDE;
222     virtual bool hasCustomFocusLogic() const OVERRIDE;
223
224     virtual bool shouldDispatchFormControlChangeEvent(String&, String&);
225
226 protected:
227     InputType(HTMLInputElement& element) : InputTypeView(element) { }
228     Chrome* chrome() const;
229     Locale& locale() const;
230     Decimal parseToNumberOrNaN(const String&) const;
231     void countUsageIfVisible(UseCounter::Feature) const;
232
233     // Derive the step base, following the HTML algorithm steps.
234     Decimal findStepBase(const Decimal&) const;
235
236     StepRange createStepRange(AnyStepHandling, const Decimal& stepBaseDefault, const Decimal& minimumDefault, const Decimal& maximumDefault, const StepRange::StepDescription&) const;
237
238 private:
239     // Helper for stepUp()/stepDown(). Adds step value * count to the current value.
240     void applyStep(const Decimal&, int count, AnyStepHandling, TextFieldEventBehavior, ExceptionState&);
241 };
242
243 } // namespace blink
244 #endif