Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / nsIFormControl.h
blob051f83215ff52b4eb922f8f7dfdd912da45e60c8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsIFormControl_h___
7 #define nsIFormControl_h___
9 #include "mozilla/EventForwards.h"
10 #include "mozilla/StaticPrefs_dom.h"
11 #include "nsISupports.h"
13 namespace mozilla {
14 class PresState;
15 namespace dom {
16 class Element;
17 class FormData;
18 class HTMLFieldSetElement;
19 class HTMLFormElement;
20 } // namespace dom
21 } // namespace mozilla
23 // Elements with different types, the value is used as a mask.
24 // When changing the order, adding or removing elements, be sure to update
25 // the static_assert checks accordingly.
26 constexpr uint8_t kFormControlButtonElementMask = 0x40; // 0b01000000
27 constexpr uint8_t kFormControlInputElementMask = 0x80; // 0b10000000
29 enum class FormControlType : uint8_t {
30 Fieldset = 1,
31 Output,
32 Select,
33 Textarea,
34 Object,
35 FormAssociatedCustomElement,
37 LastWithoutSubtypes = FormAssociatedCustomElement,
39 ButtonButton = kFormControlButtonElementMask + 1,
40 ButtonReset,
41 ButtonSubmit,
42 LastButtonElement = ButtonSubmit,
44 InputButton = kFormControlInputElementMask + 1,
45 InputCheckbox,
46 InputColor,
47 InputDate,
48 InputEmail,
49 InputFile,
50 InputHidden,
51 InputReset,
52 InputImage,
53 InputMonth,
54 InputNumber,
55 InputPassword,
56 InputRadio,
57 InputSearch,
58 InputSubmit,
59 InputTel,
60 InputText,
61 InputTime,
62 InputUrl,
63 InputRange,
64 InputWeek,
65 InputDatetimeLocal,
66 LastInputElement = InputDatetimeLocal,
69 static_assert(uint8_t(FormControlType::LastWithoutSubtypes) <
70 kFormControlButtonElementMask,
71 "Too many FormControlsTypes without sub-types");
72 static_assert(uint8_t(FormControlType::LastButtonElement) <
73 kFormControlInputElementMask,
74 "Too many ButtonElementTypes");
75 static_assert(uint32_t(FormControlType::LastInputElement) < (1 << 8),
76 "Too many form control types");
78 #define NS_IFORMCONTROL_IID \
79 { \
80 0x4b89980c, 0x4dcd, 0x428f, { \
81 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec \
82 } \
85 /**
86 * Interface which all form controls (e.g. buttons, checkboxes, text,
87 * radio buttons, select, etc) implement in addition to their dom specific
88 * interface.
90 class nsIFormControl : public nsISupports {
91 public:
92 nsIFormControl(FormControlType aType) : mType(aType) {}
94 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
96 /**
97 * Get the fieldset for this form control.
98 * @return the fieldset
100 virtual mozilla::dom::HTMLFieldSetElement* GetFieldSet() = 0;
103 * Get the form for this form control.
104 * @return the form
106 virtual mozilla::dom::HTMLFormElement* GetForm() const = 0;
109 * Set the form for this form control.
110 * @param aForm the form. This must not be null.
112 * @note that when setting the form the control is not added to the
113 * form. It adds itself when it gets bound to the tree thereafter,
114 * so that it can be properly sorted with the other controls in the
115 * form.
117 virtual void SetForm(mozilla::dom::HTMLFormElement* aForm) = 0;
120 * Tell the control to forget about its form.
122 * @param aRemoveFromForm set false if you do not want this element removed
123 * from the form. (Used by nsFormControlList::Clear())
124 * @param aUnbindOrDelete set true if the element is being deleted or unbound
125 * from tree.
127 virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
130 * Get the type of this control as an int (see NS_FORM_* above)
131 * @return the type of this control
133 FormControlType ControlType() const { return mType; }
136 * Reset this form control (as it should be when the user clicks the Reset
137 * button)
139 NS_IMETHOD Reset() = 0;
142 * Tells the form control to submit its names and values to the form data
143 * object
145 * @param aFormData the form data to notify of names/values/files to submit
147 NS_IMETHOD
148 SubmitNamesValues(mozilla::dom::FormData* aFormData) = 0;
151 * Returns whether this is a control which submits the form when activated by
152 * the user.
153 * @return whether this is a submit control.
155 inline bool IsSubmitControl() const;
158 * Returns whether this is a text control.
159 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
160 * @return whether this is a text control.
162 inline bool IsTextControl(bool aExcludePassword) const;
165 * Returns whether this is a single line text control.
166 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
167 * @return whether this is a single line text control.
169 inline bool IsSingleLineTextControl(bool aExcludePassword) const;
172 * Returns whether this is a submittable form control.
173 * @return whether this is a submittable form control.
175 inline bool IsSubmittableControl() const;
178 * https://html.spec.whatwg.org/multipage/forms.html#concept-button
180 inline bool IsConceptButton() const;
183 * Returns whether this is an ordinal button or a concept button that has no
184 * form associated.
186 inline bool IsButtonControl() const;
189 * Returns whether this form control can have draggable children.
190 * @return whether this form control can have draggable children.
192 inline bool AllowDraggableChildren() const;
194 // Returns a number for this form control that is unique within its
195 // owner document. This is used by nsContentUtils::GenerateStateKey
196 // to identify form controls that are inserted into the document by
197 // the parser. -1 is returned for form controls with no state or
198 // which were inserted into the document by some other means than
199 // the parser from the network.
200 virtual int32_t GetParserInsertedControlNumberForStateKey() const {
201 return -1;
204 protected:
206 * Returns whether mType corresponds to a single line text control type.
207 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
208 * @param aType the type to be tested.
209 * @return whether mType corresponds to a single line text control type.
211 inline static bool IsSingleLineTextControl(bool aExcludePassword,
212 FormControlType);
214 inline static bool IsButtonElement(FormControlType aType) {
215 return uint8_t(aType) & kFormControlButtonElementMask;
218 inline static bool IsInputElement(FormControlType aType) {
219 return uint8_t(aType) & kFormControlInputElementMask;
222 FormControlType mType;
225 bool nsIFormControl::IsSubmitControl() const {
226 FormControlType type = ControlType();
227 return type == FormControlType::InputSubmit ||
228 type == FormControlType::InputImage ||
229 type == FormControlType::ButtonSubmit;
232 bool nsIFormControl::IsTextControl(bool aExcludePassword) const {
233 FormControlType type = ControlType();
234 return type == FormControlType::Textarea ||
235 IsSingleLineTextControl(aExcludePassword, type);
238 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const {
239 return IsSingleLineTextControl(aExcludePassword, ControlType());
242 /*static*/
243 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
244 FormControlType aType) {
245 switch (aType) {
246 case FormControlType::InputText:
247 case FormControlType::InputEmail:
248 case FormControlType::InputSearch:
249 case FormControlType::InputTel:
250 case FormControlType::InputUrl:
251 case FormControlType::InputNumber:
252 // TODO: those are temporary until bug 773205 is fixed.
253 case FormControlType::InputMonth:
254 case FormControlType::InputWeek:
255 return true;
256 case FormControlType::InputPassword:
257 return !aExcludePassword;
258 default:
259 return false;
263 bool nsIFormControl::IsSubmittableControl() const {
264 auto type = ControlType();
265 return type == FormControlType::Object || type == FormControlType::Textarea ||
266 type == FormControlType::Select || IsButtonElement(type) ||
267 IsInputElement(type);
270 bool nsIFormControl::IsConceptButton() const {
271 auto type = ControlType();
272 return IsSubmitControl() || type == FormControlType::InputReset ||
273 type == FormControlType::InputButton || IsButtonElement(type);
276 bool nsIFormControl::IsButtonControl() const {
277 return IsConceptButton() && (!GetForm() || !IsSubmitControl());
280 bool nsIFormControl::AllowDraggableChildren() const {
281 auto type = ControlType();
282 return type == FormControlType::Object || type == FormControlType::Fieldset ||
283 type == FormControlType::Output;
286 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
288 #endif /* nsIFormControl_h___ */