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-2018 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#pragma once
25
26#include "Autofill.h"
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 DOMFormData;
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 {
46 WTF_MAKE_ISO_ALLOCATED(HTMLFormControlElement);
47public:
48 virtual ~HTMLFormControlElement();
49
50 HTMLFormElement* form() const final { return FormAssociatedElement::form(); }
51
52 WEBCORE_EXPORT String formEnctype() const;
53 WEBCORE_EXPORT void setFormEnctype(const String&);
54 WEBCORE_EXPORT String formMethod() const;
55 WEBCORE_EXPORT void setFormMethod(const String&);
56 bool formNoValidate() const;
57 WEBCORE_EXPORT String formAction() const;
58 WEBCORE_EXPORT void setFormAction(const AtomString&);
59
60 void setAncestorDisabled(bool isDisabled);
61
62 virtual void reset() { }
63
64 bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
65 void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
66
67 bool wasChangedSinceLastFormControlChangeEvent() const { return m_wasChangedSinceLastFormControlChangeEvent; }
68 void setChangedSinceLastFormControlChangeEvent(bool);
69
70 virtual void dispatchFormControlChangeEvent();
71 void dispatchChangeEvent();
72 void dispatchFormControlInputEvent();
73
74 bool isDisabledFormControl() const override;
75
76 bool isEnumeratable() const override { return false; }
77
78 bool isRequired() const;
79
80 const AtomString& type() const { return formControlType(); }
81
82 virtual const AtomString& formControlType() const = 0;
83
84 virtual bool canTriggerImplicitSubmission() const { return false; }
85
86 // Override in derived classes to get the encoded name=value pair for submitting.
87 // Return true for a successful control (see HTML4-17.13.2).
88 bool appendFormData(DOMFormData&, bool) override { return false; }
89
90 virtual bool isSuccessfulSubmitButton() const { return false; }
91 virtual bool isActivatedSubmit() const { return false; }
92 virtual void setActivatedSubmit(bool) { }
93
94#if ENABLE(IOS_AUTOCORRECT_AND_AUTOCAPITALIZE)
95 WEBCORE_EXPORT bool shouldAutocorrect() const final;
96 WEBCORE_EXPORT AutocapitalizeType autocapitalizeType() const final;
97#endif
98
99 WEBCORE_EXPORT bool willValidate() const final;
100 void updateVisibleValidationMessage();
101 void hideVisibleValidationMessage();
102 WEBCORE_EXPORT bool checkValidity(Vector<RefPtr<HTMLFormControlElement>>* unhandledInvalidControls = nullptr);
103 bool reportValidity();
104 void focusAndShowValidationMessage();
105 bool isShowingValidationMessage() const;
106 // This must be called when a validation constraint or control value is changed.
107 void updateValidity();
108 void setCustomValidity(const String&) override;
109
110 bool isReadOnly() const { return m_isReadOnly; }
111 bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
112
113 bool hasAutofocused() { return m_hasAutofocused; }
114 void setAutofocused() { m_hasAutofocused = true; }
115
116 static HTMLFormControlElement* enclosingFormControlElement(Node*);
117
118 WEBCORE_EXPORT String autocomplete() const;
119 WEBCORE_EXPORT void setAutocomplete(const String&);
120
121 AutofillMantle autofillMantle() const;
122
123 WEBCORE_EXPORT AutofillData autofillData() const;
124
125 using Node::ref;
126 using Node::deref;
127
128protected:
129 HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
130
131 bool disabledByAncestorFieldset() const { return m_disabledByAncestorFieldset; }
132
133 void parseAttribute(const QualifiedName&, const AtomString&) override;
134 virtual void disabledAttributeChanged();
135 virtual void disabledStateChanged();
136 virtual void readOnlyStateChanged();
137 virtual void requiredStateChanged();
138 void didAttachRenderers() override;
139 InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode&) override;
140 void didFinishInsertingNode() override;
141 void removedFromAncestor(RemovalType, ContainerNode&) override;
142 void didMoveToNewDocument(Document& oldDocument, Document& newDocument) override;
143
144 bool supportsFocus() const override;
145 bool isKeyboardFocusable(KeyboardEvent*) const override;
146 bool isMouseFocusable() const override;
147
148 void didRecalcStyle(Style::Change) override;
149
150 void dispatchBlurEvent(RefPtr<Element>&& newFocusedElement) override;
151
152 // This must be called any time the result of willValidate() has changed.
153 void setNeedsWillValidateCheck();
154 virtual bool computeWillValidate() const;
155
156 bool validationMessageShadowTreeContains(const Node&) const;
157
158 void willChangeForm() override;
159 void didChangeForm() override;
160
161private:
162 void refFormAssociatedElement() override { ref(); }
163 void derefFormAssociatedElement() override { deref(); }
164
165 bool matchesValidPseudoClass() const override;
166 bool matchesInvalidPseudoClass() const override;
167
168 bool isFormControlElement() const final { return true; }
169
170 int tabIndex() const final;
171
172 bool isValidFormControlElement() const;
173
174 bool computeIsDisabledByFieldsetAncestor() const;
175
176 HTMLElement& asHTMLElement() final { return *this; }
177 const HTMLFormControlElement& asHTMLElement() const final { return *this; }
178 FormNamedItem* asFormNamedItem() final { return this; }
179 FormAssociatedElement* asFormAssociatedElement() final { return this; }
180
181 bool needsMouseFocusableQuirk() const;
182
183 std::unique_ptr<ValidationMessage> m_validationMessage;
184 unsigned m_disabled : 1;
185 unsigned m_isReadOnly : 1;
186 unsigned m_isRequired : 1;
187 unsigned m_valueMatchesRenderer : 1;
188 unsigned m_disabledByAncestorFieldset : 1;
189
190 enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
191 mutable unsigned m_dataListAncestorState : 2;
192
193 // The initial value of m_willValidate depends on the derived class. We can't
194 // initialize it with a virtual function in the constructor. m_willValidate
195 // is not deterministic as long as m_willValidateInitialized is false.
196 mutable bool m_willValidateInitialized: 1;
197 mutable bool m_willValidate : 1;
198
199 // Cache of validity()->valid().
200 // But "candidate for constraint validation" doesn't affect m_isValid.
201 unsigned m_isValid : 1;
202
203 unsigned m_wasChangedSinceLastFormControlChangeEvent : 1;
204
205 unsigned m_hasAutofocused : 1;
206};
207
208} // namespace WebCore
209
210SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::HTMLFormControlElement)
211 static bool isType(const WebCore::Element& element) { return element.isFormControlElement(); }
212 static bool isType(const WebCore::Node& node) { return is<WebCore::Element>(node) && isType(downcast<WebCore::Element>(node)); }
213 static bool isType(const WebCore::FormAssociatedElement& element) { return element.isFormControlElement(); }
214SPECIALIZE_TYPE_TRAITS_END()
215