2 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
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.
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.
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.
21 #ifndef InputElement_h
22 #define InputElement_h
24 #include "PlatformString.h"
25 #include <wtf/text/AtomicString.h>
33 class InputElementData;
37 virtual ~InputElement() { }
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;
49 virtual bool supportsMaxLength() const = 0;
50 virtual bool hasSpinButton() const { return false; }
51 #if ENABLE(INPUT_SPEECH)
52 virtual bool isSpeechEnabled() const = 0;
55 virtual bool searchEventsShouldBeDispatched() const = 0;
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;
63 // Returns true if the specified string can be set as the value of InputElement.
64 virtual bool isAcceptableValue(const String&) const = 0;
65 virtual String sanitizeValue(const String&) const = 0;
66 virtual void setValueFromRenderer(const String&) = 0;
68 virtual void cacheSelection(int start, int end) = 0;
69 virtual void select() = 0;
72 virtual InputElementData data() const = 0;
75 static const int s_maximumLength;
76 static const int s_defaultSize;
79 static void dispatchFocusEvent(InputElement*, Element*);
80 static void dispatchBlurEvent(InputElement*, Element*);
81 static void updateFocusAppearance(InputElementData&, InputElement*, Element*, bool restorePreviousSelection);
82 static void updateSelectionRange(InputElement*, Element*, int start, int end);
83 static void aboutToUnload(InputElement*, Element*);
84 static void setValueFromRenderer(InputElementData&, InputElement*, Element*, const String&);
85 // Replaces CRs and LFs, shrinks the value for s_maximumLength.
86 // This should be applied to values from the HTML value attribute and the DOM value property.
87 // This function should be called only by sanitizeValue() implementations.
88 static String sanitizeValueForTextField(const InputElement*, const String&);
89 // Replaces CRs and LFs, shrinks the value for the specified maximum length.
90 // This should be applied to values specified by users.
91 // The input string may be a fragment of the whole value.
92 static String sanitizeUserInputValue(const InputElement*, const String&, int);
93 static void handleBeforeTextInsertedEvent(InputElementData&, InputElement*, Element*, Event*);
94 static void parseSizeAttribute(InputElementData&, Element*, Attribute*);
95 static void parseMaxLengthAttribute(InputElementData&, InputElement*, Element*, Attribute*);
96 static void updateValueIfNeeded(InputElementData&, InputElement*);
97 static void notifyFormStateChanged(Element*);
99 static bool isConformToInputMask(const InputElementData&, const String&);
100 static bool isConformToInputMask(const InputElementData&, UChar, unsigned);
101 static String validateInputMask(InputElementData&, String&);
105 // HTML/WMLInputElement hold this struct as member variable
106 // and pass it to the static helper functions in InputElement
107 class InputElementData {
112 const AtomicString& name() const;
113 void setName(const AtomicString& value) { m_name = value; }
115 String value() const { return m_value; }
116 void setValue(const String& value) { m_value = value; }
118 const String& suggestedValue() const { return m_suggestedValue; }
119 void setSuggestedValue(const String& value) { m_suggestedValue = value; }
121 int size() const { return m_size; }
122 void setSize(int value) { m_size = value; }
124 int maxLength() const { return m_maxLength; }
125 void setMaxLength(int value) { m_maxLength = value; }
127 int cachedSelectionStart() const { return m_cachedSelectionStart; }
128 void setCachedSelectionStart(int value) { m_cachedSelectionStart = value; }
130 int cachedSelectionEnd() const { return m_cachedSelectionEnd; }
131 void setCachedSelectionEnd(int value) { m_cachedSelectionEnd = value; }
134 String inputFormatMask() const { return m_inputFormatMask; }
135 void setInputFormatMask(const String& mask) { m_inputFormatMask = mask; }
137 unsigned maxInputCharsAllowed() const { return m_maxInputCharsAllowed; }
138 void setMaxInputCharsAllowed(unsigned maxLength) { m_maxInputCharsAllowed = maxLength; }
145 String m_suggestedValue;
148 int m_cachedSelectionStart;
149 int m_cachedSelectionEnd;
151 String m_inputFormatMask;
152 unsigned m_maxInputCharsAllowed;
156 InputElement* toInputElement(Element*);