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 "FormAssociatedElement.h"
28#include "LabelableElement.h"
29
30namespace WebCore {
31
32class FormDataList;
33class HTMLFieldSetElement;
34class HTMLFormElement;
35class HTMLLegendElement;
36class ValidationMessage;
37class 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.
42class HTMLFormControlElement : public LabelableElement, public FormAssociatedElement {
43public:
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    virtual void reset() { }
57
58    virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
59    virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
60
61    bool wasChangedSinceLastFormControlChangeEvent() const { return m_wasChangedSinceLastFormControlChangeEvent; }
62    void setChangedSinceLastFormControlChangeEvent(bool);
63
64    virtual void dispatchFormControlChangeEvent();
65    void dispatchChangeEvent();
66    void dispatchFormControlInputEvent();
67
68    virtual bool isDisabledFormControl() const OVERRIDE;
69
70    virtual bool isFocusable() const OVERRIDE;
71    virtual bool isEnumeratable() const { return false; }
72
73    bool isRequired() const;
74
75    const AtomicString& type() const { return formControlType(); }
76
77    virtual const AtomicString& formControlType() const = 0;
78
79    virtual bool canTriggerImplicitSubmission() const { return false; }
80
81    // Override in derived classes to get the encoded name=value pair for submitting.
82    // Return true for a successful control (see HTML4-17.13.2).
83    virtual bool appendFormData(FormDataList&, bool) { return false; }
84
85    virtual bool isSuccessfulSubmitButton() const { return false; }
86    virtual bool isActivatedSubmit() const { return false; }
87    virtual void setActivatedSubmit(bool) { }
88
89    virtual bool willValidate() const;
90    void updateVisibleValidationMessage();
91    void hideVisibleValidationMessage();
92    bool checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls = 0);
93    // This must be called when a validation constraint or control value is changed.
94    void setNeedsValidityCheck();
95    virtual void setCustomValidity(const String&) OVERRIDE;
96
97    bool isReadOnly() const { return m_isReadOnly; }
98    bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
99
100    bool hasAutofocused() { return m_hasAutofocused; }
101    void setAutofocused() { m_hasAutofocused = true; }
102
103    static HTMLFormControlElement* enclosingFormControlElement(Node*);
104
105    using Node::ref;
106    using Node::deref;
107
108protected:
109    HTMLFormControlElement(const QualifiedName& tagName, Document*, HTMLFormElement*);
110
111    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
112    virtual void requiredAttributeChanged();
113    virtual void disabledAttributeChanged();
114    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
115    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
116    virtual void removedFrom(ContainerNode*) OVERRIDE;
117    virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
118
119    virtual bool supportsFocus() const OVERRIDE;
120    virtual bool isKeyboardFocusable(KeyboardEvent*) const OVERRIDE;
121    virtual bool isMouseFocusable() const OVERRIDE;
122
123    virtual void didRecalcStyle(StyleChange) OVERRIDE;
124
125    virtual void dispatchBlurEvent(PassRefPtr<Element> newFocusedElement) OVERRIDE;
126
127    // This must be called any time the result of willValidate() has changed.
128    void setNeedsWillValidateCheck();
129    virtual bool recalcWillValidate() const;
130
131    bool validationMessageShadowTreeContains(Node*) const;
132
133private:
134    virtual void refFormAssociatedElement() { ref(); }
135    virtual void derefFormAssociatedElement() { deref(); }
136
137    virtual bool isFormControlElement() const { return true; }
138    virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
139
140    virtual short tabIndex() const OVERRIDE FINAL;
141
142    virtual HTMLFormElement* virtualForm() const;
143    virtual bool isDefaultButtonForForm() const;
144    virtual bool isValidFormControlElement();
145    void updateAncestorDisabledState() const;
146
147    OwnPtr<ValidationMessage> m_validationMessage;
148    bool m_disabled : 1;
149    bool m_isReadOnly : 1;
150    bool m_isRequired : 1;
151    bool m_valueMatchesRenderer : 1;
152
153    enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
154    mutable AncestorDisabledState m_ancestorDisabledState;
155    enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
156    mutable enum DataListAncestorState m_dataListAncestorState;
157
158    // The initial value of m_willValidate depends on the derived class. We can't
159    // initialize it with a virtual function in the constructor. m_willValidate
160    // is not deterministic as long as m_willValidateInitialized is false.
161    mutable bool m_willValidateInitialized: 1;
162    mutable bool m_willValidate : 1;
163
164    // Cache of validity()->valid().
165    // But "candidate for constraint validation" doesn't affect m_isValid.
166    bool m_isValid : 1;
167
168    bool m_wasChangedSinceLastFormControlChangeEvent : 1;
169
170    bool m_hasAutofocused : 1;
171};
172
173inline bool isHTMLFormControlElement(const Node* node)
174{
175    return node->isElementNode() && toElement(node)->isFormControlElement();
176}
177
178inline HTMLFormControlElement* toHTMLFormControlElement(Node* node)
179{
180    ASSERT_WITH_SECURITY_IMPLICATION(!node || isHTMLFormControlElement(node));
181    return static_cast<HTMLFormControlElement*>(node);
182}
183
184// This will catch anyone doing an unnecessary cast.
185void toHTMLFormControlElement(const HTMLFormControlElement*);
186
187} // namespace
188
189#endif
190