Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  * Copyright (C) 2011 Apple Inc. All rights reserved.
      4  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  * notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  * copyright notice, this list of conditions and the following disclaimer
     14  * in the documentation and/or other materials provided with the
     15  * distribution.
     16  *     * Neither the name of Google Inc. nor the names of its
     17  * contributors may be used to endorse or promote products derived from
     18  * this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #ifndef InputType_h
     34 #define InputType_h
     35 
     36 #include "core/html/HTMLTextFormControlElement.h"
     37 #include "core/html/StepRange.h"
     38 #include "core/page/UseCounter.h"
     39 #include "wtf/FastAllocBase.h"
     40 #include "wtf/Forward.h"
     41 #include "wtf/Noncopyable.h"
     42 #include "wtf/RefPtr.h"
     43 
     44 namespace WebCore {
     45 
     46 class BeforeTextInsertedEvent;
     47 class Chrome;
     48 class Color;
     49 class DateComponents;
     50 class DragData;
     51 class Event;
     52 class ExceptionState;
     53 class FileList;
     54 class FormDataList;
     55 class HTMLElement;
     56 class HTMLFormElement;
     57 class HTMLInputElement;
     58 class KeyboardEvent;
     59 class MouseEvent;
     60 class Node;
     61 class RenderObject;
     62 class RenderStyle;
     63 class TouchEvent;
     64 
     65 struct ClickHandlingState {
     66     WTF_MAKE_FAST_ALLOCATED;
     67 
     68 public:
     69     bool checked;
     70     bool indeterminate;
     71     RefPtr<HTMLInputElement> checkedRadioButton;
     72 };
     73 
     74 // An InputType object represents the type-specific part of an HTMLInputElement.
     75 // Do not expose instances of InputType and classes derived from it to classes
     76 // other than HTMLInputElement.
     77 class InputType {
     78     WTF_MAKE_NONCOPYABLE(InputType);
     79     WTF_MAKE_FAST_ALLOCATED;
     80 
     81 public:
     82     static PassOwnPtr<InputType> create(HTMLInputElement*, const AtomicString&);
     83     static PassOwnPtr<InputType> createText(HTMLInputElement*);
     84     virtual ~InputType();
     85 
     86     static bool themeSupportsDataListUI(InputType*);
     87 
     88     virtual const AtomicString& formControlType() const = 0;
     89     virtual bool canChangeFromAnotherType() const;
     90 
     91     // Type query functions
     92 
     93     // Any time we are using one of these functions it's best to refactor
     94     // to add a virtual function to allow the input type object to do the
     95     // work instead, or at least make a query function that asks a higher
     96     // level question. These functions make the HTMLInputElement class
     97     // inflexible because it's harder to add new input types if there is
     98     // scattered code with special cases for various types.
     99 
    100     virtual bool isCheckbox() const;
    101     virtual bool isColorControl() const;
    102     virtual bool isDateField() const;
    103     virtual bool isDateTimeLocalField() const;
    104     virtual bool isEmailField() const;
    105     virtual bool isFileUpload() const;
    106     virtual bool isHiddenType() const;
    107     virtual bool isImageButton() const;
    108     virtual bool supportLabels() const;
    109     virtual bool isMonthField() const;
    110     virtual bool isNumberField() const;
    111     virtual bool isPasswordField() const;
    112     virtual bool isRadioButton() const;
    113     virtual bool isRangeControl() const;
    114     virtual bool isSearchField() const;
    115     virtual bool isSubmitButton() const;
    116     virtual bool isTelephoneField() const;
    117     virtual bool isTextButton() const;
    118     virtual bool isTextField() const;
    119     virtual bool isTextType() const;
    120     virtual bool isTimeField() const;
    121     virtual bool isURLField() const;
    122     virtual bool isWeekField() const;
    123 
    124     // Form value functions
    125 
    126     virtual bool shouldSaveAndRestoreFormControlState() const;
    127     virtual FormControlState saveFormControlState() const;
    128     virtual void restoreFormControlState(const FormControlState&);
    129     virtual bool isFormDataAppendable() const;
    130     virtual bool appendFormData(FormDataList&, bool multipart) const;
    131 
    132     // DOM property functions
    133 
    134     virtual bool getTypeSpecificValue(String&); // Checked first, before internal storage or the value attribute.
    135     virtual String fallbackValue() const; // Checked last, if both internal storage and value attribute are missing.
    136     virtual String defaultValue() const; // Checked after even fallbackValue, only when the valueWithDefault function is called.
    137     virtual double valueAsDate() const;
    138     virtual void setValueAsDate(double, ExceptionState&) const;
    139     virtual double valueAsDouble() const;
    140     virtual void setValueAsDouble(double, TextFieldEventBehavior, ExceptionState&) const;
    141     virtual void setValueAsDecimal(const Decimal&, TextFieldEventBehavior, ExceptionState&) const;
    142 
    143     // Validation functions
    144     virtual String validationMessage() const;
    145     virtual bool supportsValidation() const;
    146     virtual bool typeMismatchFor(const String&) const;
    147     // Type check for the current input value. We do nothing for some types
    148     // though typeMismatchFor() does something for them because of value
    149     // sanitization.
    150     virtual bool typeMismatch() const;
    151     virtual bool supportsRequired() const;
    152     virtual bool valueMissing(const String&) const;
    153     virtual bool hasBadInput() const;
    154     virtual bool patternMismatch(const String&) const;
    155     bool rangeUnderflow(const String&) const;
    156     bool rangeOverflow(const String&) const;
    157     bool isInRange(const String&) const;
    158     bool isOutOfRange(const String&) const;
    159     virtual Decimal defaultValueForStepUp() const;
    160     double minimum() const;
    161     double maximum() const;
    162     virtual bool sizeShouldIncludeDecoration(int defaultSize, int& preferredSize) const;
    163     bool stepMismatch(const String&) const;
    164     virtual bool getAllowedValueStep(Decimal*) const;
    165     virtual StepRange createStepRange(AnyStepHandling) const;
    166     virtual void stepUp(int, ExceptionState&);
    167     virtual void stepUpFromRenderer(int);
    168     virtual String badInputText() const;
    169     virtual String typeMismatchText() const;
    170     virtual String valueMissingText() const;
    171     virtual bool canSetStringValue() const;
    172     virtual String localizeValue(const String&) const;
    173     virtual String visibleValue() const;
    174     // Returing the null string means "use the default value."
    175     // This function must be called only by HTMLInputElement::sanitizeValue().
    176     virtual String sanitizeValue(const String&) const;
    177 
    178     // Event handlers
    179 
    180     virtual void handleClickEvent(MouseEvent*);
    181     virtual void handleMouseDownEvent(MouseEvent*);
    182     virtual PassOwnPtr<ClickHandlingState> willDispatchClick();
    183     virtual void didDispatchClick(Event*, const ClickHandlingState&);
    184     virtual void handleDOMActivateEvent(Event*);
    185     virtual void handleKeydownEvent(KeyboardEvent*);
    186     virtual void handleKeypressEvent(KeyboardEvent*);
    187     virtual void handleKeyupEvent(KeyboardEvent*);
    188     virtual void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent*);
    189     virtual void handleTouchEvent(TouchEvent*);
    190     virtual void forwardEvent(Event*);
    191     // Helpers for event handlers.
    192     virtual bool shouldSubmitImplicitly(Event*);
    193     virtual PassRefPtr<HTMLFormElement> formForSubmission() const;
    194     virtual bool hasCustomFocusLogic() const;
    195     virtual bool isKeyboardFocusable() const;
    196     virtual bool shouldShowFocusRingOnMouseFocus() const;
    197     virtual bool shouldUseInputMethod() const;
    198     virtual void handleFocusEvent(Element* oldFocusedElement, FocusDirection);
    199     virtual void handleBlurEvent();
    200     virtual void accessKeyAction(bool sendMouseEvents);
    201     virtual bool canBeSuccessfulSubmitButton();
    202     virtual void subtreeHasChanged();
    203     virtual bool hasTouchEventHandler() const;
    204 
    205     virtual void blur();
    206 
    207     // Shadow tree handling
    208 
    209     virtual void createShadowSubtree();
    210     virtual void destroyShadowSubtree();
    211 
    212     virtual HTMLElement* containerElement() const { return 0; }
    213     virtual HTMLElement* innerBlockElement() const { return 0; }
    214     virtual HTMLElement* innerTextElement() const { return 0; }
    215     virtual HTMLElement* innerSpinButtonElement() const { return 0; }
    216 #if ENABLE(INPUT_SPEECH)
    217     virtual HTMLElement* speechButtonElement() const { return 0; }
    218 #endif
    219     virtual HTMLElement* passwordGeneratorButtonElement() const { return 0; }
    220     virtual HTMLElement* sliderThumbElement() const { return 0; }
    221     virtual HTMLElement* sliderTrackElement() const { return 0; }
    222     virtual HTMLElement* placeholderElement() const;
    223 
    224     // Miscellaneous functions
    225 
    226     virtual bool rendererIsNeeded();
    227     virtual RenderObject* createRenderer(RenderStyle*) const;
    228     virtual PassRefPtr<RenderStyle> customStyleForRenderer(PassRefPtr<RenderStyle>);
    229     virtual void attach();
    230     virtual void detach();
    231     virtual void minOrMaxAttributeChanged();
    232     virtual void stepAttributeChanged();
    233     virtual void altAttributeChanged();
    234     virtual void srcAttributeChanged();
    235     virtual bool shouldRespectAlignAttribute();
    236     virtual FileList* files();
    237     virtual void setFiles(PassRefPtr<FileList>);
    238     // Should return true if the given DragData has more than one dropped files.
    239     virtual bool receiveDroppedFiles(const DragData*);
    240     virtual String droppedFileSystemId();
    241     // Should return true if the corresponding renderer for a type can display a suggested value.
    242     virtual bool canSetSuggestedValue();
    243     virtual bool shouldSendChangeEventAfterCheckedChanged();
    244     virtual bool canSetValue(const String&);
    245     virtual bool storesValueSeparateFromAttribute();
    246     virtual void setValue(const String&, bool valueChanged, TextFieldEventBehavior);
    247     virtual bool shouldResetOnDocumentActivation();
    248     virtual bool shouldRespectListAttribute();
    249     virtual bool shouldRespectSpeechAttribute();
    250     virtual bool isEnumeratable();
    251     virtual bool isCheckable();
    252     virtual bool isSteppable() const;
    253     virtual bool shouldRespectHeightAndWidthAttributes();
    254     virtual bool supportsPlaceholder() const;
    255     virtual bool supportsReadOnly() const;
    256     virtual void updateInnerTextValue();
    257     virtual void updatePlaceholderText();
    258     virtual void attributeChanged();
    259     virtual void multipleAttributeChanged();
    260     virtual void disabledAttributeChanged();
    261     virtual void readonlyAttributeChanged();
    262     virtual void requiredAttributeChanged();
    263     virtual void valueAttributeChanged();
    264     virtual String defaultToolTip() const;
    265     virtual void listAttributeTargetChanged();
    266     virtual Decimal findClosestTickMarkValue(const Decimal&);
    267     virtual void updateClearButtonVisibility();
    268 
    269     // Parses the specified string for the type, and return
    270     // the Decimal value for the parsing result if the parsing
    271     // succeeds; Returns defaultValue otherwise. This function can
    272     // return NaN or Infinity only if defaultValue is NaN or Infinity.
    273     virtual Decimal parseToNumber(const String&, const Decimal& defaultValue) const;
    274 
    275     // Parses the specified string for this InputType, and returns true if it
    276     // is successfully parsed. An instance pointed by the DateComponents*
    277     // parameter will have parsed values and be modified even if the parsing
    278     // fails. The DateComponents* parameter may be 0.
    279     virtual bool parseToDateComponents(const String&, DateComponents*) const;
    280 
    281     // Create a string representation of the specified Decimal value for the
    282     // input type. If NaN or Infinity is specified, this returns an empty
    283     // string. This should not be called for types without valueAsNumber.
    284     virtual String serialize(const Decimal&) const;
    285 
    286     virtual bool supportsIndeterminateAppearance() const;
    287 
    288     virtual bool supportsInputModeAttribute() const;
    289 
    290     virtual bool supportsSelectionAPI() const;
    291 
    292     // Gets width and height of the input element if the type of the
    293     // element is image. It returns 0 if the element is not image type.
    294     virtual unsigned height() const;
    295     virtual unsigned width() const;
    296 
    297     void dispatchSimulatedClickIfActive(KeyboardEvent*) const;
    298 
    299 protected:
    300     InputType(HTMLInputElement* element) : m_element(element) { }
    301     HTMLInputElement* element() const { return m_element; }
    302     Chrome* chrome() const;
    303     Decimal parseToNumberOrNaN(const String&) const;
    304     void observeFeatureIfVisible(UseCounter::Feature) const;
    305 
    306 private:
    307     // Helper for stepUp()/stepDown(). Adds step value * count to the current value.
    308     void applyStep(int count, AnyStepHandling, TextFieldEventBehavior, ExceptionState&);
    309 
    310     // Raw pointer because the HTMLInputElement object owns this InputType object.
    311     HTMLInputElement* m_element;
    312 };
    313 
    314 } // namespace WebCore
    315 #endif
    316