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___
8
9 #include "mozilla/EventForwards.h"
10 #include "mozilla/StaticPrefs_dom.h"
11 #include "nsISupports.h"
12
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
22
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
28
29 enum class FormControlType : uint8_t {
30 Fieldset = 1,
31 Output,
32 Select,
33 Textarea,
34 Object,
35 FormAssociatedCustomElement,
36
37 LastWithoutSubtypes = FormAssociatedCustomElement,
38
39 ButtonButton = kFormControlButtonElementMask + 1,
40 ButtonReset,
41 ButtonSubmit,
42 LastButtonElement = ButtonSubmit,
43
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,
67 };
68
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");
77
78 #define NS_IFORMCONTROL_IID \
79 { \
80 0x4b89980c, 0x4dcd, 0x428f, { \
81 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec \
82 } \
83 }
84
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.
89 */
90 class nsIFormControl : public nsISupports {
91 public:
nsIFormControl(FormControlType aType)92 nsIFormControl(FormControlType aType) : mType(aType) {}
93
94 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
95
96 /**
97 * Get the fieldset for this form control.
98 * @return the fieldset
99 */
100 virtual mozilla::dom::HTMLFieldSetElement* GetFieldSet() = 0;
101
102 /**
103 * Get the form for this form control.
104 * @return the form
105 */
106 virtual mozilla::dom::HTMLFormElement* GetForm() const = 0;
107
108 /**
109 * Set the form for this form control.
110 * @param aForm the form. This must not be null.
111 *
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.
116 */
117 virtual void SetForm(mozilla::dom::HTMLFormElement* aForm) = 0;
118
119 /**
120 * Tell the control to forget about its form.
121 *
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.
126 */
127 virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
128
129 /**
130 * Get the type of this control as an int (see NS_FORM_* above)
131 * @return the type of this control
132 */
ControlType()133 FormControlType ControlType() const { return mType; }
134
135 /**
136 * Reset this form control (as it should be when the user clicks the Reset
137 * button)
138 */
139 NS_IMETHOD Reset() = 0;
140
141 /**
142 * Tells the form control to submit its names and values to the form data
143 * object
144 *
145 * @param aFormData the form data to notify of names/values/files to submit
146 */
147 NS_IMETHOD
148 SubmitNamesValues(mozilla::dom::FormData* aFormData) = 0;
149
150 virtual bool AllowDrop() = 0;
151
152 /**
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.
156 */
157 inline bool IsSubmitControl() const;
158
159 /**
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.
163 */
164 inline bool IsTextControl(bool aExcludePassword) const;
165
166 /**
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.
170 */
171 inline bool IsSingleLineTextControl(bool aExcludePassword) const;
172
173 /**
174 * Returns whether this is a submittable form control.
175 * @return whether this is a submittable form control.
176 */
177 inline bool IsSubmittableControl() const;
178
179 /**
180 * Returns whether this form control can have draggable children.
181 * @return whether this form control can have draggable children.
182 */
183 inline bool AllowDraggableChildren() const;
184
185 // Returns a number for this form control that is unique within its
186 // owner document. This is used by nsContentUtils::GenerateStateKey
187 // to identify form controls that are inserted into the document by
188 // the parser. -1 is returned for form controls with no state or
189 // which were inserted into the document by some other means than
190 // the parser from the network.
GetParserInsertedControlNumberForStateKey()191 virtual int32_t GetParserInsertedControlNumberForStateKey() const {
192 return -1;
193 };
194
195 protected:
196 /**
197 * Returns whether mType corresponds to a single line text control type.
198 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
199 * @param aType the type to be tested.
200 * @return whether mType corresponds to a single line text control type.
201 */
202 inline static bool IsSingleLineTextControl(bool aExcludePassword,
203 FormControlType);
204
IsButtonElement(FormControlType aType)205 inline static bool IsButtonElement(FormControlType aType) {
206 return uint8_t(aType) & kFormControlButtonElementMask;
207 }
208
IsInputElement(FormControlType aType)209 inline static bool IsInputElement(FormControlType aType) {
210 return uint8_t(aType) & kFormControlInputElementMask;
211 }
212
213 FormControlType mType;
214 };
215
IsSubmitControl()216 bool nsIFormControl::IsSubmitControl() const {
217 FormControlType type = ControlType();
218 return type == FormControlType::InputSubmit ||
219 type == FormControlType::InputImage ||
220 type == FormControlType::ButtonSubmit;
221 }
222
IsTextControl(bool aExcludePassword)223 bool nsIFormControl::IsTextControl(bool aExcludePassword) const {
224 FormControlType type = ControlType();
225 return type == FormControlType::Textarea ||
226 IsSingleLineTextControl(aExcludePassword, type);
227 }
228
IsSingleLineTextControl(bool aExcludePassword)229 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const {
230 return IsSingleLineTextControl(aExcludePassword, ControlType());
231 }
232
233 /*static*/
IsSingleLineTextControl(bool aExcludePassword,FormControlType aType)234 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
235 FormControlType aType) {
236 switch (aType) {
237 case FormControlType::InputText:
238 case FormControlType::InputEmail:
239 case FormControlType::InputSearch:
240 case FormControlType::InputTel:
241 case FormControlType::InputUrl:
242 case FormControlType::InputNumber:
243 // TODO: those are temporary until bug 773205 is fixed.
244 case FormControlType::InputMonth:
245 case FormControlType::InputWeek:
246 return true;
247 case FormControlType::InputDatetimeLocal:
248 return !mozilla::StaticPrefs::dom_forms_datetime_local_widget();
249 case FormControlType::InputPassword:
250 return !aExcludePassword;
251 default:
252 return false;
253 }
254 }
255
IsSubmittableControl()256 bool nsIFormControl::IsSubmittableControl() const {
257 auto type = ControlType();
258 return type == FormControlType::Object || type == FormControlType::Textarea ||
259 type == FormControlType::Select || IsButtonElement(type) ||
260 IsInputElement(type);
261 }
262
AllowDraggableChildren()263 bool nsIFormControl::AllowDraggableChildren() const {
264 auto type = ControlType();
265 return type == FormControlType::Object || type == FormControlType::Fieldset ||
266 type == FormControlType::Output;
267 }
268
269 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
270
271 #endif /* nsIFormControl_h___ */
272