• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DateComponents;
45 class Event;
46 class FileList;
47 class FormDataList;
48 class HTMLFormElement;
49 class HTMLInputElement;
50 class KeyboardEvent;
51 class MouseEvent;
52 class RenderArena;
53 class RenderObject;
54 class RenderStyle;
55 class WheelEvent;
56 
57 #if PLATFORM(ANDROID) && ENABLE(TOUCH_EVENTS)
58 class TouchEvent;
59 #endif
60 
61 typedef int ExceptionCode;
62 
63 struct ClickHandlingState {
64     WTF_MAKE_FAST_ALLOCATED;
65 public:
66     bool checked;
67     bool indeterminate;
68     RefPtr<HTMLInputElement> checkedRadioButton;
69 };
70 
71 // An InputType object represents the type-specific part of an HTMLInputElement.
72 // Do not expose instances of InputType and classes derived from it to classes
73 // other than HTMLInputElement.
74 class InputType {
75     WTF_MAKE_NONCOPYABLE(InputType); WTF_MAKE_FAST_ALLOCATED;
76 public:
77     static PassOwnPtr<InputType> create(HTMLInputElement*, const String&);
78     static PassOwnPtr<InputType> createText(HTMLInputElement*);
79     virtual ~InputType();
80 
81     virtual const AtomicString& formControlType() const = 0;
82     virtual bool canChangeFromAnotherType() const;
83 
84     // Type query functions
85 
86     // Any time we are using one of these functions it's best to refactor
87     // to add a virtual function to allow the input type object to do the
88     // work instead, or at least make a query function that asks a higher
89     // level question. These functions make the HTMLInputElement class
90     // inflexible because it's harder to add new input types if there is
91     // scattered code with special cases for various types.
92 
93     virtual bool isCheckbox() const;
94     virtual bool isEmailField() const;
95     virtual bool isFileUpload() const;
96     virtual bool isHiddenType() const;
97     virtual bool isImageButton() const;
98     virtual bool isNumberField() const;
99     virtual bool isPasswordField() const;
100     virtual bool isRadioButton() const;
101     virtual bool isRangeControl() const;
102     virtual bool isSearchField() const;
103     virtual bool isSubmitButton() const;
104     virtual bool isTelephoneField() const;
105     virtual bool isTextButton() const;
106     virtual bool isTextField() const;
107     virtual bool isTextType() const;
108     virtual bool isURLField() const;
109 
110     // Form value functions
111 
112     virtual bool saveFormControlState(String&) const;
113     virtual void restoreFormControlState(const String&) const;
114     virtual bool isFormDataAppendable() const;
115     virtual bool appendFormData(FormDataList&, bool multipart) const;
116 
117     // DOM property functions
118 
119     virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
120     virtual String fallbackValue(); // Checked last, if both internal storage and value attribute are missing.
121     virtual String defaultValue(); // Checked after even fallbackValue, only when the valueWithDefault function is called.
122     virtual double valueAsDate() const;
123     virtual void setValueAsDate(double, ExceptionCode&) const;
124     virtual double valueAsNumber() const;
125     virtual void setValueAsNumber(double, ExceptionCode&) const;
126 
127     // Validation functions
128 
129     virtual bool supportsValidation() const;
130     virtual bool typeMismatchFor(const String&) const;
131     // Type check for the current input value. We do nothing for some types
132     // though typeMismatchFor() does something for them because of value
133     // sanitization.
134     virtual bool typeMismatch() const;
135     virtual bool supportsRequired() const;
136     virtual bool valueMissing(const String&) const;
137     virtual bool patternMismatch(const String&) const;
138     virtual bool rangeUnderflow(const String&) const;
139     virtual bool rangeOverflow(const String&) const;
140     virtual bool supportsRangeLimitation() const;
141     virtual double defaultValueForStepUp() const;
142     virtual double minimum() const;
143     virtual double maximum() const;
144     virtual bool stepMismatch(const String&, double step) const;
145     virtual double stepBase() const;
146     virtual double stepBaseWithDecimalPlaces(unsigned*) const;
147     virtual double defaultStep() const;
148     virtual double stepScaleFactor() const;
149     virtual bool parsedStepValueShouldBeInteger() const;
150     virtual bool scaledStepValueShouldBeInteger() const;
151     virtual double acceptableError(double) const;
152     virtual String typeMismatchText() const;
153     virtual String valueMissingText() const;
154     virtual bool canSetStringValue() const;
155     virtual String visibleValue() const;
156     virtual String convertFromVisibleValue(const String&) const;
157     virtual bool isAcceptableValue(const String&);
158     // Returing the null string means "use the default value."
159     virtual String sanitizeValue(const String&);
160     virtual bool hasUnacceptableValue();
161 
162     // Event handlers
163 
164     virtual void handleClickEvent(MouseEvent*);
165     virtual void handleMouseDownEvent(MouseEvent*);
166     virtual PassOwnPtr<ClickHandlingState> willDispatchClick();
167     virtual void didDispatchClick(Event*, const ClickHandlingState&);
168     virtual void handleDOMActivateEvent(Event*);
169     virtual void handleKeydownEvent(KeyboardEvent*);
170     virtual void handleKeypressEvent(KeyboardEvent*);
171     virtual void handleKeyupEvent(KeyboardEvent*);
172     virtual void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent*);
173     virtual void handleWheelEvent(WheelEvent*);
174     virtual void forwardEvent(Event*);
175 
176 #if PLATFORM(ANDROID) && ENABLE(TOUCH_EVENTS)
177     virtual void handleTouchStartEvent(TouchEvent*);
178 #endif
179 
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 handleBlurEvent();
186     virtual void accessKeyAction(bool sendToAnyElement);
187     virtual bool canBeSuccessfulSubmitButton();
188 
189 
190     // Shadow tree handling
191 
192     virtual void createShadowSubtree();
193     void destroyShadowSubtree();
194 
195     // Miscellaneous functions
196 
197     virtual bool rendererIsNeeded();
198     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*) const;
199     virtual void attach();
200     virtual void minOrMaxAttributeChanged();
201     virtual void altAttributeChanged();
202     virtual void srcAttributeChanged();
203     virtual void valueChanged();
204     virtual void willMoveToNewOwnerDocument();
205     virtual bool shouldRespectAlignAttribute();
206     virtual FileList* files();
207     // Should return true if the corresponding renderer for a type can display a suggested value.
208     virtual bool canSetSuggestedValue();
209     virtual bool shouldSendChangeEventAfterCheckedChanged();
210     virtual bool canSetValue(const String&);
211     virtual bool storesValueSeparateFromAttribute();
212     virtual void setFileList(const Vector<String>& paths);
213     virtual bool shouldResetOnDocumentActivation();
214     virtual bool shouldRespectListAttribute();
215     virtual bool shouldRespectSpeechAttribute();
216     virtual bool isEnumeratable();
217     virtual bool isCheckable();
218     virtual bool hasSpinButton();
219     virtual bool shouldRespectHeightAndWidthAttributes();
220 
221     // Parses the specified string for the type, and return
222     // the double value for the parsing result if the parsing
223     // succeeds; Returns defaultValue otherwise. This function can
224     // return NaN or Infinity only if defaultValue is NaN or Infinity.
225     virtual double parseToDouble(const String&, double defaultValue) const;
226 
227     // Parses the specified string for the type as parseToDouble() does.
228     // In addition, it stores the number of digits after the decimal point
229     // into *decimalPlaces.
230     virtual double parseToDoubleWithDecimalPlaces(const String&, double defaultValue, unsigned* decimalPlaces) const;
231 
232     // Parses the specified string for this InputType, and returns true if it
233     // is successfully parsed. An instance pointed by the DateComponents*
234     // parameter will have parsed values and be modified even if the parsing
235     // fails. The DateComponents* parameter may be 0.
236     virtual bool parseToDateComponents(const String&, DateComponents*) const;
237 
238     // Create a string representation of the specified double value for the
239     // input type. If NaN or Infinity is specified, this returns an empty
240     // string. This should not be called for types without valueAsNumber.
241     virtual String serialize(double) const;
242 
243 protected:
InputType(HTMLInputElement * element)244     InputType(HTMLInputElement* element) : m_element(element) { }
element()245     HTMLInputElement* element() const { return m_element; }
246     void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
247     // We can't make this a static const data member because VC++ doesn't like it.
defaultStepBase()248     static double defaultStepBase() { return 0.0; }
249 
250 private:
251     // Raw pointer because the HTMLInputElement object owns this InputType object.
252     HTMLInputElement* m_element;
253 };
254 
255 namespace InputTypeNames {
256 
257 const AtomicString& button();
258 const AtomicString& checkbox();
259 const AtomicString& color();
260 const AtomicString& date();
261 const AtomicString& datetime();
262 const AtomicString& datetimelocal();
263 const AtomicString& email();
264 const AtomicString& file();
265 const AtomicString& hidden();
266 const AtomicString& image();
267 const AtomicString& isindex();
268 const AtomicString& month();
269 const AtomicString& number();
270 const AtomicString& password();
271 const AtomicString& radio();
272 const AtomicString& range();
273 const AtomicString& reset();
274 const AtomicString& search();
275 const AtomicString& submit();
276 const AtomicString& telephone();
277 const AtomicString& text();
278 const AtomicString& time();
279 const AtomicString& url();
280 const AtomicString& week();
281 
282 } // namespace WebCore::InputTypeNames
283 
284 } // namespace WebCore
285 
286 #endif
287