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/FastAllocBase.h>
37 #include <wtf/Noncopyable.h>
38 #include <wtf/RefPtr.h>
39 #include <wtf/Vector.h>
43 class BeforeTextInsertedEvent;
51 class HTMLFormElement;
52 class HTMLInputElement;
62 typedef int ExceptionCode;
64 struct ClickHandlingState {
65 WTF_MAKE_FAST_ALLOCATED;
69 RefPtr<HTMLInputElement> checkedRadioButton;
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.
76 WTF_MAKE_NONCOPYABLE(InputType); WTF_MAKE_FAST_ALLOCATED;
78 static PassOwnPtr<InputType> create(HTMLInputElement*, const String&);
79 static PassOwnPtr<InputType> createText(HTMLInputElement*);
82 virtual const AtomicString& formControlType() const = 0;
83 virtual bool canChangeFromAnotherType() const;
85 // Type query functions
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.
94 #if ENABLE(INPUT_COLOR)
95 virtual bool isColorControl() const;
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;
114 // Form value functions
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;
121 // DOM property functions
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;
131 // Validation functions
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
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();
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();
190 // Shadow tree handling
192 virtual void createShadowSubtree();
193 virtual void destroyShadowSubtree();
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; }
204 virtual HTMLElement* placeholderElement() const;
206 // Miscellaneous functions
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();
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;
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;
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;
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;
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;
272 // Raw pointer because the HTMLInputElement object owns this InputType object.
273 HTMLInputElement* m_element;
276 namespace InputTypeNames {
278 const AtomicString& button();
279 const AtomicString& checkbox();
280 #if ENABLE(INPUT_COLOR)
281 const AtomicString& color();
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();
305 } // namespace WebCore::InputTypeNames
307 } // namespace WebCore