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