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"
20 class HTMLFieldSetElement
;
21 class HTMLFormElement
;
23 } // namespace mozilla
25 // Elements with different types, the value is used as a mask.
26 // When changing the order, adding or removing elements, be sure to update
27 // the static_assert checks accordingly.
28 constexpr uint8_t kFormControlButtonElementMask
= 0x40; // 0b01000000
29 constexpr uint8_t kFormControlInputElementMask
= 0x80; // 0b10000000
31 enum class FormControlType
: uint8_t {
37 FormAssociatedCustomElement
,
39 LastWithoutSubtypes
= FormAssociatedCustomElement
,
41 ButtonButton
= kFormControlButtonElementMask
+ 1,
44 LastButtonElement
= ButtonSubmit
,
46 InputButton
= kFormControlInputElementMask
+ 1,
68 LastInputElement
= InputDatetimeLocal
,
71 static_assert(uint8_t(FormControlType::LastWithoutSubtypes
) <
72 kFormControlButtonElementMask
,
73 "Too many FormControlsTypes without sub-types");
74 static_assert(uint8_t(FormControlType::LastButtonElement
) <
75 kFormControlInputElementMask
,
76 "Too many ButtonElementTypes");
77 static_assert(uint32_t(FormControlType::LastInputElement
) < (1 << 8),
78 "Too many form control types");
80 #define NS_IFORMCONTROL_IID \
82 0x4b89980c, 0x4dcd, 0x428f, { \
83 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec \
88 * Interface which all form controls (e.g. buttons, checkboxes, text,
89 * radio buttons, select, etc) implement in addition to their dom specific
92 class nsIFormControl
: public nsISupports
{
94 nsIFormControl(FormControlType aType
) : mType(aType
) {}
96 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID
)
98 static nsIFormControl
* FromEventTarget(mozilla::dom::EventTarget
* aTarget
);
99 static nsIFormControl
* FromEventTargetOrNull(
100 mozilla::dom::EventTarget
* aTarget
);
101 static const nsIFormControl
* FromEventTarget(
102 const mozilla::dom::EventTarget
* aTarget
);
103 static const nsIFormControl
* FromEventTargetOrNull(
104 const mozilla::dom::EventTarget
* aTarget
);
106 static nsIFormControl
* FromNode(nsINode
* aNode
);
107 static nsIFormControl
* FromNodeOrNull(nsINode
* aNode
);
108 static const nsIFormControl
* FromNode(const nsINode
* aNode
);
109 static const nsIFormControl
* FromNodeOrNull(const nsINode
* aNode
);
112 * Get the fieldset for this form control.
113 * @return the fieldset
115 virtual mozilla::dom::HTMLFieldSetElement
* GetFieldSet() = 0;
118 * Get the form for this form control.
121 virtual mozilla::dom::HTMLFormElement
* GetForm() const = 0;
124 * Set the form for this form control.
125 * @param aForm the form. This must not be null.
127 * @note that when setting the form the control is not added to the
128 * form. It adds itself when it gets bound to the tree thereafter,
129 * so that it can be properly sorted with the other controls in the
132 virtual void SetForm(mozilla::dom::HTMLFormElement
* aForm
) = 0;
135 * Tell the control to forget about its form.
137 * @param aRemoveFromForm set false if you do not want this element removed
138 * from the form. (Used by nsFormControlList::Clear())
139 * @param aUnbindOrDelete set true if the element is being deleted or unbound
142 virtual void ClearForm(bool aRemoveFromForm
, bool aUnbindOrDelete
) = 0;
145 * Get the type of this control as an int (see NS_FORM_* above)
146 * @return the type of this control
148 FormControlType
ControlType() const { return mType
; }
151 * Reset this form control (as it should be when the user clicks the Reset
154 NS_IMETHOD
Reset() = 0;
157 * Tells the form control to submit its names and values to the form data
160 * @param aFormData the form data to notify of names/values/files to submit
163 SubmitNamesValues(mozilla::dom::FormData
* aFormData
) = 0;
166 * Returns whether this is a control which submits the form when activated by
168 * @return whether this is a submit control.
170 inline bool IsSubmitControl() const;
173 * Returns whether this is a text control.
174 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
175 * @return whether this is a text control.
177 inline bool IsTextControl(bool aExcludePassword
) const;
180 * Returns whether this is a single line text control.
181 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
182 * @return whether this is a single line text control.
184 inline bool IsSingleLineTextControl(bool aExcludePassword
) const;
187 * Returns whether this is a submittable form control.
188 * @return whether this is a submittable form control.
190 inline bool IsSubmittableControl() const;
193 * https://html.spec.whatwg.org/multipage/forms.html#concept-button
195 inline bool IsConceptButton() const;
198 * Returns whether this is an ordinal button or a concept button that has no
201 inline bool IsButtonControl() const;
204 * Returns whether this form control can have draggable children.
205 * @return whether this form control can have draggable children.
207 inline bool AllowDraggableChildren() const;
209 // Returns a number for this form control that is unique within its
210 // owner document. This is used by nsContentUtils::GenerateStateKey
211 // to identify form controls that are inserted into the document by
212 // the parser. -1 is returned for form controls with no state or
213 // which were inserted into the document by some other means than
214 // the parser from the network.
215 virtual int32_t GetParserInsertedControlNumberForStateKey() const {
221 * Returns whether mType corresponds to a single line text control type.
222 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
223 * @param aType the type to be tested.
224 * @return whether mType corresponds to a single line text control type.
226 inline static bool IsSingleLineTextControl(bool aExcludePassword
,
229 inline static bool IsButtonElement(FormControlType aType
) {
230 return uint8_t(aType
) & kFormControlButtonElementMask
;
233 inline static bool IsInputElement(FormControlType aType
) {
234 return uint8_t(aType
) & kFormControlInputElementMask
;
237 FormControlType mType
;
240 bool nsIFormControl::IsSubmitControl() const {
241 FormControlType type
= ControlType();
242 return type
== FormControlType::InputSubmit
||
243 type
== FormControlType::InputImage
||
244 type
== FormControlType::ButtonSubmit
;
247 bool nsIFormControl::IsTextControl(bool aExcludePassword
) const {
248 FormControlType type
= ControlType();
249 return type
== FormControlType::Textarea
||
250 IsSingleLineTextControl(aExcludePassword
, type
);
253 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword
) const {
254 return IsSingleLineTextControl(aExcludePassword
, ControlType());
258 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword
,
259 FormControlType aType
) {
261 case FormControlType::InputText
:
262 case FormControlType::InputEmail
:
263 case FormControlType::InputSearch
:
264 case FormControlType::InputTel
:
265 case FormControlType::InputUrl
:
266 case FormControlType::InputNumber
:
267 // TODO: those are temporary until bug 773205 is fixed.
268 case FormControlType::InputMonth
:
269 case FormControlType::InputWeek
:
271 case FormControlType::InputPassword
:
272 return !aExcludePassword
;
278 bool nsIFormControl::IsSubmittableControl() const {
279 auto type
= ControlType();
280 return type
== FormControlType::Object
|| type
== FormControlType::Textarea
||
281 type
== FormControlType::Select
|| IsButtonElement(type
) ||
282 IsInputElement(type
);
285 bool nsIFormControl::IsConceptButton() const {
286 auto type
= ControlType();
287 return IsSubmitControl() || type
== FormControlType::InputReset
||
288 type
== FormControlType::InputButton
|| IsButtonElement(type
);
291 bool nsIFormControl::IsButtonControl() const {
292 return IsConceptButton() && (!GetForm() || !IsSubmitControl());
295 bool nsIFormControl::AllowDraggableChildren() const {
296 auto type
= ControlType();
297 return type
== FormControlType::Object
|| type
== FormControlType::Fieldset
||
298 type
== FormControlType::Output
;
301 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl
, NS_IFORMCONTROL_IID
)
303 #endif /* nsIFormControl_h___ */