WebCore/html/HTMLFormControlElement.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
       
     5  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public License
       
    18  * along with this library; see the file COPYING.LIB.  If not, write to
       
    19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    20  * Boston, MA 02110-1301, USA.
       
    21  *
       
    22  */
       
    23 
       
    24 #ifndef HTMLFormControlElement_h
       
    25 #define HTMLFormControlElement_h
       
    26 
       
    27 #include "HTMLElement.h"
       
    28 
       
    29 namespace WebCore {
       
    30 
       
    31 class FormDataList;
       
    32 class HTMLFormElement;
       
    33 class RenderTextControl;
       
    34 class ValidityState;
       
    35 class VisibleSelection;
       
    36 
       
    37 // FIXME: The HTML5 specification calls these form-associated elements.
       
    38 // So consider renaming this to HTMLFormAssociatedElement.
       
    39 class HTMLFormControlElement : public HTMLElement {
       
    40 public:
       
    41     virtual ~HTMLFormControlElement();
       
    42 
       
    43     HTMLFormElement* form() const { return m_form; }
       
    44     ValidityState* validity();
       
    45 
       
    46     bool formNoValidate() const;
       
    47 
       
    48     virtual void reset() { }
       
    49 
       
    50     virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
       
    51     virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
       
    52 
       
    53     virtual void dispatchFormControlChangeEvent();
       
    54 
       
    55     bool disabled() const { return m_disabled; }
       
    56     void setDisabled(bool);
       
    57 
       
    58     virtual bool isFocusable() const;
       
    59     virtual bool isEnumeratable() const { return false; }
       
    60 
       
    61     // Determines whether or not a control will be automatically focused.
       
    62     virtual bool autofocus() const;
       
    63 
       
    64     bool required() const;
       
    65 
       
    66     const AtomicString& type() const { return formControlType(); }
       
    67     const AtomicString& name() const { return formControlName(); }
       
    68 
       
    69     void setName(const AtomicString& name);
       
    70 
       
    71     virtual bool isEnabledFormControl() const { return !disabled(); }
       
    72     virtual bool isReadOnlyFormControl() const { return readOnly(); }
       
    73 
       
    74     virtual bool isRadioButton() const { return false; }
       
    75     virtual bool canTriggerImplicitSubmission() const { return false; }
       
    76 
       
    77     // Override in derived classes to get the encoded name=value pair for submitting.
       
    78     // Return true for a successful control (see HTML4-17.13.2).
       
    79     virtual bool appendFormData(FormDataList&, bool) { return false; }
       
    80 
       
    81     virtual bool isSuccessfulSubmitButton() const { return false; }
       
    82     virtual bool isActivatedSubmit() const { return false; }
       
    83     virtual void setActivatedSubmit(bool) { }
       
    84 
       
    85     virtual bool willValidate() const;
       
    86     String validationMessage();
       
    87     bool checkValidity(Vector<RefPtr<HTMLFormControlElement> >* unhandledInvalidControls = 0);
       
    88     // This must be called when a validation constraint or control value is changed.
       
    89     void setNeedsValidityCheck();
       
    90     void setCustomValidity(const String&);
       
    91     virtual bool valueMissing() const { return false; }
       
    92     virtual bool patternMismatch() const { return false; }
       
    93     virtual bool tooLong() const { return false; }
       
    94 
       
    95     void formDestroyed() { m_form = 0; }
       
    96 
       
    97     bool isLabelable() const;
       
    98     PassRefPtr<NodeList> labels();
       
    99     
       
   100     bool readOnly() const { return m_readOnly; }
       
   101 
       
   102 protected:
       
   103     HTMLFormControlElement(const QualifiedName& tagName, Document*, HTMLFormElement*);
       
   104 
       
   105     virtual void parseMappedAttribute(Attribute*);
       
   106     virtual void attach();
       
   107     virtual void insertedIntoTree(bool deep);
       
   108     virtual void removedFromTree(bool deep);
       
   109 
       
   110     virtual bool isKeyboardFocusable(KeyboardEvent*) const;
       
   111     virtual bool isMouseFocusable() const;
       
   112 
       
   113     virtual void recalcStyle(StyleChange);
       
   114 
       
   115     virtual void dispatchFocusEvent();
       
   116     virtual void dispatchBlurEvent();
       
   117 
       
   118     void removeFromForm();
       
   119 
       
   120     // This must be called any time the result of willValidate() has changed.
       
   121     void setNeedsWillValidateCheck();
       
   122     virtual bool recalcWillValidate() const;
       
   123 
       
   124 private:
       
   125     virtual HTMLTagStatus endTagRequirement() const { return TagStatusRequired; }
       
   126     virtual int tagPriority() const { return 1; }
       
   127 
       
   128     virtual const AtomicString& formControlName() const;
       
   129     virtual const AtomicString& formControlType() const = 0;
       
   130 
       
   131     virtual bool isFormControlElement() const { return true; }
       
   132 
       
   133     virtual bool supportsFocus() const;
       
   134 
       
   135     virtual short tabIndex() const;
       
   136 
       
   137     virtual HTMLFormElement* virtualForm() const;
       
   138     virtual bool isDefaultButtonForForm() const;
       
   139     virtual bool isValidFormControlElement();
       
   140 
       
   141     HTMLFormElement* m_form;
       
   142     OwnPtr<ValidityState> m_validityState;
       
   143     bool m_disabled : 1;
       
   144     bool m_readOnly : 1;
       
   145     bool m_required : 1;
       
   146     bool m_valueMatchesRenderer : 1;
       
   147 
       
   148     // The initial value of m_willValidate depends on the derived class. We can't
       
   149     // initialize it with a virtual function in the constructor. m_willValidate
       
   150     // is not deterministic as long as m_willValidateInitialized is false.
       
   151     mutable bool m_willValidateInitialized: 1;
       
   152     mutable bool m_willValidate : 1;
       
   153 
       
   154     // Cache of validity()->valid().
       
   155     // But "candidate for constraint validation" doesn't affect m_isValid.
       
   156     bool m_isValid : 1;
       
   157 };
       
   158 
       
   159 // FIXME: Give this class its own header file.
       
   160 class HTMLFormControlElementWithState : public HTMLFormControlElement {
       
   161 public:
       
   162     virtual ~HTMLFormControlElementWithState();
       
   163 
       
   164 protected:
       
   165     HTMLFormControlElementWithState(const QualifiedName& tagName, Document*, HTMLFormElement*);
       
   166 
       
   167     virtual bool autoComplete() const;
       
   168 
       
   169     virtual void willMoveToNewOwnerDocument();
       
   170     virtual void didMoveToNewOwnerDocument();
       
   171     virtual void defaultEventHandler(Event*);
       
   172 
       
   173 private:
       
   174     virtual bool shouldSaveAndRestoreFormControlState() const;
       
   175     virtual void finishParsingChildren();
       
   176 };
       
   177 
       
   178 // FIXME: Give this class its own header file.
       
   179 class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
       
   180 public:
       
   181     virtual ~HTMLTextFormControlElement();
       
   182 
       
   183     String strippedPlaceholder() const;
       
   184 
       
   185     int selectionStart();
       
   186     int selectionEnd();
       
   187     void setSelectionStart(int);
       
   188     void setSelectionEnd(int);
       
   189     void select();
       
   190     void setSelectionRange(int start, int end);
       
   191     VisibleSelection selection() const;
       
   192 
       
   193 protected:
       
   194     HTMLTextFormControlElement(const QualifiedName&, Document*, HTMLFormElement*);
       
   195 
       
   196     bool placeholderShouldBeVisible() const;
       
   197     void updatePlaceholderVisibility(bool);
       
   198 
       
   199     virtual void parseMappedAttribute(Attribute*);
       
   200 
       
   201 private:
       
   202     virtual void dispatchFocusEvent();
       
   203     virtual void dispatchBlurEvent();
       
   204 
       
   205     bool isPlaceholderEmpty() const;
       
   206 
       
   207     virtual int cachedSelectionStart() const = 0;
       
   208     virtual int cachedSelectionEnd() const = 0;
       
   209 
       
   210     // The derived class should return true if placeholder processing is needed.
       
   211     virtual bool supportsPlaceholder() const = 0;
       
   212     // Returns true if user-editable value is empty. Used to check placeholder visibility.
       
   213     virtual bool isEmptyValue() const = 0;
       
   214     // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
       
   215     virtual void handleFocusEvent() { }
       
   216     // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
       
   217     virtual void handleBlurEvent() { }
       
   218 
       
   219     RenderTextControl* textRendererAfterUpdateLayout();
       
   220 };
       
   221 
       
   222 } // namespace
       
   223 
       
   224 #endif