Bug 1719855 - Take over preventDefaulted infomation for long-tap events to the origin...
[gecko.git] / dom / html / nsIFormControl.h
blob0ca8169675a4bfd20c5fe0daed2fa88d42d54263
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;
150 virtual bool AllowDrop() = 0;
153 * Returns whether this is a control which submits the form when activated by
154 * the user.
155 * @return whether this is a submit control.
157 inline bool IsSubmitControl() const;
160 * Returns whether this is a text control.
161 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
162 * @return whether this is a text control.
164 inline bool IsTextControl(bool aExcludePassword) const;
167 * Returns whether this is a single line text control.
168 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
169 * @return whether this is a single line text control.
171 inline bool IsSingleLineTextControl(bool aExcludePassword) const;
174 * Returns whether this is a submittable form control.
175 * @return whether this is a submittable form control.
177 inline bool IsSubmittableControl() const;
180 * https://html.spec.whatwg.org/multipage/forms.html#concept-button
182 inline bool IsConceptButton() const;
185 * Returns whether this is an ordinal button or a concept button that has no
186 * form associated.
188 inline bool IsButtonControl() const;
191 * Returns whether this form control can have draggable children.
192 * @return whether this form control can have draggable children.
194 inline bool AllowDraggableChildren() const;
196 // Returns a number for this form control that is unique within its
197 // owner document. This is used by nsContentUtils::GenerateStateKey
198 // to identify form controls that are inserted into the document by
199 // the parser. -1 is returned for form controls with no state or
200 // which were inserted into the document by some other means than
201 // the parser from the network.
202 virtual int32_t GetParserInsertedControlNumberForStateKey() const {
203 return -1;
206 protected:
208 * Returns whether mType corresponds to a single line text control type.
209 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
210 * @param aType the type to be tested.
211 * @return whether mType corresponds to a single line text control type.
213 inline static bool IsSingleLineTextControl(bool aExcludePassword,
214 FormControlType);
216 inline static bool IsButtonElement(FormControlType aType) {
217 return uint8_t(aType) & kFormControlButtonElementMask;
220 inline static bool IsInputElement(FormControlType aType) {
221 return uint8_t(aType) & kFormControlInputElementMask;
224 FormControlType mType;
227 bool nsIFormControl::IsSubmitControl() const {
228 FormControlType type = ControlType();
229 return type == FormControlType::InputSubmit ||
230 type == FormControlType::InputImage ||
231 type == FormControlType::ButtonSubmit;
234 bool nsIFormControl::IsTextControl(bool aExcludePassword) const {
235 FormControlType type = ControlType();
236 return type == FormControlType::Textarea ||
237 IsSingleLineTextControl(aExcludePassword, type);
240 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const {
241 return IsSingleLineTextControl(aExcludePassword, ControlType());
244 /*static*/
245 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
246 FormControlType aType) {
247 switch (aType) {
248 case FormControlType::InputText:
249 case FormControlType::InputEmail:
250 case FormControlType::InputSearch:
251 case FormControlType::InputTel:
252 case FormControlType::InputUrl:
253 case FormControlType::InputNumber:
254 // TODO: those are temporary until bug 773205 is fixed.
255 case FormControlType::InputMonth:
256 case FormControlType::InputWeek:
257 return true;
258 case FormControlType::InputPassword:
259 return !aExcludePassword;
260 default:
261 return false;
265 bool nsIFormControl::IsSubmittableControl() const {
266 auto type = ControlType();
267 return type == FormControlType::Object || type == FormControlType::Textarea ||
268 type == FormControlType::Select || IsButtonElement(type) ||
269 IsInputElement(type);
272 bool nsIFormControl::IsConceptButton() const {
273 auto type = ControlType();
274 return IsSubmitControl() || type == FormControlType::InputReset ||
275 type == FormControlType::InputButton || IsButtonElement(type);
278 bool nsIFormControl::IsButtonControl() const {
279 return IsConceptButton() && (!GetForm() || !IsSubmitControl());
282 bool nsIFormControl::AllowDraggableChildren() const {
283 auto type = ControlType();
284 return type == FormControlType::Object || type == FormControlType::Fieldset ||
285 type == FormControlType::Output;
288 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
290 #endif /* nsIFormControl_h___ */