2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
35 #include <wtf/Forward.h>
36 #include <wtf/Noncopyable.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/Vector.h>
42 class BeforeTextInsertedEvent;
47 class HTMLFormElement;
48 class HTMLInputElement;
56 typedef int ExceptionCode;
58 struct ClickHandlingState {
61 RefPtr<HTMLInputElement> checkedRadioButton;
63 WTF_MAKE_FAST_ALLOCATED
66 // An InputType object represents the type-specific part of an HTMLInputElement.
67 // Do not expose instances of InputType and classes derived from it to classes
68 // other than HTMLInputElement.
69 class InputType : public Noncopyable {
71 static PassOwnPtr<InputType> create(HTMLInputElement*, const String&);
72 static PassOwnPtr<InputType> createText(HTMLInputElement*);
75 virtual const AtomicString& formControlType() const = 0;
76 virtual bool canChangeFromAnotherType() const;
78 // Type query functions
80 // Any time we are using one of these functions it's best to refactor
81 // to add a virtual function to allow the input type object to do the
82 // work instead, or at least make a query function that asks a higher
83 // level question. These functions make the HTMLInputElement class
84 // inflexible because it's harder to add new input types if there is
85 // scattered code with special cases for various types.
87 virtual bool isCheckbox() const;
88 virtual bool isEmailField() const;
89 virtual bool isFileUpload() const;
90 virtual bool isHiddenType() const;
91 virtual bool isImageButton() const;
92 virtual bool isNumberField() const;
93 virtual bool isPasswordField() const;
94 virtual bool isRadioButton() const;
95 virtual bool isRangeControl() const;
96 virtual bool isSearchField() const;
97 virtual bool isSubmitButton() const;
98 virtual bool isTelephoneField() const;
99 virtual bool isTextButton() const;
100 virtual bool isTextField() const;
101 virtual bool isTextType() const;
102 virtual bool isURLField() const;
104 // Form value functions
106 virtual bool saveFormControlState(String&) const;
107 virtual void restoreFormControlState(const String&) const;
108 virtual bool isFormDataAppendable() const;
109 virtual bool appendFormData(FormDataList&, bool multipart) const;
111 // DOM property functions
113 virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
114 virtual String fallbackValue(); // Checked last, if both internal storage and value attribute are missing.
115 virtual String defaultValue(); // Checked after even fallbackValue, only when the valueWithDefault function is called.
116 virtual double valueAsDate() const;
117 virtual void setValueAsDate(double, ExceptionCode&) const;
118 virtual double valueAsNumber() const;
119 virtual void setValueAsNumber(double, ExceptionCode&) const;
121 // Validation functions
123 virtual bool supportsValidation() const;
124 virtual bool typeMismatchFor(const String&) const;
125 // Type check for the current input value. We do nothing for some types
126 // though typeMismatchFor() does something for them because of value
128 virtual bool typeMismatch() const;
129 virtual bool supportsRequired() const;
130 virtual bool valueMissing(const String&) const;
131 virtual bool patternMismatch(const String&) const;
132 virtual bool rangeUnderflow(const String&) const;
133 virtual bool rangeOverflow(const String&) const;
134 virtual bool supportsRangeLimitation() const;
135 virtual double defaultValueForStepUp() const;
136 virtual double minimum() const;
137 virtual double maximum() const;
138 virtual bool stepMismatch(const String&, double step) const;
139 virtual double stepBase() const;
140 virtual double stepBaseWithDecimalPlaces(unsigned*) const;
141 virtual double defaultStep() const;
142 virtual double stepScaleFactor() const;
143 virtual bool parsedStepValueShouldBeInteger() const;
144 virtual bool scaledStepValueShouldBeInteger() const;
145 virtual double acceptableError(double) const;
146 virtual String typeMismatchText() const;
147 virtual String valueMissingText() const;
148 virtual bool canSetStringValue() const;
149 virtual bool isAcceptableValue(const String&);
150 virtual String sanitizeValue(const String&);
151 virtual bool hasUnacceptableValue();
155 virtual void handleClickEvent(MouseEvent*);
156 virtual PassOwnPtr<ClickHandlingState> willDispatchClick();
157 virtual void didDispatchClick(Event*, const ClickHandlingState&);
158 virtual void handleDOMActivateEvent(Event*);
159 virtual void handleKeydownEvent(KeyboardEvent*);
160 virtual void handleKeypressEvent(KeyboardEvent*);
161 virtual void handleKeyupEvent(KeyboardEvent*);
162 virtual void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent*);
163 virtual void handleWheelEvent(WheelEvent*);
164 virtual void forwardEvent(Event*);
165 // Helpers for event handlers.
166 virtual bool shouldSubmitImplicitly(Event*);
167 virtual PassRefPtr<HTMLFormElement> formForSubmission() const;
168 virtual bool isKeyboardFocusable() const;
169 virtual bool shouldUseInputMethod() const;
170 virtual void handleBlurEvent();
171 virtual void accessKeyAction(bool sendToAnyElement);
172 virtual bool canBeSuccessfulSubmitButton();
174 // Miscellaneous functions
176 virtual bool rendererIsNeeded();
177 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*) const;
178 virtual void attach();
179 virtual void minOrMaxAttributeChanged();
180 virtual void altAttributeChanged();
181 virtual void srcAttributeChanged();
182 virtual void willMoveToNewOwnerDocument();
183 virtual bool shouldRespectAlignAttribute();
184 virtual FileList* files();
185 virtual bool canSetSuggestedValue();
186 virtual bool shouldSendChangeEventAfterCheckedChanged();
187 virtual bool canSetValue(const String&);
188 virtual bool storesValueSeparateFromAttribute();
189 virtual void setFileList(const Vector<String>& paths);
190 virtual bool shouldResetOnDocumentActivation();
191 virtual bool shouldRespectListAttribute();
192 virtual bool shouldRespectSpeechAttribute();
193 virtual bool isEnumeratable();
194 virtual bool isCheckable();
195 virtual bool hasSpinButton();
196 virtual bool shouldRespectHeightAndWidthAttributes();
198 // Parses the specified string for the type, and return
199 // the double value for the parsing result if the parsing
200 // succeeds; Returns defaultValue otherwise. This function can
201 // return NaN or Infinity only if defaultValue is NaN or Infinity.
202 virtual double parseToDouble(const String&, double defaultValue) const;
204 // Parses the specified string for the type as parseToDouble() does.
205 // In addition, it stores the number of digits after the decimal point
206 // into *decimalPlaces.
207 virtual double parseToDoubleWithDecimalPlaces(const String&, double defaultValue, unsigned* decimalPlaces) const;
209 // Parses the specified string for this InputType, and returns true if it
210 // is successfully parsed. An instance pointed by the DateComponents*
211 // parameter will have parsed values and be modified even if the parsing
212 // fails. The DateComponents* parameter may be 0.
213 virtual bool parseToDateComponents(const String&, DateComponents*) const;
215 // Create a string representation of the specified double value for the
216 // input type. If NaN or Infinity is specified, this returns an empty
217 // string. This should not be called for types without valueAsNumber.
218 virtual String serialize(double) const;
221 InputType(HTMLInputElement* element) : m_element(element) { }
222 HTMLInputElement* element() const { return m_element; }
223 void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
224 // We can't make this a static const data member because VC++ doesn't like it.
225 static double defaultStepBase() { return 0.0; }
228 // Raw pointer because the HTMLInputElement object owns this InputType object.
229 HTMLInputElement* m_element;
232 namespace InputTypeNames {
234 const AtomicString& button();
235 const AtomicString& checkbox();
236 const AtomicString& color();
237 const AtomicString& date();
238 const AtomicString& datetime();
239 const AtomicString& datetimelocal();
240 const AtomicString& email();
241 const AtomicString& file();
242 const AtomicString& hidden();
243 const AtomicString& image();
244 const AtomicString& isindex();
245 const AtomicString& month();
246 const AtomicString& number();
247 const AtomicString& password();
248 const AtomicString& radio();
249 const AtomicString& range();
250 const AtomicString& reset();
251 const AtomicString& search();
252 const AtomicString& submit();
253 const AtomicString& telephone();
254 const AtomicString& text();
255 const AtomicString& time();
256 const AtomicString& url();
257 const AtomicString& week();
259 } // namespace WebCore::InputTypeNames
261 } // namespace WebCore