2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef HTMLInputElement_h
25 #define HTMLInputElement_h
27 #include "HTMLTextFormControlElement.h"
32 class HTMLDataListElement;
33 class HTMLOptionElement;
38 class HTMLInputElement : public HTMLTextFormControlElement {
40 static PassRefPtr<HTMLInputElement> create(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
41 virtual ~HTMLInputElement();
43 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitspeechchange);
45 virtual HTMLInputElement* toInputElement() { return this; }
47 virtual bool shouldAutocomplete() const;
50 bool typeMismatch() const;
51 // valueMissing() ignores the specified string value for CHECKBOX and RADIO.
52 bool valueMissing(const String&) const;
53 bool patternMismatch(const String&) const;
54 bool tooLong(const String&, NeedsToCheckDirtyFlag) const;
55 bool rangeUnderflow(const String&) const;
56 bool rangeOverflow(const String&) const;
57 // Returns the minimum value for type=date, number, or range. Don't call this for other types.
58 double minimum() const;
59 // Returns the maximum value for type=date, number, or range. Don't call this for other types.
60 // This always returns a value which is >= minimum().
61 double maximum() const;
62 // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
63 // Returns false if there is no "allowed value step."
64 bool getAllowedValueStep(double*) const;
67 bool stepMismatch(const String&) const;
68 String minimumString() const;
69 String maximumString() const;
70 String stepBaseString() const;
71 String stepString() const;
72 String typeMismatchText() const;
73 String valueMissingText() const;
75 // Implementations of HTMLInputElement::stepUp() and stepDown().
76 void stepUp(int, ExceptionCode&);
77 void stepDown(int, ExceptionCode&);
78 void stepUp(ExceptionCode& ec) { stepUp(1, ec); }
79 void stepDown(ExceptionCode& ec) { stepDown(1, ec); }
80 // stepUp()/stepDown() for user-interaction.
81 bool isSteppable() const;
82 void stepUpFromRenderer(int);
84 bool isTextButton() const;
86 bool isRadioButton() const;
87 bool isTextField() const;
88 bool isSearchField() const;
89 bool isInputTypeHidden() const;
90 bool isPasswordField() const;
91 bool isCheckbox() const;
92 bool isRangeControl() const;
94 // FIXME: It's highly likely that any call site calling this function should instead
95 // be using a different one. Many input elements behave like text fields, and in addition
96 // any unknown input type is treated as text. Consider, for example, isTextField or
97 // isTextField && !isPasswordField.
100 bool isEmailField() const;
101 bool isFileUpload() const;
102 bool isImageButton() const;
103 bool isNumberField() const;
104 bool isSubmitButton() const;
105 bool isTelephoneField() const;
106 bool isURLField() const;
108 #if ENABLE(INPUT_SPEECH)
109 bool isSpeechEnabled() const;
112 HTMLElement* containerElement() const;
113 virtual HTMLElement* innerTextElement() const;
114 HTMLElement* innerBlockElement() const;
115 HTMLElement* innerSpinButtonElement() const;
116 HTMLElement* resultsButtonElement() const;
117 HTMLElement* cancelButtonElement() const;
118 #if ENABLE(INPUT_SPEECH)
119 HTMLElement* speechButtonElement() const;
121 virtual HTMLElement* placeholderElement() const;
123 bool checked() const { return m_isChecked; }
124 void setChecked(bool, TextFieldEventBehavior = DispatchNoEvent);
126 // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
127 bool indeterminate() const { return m_isIndeterminate; }
128 void setIndeterminate(bool);
129 // shouldAppearChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
130 bool shouldAppearChecked() const;
131 virtual bool isIndeterminate() const;
134 bool sizeShouldIncludeDecoration(int& preferredSize) const;
136 void setType(const String&);
138 String value() const;
139 void setValue(const String&, TextFieldEventBehavior = DispatchNoEvent);
140 void setValueForUser(const String&);
141 // Checks if the specified string would be a valid value.
142 // We should not call this for types with no string value such as CHECKBOX and RADIO.
143 bool isValidValue(const String&) const;
144 bool hasDirtyValue() const { return !m_valueIfDirty.isNull(); };
146 String sanitizeValue(const String&) const;
148 void updateInnerTextValue();
150 // The value which is drawn by a renderer.
151 String visibleValue() const;
152 String convertFromVisibleValue(const String&) const;
153 // Returns true if the specified string can be set as the value of HTMLInputElement.
154 bool isAcceptableValue(const String&) const;
156 const String& suggestedValue() const;
157 void setSuggestedValue(const String&);
159 double valueAsDate() const;
160 void setValueAsDate(double, ExceptionCode&);
162 double valueAsNumber() const;
163 void setValueAsNumber(double, ExceptionCode&, TextFieldEventBehavior = DispatchNoEvent);
165 virtual String placeholder() const;
166 virtual void setPlaceholder(const String&);
168 String valueWithDefault() const;
170 void setValueFromRenderer(const String&);
172 bool canHaveSelection() const;
174 virtual bool rendererIsNeeded(const NodeRenderingContext&);
175 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
176 virtual void detach();
178 // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
179 // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
180 // the private virtual method.
181 virtual bool isActivatedSubmit() const;
182 virtual void setActivatedSubmit(bool flag);
184 String altText() const;
186 int maxResults() const { return m_maxResults; }
188 String defaultValue() const;
189 void setDefaultValue(const String&);
191 Vector<String> acceptMIMETypes();
192 String accept() const;
195 void setSize(unsigned);
199 virtual int maxLength() const;
200 void setMaxLength(int, ExceptionCode&);
202 bool multiple() const;
204 bool isAutofilled() const { return m_isAutofilled; }
205 void setAutofilled(bool = true);
208 void receiveDroppedFiles(const Vector<String>&);
210 // These functions are used for rendering the input active during a
211 // drag-and-drop operation.
212 bool canReceiveDroppedFiles() const;
213 void setCanReceiveDroppedFiles(bool);
215 void addSearchResult();
217 bool searchEventsShouldBeDispatched() const;
220 HTMLElement* list() const;
221 HTMLOptionElement* selectedOption() const;
224 // These functions are public so they can be used in InputType classes.
225 // Otherwise, they would be private.
226 CheckedRadioButtons& checkedRadioButtons() const;
227 void updateCheckedRadioButtons();
228 void setValueInternal(const String&, TextFieldEventBehavior);
230 void cacheSelectionInResponseToSetValue(int caretOffset) { cacheSelection(caretOffset, caretOffset, SelectionHasNoDirection); }
232 #if ENABLE(INPUT_COLOR)
233 // For test purposes.
234 void selectColorInColorChooser(const Color&);
237 String defaultToolTip() const;
239 static const int maximumLength;
242 HTMLInputElement(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
243 void createShadowSubtree();
244 virtual void defaultEventHandler(Event*);
247 enum AutoCompleteSetting { Uninitialized, On, Off };
248 enum AnyStepHandling { RejectAny, AnyIsDefaultStep };
250 virtual void willChangeForm() OVERRIDE;
251 virtual void didChangeForm() OVERRIDE;
252 virtual void insertedIntoDocument() OVERRIDE;
253 virtual void removedFromDocument() OVERRIDE;
254 virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
256 virtual bool isKeyboardFocusable(KeyboardEvent*) const;
257 virtual bool isMouseFocusable() const;
258 virtual bool isEnumeratable() const;
259 virtual bool supportLabels() const OVERRIDE;
260 virtual void updateFocusAppearance(bool restorePreviousSelection);
261 virtual void aboutToUnload();
262 virtual bool shouldUseInputMethod();
264 virtual const AtomicString& formControlName() const;
266 virtual bool isTextFormControl() const { return isTextField(); }
268 virtual bool canTriggerImplicitSubmission() const { return isTextField(); }
270 virtual const AtomicString& formControlType() const;
272 virtual bool saveFormControlState(String& value) const;
273 virtual void restoreFormControlState(const String&);
275 virtual bool canStartSelection() const;
277 virtual void accessKeyAction(bool sendMouseEvents);
279 virtual void parseAttribute(Attribute*) OVERRIDE;
280 virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
281 virtual void collectStyleForAttribute(Attribute*, StylePropertySet*) OVERRIDE;
282 virtual void finishParsingChildren();
284 virtual void copyNonAttributeProperties(const Element* source);
286 virtual void attach();
288 virtual bool appendFormData(FormDataList&, bool);
290 virtual bool isSuccessfulSubmitButton() const;
292 virtual void reset();
294 virtual void* preDispatchEventHandler(Event*);
295 virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch);
297 virtual bool isURLAttribute(Attribute*) const;
299 virtual bool hasUnacceptableValue() const;
301 virtual bool isInRange() const;
302 virtual bool isOutOfRange() const;
304 virtual void documentDidResumeFromPageCache();
306 virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;
308 bool needsSuspensionCallback();
309 void registerForSuspensionCallbackIfNeeded();
310 void unregisterForSuspensionCallbackIfNeeded();
312 bool supportsMaxLength() const { return isTextType(); }
313 bool isTextType() const;
315 virtual bool supportsPlaceholder() const;
316 virtual void updatePlaceholderText();
317 virtual bool isEmptyValue() const OVERRIDE { return innerTextValue().isEmpty(); }
318 virtual bool isEmptySuggestedValue() const { return suggestedValue().isEmpty(); }
319 virtual void handleFocusEvent();
320 virtual void handleBlurEvent();
322 virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
323 virtual bool isRequiredFormControl() const;
324 virtual bool recalcWillValidate() const;
325 virtual void requiredAttributeChanged() OVERRIDE;
329 virtual void subtreeHasChanged();
331 bool getAllowedValueStepWithDecimalPlaces(AnyStepHandling, double*, unsigned*) const;
333 // Helper for stepUp()/stepDown(). Adds step value * count to the current value.
334 void applyStep(double count, AnyStepHandling, TextFieldEventBehavior, ExceptionCode&);
335 double alignValueForStep(double value, double step, unsigned currentDecimalPlaces, unsigned stepDecimalPlaces);
338 HTMLDataListElement* dataList() const;
340 void parseMaxLengthAttribute(Attribute*);
341 void updateValueIfNeeded();
344 String m_valueIfDirty;
345 String m_suggestedValue;
349 bool m_isChecked : 1;
350 bool m_reflectsCheckedAttribute : 1;
351 bool m_isIndeterminate : 1;
353 bool m_isActivatedSubmit : 1;
354 unsigned m_autocomplete : 2; // AutoCompleteSetting
355 bool m_isAutofilled : 1;
357 bool m_hasNonEmptyList : 1;
359 bool m_stateRestored : 1;
360 bool m_parsingInProgress : 1;
361 bool m_wasModifiedByUser : 1;
362 bool m_canReceiveDroppedFiles : 1;
363 OwnPtr<InputType> m_inputType;