Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2000 Dirk Mueller (mueller (at) 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 "core/html/FormAssociatedElement.h"
     28 #include "core/html/LabelableElement.h"
     29 
     30 namespace WebCore {
     31 
     32 class FormDataList;
     33 class HTMLFieldSetElement;
     34 class HTMLFormElement;
     35 class HTMLLegendElement;
     36 class ValidationMessage;
     37 class ValidityState;
     38 
     39 // HTMLFormControlElement is the default implementation of FormAssociatedElement,
     40 // and form-associated element implementations should use HTMLFormControlElement
     41 // unless there is a special reason.
     42 class HTMLFormControlElement : public LabelableElement, public FormAssociatedElement {
     43 public:
     44     virtual ~HTMLFormControlElement();
     45 
     46     String formEnctype() const;
     47     void setFormEnctype(const AtomicString&);
     48     String formMethod() const;
     49     void setFormMethod(const AtomicString&);
     50     bool formNoValidate() const;
     51 
     52     void ancestorDisabledStateWasChanged();
     53 
     54     void reset();
     55 
     56     virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
     57     virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
     58 
     59     virtual bool wasChangedSinceLastFormControlChangeEvent() const;
     60     virtual void setChangedSinceLastFormControlChangeEvent(bool);
     61 
     62     virtual void dispatchFormControlChangeEvent();
     63     virtual void dispatchFormControlInputEvent();
     64 
     65     virtual HTMLFormElement* formOwner() const OVERRIDE;
     66 
     67     virtual bool isDisabledFormControl() const OVERRIDE;
     68 
     69     virtual bool isEnumeratable() const { return false; }
     70 
     71     bool isRequired() const;
     72 
     73     const AtomicString& type() const { return formControlType(); }
     74 
     75     virtual const AtomicString& formControlType() const OVERRIDE = 0;
     76 
     77     virtual bool canTriggerImplicitSubmission() const { return false; }
     78 
     79     // Override in derived classes to get the encoded name=value pair for submitting.
     80     // Return true for a successful control (see HTML4-17.13.2).
     81     virtual bool appendFormData(FormDataList&, bool) { return false; }
     82     virtual String resultForDialogSubmit();
     83 
     84     virtual bool canBeSuccessfulSubmitButton() const { return false; }
     85     bool isSuccessfulSubmitButton() const;
     86     virtual bool isActivatedSubmit() const { return false; }
     87     virtual void setActivatedSubmit(bool) { }
     88 
     89     enum CheckValidityDispatchEvents { CheckValidityDispatchEventsAllowed, CheckValidityDispatchEventsNone };
     90 
     91     virtual bool willValidate() const;
     92     void updateVisibleValidationMessage();
     93     void hideVisibleValidationMessage();
     94     bool checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls = 0, CheckValidityDispatchEvents = CheckValidityDispatchEventsAllowed);
     95     // This must be called when a validation constraint or control value is changed.
     96     void setNeedsValidityCheck();
     97     virtual void setCustomValidity(const String&) OVERRIDE;
     98 
     99     bool isReadOnly() const { return m_isReadOnly; }
    100     bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
    101 
    102     bool hasAutofocused() const { return m_hasAutofocused; }
    103     void setAutofocused() { m_hasAutofocused = true; }
    104     bool isAutofocusable() const;
    105 
    106     bool isAutofilled() const { return m_isAutofilled; }
    107     void setAutofilled(bool = true);
    108 
    109     static HTMLFormControlElement* enclosingFormControlElement(Node*);
    110 
    111     String nameForAutofill() const;
    112 
    113     using Node::ref;
    114     using Node::deref;
    115 
    116 protected:
    117     HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
    118 
    119     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
    120     virtual void requiredAttributeChanged();
    121     virtual void disabledAttributeChanged();
    122     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
    123     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    124     virtual void removedFrom(ContainerNode*) OVERRIDE;
    125     virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE;
    126 
    127     virtual bool supportsFocus() const OVERRIDE;
    128     virtual bool isKeyboardFocusable() const OVERRIDE;
    129     virtual bool shouldShowFocusRingOnMouseFocus() const;
    130     virtual bool shouldHaveFocusAppearance() const OVERRIDE;
    131     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
    132     virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
    133     virtual void willCallDefaultEventHandler(const Event&) OVERRIDE;
    134 
    135     virtual void didRecalcStyle(StyleRecalcChange) OVERRIDE;
    136 
    137     // This must be called any time the result of willValidate() has changed.
    138     void setNeedsWillValidateCheck();
    139     virtual bool recalcWillValidate() const;
    140 
    141     virtual void resetImpl() { }
    142 
    143 private:
    144     virtual void refFormAssociatedElement() { ref(); }
    145     virtual void derefFormAssociatedElement() { deref(); }
    146 
    147     virtual bool isFormControlElement() const { return true; }
    148     virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
    149 
    150     virtual short tabIndex() const;
    151 
    152     virtual bool isDefaultButtonForForm() const;
    153     virtual bool isValidFormControlElement();
    154     void updateAncestorDisabledState() const;
    155 
    156     OwnPtr<ValidationMessage> m_validationMessage;
    157     bool m_disabled : 1;
    158     bool m_isAutofilled : 1;
    159     bool m_isReadOnly : 1;
    160     bool m_isRequired : 1;
    161     bool m_valueMatchesRenderer : 1;
    162 
    163     enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
    164     mutable AncestorDisabledState m_ancestorDisabledState;
    165     enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
    166     mutable enum DataListAncestorState m_dataListAncestorState;
    167 
    168     // The initial value of m_willValidate depends on the derived class. We can't
    169     // initialize it with a virtual function in the constructor. m_willValidate
    170     // is not deterministic as long as m_willValidateInitialized is false.
    171     mutable bool m_willValidateInitialized: 1;
    172     mutable bool m_willValidate : 1;
    173 
    174     // Cache of valid().
    175     // But "candidate for constraint validation" doesn't affect m_isValid.
    176     bool m_isValid : 1;
    177 
    178     bool m_wasChangedSinceLastFormControlChangeEvent : 1;
    179     bool m_wasFocusedByMouse : 1;
    180     bool m_hasAutofocused : 1;
    181 };
    182 
    183 inline bool isHTMLFormControlElement(const Node& node)
    184 {
    185     return node.isElementNode() && toElement(node).isFormControlElement();
    186 }
    187 
    188 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(HTMLFormControlElement);
    189 DEFINE_TYPE_CASTS(HTMLFormControlElement, FormAssociatedElement, control, control->isFormControlElement(), control.isFormControlElement());
    190 
    191 } // namespace
    192 
    193 #endif
    194