- add third_party src.
[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     HTMLFormElement* form() const { return FormAssociatedElement::form(); }
47
48     String formEnctype() const;
49     void setFormEnctype(const String&);
50     String formMethod() const;
51     void setFormMethod(const String&);
52     bool formNoValidate() const;
53
54     void ancestorDisabledStateWasChanged();
55
56     void reset();
57
58     virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
59     virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
60
61     virtual bool wasChangedSinceLastFormControlChangeEvent() const;
62     virtual void setChangedSinceLastFormControlChangeEvent(bool);
63
64     virtual void dispatchFormControlChangeEvent();
65     virtual void dispatchFormControlInputEvent();
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 isSuccessfulSubmitButton() const { return false; }
85     virtual bool isActivatedSubmit() const { return false; }
86     virtual void setActivatedSubmit(bool) { }
87
88     enum CheckValidityDispatchEvents { CheckValidityDispatchEventsAllowed, CheckValidityDispatchEventsNone };
89
90     virtual bool willValidate() const;
91     void updateVisibleValidationMessage();
92     void hideVisibleValidationMessage();
93     bool checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls = 0, CheckValidityDispatchEvents = CheckValidityDispatchEventsAllowed);
94     // This must be called when a validation constraint or control value is changed.
95     void setNeedsValidityCheck();
96     virtual void setCustomValidity(const String&) OVERRIDE;
97
98     bool isReadOnly() const { return m_isReadOnly; }
99     bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
100
101     bool hasAutofocused() const { return m_hasAutofocused; }
102     void setAutofocused() { m_hasAutofocused = true; }
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     using Node::ref;
111     using Node::deref;
112
113 protected:
114     HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
115
116     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
117     virtual void requiredAttributeChanged();
118     virtual void disabledAttributeChanged();
119     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
120     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
121     virtual void removedFrom(ContainerNode*) OVERRIDE;
122     virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE;
123
124     virtual bool supportsFocus() const OVERRIDE;
125     virtual bool isKeyboardFocusable() const OVERRIDE;
126     virtual bool shouldShowFocusRingOnMouseFocus() const;
127     virtual bool shouldHaveFocusAppearance() const OVERRIDE;
128     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
129     virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
130     virtual void willCallDefaultEventHandler(const Event&) OVERRIDE;
131
132     virtual void didRecalcStyle(StyleRecalcChange) OVERRIDE;
133
134     // This must be called any time the result of willValidate() has changed.
135     void setNeedsWillValidateCheck();
136     virtual bool recalcWillValidate() const;
137
138     virtual void resetImpl() { }
139
140 private:
141     virtual void refFormAssociatedElement() { ref(); }
142     virtual void derefFormAssociatedElement() { deref(); }
143
144     virtual bool isFormControlElement() const { return true; }
145     virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
146
147     virtual short tabIndex() const;
148
149     virtual HTMLFormElement* virtualForm() const;
150     virtual bool isDefaultButtonForForm() const;
151     virtual bool isValidFormControlElement();
152     void updateAncestorDisabledState() const;
153
154     OwnPtr<ValidationMessage> m_validationMessage;
155     bool m_disabled : 1;
156     bool m_isAutofilled : 1;
157     bool m_isReadOnly : 1;
158     bool m_isRequired : 1;
159     bool m_valueMatchesRenderer : 1;
160
161     enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
162     mutable AncestorDisabledState m_ancestorDisabledState;
163     enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
164     mutable enum DataListAncestorState m_dataListAncestorState;
165
166     // The initial value of m_willValidate depends on the derived class. We can't
167     // initialize it with a virtual function in the constructor. m_willValidate
168     // is not deterministic as long as m_willValidateInitialized is false.
169     mutable bool m_willValidateInitialized: 1;
170     mutable bool m_willValidate : 1;
171
172     // Cache of valid().
173     // But "candidate for constraint validation" doesn't affect m_isValid.
174     bool m_isValid : 1;
175
176     bool m_wasChangedSinceLastFormControlChangeEvent : 1;
177     bool m_wasFocusedByMouse : 1;
178     bool m_hasAutofocused : 1;
179 };
180
181 inline bool isHTMLFormControlElement(const Node& node)
182 {
183     return node.isElementNode() && toElement(node).isFormControlElement();
184 }
185
186 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(HTMLFormControlElement);
187
188 inline HTMLFormControlElement* toHTMLFormControlElement(FormAssociatedElement* control)
189 {
190     ASSERT_WITH_SECURITY_IMPLICATION(!control || control->isFormControlElement());
191     return static_cast<HTMLFormControlElement*>(control);
192 }
193
194 inline HTMLFormControlElement& toHTMLFormControlElement(FormAssociatedElement& control)
195 {
196     ASSERT_WITH_SECURITY_IMPLICATION(control.isFormControlElement());
197     return static_cast<HTMLFormControlElement&>(control);
198 }
199
200 inline const HTMLFormControlElement* toHTMLFormControlElement(const FormAssociatedElement* control)
201 {
202     ASSERT_WITH_SECURITY_IMPLICATION(!control || control->isFormControlElement());
203     return static_cast<const HTMLFormControlElement*>(control);
204 }
205
206 } // namespace
207
208 #endif