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
30#if ENABLE(IOS_AUTOCORRECT_AND_AUTOCAPITALIZE)
31#include "Autocapitalize.h"
32#endif
33
34namespace WebCore {
35
36class FormDataList;
37class HTMLFieldSetElement;
38class HTMLFormElement;
39class HTMLLegendElement;
40class ValidationMessage;
41
42// HTMLFormControlElement is the default implementation of FormAssociatedElement,
43// and form-associated element implementations should use HTMLFormControlElement
44// unless there is a special reason.
45class HTMLFormControlElement : public LabelableElement, public FormAssociatedElement {
46public:
47    virtual ~HTMLFormControlElement();
48
49    HTMLFormElement* form() const { return FormAssociatedElement::form(); }
50
51    String formEnctype() const;
52    void setFormEnctype(const String&);
53    String formMethod() const;
54    void setFormMethod(const String&);
55    bool formNoValidate() const;
56
57    void setAncestorDisabled(bool isDisabled);
58
59    virtual void reset() { }
60
61    virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
62    virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
63
64    bool wasChangedSinceLastFormControlChangeEvent() const { return m_wasChangedSinceLastFormControlChangeEvent; }
65    void setChangedSinceLastFormControlChangeEvent(bool);
66
67    virtual void dispatchFormControlChangeEvent();
68    void dispatchChangeEvent();
69    void dispatchFormControlInputEvent();
70
71    virtual bool isDisabledFormControl() const override;
72
73    virtual bool isFocusable() const override;
74    virtual bool isEnumeratable() const override { return false; }
75
76    bool isRequired() const;
77
78    const AtomicString& type() const { return formControlType(); }
79
80    virtual const AtomicString& formControlType() const = 0;
81
82    virtual bool canTriggerImplicitSubmission() const { return false; }
83
84    // Override in derived classes to get the encoded name=value pair for submitting.
85    // Return true for a successful control (see HTML4-17.13.2).
86    virtual bool appendFormData(FormDataList&, bool) override { return false; }
87
88    virtual bool isSuccessfulSubmitButton() const { return false; }
89    virtual bool isActivatedSubmit() const { return false; }
90    virtual void setActivatedSubmit(bool) { }
91
92#if ENABLE(IOS_AUTOCORRECT_AND_AUTOCAPITALIZE)
93    bool autocorrect() const;
94    void setAutocorrect(bool);
95
96    WebAutocapitalizeType autocapitalizeType() const;
97    const AtomicString& autocapitalize() const;
98    void setAutocapitalize(const AtomicString&);
99#endif
100
101    virtual bool willValidate() const override;
102    void updateVisibleValidationMessage();
103    void hideVisibleValidationMessage();
104    bool checkValidity(Vector<RefPtr<FormAssociatedElement>>* unhandledInvalidControls = 0);
105    // This must be called when a validation constraint or control value is changed.
106    void setNeedsValidityCheck();
107    virtual void setCustomValidity(const String&) override;
108
109    bool isReadOnly() const { return m_isReadOnly; }
110    bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
111
112    bool hasAutofocused() { return m_hasAutofocused; }
113    void setAutofocused() { m_hasAutofocused = true; }
114
115    static HTMLFormControlElement* enclosingFormControlElement(Node*);
116
117    using Node::ref;
118    using Node::deref;
119
120protected:
121    HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
122
123    bool disabledByAncestorFieldset() const { return m_disabledByAncestorFieldset; }
124
125    virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
126    virtual void disabledAttributeChanged();
127    virtual void disabledStateChanged();
128    virtual void readOnlyAttributeChanged();
129    virtual void requiredAttributeChanged();
130    virtual void didAttachRenderers() override;
131    virtual InsertionNotificationRequest insertedInto(ContainerNode&) override;
132    virtual void removedFrom(ContainerNode&) override;
133    virtual void didMoveToNewDocument(Document* oldDocument) override;
134
135    virtual bool supportsFocus() const override;
136    virtual bool isKeyboardFocusable(KeyboardEvent*) const override;
137    virtual bool isMouseFocusable() const override;
138
139    virtual void didRecalcStyle(Style::Change) override;
140
141    virtual void dispatchBlurEvent(PassRefPtr<Element> newFocusedElement) override;
142
143    // This must be called any time the result of willValidate() has changed.
144    void setNeedsWillValidateCheck();
145    virtual bool recalcWillValidate() const;
146
147    bool validationMessageShadowTreeContains(const Node&) const;
148
149private:
150    virtual void refFormAssociatedElement() override { ref(); }
151    virtual void derefFormAssociatedElement() override { deref(); }
152
153    virtual bool isFormControlElement() const override { return true; }
154    virtual bool alwaysCreateUserAgentShadowRoot() const override { return true; }
155
156    virtual short tabIndex() const override final;
157
158    virtual HTMLFormElement* virtualForm() const override;
159    virtual bool isDefaultButtonForForm() const override;
160    virtual bool isValidFormControlElement() const override;
161
162    bool computeIsDisabledByFieldsetAncestor() const;
163
164    virtual HTMLElement& asHTMLElement() override final { return *this; }
165    virtual const HTMLFormControlElement& asHTMLElement() const override final { return *this; }
166    virtual HTMLFormControlElement* asFormNamedItem() override final { return this; }
167
168    std::unique_ptr<ValidationMessage> m_validationMessage;
169    bool m_disabled : 1;
170    bool m_isReadOnly : 1;
171    bool m_isRequired : 1;
172    bool m_valueMatchesRenderer : 1;
173    bool m_disabledByAncestorFieldset : 1;
174
175    enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
176    mutable enum DataListAncestorState m_dataListAncestorState;
177
178    // The initial value of m_willValidate depends on the derived class. We can't
179    // initialize it with a virtual function in the constructor. m_willValidate
180    // is not deterministic as long as m_willValidateInitialized is false.
181    mutable bool m_willValidateInitialized: 1;
182    mutable bool m_willValidate : 1;
183
184    // Cache of validity()->valid().
185    // But "candidate for constraint validation" doesn't affect m_isValid.
186    bool m_isValid : 1;
187
188    bool m_wasChangedSinceLastFormControlChangeEvent : 1;
189
190    bool m_hasAutofocused : 1;
191};
192
193void isHTMLFormControlElement(const HTMLFormControlElement&); // Catch unnecessary runtime check of type known at compile time.
194inline bool isHTMLFormControlElement(const Element& element) { return element.isFormControlElement(); }
195inline bool isHTMLFormControlElement(const Node& node) { return node.isElementNode() && toElement(node).isFormControlElement(); }
196template <> inline bool isElementOfType<const HTMLFormControlElement>(const Element& element) { return isHTMLFormControlElement(element); }
197
198NODE_TYPE_CASTS(HTMLFormControlElement)
199
200FORM_ASSOCIATED_ELEMENT_TYPE_CASTS(HTMLFormControlElement, isFormControlElement())
201
202} // namespace
203
204#endif
205