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 HTMLFieldSetElement;
18 class HTMLFormSubmission;
19 class DialogFormSubmission;
20 class HTMLFormElement;
21 } // namespace dom
22 } // namespace mozilla
23
24 // Elements with different types, the value is used as a mask.
25 // When changing the order, adding or removing elements, be sure to update
26 // the static_assert checks accordingly.
27 constexpr uint8_t kFormControlButtonElementMask = 0x40; // 0b01000000
28 constexpr uint8_t kFormControlInputElementMask = 0x80; // 0b10000000
29
30 enum class FormControlType : uint8_t {
31 Fieldset = 1,
32 Output,
33 Select,
34 Textarea,
35 Object,
36
37 LastWithoutSubtypes = Object,
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* GetFormElement() = 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
143 * submission object
144 * @param aFormSubmission the form submission to notify of names/values/files
145 * to submit
146 */
147 NS_IMETHOD
148 SubmitNamesValues(mozilla::dom::HTMLFormSubmission* aFormSubmission) = 0;
149
150 /**
151 * Save to presentation state. The form control will determine whether it
152 * has anything to save and if so, create an entry in the layout history for
153 * its pres context.
154 */
155 NS_IMETHOD SaveState() = 0;
156
157 /**
158 * Restore from presentation state. You pass in the presentation state for
159 * this form control (generated with GenerateStateKey() + "-C") and the form
160 * control will grab its state from there.
161 *
162 * @param aState the pres state to use to restore the control
163 * @return true if the form control was a checkbox and its
164 * checked state was restored, false otherwise.
165 */
166 virtual bool RestoreState(mozilla::PresState* aState) = 0;
167
168 virtual bool AllowDrop() = 0;
169
170 /**
171 * Returns whether this is a control which submits the form when activated by
172 * the user.
173 * @return whether this is a submit control.
174 */
175 inline bool IsSubmitControl() const;
176
177 /**
178 * Returns whether this is a text control.
179 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
180 * @return whether this is a text control.
181 */
182 inline bool IsTextControl(bool aExcludePassword) const;
183
184 /**
185 * Returns whether this is a single line text control.
186 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
187 * @return whether this is a single line text control.
188 */
189 inline bool IsSingleLineTextControl(bool aExcludePassword) const;
190
191 /**
192 * Returns whether this is a submittable form control.
193 * @return whether this is a submittable form control.
194 */
195 inline bool IsSubmittableControl() const;
196
197 /**
198 * Returns whether this form control can have draggable children.
199 * @return whether this form control can have draggable children.
200 */
201 inline bool AllowDraggableChildren() const;
202
IsDisabledForEvents(mozilla::WidgetEvent * aEvent)203 virtual bool IsDisabledForEvents(mozilla::WidgetEvent* aEvent) {
204 return false;
205 }
206
207 // Returns a number for this form control that is unique within its
208 // owner document. This is used by nsContentUtils::GenerateStateKey
209 // to identify form controls that are inserted into the document by
210 // the parser. -1 is returned for form controls with no state or
211 // which were inserted into the document by some other means than
212 // the parser from the network.
GetParserInsertedControlNumberForStateKey()213 virtual int32_t GetParserInsertedControlNumberForStateKey() const {
214 return -1;
215 };
216
217 protected:
218 /**
219 * Returns whether mType corresponds to a single line text control type.
220 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
221 * @param aType the type to be tested.
222 * @return whether mType corresponds to a single line text control type.
223 */
224 inline static bool IsSingleLineTextControl(bool aExcludePassword,
225 FormControlType);
226
IsButtonElement(FormControlType aType)227 inline static bool IsButtonElement(FormControlType aType) {
228 return uint8_t(aType) & kFormControlButtonElementMask;
229 }
230
IsInputElement(FormControlType aType)231 inline static bool IsInputElement(FormControlType aType) {
232 return uint8_t(aType) & kFormControlInputElementMask;
233 }
234
235 /**
236 * Returns whether this is a auto-focusable form control.
237 * @return whether this is a auto-focusable form control.
238 */
239 inline bool IsAutofocusable() const;
240
241 FormControlType mType;
242 };
243
IsSubmitControl()244 bool nsIFormControl::IsSubmitControl() const {
245 FormControlType type = ControlType();
246 return type == FormControlType::InputSubmit ||
247 type == FormControlType::InputImage ||
248 type == FormControlType::ButtonSubmit;
249 }
250
IsTextControl(bool aExcludePassword)251 bool nsIFormControl::IsTextControl(bool aExcludePassword) const {
252 FormControlType type = ControlType();
253 return type == FormControlType::Textarea ||
254 IsSingleLineTextControl(aExcludePassword, type);
255 }
256
IsSingleLineTextControl(bool aExcludePassword)257 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const {
258 return IsSingleLineTextControl(aExcludePassword, ControlType());
259 }
260
261 /*static*/
IsSingleLineTextControl(bool aExcludePassword,FormControlType aType)262 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
263 FormControlType aType) {
264 switch (aType) {
265 case FormControlType::InputText:
266 case FormControlType::InputEmail:
267 case FormControlType::InputSearch:
268 case FormControlType::InputTel:
269 case FormControlType::InputUrl:
270 case FormControlType::InputNumber:
271 // TODO: those are temporary until bug 773205 is fixed.
272 case FormControlType::InputMonth:
273 case FormControlType::InputWeek:
274 return true;
275 case FormControlType::InputDatetimeLocal:
276 return !mozilla::StaticPrefs::dom_forms_datetime_local_widget();
277 case FormControlType::InputPassword:
278 return !aExcludePassword;
279 default:
280 return false;
281 }
282 }
283
IsSubmittableControl()284 bool nsIFormControl::IsSubmittableControl() const {
285 auto type = ControlType();
286 return type == FormControlType::Object || type == FormControlType::Textarea ||
287 type == FormControlType::Select || IsButtonElement(type) ||
288 IsInputElement(type);
289 }
290
AllowDraggableChildren()291 bool nsIFormControl::AllowDraggableChildren() const {
292 auto type = ControlType();
293 return type == FormControlType::Object || type == FormControlType::Fieldset ||
294 type == FormControlType::Output;
295 }
296
IsAutofocusable()297 bool nsIFormControl::IsAutofocusable() const {
298 auto type = ControlType();
299 return IsInputElement(type) || IsButtonElement(type) ||
300 type == FormControlType::Textarea || type == FormControlType::Select;
301 }
302
303 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
304
305 #endif /* nsIFormControl_h___ */
306