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     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLFormControlElement);
     44 
     45 public:
     46     virtual ~HTMLFormControlElement();
     47     virtual void trace(Visitor*) OVERRIDE;
     48 
     49     String formEnctype() const;
     50     void setFormEnctype(const AtomicString&);
     51     String formMethod() const;
     52     void setFormMethod(const AtomicString&);
     53     bool formNoValidate() const;
     54 
     55     void ancestorDisabledStateWasChanged();
     56 
     57     void reset();
     58 
     59     bool wasChangedSinceLastFormControlChangeEvent() const { return m_wasChangedSinceLastFormControlChangeEvent; }
     60     void setChangedSinceLastFormControlChangeEvent(bool);
     61 
     62     virtual void dispatchFormControlChangeEvent();
     63     void dispatchChangeEvent();
     64     void dispatchFormControlInputEvent();
     65 
     66     virtual HTMLFormElement* formOwner() const OVERRIDE FINAL;
     67 
     68     virtual bool isDisabledFormControl() const OVERRIDE;
     69 
     70     virtual bool isEnumeratable() const OVERRIDE { return false; }
     71 
     72     bool isRequired() const;
     73 
     74     const AtomicString& type() const { return formControlType(); }
     75 
     76     virtual const AtomicString& formControlType() const = 0;
     77 
     78     virtual bool canTriggerImplicitSubmission() const { return false; }
     79 
     80     // Override in derived classes to get the encoded name=value pair for submitting.
     81     // Return true for a successful control (see HTML4-17.13.2).
     82     virtual bool appendFormData(FormDataList&, bool) OVERRIDE { return false; }
     83     virtual String resultForDialogSubmit();
     84 
     85     virtual bool canBeSuccessfulSubmitButton() const { return false; }
     86     bool isSuccessfulSubmitButton() const;
     87     virtual bool isActivatedSubmit() const { return false; }
     88     virtual void setActivatedSubmit(bool) { }
     89 
     90     virtual bool willValidate() const OVERRIDE;
     91     void updateVisibleValidationMessage();
     92     void hideVisibleValidationMessage();
     93     bool checkValidity(WillBeHeapVector<RefPtrWillBeMember<FormAssociatedElement> >* unhandledInvalidControls = 0);
     94     // This must be called when a validation constraint or control value is changed.
     95     void setNeedsValidityCheck();
     96     virtual void setCustomValidity(const String&) OVERRIDE FINAL;
     97 
     98     bool isReadOnly() const { return m_isReadOnly; }
     99     bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
    100 
    101     bool isAutofocusable() const;
    102 
    103     bool isAutofilled() const { return m_isAutofilled; }
    104     void setAutofilled(bool = true);
    105 
    106     static HTMLFormControlElement* enclosingFormControlElement(Node*);
    107 
    108     String nameForAutofill() const;
    109 
    110     virtual void setFocus(bool flag) OVERRIDE;
    111 
    112 #if !ENABLE(OILPAN)
    113     using Node::ref;
    114     using Node::deref;
    115 #endif
    116 
    117 protected:
    118     HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
    119 
    120     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
    121     virtual void requiredAttributeChanged();
    122     virtual void disabledAttributeChanged();
    123     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
    124     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    125     virtual void removedFrom(ContainerNode*) OVERRIDE;
    126     virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE;
    127 
    128     virtual bool supportsFocus() const OVERRIDE;
    129     virtual bool isKeyboardFocusable() const OVERRIDE;
    130     virtual bool shouldShowFocusRingOnMouseFocus() const;
    131     virtual bool shouldHaveFocusAppearance() const OVERRIDE FINAL;
    132     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE;
    133     virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
    134     virtual void willCallDefaultEventHandler(const Event&) OVERRIDE FINAL;
    135 
    136     virtual void didRecalcStyle(StyleRecalcChange) OVERRIDE FINAL;
    137 
    138     // This must be called any time the result of willValidate() has changed.
    139     void setNeedsWillValidateCheck();
    140     virtual bool recalcWillValidate() const;
    141 
    142     virtual void resetImpl() { }
    143     virtual bool supportsAutofocus() const;
    144 
    145 private:
    146 #if !ENABLE(OILPAN)
    147     virtual void refFormAssociatedElement() OVERRIDE FINAL { ref(); }
    148     virtual void derefFormAssociatedElement() OVERRIDE FINAL { deref(); }
    149 #endif
    150 
    151     virtual bool isFormControlElement() const OVERRIDE FINAL { return true; }
    152     virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
    153 
    154     virtual short tabIndex() const OVERRIDE FINAL;
    155 
    156     virtual bool isDefaultButtonForForm() const OVERRIDE FINAL;
    157     virtual bool isValidFormControlElement() OVERRIDE FINAL;
    158     void updateAncestorDisabledState() const;
    159 
    160     OwnPtr<ValidationMessage> m_validationMessage;
    161     bool m_disabled : 1;
    162     bool m_isAutofilled : 1;
    163     bool m_isReadOnly : 1;
    164     bool m_isRequired : 1;
    165 
    166     enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
    167     mutable AncestorDisabledState m_ancestorDisabledState;
    168     enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
    169     mutable enum DataListAncestorState m_dataListAncestorState;
    170 
    171     // The initial value of m_willValidate depends on the derived class. We can't
    172     // initialize it with a virtual function in the constructor. m_willValidate
    173     // is not deterministic as long as m_willValidateInitialized is false.
    174     mutable bool m_willValidateInitialized: 1;
    175     mutable bool m_willValidate : 1;
    176 
    177     // Cache of valid().
    178     // But "candidate for constraint validation" doesn't affect m_isValid.
    179     bool m_isValid : 1;
    180 
    181     bool m_wasChangedSinceLastFormControlChangeEvent : 1;
    182     bool m_wasFocusedByMouse : 1;
    183 };
    184 
    185 inline bool isHTMLFormControlElement(const Element& element)
    186 {
    187     return element.isFormControlElement();
    188 }
    189 
    190 DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(HTMLFormControlElement);
    191 DEFINE_TYPE_CASTS(HTMLFormControlElement, FormAssociatedElement, control, control->isFormControlElement(), control.isFormControlElement());
    192 
    193 } // namespace
    194 
    195 #endif
    196