Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef InputElement_h
     22 #define InputElement_h
     23 
     24 #include "PlatformString.h"
     25 #include <wtf/text/AtomicString.h>
     26 
     27 namespace WebCore {
     28 
     29 class Attribute;
     30 class Document;
     31 class Element;
     32 class Event;
     33 class InputElementData;
     34 
     35 class InputElement {
     36 public:
     37     virtual ~InputElement() { }
     38 
     39     virtual bool isAutofilled() const = 0;
     40     virtual bool isChecked() const = 0;
     41     virtual bool isIndeterminate() const = 0;
     42     virtual bool isInputTypeHidden() const = 0;
     43     virtual bool isPasswordField() const = 0;
     44     virtual bool isSearchField() const = 0;
     45     virtual bool isTextField() const = 0;
     46     virtual bool isRadioButton() const = 0;
     47     virtual bool isCheckbox() const = 0;
     48 
     49     virtual bool supportsMaxLength() const = 0;
     50     virtual bool hasSpinButton() const { return false; }
     51 #if ENABLE(INPUT_SPEECH)
     52     virtual bool isSpeechEnabled() const = 0;
     53 #endif
     54 
     55     virtual bool searchEventsShouldBeDispatched() const = 0;
     56 
     57     virtual int size() const = 0;
     58     virtual const String& suggestedValue() const = 0;
     59     virtual String value() const = 0;
     60     virtual void setValue(const String&, bool sendChangeEvent = false) = 0;
     61     virtual void setValueForUser(const String&) = 0;
     62     // The value which is drawn by a renderer.
     63     virtual String visibleValue() const = 0;
     64     virtual String convertFromVisibleValue(const String&) const = 0;
     65 
     66     // Returns true if the specified string can be set as the value of InputElement.
     67     virtual bool isAcceptableValue(const String&) const = 0;
     68     virtual String sanitizeValue(const String&) const = 0;
     69     virtual void setValueFromRenderer(const String&) = 0;
     70 
     71     virtual void cacheSelection(int start, int end) = 0;
     72     virtual void select() = 0;
     73 
     74 #if ENABLE(WCSS)
     75     virtual InputElementData data() const = 0;
     76 #endif
     77 
     78     static const int s_maximumLength;
     79     static const int s_defaultSize;
     80 
     81     // Replaces CRs and LFs, shrinks the value for s_maximumLength.
     82     // This should be applied to values from the HTML value attribute and the DOM value property.
     83     // This function should be called only by sanitizeValue() implementations.
     84     // Public so it can be called by InputType.
     85     static String sanitizeValueForTextField(const InputElement*, const String&);
     86 
     87 protected:
     88     static void dispatchFocusEvent(InputElement*, Element*);
     89     static void dispatchBlurEvent(InputElement*, Element*);
     90     static void updateFocusAppearance(InputElementData&, InputElement*, Element*, bool restorePreviousSelection);
     91     static void updateSelectionRange(InputElement*, Element*, int start, int end);
     92     static void aboutToUnload(InputElement*, Element*);
     93     static void setValueFromRenderer(InputElementData&, InputElement*, Element*, const String&);
     94     // Replaces CRs and LFs, shrinks the value for the specified maximum length.
     95     // This should be applied to values specified by users.
     96     // The input string may be a fragment of the whole value.
     97     static String sanitizeUserInputValue(const InputElement*, const String&, int);
     98     static void handleBeforeTextInsertedEvent(InputElementData&, InputElement*, Element*, Event*);
     99     static void parseSizeAttribute(InputElementData&, Element*, Attribute*);
    100     static void parseMaxLengthAttribute(InputElementData&, InputElement*, Element*, Attribute*);
    101     static void updateValueIfNeeded(InputElementData&, InputElement*);
    102     static void notifyFormStateChanged(Element*);
    103 #if ENABLE(WCSS)
    104     static bool isConformToInputMask(const InputElementData&, const String&);
    105     static bool isConformToInputMask(const InputElementData&, UChar, unsigned);
    106     static String validateInputMask(InputElementData&, String&);
    107 #endif
    108 };
    109 
    110 // HTML/WMLInputElement hold this struct as member variable
    111 // and pass it to the static helper functions in InputElement
    112 class InputElementData {
    113 public:
    114     InputElementData();
    115     ~InputElementData();
    116 
    117     const AtomicString& name() const;
    118     void setName(const AtomicString& value) { m_name = value; }
    119 
    120     // The null String represents "use the default value," and the empty String
    121     // represents the empty value.
    122     String value() const { return m_value; }
    123     void setValue(const String& value) { m_value = value; }
    124 
    125     const String& suggestedValue() const { return m_suggestedValue; }
    126     void setSuggestedValue(const String& value) { m_suggestedValue = value; }
    127 
    128     int size() const { return m_size; }
    129     void setSize(int value) { m_size = value; }
    130 
    131     int maxLength() const { return m_maxLength; }
    132     void setMaxLength(int value) { m_maxLength = value; }
    133 
    134     int cachedSelectionStart() const { return m_cachedSelectionStart; }
    135     void setCachedSelectionStart(int value) { m_cachedSelectionStart = value; }
    136 
    137     int cachedSelectionEnd() const { return m_cachedSelectionEnd; }
    138     void setCachedSelectionEnd(int value) { m_cachedSelectionEnd = value; }
    139 
    140 #if ENABLE(WCSS)
    141     String inputFormatMask() const { return m_inputFormatMask; }
    142     void setInputFormatMask(const String& mask) { m_inputFormatMask = mask; }
    143 
    144     unsigned maxInputCharsAllowed() const { return m_maxInputCharsAllowed; }
    145     void setMaxInputCharsAllowed(unsigned maxLength) { m_maxInputCharsAllowed = maxLength; }
    146 
    147 #endif
    148 
    149 private:
    150     AtomicString m_name;
    151     String m_value;
    152     String m_suggestedValue;
    153     int m_size;
    154     int m_maxLength;
    155     int m_cachedSelectionStart;
    156     int m_cachedSelectionEnd;
    157 #if ENABLE(WCSS)
    158     String m_inputFormatMask;
    159     unsigned m_maxInputCharsAllowed;
    160 #endif
    161 };
    162 
    163 }
    164 
    165 #endif
    166