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