Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/nsIFormControl.h
Line
Count
Source (jump to first uncovered line)
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 "nsISupports.h"
11
12
namespace mozilla {
13
class PresState;
14
namespace dom {
15
class Element;
16
class HTMLFieldSetElement;
17
class HTMLFormSubmission;
18
class HTMLFormElement;
19
} // namespace dom
20
} // namespace mozilla
21
22
enum FormControlsTypes {
23
  NS_FORM_FIELDSET = 1,
24
  NS_FORM_OUTPUT,
25
  NS_FORM_SELECT,
26
  NS_FORM_TEXTAREA,
27
  NS_FORM_OBJECT,
28
  eFormControlsWithoutSubTypesMax,
29
  // After this, all types will have sub-types which introduce new enum lists.
30
  // eFormControlsWithoutSubTypesMax let us know if the previous types values
31
  // are not overlapping with sub-types/masks.
32
33
  // Elements with different types, the value is used as a mask.
34
  // When changing the order, adding or removing elements, be sure to update
35
  // the static_assert checks accordingly.
36
  NS_FORM_BUTTON_ELEMENT = 0x40, // 0b01000000
37
  NS_FORM_INPUT_ELEMENT  = 0x80  // 0b10000000
38
};
39
40
enum ButtonElementTypes : uint8_t {
41
  NS_FORM_BUTTON_BUTTON = NS_FORM_BUTTON_ELEMENT + 1,
42
  NS_FORM_BUTTON_RESET,
43
  NS_FORM_BUTTON_SUBMIT,
44
  eButtonElementTypesMax
45
};
46
47
enum InputElementTypes : uint8_t {
48
  NS_FORM_INPUT_BUTTON = NS_FORM_INPUT_ELEMENT + 1,
49
  NS_FORM_INPUT_CHECKBOX,
50
  NS_FORM_INPUT_COLOR,
51
  NS_FORM_INPUT_DATE,
52
  NS_FORM_INPUT_EMAIL,
53
  NS_FORM_INPUT_FILE,
54
  NS_FORM_INPUT_HIDDEN,
55
  NS_FORM_INPUT_RESET,
56
  NS_FORM_INPUT_IMAGE,
57
  NS_FORM_INPUT_MONTH,
58
  NS_FORM_INPUT_NUMBER,
59
  NS_FORM_INPUT_PASSWORD,
60
  NS_FORM_INPUT_RADIO,
61
  NS_FORM_INPUT_SEARCH,
62
  NS_FORM_INPUT_SUBMIT,
63
  NS_FORM_INPUT_TEL,
64
  NS_FORM_INPUT_TEXT,
65
  NS_FORM_INPUT_TIME,
66
  NS_FORM_INPUT_URL,
67
  NS_FORM_INPUT_RANGE,
68
  NS_FORM_INPUT_WEEK,
69
  NS_FORM_INPUT_DATETIME_LOCAL,
70
  eInputElementTypesMax
71
};
72
73
static_assert(static_cast<uint32_t>(eFormControlsWithoutSubTypesMax) <
74
              static_cast<uint32_t>(NS_FORM_BUTTON_ELEMENT),
75
              "Too many FormControlsTypes without sub-types");
76
static_assert(static_cast<uint32_t>(eButtonElementTypesMax) <
77
              static_cast<uint32_t>(NS_FORM_INPUT_ELEMENT),
78
              "Too many ButtonElementTypes");
79
static_assert(static_cast<uint32_t>(eInputElementTypesMax) < 1<<8,
80
              "Too many form control types");
81
82
#define NS_IFORMCONTROL_IID   \
83
{ 0x4b89980c, 0x4dcd, 0x428f, \
84
  { 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec } }
85
86
/**
87
 * Interface which all form controls (e.g. buttons, checkboxes, text,
88
 * radio buttons, select, etc) implement in addition to their dom specific
89
 * interface.
90
 */
91
class nsIFormControl : public nsISupports
92
{
93
public:
94
  nsIFormControl(uint8_t aType)
95
  : mType(aType)
96
  {
97
  }
98
99
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
100
101
  /**
102
   * Get the fieldset for this form control.
103
   * @return the fieldset
104
   */
105
  virtual mozilla::dom::HTMLFieldSetElement *GetFieldSet() = 0;
106
107
  /**
108
   * Get the form for this form control.
109
   * @return the form
110
   */
111
  virtual mozilla::dom::Element *GetFormElement() = 0;
112
113
  /**
114
   * Set the form for this form control.
115
   * @param aForm the form.  This must not be null.
116
   *
117
   * @note that when setting the form the control is not added to the
118
   * form.  It adds itself when it gets bound to the tree thereafter,
119
   * so that it can be properly sorted with the other controls in the
120
   * form.
121
   */
122
  virtual void SetForm(mozilla::dom::HTMLFormElement* aForm) = 0;
123
124
  /**
125
   * Tell the control to forget about its form.
126
   *
127
   * @param aRemoveFromForm set false if you do not want this element removed
128
   *        from the form.  (Used by nsFormControlList::Clear())
129
   * @param aUnbindOrDelete set true if the element is being deleted or unbound
130
   *        from tree.
131
   */
132
  virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
133
134
  /**
135
   * Get the type of this control as an int (see NS_FORM_* above)
136
   * @return the type of this control
137
   */
138
  uint32_t ControlType() const { return mType; }
139
140
  /**
141
   * Reset this form control (as it should be when the user clicks the Reset
142
   * button)
143
   */
144
  NS_IMETHOD Reset() = 0;
145
146
  /**
147
   * Tells the form control to submit its names and values to the form
148
   * submission object
149
   * @param aFormSubmission the form submission to notify of names/values/files
150
   *                       to submit
151
   */
152
  NS_IMETHOD
153
  SubmitNamesValues(mozilla::dom::HTMLFormSubmission* aFormSubmission) = 0;
154
155
  /**
156
   * Save to presentation state.  The form control will determine whether it
157
   * has anything to save and if so, create an entry in the layout history for
158
   * its pres context.
159
   */
160
  NS_IMETHOD SaveState() = 0;
161
162
  /**
163
   * Restore from presentation state.  You pass in the presentation state for
164
   * this form control (generated with GenerateStateKey() + "-C") and the form
165
   * control will grab its state from there.
166
   *
167
   * @param aState the pres state to use to restore the control
168
   * @return true if the form control was a checkbox and its
169
   *         checked state was restored, false otherwise.
170
   */
171
  virtual bool RestoreState(mozilla::PresState* aState) = 0;
172
173
  virtual bool AllowDrop() = 0;
174
175
  /**
176
   * Returns whether this is a control which submits the form when activated by
177
   * the user.
178
   * @return whether this is a submit control.
179
   */
180
  inline bool IsSubmitControl() const;
181
182
  /**
183
   * Returns whether this is a text control.
184
   * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
185
   * @return whether this is a text control.
186
   */
187
  inline bool IsTextControl(bool aExcludePassword) const;
188
189
  /**
190
   * Returns true if this is a text control or a number control.
191
   * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
192
   * @return true if this is a text control or a number control.
193
   */
194
  inline bool IsTextOrNumberControl(bool aExcludePassword) const;
195
196
  /**
197
   * Returns whether this is a single line text control.
198
   * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
199
   * @return whether this is a single line text control.
200
   */
201
  inline bool IsSingleLineTextControl(bool aExcludePassword) const;
202
203
  /**
204
  * Returns true if this is a single line text control or a number control.
205
  * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
206
  * @return true if this is a single line text control or a number control.
207
  */
208
  inline bool IsSingleLineTextOrNumberControl(bool aExcludePassword) const;
209
210
  /**
211
   * Returns whether this is a submittable form control.
212
   * @return whether this is a submittable form control.
213
   */
214
  inline bool IsSubmittableControl() const;
215
216
  /**
217
   * Returns whether this form control can have draggable children.
218
   * @return whether this form control can have draggable children.
219
   */
220
  inline bool AllowDraggableChildren() const;
221
222
  virtual bool IsDisabledForEvents(mozilla::EventMessage aMessage)
223
  {
224
    return false;
225
  }
226
protected:
227
228
  /**
229
   * Returns whether mType corresponds to a single line text control type.
230
   * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
231
   * @param aType the type to be tested.
232
   * @return whether mType corresponds to a single line text control type.
233
   */
234
  inline static bool IsSingleLineTextControl(bool aExcludePassword, uint32_t aType);
235
236
  /**
237
   * Returns whether this is a auto-focusable form control.
238
   * @return whether this is a auto-focusable form control.
239
   */
240
  inline bool IsAutofocusable() const;
241
242
  uint8_t                  mType;
243
};
244
245
bool
246
nsIFormControl::IsSubmitControl() const
247
{
248
  uint32_t type = ControlType();
249
  return type == NS_FORM_INPUT_SUBMIT ||
250
         type == NS_FORM_INPUT_IMAGE ||
251
         type == NS_FORM_BUTTON_SUBMIT;
252
}
253
254
bool
255
nsIFormControl::IsTextControl(bool aExcludePassword) const
256
{
257
  uint32_t type = ControlType();
258
  return type == NS_FORM_TEXTAREA ||
259
         IsSingleLineTextControl(aExcludePassword, type);
260
}
261
262
bool
263
nsIFormControl::IsTextOrNumberControl(bool aExcludePassword) const
264
{
265
  return IsTextControl(aExcludePassword) || ControlType() == NS_FORM_INPUT_NUMBER;
266
}
267
268
bool
269
nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const
270
{
271
  return IsSingleLineTextControl(aExcludePassword, ControlType());
272
}
273
274
bool
275
nsIFormControl::IsSingleLineTextOrNumberControl(bool aExcludePassword) const
276
{
277
  return IsSingleLineTextControl(aExcludePassword) ||
278
         ControlType() == NS_FORM_INPUT_NUMBER;
279
}
280
281
/*static*/
282
bool
283
nsIFormControl::IsSingleLineTextControl(bool aExcludePassword, uint32_t aType)
284
{
285
  return aType == NS_FORM_INPUT_TEXT ||
286
         aType == NS_FORM_INPUT_EMAIL ||
287
         aType == NS_FORM_INPUT_SEARCH ||
288
         aType == NS_FORM_INPUT_TEL ||
289
         aType == NS_FORM_INPUT_URL ||
290
         // TODO: those are temporary until bug 773205 is fixed.
291
         aType == NS_FORM_INPUT_MONTH ||
292
         aType == NS_FORM_INPUT_WEEK ||
293
         aType == NS_FORM_INPUT_DATETIME_LOCAL ||
294
         (!aExcludePassword && aType == NS_FORM_INPUT_PASSWORD);
295
}
296
297
bool
298
nsIFormControl::IsSubmittableControl() const
299
0
{
300
0
  // TODO: keygen should be in that list, see bug 101019.
301
0
  uint32_t type = ControlType();
302
0
  return type == NS_FORM_OBJECT ||
303
0
         type == NS_FORM_TEXTAREA ||
304
0
         type == NS_FORM_SELECT ||
305
0
         // type == NS_FORM_KEYGEN ||
306
0
         type & NS_FORM_BUTTON_ELEMENT ||
307
0
         type & NS_FORM_INPUT_ELEMENT;
308
0
}
309
310
bool
311
nsIFormControl::AllowDraggableChildren() const
312
{
313
  uint32_t type = ControlType();
314
  return type == NS_FORM_OBJECT ||
315
         type == NS_FORM_FIELDSET ||
316
         type == NS_FORM_OUTPUT;
317
}
318
319
bool
320
nsIFormControl::IsAutofocusable() const
321
{
322
  uint32_t type = ControlType();
323
  return type & NS_FORM_INPUT_ELEMENT ||
324
         type & NS_FORM_BUTTON_ELEMENT ||
325
         type == NS_FORM_TEXTAREA ||
326
         type == NS_FORM_SELECT;
327
}
328
329
NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
330
331
#endif /* nsIFormControl_h___ */