Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLFormControlElement.h
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 "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     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     enum CheckValidityDispatchEvents { CheckValidityDispatchEventsAllowed, CheckValidityDispatchEventsNone };
91
92     virtual bool willValidate() const OVERRIDE;
93     void updateVisibleValidationMessage();
94     void hideVisibleValidationMessage();
95     bool checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls = 0, CheckValidityDispatchEvents = CheckValidityDispatchEventsAllowed);
96     // This must be called when a validation constraint or control value is changed.
97     void setNeedsValidityCheck();
98     virtual void setCustomValidity(const String&) OVERRIDE FINAL;
99
100     bool isReadOnly() const { return m_isReadOnly; }
101     bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
102
103     bool isAutofocusable() const;
104
105     bool isAutofilled() const { return m_isAutofilled; }
106     void setAutofilled(bool = true);
107
108     static HTMLFormControlElement* enclosingFormControlElement(Node*);
109
110     String nameForAutofill() const;
111
112     virtual void setFocus(bool flag) OVERRIDE;
113
114     using Node::ref;
115     using Node::deref;
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     virtual void refFormAssociatedElement() OVERRIDE FINAL { ref(); }
147     virtual void derefFormAssociatedElement() OVERRIDE FINAL { deref(); }
148
149     virtual bool isFormControlElement() const OVERRIDE FINAL { return true; }
150     virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
151
152     virtual short tabIndex() const OVERRIDE FINAL;
153
154     virtual bool isDefaultButtonForForm() const OVERRIDE FINAL;
155     virtual bool isValidFormControlElement() OVERRIDE FINAL;
156     void updateAncestorDisabledState() const;
157
158     OwnPtr<ValidationMessage> m_validationMessage;
159     bool m_disabled : 1;
160     bool m_isAutofilled : 1;
161     bool m_isReadOnly : 1;
162     bool m_isRequired : 1;
163     bool m_valueMatchesRenderer : 1;
164
165     enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
166     mutable AncestorDisabledState m_ancestorDisabledState;
167     enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
168     mutable enum DataListAncestorState m_dataListAncestorState;
169
170     // The initial value of m_willValidate depends on the derived class. We can't
171     // initialize it with a virtual function in the constructor. m_willValidate
172     // is not deterministic as long as m_willValidateInitialized is false.
173     mutable bool m_willValidateInitialized: 1;
174     mutable bool m_willValidate : 1;
175
176     // Cache of valid().
177     // But "candidate for constraint validation" doesn't affect m_isValid.
178     bool m_isValid : 1;
179
180     bool m_wasChangedSinceLastFormControlChangeEvent : 1;
181     bool m_wasFocusedByMouse : 1;
182 };
183
184 inline bool isHTMLFormControlElement(const Node& node)
185 {
186     return node.isElementNode() && toElement(node).isFormControlElement();
187 }
188
189 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(HTMLFormControlElement);
190 DEFINE_TYPE_CASTS(HTMLFormControlElement, FormAssociatedElement, control, control->isFormControlElement(), control.isFormControlElement());
191
192 } // namespace
193
194 #endif