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.
6 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef HTMLInputElement_h
26 #define HTMLInputElement_h
28 #include "core/html/HTMLTextFormControlElement.h"
29 #include "core/html/forms/StepRange.h"
30 #include "platform/FileChooser.h"
37 class HTMLDataListElement;
38 class HTMLImageLoader;
39 class HTMLOptionElement;
43 class ListAttributeTargetObserver;
44 class RadioButtonGroupScope;
45 struct DateTimeChooserParameters;
47 class HTMLInputElement : public HTMLTextFormControlElement {
49 static PassRefPtrWillBeRawPtr<HTMLInputElement> create(Document&, HTMLFormElement*, bool createdByParser);
50 virtual ~HTMLInputElement();
51 virtual void trace(Visitor*) OVERRIDE;
53 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitspeechchange);
55 virtual bool shouldAutocomplete() const OVERRIDE FINAL;
58 virtual bool hasBadInput() const OVERRIDE FINAL;
59 virtual bool patternMismatch() const OVERRIDE FINAL;
60 virtual bool rangeUnderflow() const OVERRIDE FINAL;
61 virtual bool rangeOverflow() const OVERRIDE FINAL;
62 virtual bool stepMismatch() const OVERRIDE FINAL;
63 virtual bool tooLong() const OVERRIDE FINAL;
64 virtual bool typeMismatch() const OVERRIDE FINAL;
65 virtual bool valueMissing() const OVERRIDE FINAL;
66 virtual String validationMessage() const OVERRIDE FINAL;
68 // Returns the minimum value for type=date, number, or range. Don't call this for other types.
69 double minimum() const;
70 // Returns the maximum value for type=date, number, or range. Don't call this for other types.
71 // This always returns a value which is >= minimum().
72 double maximum() const;
73 // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
74 // Returns false if there is no "allowed value step."
75 bool getAllowedValueStep(Decimal*) const;
76 StepRange createStepRange(AnyStepHandling) const;
78 Decimal findClosestTickMarkValue(const Decimal&);
80 // Implementations of HTMLInputElement::stepUp() and stepDown().
81 void stepUp(int, ExceptionState&);
82 void stepDown(int, ExceptionState&);
83 void stepUp(ExceptionState& exceptionState) { stepUp(1, exceptionState); }
84 void stepDown(ExceptionState& exceptionState) { stepDown(1, exceptionState); }
85 // stepUp()/stepDown() for user-interaction.
86 bool isSteppable() const;
88 bool isTextButton() const;
90 bool isRadioButton() const;
91 bool isTextField() const;
92 bool isSearchField() const;
93 bool isInputTypeHidden() const;
94 bool isPasswordField() const;
95 bool isCheckbox() const;
96 bool isRangeControl() const;
98 // FIXME: It's highly likely that any call site calling this function should instead
99 // be using a different one. Many input elements behave like text fields, and in addition
100 // any unknown input type is treated as text. Consider, for example, isTextField or
101 // isTextField && !isPasswordField.
104 bool isEmailField() const;
105 bool isFileUpload() const;
106 bool isImageButton() const;
107 bool isNumberField() const;
108 bool isTelephoneField() const;
109 bool isURLField() const;
110 bool isDateField() const;
111 bool isDateTimeLocalField() const;
112 bool isMonthField() const;
113 bool isTimeField() const;
114 bool isWeekField() const;
116 HTMLElement* passwordGeneratorButtonElement() const;
118 bool checked() const { return m_isChecked; }
119 void setChecked(bool, TextFieldEventBehavior = DispatchNoEvent);
121 // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
122 bool indeterminate() const { return m_isIndeterminate; }
123 void setIndeterminate(bool);
124 // shouldAppearChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
125 bool shouldAppearChecked() const;
126 virtual bool shouldAppearIndeterminate() const OVERRIDE;
129 bool sizeShouldIncludeDecoration(int& preferredSize) const;
131 void setType(const AtomicString&);
133 virtual String value() const OVERRIDE;
134 void setValue(const String&, ExceptionState&, TextFieldEventBehavior = DispatchNoEvent);
135 void setValue(const String&, TextFieldEventBehavior = DispatchNoEvent);
136 void setValueForUser(const String&);
137 // Checks if the specified string would be a valid value.
138 // We should not call this for types with no string value such as CHECKBOX and RADIO.
139 bool isValidValue(const String&) const;
140 bool hasDirtyValue() const { return !m_valueIfDirty.isNull(); };
142 String sanitizeValue(const String&) const;
144 String localizeValue(const String&) const;
146 const String& suggestedValue() const;
147 void setSuggestedValue(const String&);
149 void setEditingValue(const String&);
151 double valueAsDate(bool& isNull) const;
152 void setValueAsDate(double, ExceptionState&);
154 double valueAsNumber() const;
155 void setValueAsNumber(double, ExceptionState&, TextFieldEventBehavior = DispatchNoEvent);
157 String valueWithDefault() const;
159 void setValueFromRenderer(const String&);
161 int selectionStartForBinding(ExceptionState&) const;
162 int selectionEndForBinding(ExceptionState&) const;
163 String selectionDirectionForBinding(ExceptionState&) const;
164 void setSelectionStartForBinding(int, ExceptionState&);
165 void setSelectionEndForBinding(int, ExceptionState&);
166 void setSelectionDirectionForBinding(const String&, ExceptionState&);
167 void setSelectionRangeForBinding(int start, int end, ExceptionState&);
168 void setSelectionRangeForBinding(int start, int end, const String& direction, ExceptionState&);
170 virtual bool rendererIsNeeded(const RenderStyle&) OVERRIDE FINAL;
171 virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
172 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE FINAL;
173 virtual void updateFocusAppearance(bool restorePreviousSelection) OVERRIDE FINAL;
175 // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
176 // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
177 // the private virtual method.
178 virtual bool isActivatedSubmit() const OVERRIDE FINAL;
179 virtual void setActivatedSubmit(bool flag) OVERRIDE FINAL;
181 String altText() const;
183 int maxResults() const { return m_maxResults; }
185 const AtomicString& defaultValue() const;
186 void setDefaultValue(const AtomicString&);
188 Vector<String> acceptMIMETypes();
189 Vector<String> acceptFileExtensions();
190 const AtomicString& alt() const;
192 void setSize(unsigned);
193 void setSize(unsigned, ExceptionState&);
197 int maxLength() const;
198 void setMaxLength(int, ExceptionState&);
200 bool multiple() const;
202 FileList* files() const;
203 void setFiles(PassRefPtrWillBeRawPtr<FileList>);
205 // Returns true if the given DragData has more than one dropped files.
206 bool receiveDroppedFiles(const DragData*);
208 String droppedFileSystemId();
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);
217 void updateClearButtonVisibility();
219 virtual bool willRespondToMouseClickEvents() OVERRIDE;
221 HTMLElement* list() const;
222 HTMLDataListElement* dataList() const;
223 bool hasValidDataListOptions() const;
224 void listAttributeTargetChanged();
226 HTMLInputElement* checkedRadioButtonForGroup() const;
227 bool isInRequiredRadioButtonGroup();
229 // Functions for InputType classes.
230 void setValueInternal(const String&, TextFieldEventBehavior);
231 bool valueAttributeWasUpdatedAfterParsing() const { return m_valueAttributeWasUpdatedAfterParsing; }
233 bool needsToUpdateViewValue() const { return m_needsToUpdateViewValue; }
234 virtual void setInnerTextValue(const String&) OVERRIDE;
236 void cacheSelectionInResponseToSetValue(int caretOffset) { cacheSelection(caretOffset, caretOffset, SelectionHasNoDirection); }
238 // For test purposes.
239 void selectColorInColorChooser(const Color&);
241 String defaultToolTip() const;
243 #if ENABLE(MEDIA_CAPTURE)
244 bool capture() const;
247 static const int maximumLength;
249 unsigned height() const;
250 unsigned width() const;
251 void setHeight(unsigned);
252 void setWidth(unsigned);
254 virtual void blur() OVERRIDE FINAL;
257 virtual const AtomicString& name() const OVERRIDE FINAL;
262 static Vector<FileChooserFileInfo> filesFromFileInputFormControlState(const FormControlState&);
264 virtual bool matchesReadOnlyPseudoClass() const OVERRIDE FINAL;
265 virtual bool matchesReadWritePseudoClass() const OVERRIDE FINAL;
266 virtual void setRangeText(const String& replacement, ExceptionState&) OVERRIDE FINAL;
267 virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionState&) OVERRIDE FINAL;
269 bool hasImageLoader() const { return m_imageLoader; }
270 HTMLImageLoader* imageLoader();
272 bool setupDateTimeChooserParameters(DateTimeChooserParameters&);
274 bool supportsInputModeAttribute() const;
276 void setShouldRevealPassword(bool value);
277 bool shouldRevealPassword() const { return m_shouldRevealPassword; }
280 HTMLInputElement(Document&, HTMLFormElement*, bool createdByParser);
282 virtual void defaultEventHandler(Event*) OVERRIDE;
285 enum AutoCompleteSetting { Uninitialized, On, Off };
287 virtual void didAddUserAgentShadowRoot(ShadowRoot&) OVERRIDE FINAL;
288 virtual void willAddFirstAuthorShadowRoot() OVERRIDE FINAL;
290 virtual void willChangeForm() OVERRIDE FINAL;
291 virtual void didChangeForm() OVERRIDE FINAL;
292 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
293 virtual void removedFrom(ContainerNode*) OVERRIDE FINAL;
294 virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE FINAL;
295 virtual void removeAllEventListeners() OVERRIDE FINAL;
297 virtual bool hasCustomFocusLogic() const OVERRIDE FINAL;
298 virtual bool isKeyboardFocusable() const OVERRIDE FINAL;
299 virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE FINAL;
300 virtual bool isEnumeratable() const OVERRIDE FINAL;
301 virtual bool isInteractiveContent() const OVERRIDE FINAL;
302 virtual bool supportLabels() const OVERRIDE FINAL;
303 virtual bool shouldUseInputMethod() OVERRIDE FINAL;
305 virtual bool isTextFormControl() const OVERRIDE FINAL { return isTextField(); }
307 virtual bool canTriggerImplicitSubmission() const OVERRIDE FINAL { return isTextField(); }
309 virtual const AtomicString& formControlType() const OVERRIDE FINAL;
311 virtual bool shouldSaveAndRestoreFormControlState() const OVERRIDE FINAL;
312 virtual FormControlState saveFormControlState() const OVERRIDE FINAL;
313 virtual void restoreFormControlState(const FormControlState&) OVERRIDE FINAL;
315 virtual bool canStartSelection() const OVERRIDE FINAL;
317 virtual void accessKeyAction(bool sendMouseEvents) OVERRIDE FINAL;
319 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
320 virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE FINAL;
321 virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) OVERRIDE FINAL;
322 virtual void finishParsingChildren() OVERRIDE FINAL;
324 virtual void copyNonAttributePropertiesFromElement(const Element&) OVERRIDE FINAL;
326 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE FINAL;
328 virtual bool appendFormData(FormDataList&, bool) OVERRIDE FINAL;
329 virtual String resultForDialogSubmit() OVERRIDE FINAL;
331 virtual bool canBeSuccessfulSubmitButton() const OVERRIDE FINAL;
333 virtual void resetImpl() OVERRIDE FINAL;
334 virtual bool supportsAutofocus() const OVERRIDE FINAL;
336 virtual void* preDispatchEventHandler(Event*) OVERRIDE FINAL;
337 virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch) OVERRIDE FINAL;
339 virtual bool isURLAttribute(const Attribute&) const OVERRIDE FINAL;
340 virtual bool hasLegalLinkAttribute(const QualifiedName&) const OVERRIDE FINAL;
341 virtual const QualifiedName& subResourceAttributeName() const OVERRIDE FINAL;
342 virtual bool isInRange() const OVERRIDE FINAL;
343 virtual bool isOutOfRange() const OVERRIDE FINAL;
345 bool isTextType() const;
346 bool tooLong(const String&, NeedsToCheckDirtyFlag) const;
348 virtual bool supportsPlaceholder() const OVERRIDE FINAL;
349 virtual void updatePlaceholderText() OVERRIDE FINAL;
350 virtual bool isEmptyValue() const OVERRIDE FINAL { return innerTextValue().isEmpty(); }
351 virtual bool isEmptySuggestedValue() const OVERRIDE FINAL { return suggestedValue().isEmpty(); }
352 virtual void handleFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE FINAL;
353 virtual void handleBlurEvent() OVERRIDE FINAL;
355 virtual bool isOptionalFormControl() const OVERRIDE FINAL { return !isRequiredFormControl(); }
356 virtual bool isRequiredFormControl() const OVERRIDE FINAL;
357 virtual bool recalcWillValidate() const OVERRIDE FINAL;
358 virtual void requiredAttributeChanged() OVERRIDE FINAL;
362 virtual void subtreeHasChanged() OVERRIDE FINAL;
364 void setListAttributeTargetObserver(PassOwnPtrWillBeRawPtr<ListAttributeTargetObserver>);
365 void resetListAttributeTargetObserver();
366 void parseMaxLengthAttribute(const AtomicString&);
367 void updateValueIfNeeded();
369 // Returns null if this isn't associated with any radio button group.
370 RadioButtonGroupScope* radioButtonGroupScope() const;
371 void addToRadioButtonGroup();
372 void removeFromRadioButtonGroup();
373 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
374 virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE;
377 virtual bool shouldDispatchFormControlChangeEvent(String&, String&) OVERRIDE;
380 String m_valueIfDirty;
381 String m_suggestedValue;
385 bool m_isChecked : 1;
386 bool m_reflectsCheckedAttribute : 1;
387 bool m_isIndeterminate : 1;
388 bool m_isActivatedSubmit : 1;
389 unsigned m_autocomplete : 2; // AutoCompleteSetting
390 bool m_hasNonEmptyList : 1;
391 bool m_stateRestored : 1;
392 bool m_parsingInProgress : 1;
393 bool m_valueAttributeWasUpdatedAfterParsing : 1;
394 bool m_canReceiveDroppedFiles : 1;
395 bool m_hasTouchEventHandler : 1;
396 bool m_shouldRevealPassword : 1;
397 bool m_needsToUpdateViewValue : 1;
398 RefPtrWillBeMember<InputType> m_inputType;
399 RefPtrWillBeMember<InputTypeView> m_inputTypeView;
400 // The ImageLoader must be owned by this element because the loader code assumes
401 // that it lives as long as its owning element lives. If we move the loader into
402 // the ImageInput object we may delete the loader while this element lives on.
403 OwnPtr<HTMLImageLoader> m_imageLoader;
404 OwnPtrWillBeMember<ListAttributeTargetObserver> m_listAttributeTargetObserver;