Bug 1270832 - Activate standard c++ library hardening r=glandium
[gecko.git] / dom / html / nsIFormControl.h
blob3f2fafb9caa40423104dec96d28aa0041e71bb7f
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 class nsINode;
14 namespace mozilla {
15 class PresState;
16 namespace dom {
17 class Element;
18 class EventTarget;
19 class FormData;
20 class HTMLFieldSetElement;
21 class HTMLFormElement;
22 } // namespace dom
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 {
32 Fieldset = 1,
33 Output,
34 Select,
35 Textarea,
36 Object,
37 FormAssociatedCustomElement,
39 LastWithoutSubtypes = FormAssociatedCustomElement,
41 ButtonButton = kFormControlButtonElementMask + 1,
42 ButtonReset,
43 ButtonSubmit,
44 LastButtonElement = ButtonSubmit,
46 InputButton = kFormControlInputElementMask + 1,
47 InputCheckbox,
48 InputColor,
49 InputDate,
50 InputEmail,
51 InputFile,
52 InputHidden,
53 InputReset,
54 InputImage,
55 InputMonth,
56 InputNumber,
57 InputPassword,
58 InputRadio,
59 InputSearch,
60 InputSubmit,
61 InputTel,
62 InputText,
63 InputTime,
64 InputUrl,
65 InputRange,
66 InputWeek,
67 InputDatetimeLocal,
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 \
81 { \
82 0x4b89980c, 0x4dcd, 0x428f, { \
83 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec \
84 } \
87 /**
88 * Interface which all form controls (e.g. buttons, checkboxes, text,
89 * radio buttons, select, etc) implement in addition to their dom specific
90 * interface.
92 class nsIFormControl : public nsISupports {
93 public:
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.
119 * @return the form
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
130 * form.
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
140 * from tree.
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
152 * button)
154 NS_IMETHOD Reset() = 0;
157 * Tells the form control to submit its names and values to the form data
158 * object
160 * @param aFormData the form data to notify of names/values/files to submit
162 NS_IMETHOD
163 SubmitNamesValues(mozilla::dom::FormData* aFormData) = 0;
166 * Returns whether this is a control which submits the form when activated by
167 * the user.
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
199 * form associated.
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 {
216 return -1;
219 protected:
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,
227 FormControlType);
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());
257 /*static*/
258 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
259 FormControlType aType) {
260 switch (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:
270 return true;
271 case FormControlType::InputPassword:
272 return !aExcludePassword;
273 default:
274 return false;
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___ */