1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsIFormControl_h___
#define nsIFormControl_h___
#include "mozilla/EventForwards.h"
#include "mozilla/StaticPrefs_dom.h"
#include "nsISupports.h"
class nsINode;
namespace mozilla {
class PresState;
namespace dom {
class Element;
class EventTarget;
class FormData;
class HTMLFieldSetElement;
class HTMLFormElement;
} // namespace dom
} // namespace mozilla
// Elements with different types, the value is used as a mask.
// When changing the order, adding or removing elements, be sure to update
// the static_assert checks accordingly.
constexpr uint8_t kFormControlButtonElementMask = 0x40; // 0b01000000
constexpr uint8_t kFormControlInputElementMask = 0x80; // 0b10000000
enum class FormControlType : uint8_t {
Fieldset = 1,
Output,
Select,
Textarea,
Object,
FormAssociatedCustomElement,
LastWithoutSubtypes = FormAssociatedCustomElement,
ButtonButton = kFormControlButtonElementMask + 1,
ButtonReset,
ButtonSubmit,
LastButtonElement = ButtonSubmit,
InputButton = kFormControlInputElementMask + 1,
InputCheckbox,
InputColor,
InputDate,
InputEmail,
InputFile,
InputHidden,
InputReset,
InputImage,
InputMonth,
InputNumber,
InputPassword,
InputRadio,
InputSearch,
InputSubmit,
InputTel,
InputText,
InputTime,
InputUrl,
InputRange,
InputWeek,
InputDatetimeLocal,
LastInputElement = InputDatetimeLocal,
};
static_assert(uint8_t(FormControlType::LastWithoutSubtypes) <
kFormControlButtonElementMask,
"Too many FormControlsTypes without sub-types");
static_assert(uint8_t(FormControlType::LastButtonElement) <
kFormControlInputElementMask,
"Too many ButtonElementTypes");
static_assert(uint32_t(FormControlType::LastInputElement) < (1 << 8),
"Too many form control types");
#define NS_IFORMCONTROL_IID \
{0x4b89980c, 0x4dcd, 0x428f, {0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec}}
/**
* Interface which all form controls (e.g. buttons, checkboxes, text,
* radio buttons, select, etc) implement in addition to their dom specific
* interface.
*/
class nsIFormControl : public nsISupports {
public:
nsIFormControl(FormControlType aType) : mType(aType) {}
NS_INLINE_DECL_STATIC_IID(NS_IFORMCONTROL_IID)
static nsIFormControl* FromEventTarget(mozilla::dom::EventTarget* aTarget);
static nsIFormControl* FromEventTargetOrNull(
mozilla::dom::EventTarget* aTarget);
static const nsIFormControl* FromEventTarget(
const mozilla::dom::EventTarget* aTarget);
static const nsIFormControl* FromEventTargetOrNull(
const mozilla::dom::EventTarget* aTarget);
static nsIFormControl* FromNode(nsINode* aNode);
static nsIFormControl* FromNodeOrNull(nsINode* aNode);
static const nsIFormControl* FromNode(const nsINode* aNode);
static const nsIFormControl* FromNodeOrNull(const nsINode* aNode);
/**
* Get the fieldset for this form control.
* @return the fieldset
*/
virtual mozilla::dom::HTMLFieldSetElement* GetFieldSet() = 0;
/**
* Get the form for this form control.
* @return the form
*/
virtual mozilla::dom::HTMLFormElement* GetForm() const = 0;
/**
* Set the form for this form control.
* @param aForm the form. This must not be null.
*
* @note that when setting the form the control is not added to the
* form. It adds itself when it gets bound to the tree thereafter,
* so that it can be properly sorted with the other controls in the
* form.
*/
virtual void SetForm(mozilla::dom::HTMLFormElement* aForm) = 0;
/**
* Tell the control to forget about its form.
*
* @param aRemoveFromForm set false if you do not want this element removed
* from the form. (Used by nsFormControlList::Clear())
* @param aUnbindOrDelete set true if the element is being deleted or unbound
* from tree.
*/
virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
/**
* Get the type of this control as an int (see NS_FORM_* above)
* @return the type of this control
*/
FormControlType ControlType() const { return mType; }
/**
* Reset this form control (as it should be when the user clicks the Reset
* button)
*/
NS_IMETHOD Reset() = 0;
/**
* Tells the form control to submit its names and values to the form data
* object
*
* @param aFormData the form data to notify of names/values/files to submit
*/
NS_IMETHOD
SubmitNamesValues(mozilla::dom::FormData* aFormData) = 0;
/**
* Returns whether this is a control which submits the form when activated by
* the user.
* @return whether this is a submit control.
*/
inline bool IsSubmitControl() const;
/**
* Returns whether this is a text control.
* @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
* @return whether this is a text control.
*/
inline bool IsTextControl(bool aExcludePassword) const;
/**
* Returns whether this is a single line text control.
* @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
* @return whether this is a single line text control.
*/
inline bool IsSingleLineTextControl(bool aExcludePassword) const;
/**
* Returns whether this is a submittable form control.
* @return whether this is a submittable form control.
*/
inline bool IsSubmittableControl() const;
/**
* https://html.spec.whatwg.org/multipage/forms.html#concept-button
*/
inline bool IsConceptButton() const;
/**
* Returns whether this is an ordinal button or a concept button that has no
* form associated.
*/
inline bool IsButtonControl() const;
/**
* Returns whether this form control can have draggable children.
* @return whether this form control can have draggable children.
*/
inline bool AllowDraggableChildren() const;
// Returns a number for this form control that is unique within its
// owner document. This is used by nsContentUtils::GenerateStateKey
// to identify form controls that are inserted into the document by
// the parser. -1 is returned for form controls with no state or
// which were inserted into the document by some other means than
// the parser from the network.
virtual int32_t GetParserInsertedControlNumberForStateKey() const {
return -1;
};
protected:
/**
* Returns whether mType corresponds to a single line text control type.
* @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
* @param aType the type to be tested.
* @return whether mType corresponds to a single line text control type.
*/
inline static bool IsSingleLineTextControl(bool aExcludePassword,
FormControlType);
inline static bool IsButtonElement(FormControlType aType) {
return uint8_t(aType) & kFormControlButtonElementMask;
}
inline static bool IsInputElement(FormControlType aType) {
return uint8_t(aType) & kFormControlInputElementMask;
}
FormControlType mType;
};
bool nsIFormControl::IsSubmitControl() const {
FormControlType type = ControlType();
return type == FormControlType::InputSubmit ||
type == FormControlType::InputImage ||
type == FormControlType::ButtonSubmit;
}
bool nsIFormControl::IsTextControl(bool aExcludePassword) const {
FormControlType type = ControlType();
return type == FormControlType::Textarea ||
IsSingleLineTextControl(aExcludePassword, type);
}
bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const {
return IsSingleLineTextControl(aExcludePassword, ControlType());
}
/*static*/
bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
FormControlType aType) {
switch (aType) {
case FormControlType::InputText:
case FormControlType::InputEmail:
case FormControlType::InputSearch:
case FormControlType::InputTel:
case FormControlType::InputUrl:
case FormControlType::InputNumber:
// TODO: those are temporary until bug 773205 is fixed.
case FormControlType::InputMonth:
case FormControlType::InputWeek:
return true;
case FormControlType::InputPassword:
return !aExcludePassword;
default:
return false;
}
}
bool nsIFormControl::IsSubmittableControl() const {
auto type = ControlType();
return type == FormControlType::Object || type == FormControlType::Textarea ||
type == FormControlType::Select || IsButtonElement(type) ||
IsInputElement(type);
}
bool nsIFormControl::IsConceptButton() const {
auto type = ControlType();
return IsSubmitControl() || type == FormControlType::InputReset ||
type == FormControlType::InputButton || IsButtonElement(type);
}
bool nsIFormControl::IsButtonControl() const {
return IsConceptButton() && (!GetForm() || !IsSubmitControl());
}
bool nsIFormControl::AllowDraggableChildren() const {
auto type = ControlType();
return type == FormControlType::Object || type == FormControlType::Fieldset ||
type == FormControlType::Output;
}
#endif /* nsIFormControl_h___ */
|