HTMLInputElement.h 12.5 KB
Newer Older
1 2 3 4
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
5
 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
19 20
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
21 22 23
 *
 */

24 25
#ifndef HTMLInputElement_h
#define HTMLInputElement_h
26

darin@apple.com's avatar
darin@apple.com committed
27
#include "HTMLFormControlElement.h"
28
#include "InputElement.h"
adele's avatar
adele committed
29
#include <wtf/OwnPtr.h>
30

darin's avatar
darin committed
31
namespace WebCore {
32

33
class DateComponents;
34
class FileList;
35
class HTMLDataListElement;
36
class HTMLImageLoader;
37
class HTMLOptionElement;
darin@apple.com's avatar
darin@apple.com committed
38
class KURL;
39
class VisibleSelection;
40

41
class HTMLInputElement : public HTMLTextFormControlElement, public InputElement {
42
public:
darin's avatar
darin committed
43
    enum InputType {
44
        TEXT = 0, // TEXT must be 0.
45 46 47 48 49 50 51 52 53 54 55
        PASSWORD,
        ISINDEX,
        CHECKBOX,
        RADIO,
        SUBMIT,
        RESET,
        FILE,
        HIDDEN,
        IMAGE,
        BUTTON,
        SEARCH,
bolsinga@apple.com's avatar
bolsinga@apple.com committed
56 57 58 59
        RANGE,
        EMAIL,
        NUMBER,
        TELEPHONE,
60
        URL,
61 62 63 64 65 66 67 68
        COLOR,
        DATE,
        DATETIME,
        DATETIMELOCAL,
        MONTH,
        TIME,
        WEEK,
        // If you add new types or change the order of enum values, update numberOfTypes below.
69
    };
70 71
    static const int numberOfTypes = WEEK + 1;

72
    static PassRefPtr<HTMLInputElement> create(const QualifiedName&, Document*, HTMLFormElement*);
darin's avatar
darin committed
73
    virtual ~HTMLInputElement();
74

beidson@apple.com's avatar
beidson@apple.com committed
75
    bool autoComplete() const;
76

77 78 79
    // For ValidityState
    bool rangeUnderflow() const;
    bool rangeOverflow() const;
80
    // Returns the minimum value for type=date, number, or range.  Don't call this for other types.
81
    double minimum() const;
82
    // Returns the maximum value for type=date, number, or range.  Don't call this for other types.
83 84
    // This always returns a value which is >= minimum().
    double maximum() const;
85 86 87 88 89
    // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
    // Returns false if there is no "allowed value step."
    bool getAllowedValueStep(double*) const;
    // For ValidityState.
    bool stepMismatch() const;
90 91 92 93 94
    // Implementations of HTMLInputElement::stepUp() and stepDown().
    void stepUp(int, ExceptionCode&);
    void stepDown(int, ExceptionCode&);
    void stepUp(ExceptionCode& ec) { stepUp(1, ec); }
    void stepDown(ExceptionCode& ec) { stepDown(1, ec); }
95 96
    // stepUp()/stepDown() for user-interaction.
    void stepUpFromRenderer(int);
pkasting@chromium.org's avatar
pkasting@chromium.org committed
97

98 99
    bool isTextButton() const { return m_type == SUBMIT || m_type == RESET || m_type == BUTTON; }
    virtual bool isRadioButton() const { return m_type == RADIO; }
100
    virtual bool isTextField() const;
101
    virtual bool isSearchField() const { return m_type == SEARCH; }
bdakin@apple.com's avatar
bdakin@apple.com committed
102
    virtual bool isInputTypeHidden() const { return m_type == HIDDEN; }
103
    virtual bool isPasswordField() const { return m_type == PASSWORD; }
104
    virtual bool isCheckbox() const { return m_type == CHECKBOX; }
105 106 107 108
    bool isTelephoneField() const { return m_type == TELEPHONE; }
    bool isNumberField() const { return m_type == NUMBER; }
    bool isEmailField() const { return m_type == EMAIL; }
    bool isUrlField() const { return m_type == URL; }
109 110 111
#if ENABLE(INPUT_SPEECH)
    virtual bool isSpeechEnabled() const;
#endif    
112 113

    bool checked() const { return m_checked; }
mjs's avatar
mjs committed
114
    void setChecked(bool, bool sendChangeEvent = false);
115

116
    // 'indeterminate' is a state independent of the checked state that causes the control to draw in a way that hides the actual state.
117 118
    bool indeterminate() const { return m_indeterminate; }
    void setIndeterminate(bool);
119

120
    virtual int size() const;
121

122
    void setType(const String&);
123

124
    virtual String value() const;
125 126
    virtual void setValue(const String&, bool sendChangeEvent = false);
    virtual void setValueForUser(const String&);
127

128 129 130
    virtual const String& suggestedValue() const;
    void setSuggestedValue(const String&);

131 132 133
    double valueAsDate() const;
    void setValueAsDate(double, ExceptionCode&);

134 135 136
    double valueAsNumber() const;
    void setValueAsNumber(double, ExceptionCode&);

137 138 139
    virtual String placeholder() const;
    virtual void setPlaceholder(const String&);

darin's avatar
darin committed
140
    String valueWithDefault() const;
141

142
    virtual void setValueFromRenderer(const String&);
adele@apple.com's avatar
adele@apple.com committed
143
    void setFileListFromRenderer(const Vector<String>&);
144

darin's avatar
darin committed
145
    bool canHaveSelection() const;
146
    virtual void select() { HTMLTextFormControlElement::select(); }
darin's avatar
darin committed
147

darin's avatar
darin committed
148 149
    virtual bool rendererIsNeeded(RenderStyle*);
    virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
150 151
    virtual void detach();

152 153 154 155 156 157
    // FIXME: For isActivatedSubmit and setActivatedSubmit, we should use the NVI-idiom here by making
    // it private virtual in all classes and expose a public method in HTMLFormControlElement to call
    // the private virtual method.
    virtual bool isActivatedSubmit() const;
    virtual void setActivatedSubmit(bool flag);

darin's avatar
darin committed
158 159
    InputType inputType() const { return static_cast<InputType>(m_type); }
    void setInputType(const String&);
160

darin's avatar
darin committed
161
    String altText() const;
162 163 164

    int maxResults() const { return m_maxResults; }

darin's avatar
darin committed
165
    String defaultValue() const;
darin's avatar
darin committed
166
    void setDefaultValue(const String&);
167
    
168 169
    bool defaultChecked() const;

170 171
    void setDefaultName(const AtomicString&);

darin's avatar
darin committed
172 173
    String accept() const;
    String alt() const;
174 175 176

    void setSize(unsigned);

darin@apple.com's avatar
darin@apple.com committed
177
    KURL src() const;
178

179
    int maxLength() const;
180
    void setMaxLength(int, ExceptionCode&);
181

adele@apple.com's avatar
adele@apple.com committed
182
    bool multiple() const;
183

184 185 186 187
#if ENABLE(DIRECTORY_UPLOAD)
    bool webkitdirectory() const;
#endif

188
    virtual bool isAutofilled() const { return m_autofilled; }
189
    void setAutofilled(bool value = true);
190 191 192

    FileList* files();

adele's avatar
adele committed
193
    void addSearchResult();
adele's avatar
adele committed
194
    void onSearch();
195

196
    // Parses the specified string as the InputType, and returns true if it is successfully parsed.
197 198
    // An instance pointed by the DateComponents* parameter will have parsed values and be
    // modified even if the parsing fails.  The DateComponents* parameter may be 0.
199
    static bool parseToDateComponents(InputType, const String&, DateComponents*);
200

201 202 203
#if ENABLE(DATALIST)
    HTMLElement* list() const;
    HTMLOptionElement* selectedOption() const;
204
#endif
205

206 207 208 209
#if ENABLE(WCSS)
    void setWapInputFormat(String& mask);
#endif

210
protected:
211 212
    HTMLInputElement(const QualifiedName&, Document*, HTMLFormElement* = 0);

213
    virtual void defaultEventHandler(Event*);
darin's avatar
darin committed
214 215

private:
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    enum AutoCompleteSetting { Uninitialized, On, Off };

    virtual void willMoveToNewOwnerDocument();
    virtual void didMoveToNewOwnerDocument();

    virtual HTMLTagStatus endTagRequirement() const { return TagStatusForbidden; }
    virtual int tagPriority() const { return 0; }

    virtual bool isKeyboardFocusable(KeyboardEvent*) const;
    virtual bool isMouseFocusable() const;
    virtual bool isEnumeratable() const { return inputType() != IMAGE; }
    virtual void updateFocusAppearance(bool restorePreviousSelection);
    virtual void aboutToUnload();
    virtual bool shouldUseInputMethod() const;

    virtual const AtomicString& formControlName() const;
 
    // isChecked is used by the rendering tree/CSS while checked() is used by JS to determine checked state
    virtual bool isChecked() const { return checked() && (inputType() == CHECKBOX || inputType() == RADIO); }
    virtual bool isIndeterminate() const { return indeterminate(); }
    
    virtual bool isTextFormControl() const { return isTextField(); }

    virtual bool valueMissing() const;
    virtual bool patternMismatch() const;
    virtual bool tooLong() const;

    virtual bool hasSpinButton() const { return m_type == NUMBER || m_type == DATE || m_type == DATETIME || m_type == DATETIMELOCAL || m_type == MONTH || m_type == TIME || m_type == WEEK; }
    virtual bool canTriggerImplicitSubmission() const { return isTextField(); }

    bool allowsIndeterminate() const { return inputType() == CHECKBOX || inputType() == RADIO; }

    virtual const AtomicString& formControlType() const;

    virtual bool searchEventsShouldBeDispatched() const;

    virtual bool saveFormControlState(String& value) const;
    virtual void restoreFormControlState(const String&);

    virtual bool canStartSelection() const;
    
    virtual void accessKeyAction(bool sendToAnyElement);

    virtual bool mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const;
    virtual void parseMappedAttribute(Attribute*);

    virtual void copyNonAttributeProperties(const Element* source);

    virtual void attach();

    virtual bool appendFormData(FormDataList&, bool);

    virtual bool isSuccessfulSubmitButton() const;

    // Report if this input type uses height & width attributes
    bool respectHeightAndWidthAttrs() const { return inputType() == IMAGE || inputType() == HIDDEN; }

    virtual void reset();

    virtual void* preDispatchEventHandler(Event*);
    virtual void postDispatchEventHandler(Event*, void* dataFromPreDispatch);

    virtual bool isURLAttribute(Attribute*) const;

    virtual void cacheSelection(int start, int end);

    virtual String sanitizeValue(const String&) const;

    virtual void documentDidBecomeActive();

    virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;

288
    bool storesValueSeparateFromAttribute() const;
289

290 291 292
    bool needsActivationCallback();
    void registerForActivationCallbackIfNeeded();
    void unregisterForActivationCallbackIfNeeded();
293

294 295 296 297
    virtual bool supportsMaxLength() const { return isTextType(); }
    bool isTextType() const;

    virtual bool supportsPlaceholder() const { return isTextType() || inputType() == ISINDEX; }
298 299 300
    virtual bool isEmptyValue() const { return value().isEmpty(); }
    virtual void handleFocusEvent();
    virtual void handleBlurEvent();
301 302
    virtual int cachedSelectionStart() const { return m_data.cachedSelectionStart(); }
    virtual int cachedSelectionEnd() const { return m_data.cachedSelectionEnd(); }
303

pkasting@chromium.org's avatar
pkasting@chromium.org committed
304 305
    virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
    virtual bool isRequiredFormControl() const;
306
    virtual bool recalcWillValidate() const;
pkasting@chromium.org's avatar
pkasting@chromium.org committed
307

308 309
    void updateCheckedRadioButtons();
    
310
    void handleBeforeTextInsertedEvent(Event*);
311
    PassRefPtr<HTMLFormElement> createTemporaryFormForIsIndex();
312 313
    // Helper for getAllowedValueStep();
    bool getStepParameters(double* defaultStep, double* stepScaleFactor) const;
314 315
    // Helper for stepUp()/stepDown().  Adds step value * count to the current value.
    void applyStep(double count, ExceptionCode&);
316 317 318
    // Helper for applyStepForNumberOrRange().
    double stepBase() const;

319 320 321 322 323
    // Parses the specified string for the current type, and return
    // the double value for the parsing result if the parsing
    // succeeds; Returns defaultValue otherwise. This function can
    // return NaN or Infinity only if defaultValue is NaN or Infinity.
    double parseToDouble(const String&, double defaultValue) const;
324 325 326 327 328 329 330 331
    // Create a string representation of the specified double value for the
    // current input type. If NaN or Infinity is specified, this returns an
    // emtpy string. This should not be called for types without valueAsNumber.
    String serialize(double) const;
    // Create a string representation of the specified double value for the
    // current input type. The type must be one of DATE, DATETIME,
    // DATETIMELOCAL, MONTH, TIME, and WEEK.
    String serializeForDateTimeTypes(double) const;
332

333 334 335 336
#if ENABLE(DATALIST)
    HTMLDataListElement* dataList() const;
#endif

337 338 339 340
#if ENABLE(WCSS)
    virtual InputElementData data() const { return m_data; }
#endif

341 342 343
    InputElementData m_data;
    int m_xPos;
    int m_yPos;
darin's avatar
darin committed
344
    short m_maxResults;
adele's avatar
adele committed
345
    OwnPtr<HTMLImageLoader> m_imageLoader;
346
    RefPtr<FileList> m_fileList;
bolsinga@apple.com's avatar
bolsinga@apple.com committed
347
    unsigned m_type : 5; // InputType 
348 349 350 351 352 353
    bool m_checked : 1;
    bool m_defaultChecked : 1;
    bool m_useDefaultChecked : 1;
    bool m_indeterminate : 1;
    bool m_haveType : 1;
    bool m_activeSubmit : 1;
beidson@apple.com's avatar
beidson@apple.com committed
354
    unsigned m_autocomplete : 2; // AutoCompleteSetting
adele's avatar
adele committed
355
    bool m_autofilled : 1;
darin's avatar
darin committed
356
    bool m_inited : 1;
357 358 359
#if ENABLE(DATALIST)
    bool m_hasNonEmptyList : 1;
#endif
360 361 362 363 364
};

} //namespace

#endif