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