Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2000 Dirk Mueller (mueller (at) kde.org)
      5  * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
      6  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
      7  *
      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.
     12  *
     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.
     17  *
     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.
     22  *
     23  */
     24 
     25 #ifndef HTMLInputElement_h
     26 #define HTMLInputElement_h
     27 
     28 #include "core/html/HTMLTextFormControlElement.h"
     29 #include "core/html/StepRange.h"
     30 #include "core/platform/FileChooser.h"
     31 
     32 namespace WebCore {
     33 
     34 class CheckedRadioButtons;
     35 class DragData;
     36 class ExceptionState;
     37 class FileList;
     38 class HTMLDataListElement;
     39 class HTMLImageLoader;
     40 class HTMLOptionElement;
     41 class Icon;
     42 class InputType;
     43 class KURL;
     44 class ListAttributeTargetObserver;
     45 struct DateTimeChooserParameters;
     46 
     47 class HTMLInputElement : public HTMLTextFormControlElement {
     48 public:
     49     static PassRefPtr<HTMLInputElement> create(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
     50     virtual ~HTMLInputElement();
     51 
     52     DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitspeechchange);
     53 
     54     virtual bool shouldAutocomplete() const;
     55 
     56     // For ValidityState
     57     virtual bool hasBadInput() const OVERRIDE;
     58     virtual bool patternMismatch() const OVERRIDE;
     59     virtual bool rangeUnderflow() const OVERRIDE;
     60     virtual bool rangeOverflow() const;
     61     virtual bool stepMismatch() const OVERRIDE;
     62     virtual bool tooLong() const OVERRIDE;
     63     virtual bool typeMismatch() const OVERRIDE;
     64     virtual bool valueMissing() const OVERRIDE;
     65     virtual String validationMessage() const OVERRIDE;
     66 
     67     // Returns the minimum value for type=date, number, or range.  Don't call this for other types.
     68     double minimum() const;
     69     // Returns the maximum value for type=date, number, or range.  Don't call this for other types.
     70     // This always returns a value which is >= minimum().
     71     double maximum() const;
     72     // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
     73     // Returns false if there is no "allowed value step."
     74     bool getAllowedValueStep(Decimal*) const;
     75     StepRange createStepRange(AnyStepHandling) const;
     76 
     77     Decimal findClosestTickMarkValue(const Decimal&);
     78 
     79     // Implementations of HTMLInputElement::stepUp() and stepDown().
     80     void stepUp(int, ExceptionState&);
     81     void stepDown(int, ExceptionState&);
     82     void stepUp(ExceptionState& es) { stepUp(1, es); }
     83     void stepDown(ExceptionState& es) { stepDown(1, es); }
     84     // stepUp()/stepDown() for user-interaction.
     85     bool isSteppable() const;
     86 
     87     bool isTextButton() const;
     88 
     89     bool isRadioButton() const;
     90     bool isTextField() const;
     91     bool isSearchField() const;
     92     bool isInputTypeHidden() const;
     93     bool isPasswordField() const;
     94     bool isCheckbox() const;
     95     bool isRangeControl() const;
     96     bool isColorControl() const;
     97 
     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.
    102     bool isText() const;
    103 
    104     bool isEmailField() const;
    105     bool isFileUpload() const;
    106     bool isImageButton() const;
    107     bool isNumberField() const;
    108     bool isSubmitButton() const;
    109     bool isTelephoneField() const;
    110     bool isURLField() const;
    111     bool isDateField() const;
    112     bool isDateTimeLocalField() const;
    113     bool isMonthField() const;
    114     bool isTimeField() const;
    115     bool isWeekField() const;
    116 
    117 #if ENABLE(INPUT_SPEECH)
    118     bool isSpeechEnabled() const;
    119 #endif
    120 
    121     HTMLElement* containerElement() const;
    122     virtual HTMLElement* innerTextElement() const;
    123     HTMLElement* innerBlockElement() const;
    124     HTMLElement* innerSpinButtonElement() const;
    125 #if ENABLE(INPUT_SPEECH)
    126     HTMLElement* speechButtonElement() const;
    127 #endif
    128     HTMLElement* sliderThumbElement() const;
    129     HTMLElement* sliderTrackElement() const;
    130     HTMLElement* passwordGeneratorButtonElement() const;
    131     virtual HTMLElement* placeholderElement() const;
    132 
    133     bool checked() const { return m_isChecked; }
    134     void setChecked(bool, TextFieldEventBehavior = DispatchNoEvent);
    135 
    136     // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
    137     bool indeterminate() const { return m_isIndeterminate; }
    138     void setIndeterminate(bool);
    139     // shouldAppearChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
    140     bool shouldAppearChecked() const;
    141     virtual bool shouldAppearIndeterminate() const OVERRIDE;
    142 
    143     int size() const;
    144     bool sizeShouldIncludeDecoration(int& preferredSize) const;
    145 
    146     void setType(const String&);
    147 
    148     String value() const;
    149     void setValue(const String&, ExceptionState&, TextFieldEventBehavior = DispatchNoEvent);
    150     void setValue(const String&, TextFieldEventBehavior = DispatchNoEvent);
    151     void setValueForUser(const String&);
    152     // Checks if the specified string would be a valid value.
    153     // We should not call this for types with no string value such as CHECKBOX and RADIO.
    154     bool isValidValue(const String&) const;
    155     bool hasDirtyValue() const { return !m_valueIfDirty.isNull(); };
    156 
    157     String sanitizeValue(const String&) const;
    158 
    159     String localizeValue(const String&) const;
    160 
    161     // The value which is drawn by a renderer.
    162     String visibleValue() const;
    163 
    164     const String& suggestedValue() const;
    165     void setSuggestedValue(const String&);
    166 
    167     void setEditingValue(const String&);
    168 
    169     double valueAsDate() const;
    170     void setValueAsDate(double, ExceptionState&);
    171 
    172     double valueAsNumber() const;
    173     void setValueAsNumber(double, ExceptionState&, TextFieldEventBehavior = DispatchNoEvent);
    174 
    175     String valueWithDefault() const;
    176 
    177     void setValueFromRenderer(const String&);
    178 
    179     int selectionStartForBinding(ExceptionState&) const;
    180     int selectionEndForBinding(ExceptionState&) const;
    181     String selectionDirectionForBinding(ExceptionState&) const;
    182     void setSelectionStartForBinding(int, ExceptionState&);
    183     void setSelectionEndForBinding(int, ExceptionState&);
    184     void setSelectionDirectionForBinding(const String&, ExceptionState&);
    185     void setSelectionRangeForBinding(int start, int end, ExceptionState&);
    186     void setSelectionRangeForBinding(int start, int end, const String& direction, ExceptionState&);
    187 
    188     virtual bool rendererIsNeeded(const NodeRenderingContext&);
    189     virtual RenderObject* createRenderer(RenderStyle*);
    190     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
    191 
    192     // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
    193     // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
    194     // the private virtual method.
    195     virtual bool isActivatedSubmit() const;
    196     virtual void setActivatedSubmit(bool flag);
    197 
    198     String altText() const;
    199 
    200     int maxResults() const { return m_maxResults; }
    201 
    202     String defaultValue() const;
    203     void setDefaultValue(const String&);
    204 
    205     Vector<String> acceptMIMETypes();
    206     Vector<String> acceptFileExtensions();
    207     String accept() const;
    208     String alt() const;
    209 
    210     void setSize(unsigned);
    211     void setSize(unsigned, ExceptionState&);
    212 
    213     KURL src() const;
    214 
    215     virtual int maxLength() const;
    216     void setMaxLength(int, ExceptionState&);
    217 
    218     bool multiple() const;
    219 
    220     bool isAutofilled() const { return m_isAutofilled; }
    221     void setAutofilled(bool = true);
    222 
    223     FileList* files();
    224     void setFiles(PassRefPtr<FileList>);
    225 
    226     // Returns true if the given DragData has more than one dropped files.
    227     bool receiveDroppedFiles(const DragData*);
    228 
    229     String droppedFileSystemId();
    230 
    231     Icon* icon() const;
    232     // These functions are used for rendering the input active during a
    233     // drag-and-drop operation.
    234     bool canReceiveDroppedFiles() const;
    235     void setCanReceiveDroppedFiles(bool);
    236 
    237     void onSearch();
    238 
    239     void updateClearButtonVisibility();
    240 
    241     virtual bool willRespondToMouseClickEvents() OVERRIDE;
    242 
    243     HTMLElement* list() const;
    244     HTMLDataListElement* dataList() const;
    245     void listAttributeTargetChanged();
    246 
    247     HTMLInputElement* checkedRadioButtonForGroup() const;
    248     bool isInRequiredRadioButtonGroup();
    249 
    250     // Functions for InputType classes.
    251     void setValueInternal(const String&, TextFieldEventBehavior);
    252     bool valueAttributeWasUpdatedAfterParsing() const { return m_valueAttributeWasUpdatedAfterParsing; }
    253 
    254     void cacheSelectionInResponseToSetValue(int caretOffset) { cacheSelection(caretOffset, caretOffset, SelectionHasNoDirection); }
    255 
    256     // For test purposes.
    257     void selectColorInColorChooser(const Color&);
    258 
    259     String defaultToolTip() const;
    260 
    261 #if ENABLE(MEDIA_CAPTURE)
    262     bool capture() const;
    263 #endif
    264 
    265     static const unsigned maximumLength;
    266 
    267     unsigned height() const;
    268     unsigned width() const;
    269     void setHeight(unsigned);
    270     void setWidth(unsigned);
    271 
    272     virtual void blur() OVERRIDE;
    273     void defaultBlur();
    274 
    275     virtual const AtomicString& name() const OVERRIDE;
    276 
    277     void beginEditing();
    278     void endEditing();
    279 
    280     static Vector<FileChooserFileInfo> filesFromFileInputFormControlState(const FormControlState&);
    281 
    282     virtual bool matchesReadOnlyPseudoClass() const OVERRIDE;
    283     virtual bool matchesReadWritePseudoClass() const OVERRIDE;
    284     virtual void setRangeText(const String& replacement, ExceptionState&) OVERRIDE;
    285     virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionState&) OVERRIDE;
    286 
    287     bool hasImageLoader() const { return m_imageLoader; }
    288     HTMLImageLoader* imageLoader();
    289 
    290     bool setupDateTimeChooserParameters(DateTimeChooserParameters&);
    291 
    292     bool supportsInputModeAttribute() const;
    293 
    294 protected:
    295     HTMLInputElement(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
    296 
    297     virtual void defaultEventHandler(Event*);
    298 
    299 private:
    300     enum AutoCompleteSetting { Uninitialized, On, Off };
    301 
    302     virtual void didAddUserAgentShadowRoot(ShadowRoot*) OVERRIDE;
    303 
    304     virtual void willChangeForm() OVERRIDE;
    305     virtual void didChangeForm() OVERRIDE;
    306     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    307     virtual void removedFrom(ContainerNode*) OVERRIDE;
    308     virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
    309 
    310     virtual bool hasCustomFocusLogic() const OVERRIDE;
    311     virtual bool isKeyboardFocusable() const OVERRIDE;
    312     virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE;
    313     virtual bool isEnumeratable() const;
    314     virtual bool supportLabels() const OVERRIDE;
    315     virtual void updateFocusAppearance(bool restorePreviousSelection);
    316     virtual bool shouldUseInputMethod();
    317 
    318     virtual bool isTextFormControl() const { return isTextField(); }
    319 
    320     virtual bool canTriggerImplicitSubmission() const { return isTextField(); }
    321 
    322     virtual const AtomicString& formControlType() const;
    323 
    324     virtual bool shouldSaveAndRestoreFormControlState() const OVERRIDE;
    325     virtual FormControlState saveFormControlState() const OVERRIDE;
    326     virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
    327 
    328     virtual bool canStartSelection() const;
    329 
    330     virtual void accessKeyAction(bool sendMouseEvents);
    331 
    332     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
    333     virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
    334     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) OVERRIDE;
    335     virtual void finishParsingChildren();
    336 
    337     virtual void copyNonAttributePropertiesFromElement(const Element&);
    338 
    339     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
    340 
    341     virtual bool appendFormData(FormDataList&, bool);
    342 
    343     virtual bool isSuccessfulSubmitButton() const;
    344 
    345     virtual void reset();
    346 
    347     virtual void* preDispatchEventHandler(Event*);
    348     virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch);
    349 
    350     virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
    351     virtual bool isInRange() const;
    352     virtual bool isOutOfRange() const;
    353 
    354     virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;
    355 
    356     bool supportsMaxLength() const { return isTextType(); }
    357     bool isTextType() const;
    358     bool tooLong(const String&, NeedsToCheckDirtyFlag) const;
    359 
    360     virtual bool supportsPlaceholder() const;
    361     virtual void updatePlaceholderText();
    362     virtual bool isEmptyValue() const OVERRIDE { return innerTextValue().isEmpty(); }
    363     virtual bool isEmptySuggestedValue() const { return suggestedValue().isEmpty(); }
    364     virtual void handleFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
    365     virtual void handleBlurEvent();
    366 
    367     virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
    368     virtual bool isRequiredFormControl() const;
    369     virtual bool recalcWillValidate() const;
    370     virtual void requiredAttributeChanged() OVERRIDE;
    371 
    372     void updateType();
    373 
    374     virtual void subtreeHasChanged();
    375 
    376     void resetListAttributeTargetObserver();
    377     void parseMaxLengthAttribute(const AtomicString&);
    378     void updateValueIfNeeded();
    379 
    380     bool canHaveSelection() const;
    381 
    382     // Returns null if this isn't associated with any radio button group.
    383     CheckedRadioButtons* checkedRadioButtons() const;
    384     void addToRadioButtonGroup();
    385     void removeFromRadioButtonGroup();
    386 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
    387     virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE;
    388 #endif
    389 
    390     AtomicString m_name;
    391     String m_valueIfDirty;
    392     String m_suggestedValue;
    393     int m_size;
    394     int m_maxLength;
    395     short m_maxResults;
    396     bool m_isChecked : 1;
    397     bool m_reflectsCheckedAttribute : 1;
    398     bool m_isIndeterminate : 1;
    399     bool m_hasType : 1;
    400     bool m_isActivatedSubmit : 1;
    401     unsigned m_autocomplete : 2; // AutoCompleteSetting
    402     bool m_isAutofilled : 1;
    403     bool m_hasNonEmptyList : 1;
    404     bool m_stateRestored : 1;
    405     bool m_parsingInProgress : 1;
    406     bool m_valueAttributeWasUpdatedAfterParsing : 1;
    407     bool m_wasModifiedByUser : 1;
    408     bool m_canReceiveDroppedFiles : 1;
    409     bool m_hasTouchEventHandler : 1;
    410     OwnPtr<InputType> m_inputType;
    411     // The ImageLoader must be owned by this element because the loader code assumes
    412     // that it lives as long as its owning element lives. If we move the loader into
    413     // the ImageInput object we may delete the loader while this element lives on.
    414     OwnPtr<HTMLImageLoader> m_imageLoader;
    415     OwnPtr<ListAttributeTargetObserver> m_listAttributeTargetObserver;
    416 };
    417 
    418 inline HTMLInputElement* toHTMLInputElement(Node* node)
    419 {
    420     ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::inputTag));
    421     return static_cast<HTMLInputElement*>(node);
    422 }
    423 
    424 inline const HTMLInputElement* toHTMLInputElement(const Node* node)
    425 {
    426     ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::inputTag));
    427     return static_cast<const HTMLInputElement*>(node);
    428 }
    429 
    430 // This will catch anyone doing an unnecessary cast.
    431 void toHTMLElement(const HTMLElement*);
    432 
    433 
    434 } //namespace
    435 #endif
    436