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 "FeatureObserver.h"
37 #include "HTMLTextFormControlElement.h"
38 #include "RenderPtr.h"
39 #include "StepRange.h"
40 #include <wtf/FastMalloc.h>
41 #include <wtf/Forward.h>
42 #include <wtf/Noncopyable.h>
43 #include <wtf/RefPtr.h>
46 #include "DateComponents.h"
51 class BeforeTextInsertedEvent;
59 class HTMLFormElement;
60 class HTMLInputElement;
67 class TextControlInnerTextElement;
69 struct InputElementClickState;
71 typedef int ExceptionCode;
73 // An InputType object represents the type-specific part of an HTMLInputElement.
74 // Do not expose instances of InputType and classes derived from it to classes
75 // other than HTMLInputElement.
77 WTF_MAKE_NONCOPYABLE(InputType);
78 WTF_MAKE_FAST_ALLOCATED;
81 static std::unique_ptr<InputType> create(HTMLInputElement&, const AtomicString&);
82 static std::unique_ptr<InputType> createText(HTMLInputElement&);
85 static bool themeSupportsDataListUI(InputType*);
87 virtual const AtomicString& formControlType() const = 0;
88 virtual bool canChangeFromAnotherType() const;
90 // Type query functions
92 // Any time we are using one of these functions it's best to refactor
93 // to add a virtual function to allow the input type object to do the
94 // work instead, or at least make a query function that asks a higher
95 // level question. These functions make the HTMLInputElement class
96 // inflexible because it's harder to add new input types if there is
97 // scattered code with special cases for various types.
99 virtual bool isCheckbox() const;
100 virtual bool isDateField() const;
101 virtual bool isDateTimeField() const;
102 virtual bool isDateTimeLocalField() const;
103 virtual bool isEmailField() const;
104 virtual bool isFileUpload() const;
105 virtual bool isHiddenType() const;
106 virtual bool isImageButton() const;
107 virtual bool supportLabels() const;
108 virtual bool isMonthField() const;
109 virtual bool isNumberField() const;
110 virtual bool isPasswordField() const;
111 virtual bool isRadioButton() const;
112 virtual bool isRangeControl() const;
113 virtual bool isSearchField() const;
114 virtual bool isSubmitButton() const;
115 virtual bool isTelephoneField() const;
116 virtual bool isTextButton() const;
117 virtual bool isTextField() const;
118 virtual bool isTextType() const;
119 virtual bool isTimeField() const;
120 virtual bool isURLField() const;
121 virtual bool isWeekField() const;
123 #if ENABLE(INPUT_TYPE_COLOR)
124 virtual bool isColorControl() const;
127 // Form value functions
129 virtual bool shouldSaveAndRestoreFormControlState() const;
130 virtual FormControlState saveFormControlState() const;
131 virtual void restoreFormControlState(const FormControlState&);
132 virtual bool isFormDataAppendable() const;
133 virtual bool appendFormData(FormDataList&, bool multipart) const;
135 // DOM property functions
137 virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
138 virtual String fallbackValue() const; // Checked last, if both internal storage and value attribute are missing.
139 virtual String defaultValue() const; // Checked after even fallbackValue, only when the valueWithDefault function is called.
140 virtual double valueAsDate() const;
141 virtual void setValueAsDate(double, ExceptionCode&) const;
142 virtual double valueAsDouble() const;
143 virtual void setValueAsDouble(double, TextFieldEventBehavior, ExceptionCode&) const;
144 virtual void setValueAsDecimal(const Decimal&, TextFieldEventBehavior, ExceptionCode&) const;
146 // Validation functions
147 virtual String validationMessage() const;
148 virtual bool supportsValidation() const;
149 virtual bool typeMismatchFor(const String&) const;
150 // Type check for the current input value. We do nothing for some types
151 // though typeMismatchFor() does something for them because of value
153 virtual bool typeMismatch() const;
154 virtual bool supportsRequired() const;
155 virtual bool valueMissing(const String&) const;
156 virtual bool hasBadInput() const;
157 virtual bool patternMismatch(const String&) const;
158 bool rangeUnderflow(const String&) const;
159 bool rangeOverflow(const String&) const;
160 bool isInRange(const String&) const;
161 bool isOutOfRange(const String&) const;
162 virtual Decimal defaultValueForStepUp() const;
163 double minimum() const;
164 double maximum() const;
165 virtual bool sizeShouldIncludeDecoration(int defaultSize, int& preferredSize) const;
166 virtual float decorationWidth() const;
167 bool stepMismatch(const String&) const;
168 virtual bool getAllowedValueStep(Decimal*) const;
169 virtual StepRange createStepRange(AnyStepHandling) const;
170 virtual void stepUp(int, ExceptionCode&);
171 virtual void stepUpFromRenderer(int);
172 virtual String badInputText() const;
173 virtual String typeMismatchText() const;
174 virtual String valueMissingText() const;
175 virtual bool canSetStringValue() const;
176 virtual String localizeValue(const String&) const;
177 virtual String visibleValue() const;
178 // Returing the null string means "use the default value."
179 // This function must be called only by HTMLInputElement::sanitizeValue().
180 virtual String sanitizeValue(const String&) const;
184 virtual void handleClickEvent(MouseEvent*);
185 virtual void handleMouseDownEvent(MouseEvent*);
186 virtual void willDispatchClick(InputElementClickState&);
187 virtual void didDispatchClick(Event*, const InputElementClickState&);
188 virtual void handleDOMActivateEvent(Event*);
189 virtual void handleKeydownEvent(KeyboardEvent*);
190 virtual void handleKeypressEvent(KeyboardEvent*);
191 virtual void handleKeyupEvent(KeyboardEvent*);
192 virtual void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent*);
193 virtual void forwardEvent(Event*);
195 #if ENABLE(TOUCH_EVENTS)
196 virtual void handleTouchEvent(TouchEvent*);
199 // Helpers for event handlers.
200 virtual bool shouldSubmitImplicitly(Event*);
201 virtual PassRefPtr<HTMLFormElement> formForSubmission() const;
202 virtual bool hasCustomFocusLogic() const;
203 virtual bool isKeyboardFocusable(KeyboardEvent*) const;
204 virtual bool isMouseFocusable() const;
205 virtual bool shouldUseInputMethod() const;
206 virtual void handleFocusEvent(Node* oldFocusedNode, FocusDirection);
207 virtual void handleBlurEvent();
208 virtual void accessKeyAction(bool sendMouseEvents);
209 virtual bool canBeSuccessfulSubmitButton();
210 virtual void subtreeHasChanged();
212 #if ENABLE(TOUCH_EVENTS)
213 virtual bool hasTouchEventHandler() const;
218 // Shadow tree handling
220 virtual void createShadowSubtree();
221 virtual void destroyShadowSubtree();
223 virtual HTMLElement* containerElement() const { return nullptr; }
224 virtual HTMLElement* innerBlockElement() const { return nullptr; }
225 virtual TextControlInnerTextElement* innerTextElement() const { return nullptr; }
226 virtual HTMLElement* innerSpinButtonElement() const { return nullptr; }
227 virtual HTMLElement* resultsButtonElement() const { return nullptr; }
228 virtual HTMLElement* cancelButtonElement() const { return nullptr; }
229 virtual HTMLElement* sliderThumbElement() const { return nullptr; }
230 virtual HTMLElement* sliderTrackElement() const { return nullptr; }
231 virtual HTMLElement* placeholderElement() const;
233 #if ENABLE(INPUT_SPEECH)
234 virtual HTMLElement* speechButtonElement() const { return nullptr; }
237 // Miscellaneous functions
239 virtual bool rendererIsNeeded();
240 virtual RenderPtr<RenderElement> createInputRenderer(PassRef<RenderStyle>);
241 virtual void addSearchResult();
242 virtual void attach();
243 virtual void detach();
244 virtual void minOrMaxAttributeChanged();
245 virtual void stepAttributeChanged();
246 virtual void altAttributeChanged();
247 virtual void srcAttributeChanged();
248 virtual bool shouldRespectAlignAttribute();
249 virtual FileList* files();
250 virtual void setFiles(PassRefPtr<FileList>);
251 #if ENABLE(DRAG_SUPPORT)
252 // Should return true if the given DragData has more than one dropped files.
253 virtual bool receiveDroppedFiles(const DragData&);
255 virtual Icon* icon() const;
257 virtual String displayString() const;
260 // Should return true if the corresponding renderer for a type can display a suggested value.
261 virtual bool canSetSuggestedValue();
262 virtual bool shouldSendChangeEventAfterCheckedChanged();
263 virtual bool canSetValue(const String&);
264 virtual bool storesValueSeparateFromAttribute();
265 virtual void setValue(const String&, bool valueChanged, TextFieldEventBehavior);
266 virtual bool shouldResetOnDocumentActivation();
267 virtual bool shouldRespectListAttribute();
268 virtual bool shouldRespectSpeechAttribute();
269 virtual bool isEnumeratable();
270 virtual bool isCheckable();
271 virtual bool isSteppable() const;
272 virtual bool shouldRespectHeightAndWidthAttributes();
273 virtual bool supportsPlaceholder() const;
274 virtual bool supportsReadOnly() const;
275 virtual void updateInnerTextValue();
276 virtual void updatePlaceholderText();
277 virtual void attributeChanged();
278 virtual void multipleAttributeChanged();
279 virtual void disabledAttributeChanged();
280 virtual void readonlyAttributeChanged();
281 virtual void requiredAttributeChanged();
282 virtual void valueAttributeChanged();
283 virtual String defaultToolTip() const;
284 virtual void updateClearButtonVisibility();
286 #if ENABLE(DATALIST_ELEMENT)
287 virtual void listAttributeTargetChanged();
288 virtual Decimal findClosestTickMarkValue(const Decimal&);
291 // Parses the specified string for the type, and return
292 // the Decimal value for the parsing result if the parsing
293 // succeeds; Returns defaultValue otherwise. This function can
294 // return NaN or Infinity only if defaultValue is NaN or Infinity.
295 virtual Decimal parseToNumber(const String&, const Decimal& defaultValue) const;
297 // Parses the specified string for this InputType, and returns true if it
298 // is successfully parsed. An instance pointed by the DateComponents*
299 // parameter will have parsed values and be modified even if the parsing
300 // fails. The DateComponents* parameter may be 0.
301 virtual bool parseToDateComponents(const String&, DateComponents*) const;
303 // Create a string representation of the specified Decimal value for the
304 // input type. If NaN or Infinity is specified, this returns an empty
305 // string. This should not be called for types without valueAsNumber.
306 virtual String serialize(const Decimal&) const;
309 virtual DateComponents::Type dateType() const;
312 virtual bool supportsIndeterminateAppearance() const;
314 virtual bool supportsSelectionAPI() const;
316 // Gets width and height of the input element if the type of the
317 // element is image. It returns 0 if the element is not image type.
318 virtual unsigned height() const;
319 virtual unsigned width() const;
321 void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
324 explicit InputType(HTMLInputElement& element) : m_element(element) { }
325 HTMLInputElement& element() const { return m_element; }
326 Chrome* chrome() const;
327 Decimal parseToNumberOrNaN(const String&) const;
328 void observeFeatureIfVisible(FeatureObserver::Feature) const;
331 // Helper for stepUp()/stepDown(). Adds step value * count to the current value.
332 void applyStep(int count, AnyStepHandling, TextFieldEventBehavior, ExceptionCode&);
334 // Raw pointer because the HTMLInputElement object owns this InputType object.
335 HTMLInputElement& m_element;
338 } // namespace WebCore