initial import
[vuplus_webkit] / Source / WebCore / html / InputType.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2011 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef InputType_h
33 #define InputType_h
34
35 #include <wtf/Forward.h>
36 #include <wtf/FastAllocBase.h>
37 #include <wtf/Noncopyable.h>
38 #include <wtf/RefPtr.h>
39 #include <wtf/Vector.h>
40
41 namespace WebCore {
42
43 class BeforeTextInsertedEvent;
44 class Chrome;
45 class Color;
46 class DateComponents;
47 class Event;
48 class FileList;
49 class FormDataList;
50 class HTMLElement;
51 class HTMLFormElement;
52 class HTMLInputElement;
53 class Icon;
54 class KeyboardEvent;
55 class MouseEvent;
56 class Node;
57 class RenderArena;
58 class RenderObject;
59 class RenderStyle;
60 class WheelEvent;
61
62 typedef int ExceptionCode;
63
64 struct ClickHandlingState {
65     WTF_MAKE_FAST_ALLOCATED;
66 public:
67     bool checked;
68     bool indeterminate;
69     RefPtr<HTMLInputElement> checkedRadioButton;
70 };
71
72 // An InputType object represents the type-specific part of an HTMLInputElement.
73 // Do not expose instances of InputType and classes derived from it to classes
74 // other than HTMLInputElement.
75 class InputType {
76     WTF_MAKE_NONCOPYABLE(InputType); WTF_MAKE_FAST_ALLOCATED;
77 public:
78     static PassOwnPtr<InputType> create(HTMLInputElement*, const String&);
79     static PassOwnPtr<InputType> createText(HTMLInputElement*);
80     virtual ~InputType();
81
82     virtual const AtomicString& formControlType() const = 0;
83     virtual bool canChangeFromAnotherType() const;
84
85     // Type query functions
86
87     // Any time we are using one of these functions it's best to refactor
88     // to add a virtual function to allow the input type object to do the
89     // work instead, or at least make a query function that asks a higher
90     // level question. These functions make the HTMLInputElement class
91     // inflexible because it's harder to add new input types if there is
92     // scattered code with special cases for various types.
93
94 #if ENABLE(INPUT_COLOR)
95     virtual bool isColorControl() const;
96 #endif
97     virtual bool isCheckbox() const;
98     virtual bool isEmailField() const;
99     virtual bool isFileUpload() const;
100     virtual bool isHiddenType() const;
101     virtual bool isImageButton() const;
102     virtual bool isNumberField() const;
103     virtual bool isPasswordField() const;
104     virtual bool isRadioButton() const;
105     virtual bool isRangeControl() const;
106     virtual bool isSearchField() const;
107     virtual bool isSubmitButton() const;
108     virtual bool isTelephoneField() const;
109     virtual bool isTextButton() const;
110     virtual bool isTextField() const;
111     virtual bool isTextType() const;
112     virtual bool isURLField() const;
113
114     // Form value functions
115
116     virtual bool saveFormControlState(String&) const;
117     virtual void restoreFormControlState(const String&) const;
118     virtual bool isFormDataAppendable() const;
119     virtual bool appendFormData(FormDataList&, bool multipart) const;
120
121     // DOM property functions
122
123     virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
124     virtual String fallbackValue(); // Checked last, if both internal storage and value attribute are missing.
125     virtual String defaultValue(); // Checked after even fallbackValue, only when the valueWithDefault function is called.
126     virtual double valueAsDate() const;
127     virtual void setValueAsDate(double, ExceptionCode&) const;
128     virtual double valueAsNumber() const;
129     virtual void setValueAsNumber(double, bool sendChangeEvent, ExceptionCode&) const;
130
131     // Validation functions
132
133     virtual bool supportsValidation() const;
134     virtual bool typeMismatchFor(const String&) const;
135     // Type check for the current input value. We do nothing for some types
136     // though typeMismatchFor() does something for them because of value
137     // sanitization.
138     virtual bool typeMismatch() const;
139     virtual bool supportsRequired() const;
140     virtual bool valueMissing(const String&) const;
141     virtual bool patternMismatch(const String&) const;
142     virtual bool rangeUnderflow(const String&) const;
143     virtual bool rangeOverflow(const String&) const;
144     virtual bool supportsRangeLimitation() const;
145     virtual double defaultValueForStepUp() const;
146     virtual double minimum() const;
147     virtual double maximum() const;
148     virtual bool sizeShouldIncludeDecoration(int defaultSize, int& preferredSize) const;
149     virtual bool stepMismatch(const String&, double step) const;
150     virtual double stepBase() const;
151     virtual double stepBaseWithDecimalPlaces(unsigned*) const;
152     virtual double defaultStep() const;
153     virtual double stepScaleFactor() const;
154     virtual bool parsedStepValueShouldBeInteger() const;
155     virtual bool scaledStepValueShouldBeInteger() const;
156     virtual double acceptableError(double) const;
157     virtual String typeMismatchText() const;
158     virtual String valueMissingText() const;
159     virtual bool canSetStringValue() const;
160     virtual String visibleValue() const;
161     virtual String convertFromVisibleValue(const String&) const;
162     virtual bool isAcceptableValue(const String&);
163     // Returing the null string means "use the default value."
164     virtual String sanitizeValue(const String&);
165     virtual bool hasUnacceptableValue();
166
167     // Event handlers
168
169     virtual void handleClickEvent(MouseEvent*);
170     virtual void handleMouseDownEvent(MouseEvent*);
171     virtual PassOwnPtr<ClickHandlingState> willDispatchClick();
172     virtual void didDispatchClick(Event*, const ClickHandlingState&);
173     virtual void handleDOMActivateEvent(Event*);
174     virtual void handleKeydownEvent(KeyboardEvent*);
175     virtual void handleKeypressEvent(KeyboardEvent*);
176     virtual void handleKeyupEvent(KeyboardEvent*);
177     virtual void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent*);
178     virtual void handleWheelEvent(WheelEvent*);
179     virtual void forwardEvent(Event*);
180     // Helpers for event handlers.
181     virtual bool shouldSubmitImplicitly(Event*);
182     virtual PassRefPtr<HTMLFormElement> formForSubmission() const;
183     virtual bool isKeyboardFocusable() const;
184     virtual bool shouldUseInputMethod() const;
185     virtual void willBlur();
186     virtual void accessKeyAction(bool sendToAnyElement);
187     virtual bool canBeSuccessfulSubmitButton();
188
189
190     // Shadow tree handling
191
192     virtual void createShadowSubtree();
193     virtual void destroyShadowSubtree();
194
195     virtual HTMLElement* containerElement() const { return 0; }
196     virtual HTMLElement* innerBlockElement() const { return 0; }
197     virtual HTMLElement* innerTextElement() const { return 0; }
198     virtual HTMLElement* innerSpinButtonElement() const { return 0; }
199     virtual HTMLElement* resultsButtonElement() const { return 0; }
200     virtual HTMLElement* cancelButtonElement() const { return 0; }
201 #if ENABLE(INPUT_SPEECH)
202     virtual HTMLElement* speechButtonElement() const { return 0; }
203 #endif
204     virtual HTMLElement* placeholderElement() const;
205
206     // Miscellaneous functions
207
208     virtual bool rendererIsNeeded();
209     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*) const;
210     virtual void attach();
211     virtual void detach();
212     virtual void minOrMaxAttributeChanged();
213     virtual void stepAttributeChanged();
214     virtual void altAttributeChanged();
215     virtual void srcAttributeChanged();
216     virtual void willMoveToNewOwnerDocument();
217     virtual bool shouldRespectAlignAttribute();
218     virtual FileList* files();
219     virtual void receiveDroppedFiles(const Vector<String>&);
220     virtual Icon* icon() const;
221     // Should return true if the corresponding renderer for a type can display a suggested value.
222     virtual bool canSetSuggestedValue();
223     virtual bool shouldSendChangeEventAfterCheckedChanged();
224     virtual bool canSetValue(const String&);
225     virtual bool storesValueSeparateFromAttribute();
226     virtual void setValue(const String&, bool valueChanged, bool sendChangeEvent);
227     virtual void dispatchChangeEventInResponseToSetValue();
228     virtual bool shouldResetOnDocumentActivation();
229     virtual bool shouldRespectListAttribute();
230     virtual bool shouldRespectSpeechAttribute();
231     virtual bool isEnumeratable();
232     virtual bool isCheckable();
233     virtual bool isSteppable() const;
234     virtual bool shouldRespectHeightAndWidthAttributes();
235     virtual bool supportsPlaceholder() const;
236     virtual void updatePlaceholderText();
237     virtual void multipleAttributeChanged();
238     virtual void disabledAttributeChanged();
239     virtual void readonlyAttributeChanged();
240
241     // Parses the specified string for the type, and return
242     // the double value for the parsing result if the parsing
243     // succeeds; Returns defaultValue otherwise. This function can
244     // return NaN or Infinity only if defaultValue is NaN or Infinity.
245     virtual double parseToDouble(const String&, double defaultValue) const;
246
247     // Parses the specified string for the type as parseToDouble() does.
248     // In addition, it stores the number of digits after the decimal point
249     // into *decimalPlaces.
250     virtual double parseToDoubleWithDecimalPlaces(const String&, double defaultValue, unsigned* decimalPlaces) const;
251
252     // Parses the specified string for this InputType, and returns true if it
253     // is successfully parsed. An instance pointed by the DateComponents*
254     // parameter will have parsed values and be modified even if the parsing
255     // fails. The DateComponents* parameter may be 0.
256     virtual bool parseToDateComponents(const String&, DateComponents*) const;
257
258     // Create a string representation of the specified double value for the
259     // input type. If NaN or Infinity is specified, this returns an empty
260     // string. This should not be called for types without valueAsNumber.
261     virtual String serialize(double) const;
262
263 protected:
264     InputType(HTMLInputElement* element) : m_element(element) { }
265     HTMLInputElement* element() const { return m_element; }
266     void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
267     // We can't make this a static const data member because VC++ doesn't like it.
268     static double defaultStepBase() { return 0.0; }
269     Chrome* chrome() const;
270
271 private:
272     // Raw pointer because the HTMLInputElement object owns this InputType object.
273     HTMLInputElement* m_element;
274 };
275
276 namespace InputTypeNames {
277
278 const AtomicString& button();
279 const AtomicString& checkbox();
280 #if ENABLE(INPUT_COLOR)
281 const AtomicString& color();
282 #endif
283 const AtomicString& date();
284 const AtomicString& datetime();
285 const AtomicString& datetimelocal();
286 const AtomicString& email();
287 const AtomicString& file();
288 const AtomicString& hidden();
289 const AtomicString& image();
290 const AtomicString& isindex();
291 const AtomicString& month();
292 const AtomicString& number();
293 const AtomicString& password();
294 const AtomicString& radio();
295 const AtomicString& range();
296 const AtomicString& reset();
297 const AtomicString& search();
298 const AtomicString& submit();
299 const AtomicString& telephone();
300 const AtomicString& text();
301 const AtomicString& time();
302 const AtomicString& url();
303 const AtomicString& week();
304
305 } // namespace WebCore::InputTypeNames
306
307 } // namespace WebCore
308
309 #endif