1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 #ifndef mozilla_dom_ElementInternals_h
8 #define mozilla_dom_ElementInternals_h
10 #include "js/TypeDecls.h"
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/dom/ElementInternalsBinding.h"
13 #include "mozilla/dom/UnionTypes.h"
14 #include "mozilla/dom/CustomStateSet.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsIConstraintValidation.h"
17 #include "nsIFormControl.h"
18 #include "nsWrapperCache.h"
19 #include "AttrArray.h"
20 #include "nsGkAtoms.h"
22 #define ARIA_REFLECT_ATTR(method, attr) \
23 void Get##method(nsAString& aValue) const { \
24 GetAttr(nsGkAtoms::attr, aValue); \
26 void Set##method(const nsAString& aValue, ErrorResult& aResult) { \
27 aResult = ErrorResult(SetAttr(nsGkAtoms::attr, aValue)); \
31 class nsGenericHTMLElement
;
33 namespace mozilla::dom
{
37 class HTMLFieldSetElement
;
38 class HTMLFormElement
;
42 class ElementInternals final
: public nsIFormControl
,
43 public nsIConstraintValidation
,
44 public nsWrapperCache
{
46 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
47 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(ElementInternals
,
50 explicit ElementInternals(HTMLElement
* aTarget
);
52 nsISupports
* GetParentObject();
54 virtual JSObject
* WrapObject(JSContext
* aCx
,
55 JS::Handle
<JSObject
*> aGivenProto
) override
;
58 ShadowRoot
* GetShadowRoot() const;
59 void SetFormValue(const Nullable
<FileOrUSVStringOrFormData
>& aValue
,
60 const Optional
<Nullable
<FileOrUSVStringOrFormData
>>& aState
,
62 mozilla::dom::HTMLFormElement
* GetForm(ErrorResult
& aRv
) const;
63 void SetValidity(const ValidityStateFlags
& aFlags
,
64 const Optional
<nsAString
>& aMessage
,
65 const Optional
<NonNull
<nsGenericHTMLElement
>>& aAnchor
,
67 bool GetWillValidate(ErrorResult
& aRv
) const;
68 ValidityState
* GetValidity(ErrorResult
& aRv
);
69 void GetValidationMessage(nsAString
& aValidationMessage
,
70 ErrorResult
& aRv
) const;
71 bool CheckValidity(ErrorResult
& aRv
);
72 bool ReportValidity(ErrorResult
& aRv
);
73 already_AddRefed
<nsINodeList
> GetLabels(ErrorResult
& aRv
) const;
74 nsGenericHTMLElement
* GetValidationAnchor(ErrorResult
& aRv
) const;
75 CustomStateSet
* States();
78 mozilla::dom::HTMLFieldSetElement
* GetFieldSet() override
{
81 mozilla::dom::HTMLFormElement
* GetForm() const override
{ return mForm
; }
82 void SetForm(mozilla::dom::HTMLFormElement
* aForm
) override
;
83 void ClearForm(bool aRemoveFromForm
, bool aUnbindOrDelete
) override
;
84 NS_IMETHOD
Reset() override
;
85 NS_IMETHOD
SubmitNamesValues(mozilla::dom::FormData
* aFormData
) override
;
86 int32_t GetParserInsertedControlNumberForStateKey() const override
{
87 return mControlNumber
;
90 void SetFieldSet(mozilla::dom::HTMLFieldSetElement
* aFieldSet
) {
91 mFieldSet
= aFieldSet
;
94 const Nullable
<OwningFileOrUSVStringOrFormData
>& GetFormSubmissionValue()
96 return mSubmissionValue
;
99 const Nullable
<OwningFileOrUSVStringOrFormData
>& GetFormState() const {
103 void RestoreFormValue(Nullable
<OwningFileOrUSVStringOrFormData
>&& aValue
,
104 Nullable
<OwningFileOrUSVStringOrFormData
>&& aState
);
106 const nsCString
& GetStateKey() const { return mStateKey
; }
107 void SetStateKey(nsCString
&& key
) {
108 MOZ_ASSERT(mStateKey
.IsEmpty(), "FACE state key should only be set once!");
111 void InitializeControlNumber();
113 void UpdateFormOwner();
114 void UpdateBarredFromConstraintValidation();
119 ARIA_REFLECT_ATTR(Role
, role
)
122 ARIA_REFLECT_ATTR(AriaAtomic
, aria_atomic
)
123 ARIA_REFLECT_ATTR(AriaAutoComplete
, aria_autocomplete
)
124 ARIA_REFLECT_ATTR(AriaBusy
, aria_busy
)
125 ARIA_REFLECT_ATTR(AriaBrailleLabel
, aria_braillelabel
)
126 ARIA_REFLECT_ATTR(AriaBrailleRoleDescription
, aria_brailleroledescription
)
127 ARIA_REFLECT_ATTR(AriaChecked
, aria_checked
)
128 ARIA_REFLECT_ATTR(AriaColCount
, aria_colcount
)
129 ARIA_REFLECT_ATTR(AriaColIndex
, aria_colindex
)
130 ARIA_REFLECT_ATTR(AriaColIndexText
, aria_colindextext
)
131 ARIA_REFLECT_ATTR(AriaColSpan
, aria_colspan
)
132 ARIA_REFLECT_ATTR(AriaCurrent
, aria_current
)
133 ARIA_REFLECT_ATTR(AriaDescription
, aria_description
)
134 ARIA_REFLECT_ATTR(AriaDisabled
, aria_disabled
)
135 ARIA_REFLECT_ATTR(AriaExpanded
, aria_expanded
)
136 ARIA_REFLECT_ATTR(AriaHasPopup
, aria_haspopup
)
137 ARIA_REFLECT_ATTR(AriaHidden
, aria_hidden
)
138 ARIA_REFLECT_ATTR(AriaInvalid
, aria_invalid
)
139 ARIA_REFLECT_ATTR(AriaKeyShortcuts
, aria_keyshortcuts
)
140 ARIA_REFLECT_ATTR(AriaLabel
, aria_label
)
141 ARIA_REFLECT_ATTR(AriaLevel
, aria_level
)
142 ARIA_REFLECT_ATTR(AriaLive
, aria_live
)
143 ARIA_REFLECT_ATTR(AriaModal
, aria_modal
)
144 ARIA_REFLECT_ATTR(AriaMultiLine
, aria_multiline
)
145 ARIA_REFLECT_ATTR(AriaMultiSelectable
, aria_multiselectable
)
146 ARIA_REFLECT_ATTR(AriaOrientation
, aria_orientation
)
147 ARIA_REFLECT_ATTR(AriaPlaceholder
, aria_placeholder
)
148 ARIA_REFLECT_ATTR(AriaPosInSet
, aria_posinset
)
149 ARIA_REFLECT_ATTR(AriaPressed
, aria_pressed
)
150 ARIA_REFLECT_ATTR(AriaReadOnly
, aria_readonly
)
151 ARIA_REFLECT_ATTR(AriaRelevant
, aria_relevant
)
152 ARIA_REFLECT_ATTR(AriaRequired
, aria_required
)
153 ARIA_REFLECT_ATTR(AriaRoleDescription
, aria_roledescription
)
154 ARIA_REFLECT_ATTR(AriaRowCount
, aria_rowcount
)
155 ARIA_REFLECT_ATTR(AriaRowIndex
, aria_rowindex
)
156 ARIA_REFLECT_ATTR(AriaRowIndexText
, aria_rowindextext
)
157 ARIA_REFLECT_ATTR(AriaRowSpan
, aria_rowspan
)
158 ARIA_REFLECT_ATTR(AriaSelected
, aria_selected
)
159 ARIA_REFLECT_ATTR(AriaSetSize
, aria_setsize
)
160 ARIA_REFLECT_ATTR(AriaSort
, aria_sort
)
161 ARIA_REFLECT_ATTR(AriaValueMax
, aria_valuemax
)
162 ARIA_REFLECT_ATTR(AriaValueMin
, aria_valuemin
)
163 ARIA_REFLECT_ATTR(AriaValueNow
, aria_valuenow
)
164 ARIA_REFLECT_ATTR(AriaValueText
, aria_valuetext
)
166 void GetAttr(const nsAtom
* aName
, nsAString
& aResult
) const;
168 nsresult
SetAttr(nsAtom
* aName
, const nsAString
& aValue
);
170 const AttrArray
& GetAttrs() const { return mAttrs
; }
172 DocGroup
* GetDocGroup();
175 ~ElementInternals() = default;
177 // It's a target element which is a custom element.
178 RefPtr
<HTMLElement
> mTarget
;
180 // The form that contains the target element.
181 // It's safe to use raw pointer because it will be reset via
182 // CustomElementData::Unlink when mTarget is released or unlinked.
183 HTMLFormElement
* mForm
;
185 // This is a pointer to the target element's closest fieldset parent if any.
186 // It's safe to use raw pointer because it will be reset via
187 // CustomElementData::Unlink when mTarget is released or unlinked.
188 HTMLFieldSetElement
* mFieldSet
;
190 // https://html.spec.whatwg.org/#face-submission-value
191 Nullable
<OwningFileOrUSVStringOrFormData
> mSubmissionValue
;
193 // https://html.spec.whatwg.org/#face-state
194 // TODO: Bug 1734841 - Figure out how to support autocomplete for
195 // form-associated custom element.
196 Nullable
<OwningFileOrUSVStringOrFormData
> mState
;
198 // https://html.spec.whatwg.org/#face-validation-message
199 nsString mValidationMessage
;
201 // https://html.spec.whatwg.org/#face-validation-anchor
202 RefPtr
<nsGenericHTMLElement
> mValidationAnchor
;
206 // Used to store the key to a form-associated custom element in the current
207 // session. Is empty until element has been upgraded.
210 RefPtr
<CustomStateSet
> mCustomStateSet
;
212 // A number for a form-associated custom element that is unique within its
213 // owner document. This is only set to a number for elements inserted into the
214 // document by the parser from the network. Otherwise, it is -1.
215 int32_t mControlNumber
;
218 } // namespace mozilla::dom
220 #undef ARIA_REFLECT_ATTR
222 #endif // mozilla_dom_ElementInternals_h