Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / html / ElementInternals.h
blob4bbf5c96f383267098a225e4ab149b86ea4f4e19
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); \
25 } \
26 void Set##method(const nsAString& aValue, ErrorResult& aResult) { \
27 aResult = ErrorResult(SetAttr(nsGkAtoms::attr, aValue)); \
30 #define ARIA_REFLECT_ATTR_ELEMENT(method, attr) \
31 Element* Get##method() const { return GetAttrElement(nsGkAtoms::attr); } \
33 void Set##method(Element* aElement) { \
34 SetAttrElement(nsGkAtoms::attr, aElement); \
37 class nsINodeList;
38 class nsGenericHTMLElement;
40 namespace mozilla::dom {
42 class DocGroup;
43 class HTMLElement;
44 class HTMLFieldSetElement;
45 class HTMLFormElement;
46 class ShadowRoot;
47 class ValidityState;
49 class ElementInternals final : public nsIFormControl,
50 public nsIConstraintValidation,
51 public nsWrapperCache {
52 public:
53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
54 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(ElementInternals,
55 nsIFormControl)
57 explicit ElementInternals(HTMLElement* aTarget);
59 nsISupports* GetParentObject();
61 virtual JSObject* WrapObject(JSContext* aCx,
62 JS::Handle<JSObject*> aGivenProto) override;
64 // WebIDL
65 ShadowRoot* GetShadowRoot() const;
66 void SetFormValue(const Nullable<FileOrUSVStringOrFormData>& aValue,
67 const Optional<Nullable<FileOrUSVStringOrFormData>>& aState,
68 ErrorResult& aRv);
69 mozilla::dom::HTMLFormElement* GetForm(ErrorResult& aRv) const;
70 void SetValidity(const ValidityStateFlags& aFlags,
71 const Optional<nsAString>& aMessage,
72 const Optional<NonNull<nsGenericHTMLElement>>& aAnchor,
73 ErrorResult& aRv);
74 bool GetWillValidate(ErrorResult& aRv) const;
75 ValidityState* GetValidity(ErrorResult& aRv);
76 void GetValidationMessage(nsAString& aValidationMessage,
77 ErrorResult& aRv) const;
78 bool CheckValidity(ErrorResult& aRv);
79 bool ReportValidity(ErrorResult& aRv);
80 already_AddRefed<nsINodeList> GetLabels(ErrorResult& aRv) const;
81 nsGenericHTMLElement* GetValidationAnchor(ErrorResult& aRv) const;
82 CustomStateSet* States();
84 // nsIFormControl
85 mozilla::dom::HTMLFieldSetElement* GetFieldSet() override {
86 return mFieldSet;
88 mozilla::dom::HTMLFormElement* GetForm() const override { return mForm; }
89 void SetForm(mozilla::dom::HTMLFormElement* aForm) override;
90 void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) override;
91 NS_IMETHOD Reset() override;
92 NS_IMETHOD SubmitNamesValues(mozilla::dom::FormData* aFormData) override;
93 int32_t GetParserInsertedControlNumberForStateKey() const override {
94 return mControlNumber;
97 void SetFieldSet(mozilla::dom::HTMLFieldSetElement* aFieldSet) {
98 mFieldSet = aFieldSet;
101 const Nullable<OwningFileOrUSVStringOrFormData>& GetFormSubmissionValue()
102 const {
103 return mSubmissionValue;
106 const Nullable<OwningFileOrUSVStringOrFormData>& GetFormState() const {
107 return mState;
110 void RestoreFormValue(Nullable<OwningFileOrUSVStringOrFormData>&& aValue,
111 Nullable<OwningFileOrUSVStringOrFormData>&& aState);
113 const nsCString& GetStateKey() const { return mStateKey; }
114 void SetStateKey(nsCString&& key) {
115 MOZ_ASSERT(mStateKey.IsEmpty(), "FACE state key should only be set once!");
116 mStateKey = key;
118 void InitializeControlNumber();
120 void UpdateFormOwner();
121 void UpdateBarredFromConstraintValidation();
123 void Unlink();
125 // AccessibilityRole
126 ARIA_REFLECT_ATTR(Role, role)
128 // AriaAttributes
129 ARIA_REFLECT_ATTR_ELEMENT(AriaActiveDescendantElement, aria_activedescendant)
130 ARIA_REFLECT_ATTR(AriaAtomic, aria_atomic)
131 ARIA_REFLECT_ATTR(AriaAutoComplete, aria_autocomplete)
132 ARIA_REFLECT_ATTR(AriaBusy, aria_busy)
133 ARIA_REFLECT_ATTR(AriaBrailleLabel, aria_braillelabel)
134 ARIA_REFLECT_ATTR(AriaBrailleRoleDescription, aria_brailleroledescription)
135 ARIA_REFLECT_ATTR(AriaChecked, aria_checked)
136 ARIA_REFLECT_ATTR(AriaColCount, aria_colcount)
137 ARIA_REFLECT_ATTR(AriaColIndex, aria_colindex)
138 ARIA_REFLECT_ATTR(AriaColIndexText, aria_colindextext)
139 ARIA_REFLECT_ATTR(AriaColSpan, aria_colspan)
140 ARIA_REFLECT_ATTR(AriaCurrent, aria_current)
141 ARIA_REFLECT_ATTR(AriaDescription, aria_description)
142 ARIA_REFLECT_ATTR(AriaDisabled, aria_disabled)
143 ARIA_REFLECT_ATTR(AriaExpanded, aria_expanded)
144 ARIA_REFLECT_ATTR(AriaHasPopup, aria_haspopup)
145 ARIA_REFLECT_ATTR(AriaHidden, aria_hidden)
146 ARIA_REFLECT_ATTR(AriaInvalid, aria_invalid)
147 ARIA_REFLECT_ATTR(AriaKeyShortcuts, aria_keyshortcuts)
148 ARIA_REFLECT_ATTR(AriaLabel, aria_label)
149 ARIA_REFLECT_ATTR(AriaLevel, aria_level)
150 ARIA_REFLECT_ATTR(AriaLive, aria_live)
151 ARIA_REFLECT_ATTR(AriaModal, aria_modal)
152 ARIA_REFLECT_ATTR(AriaMultiLine, aria_multiline)
153 ARIA_REFLECT_ATTR(AriaMultiSelectable, aria_multiselectable)
154 ARIA_REFLECT_ATTR(AriaOrientation, aria_orientation)
155 ARIA_REFLECT_ATTR(AriaPlaceholder, aria_placeholder)
156 ARIA_REFLECT_ATTR(AriaPosInSet, aria_posinset)
157 ARIA_REFLECT_ATTR(AriaPressed, aria_pressed)
158 ARIA_REFLECT_ATTR(AriaReadOnly, aria_readonly)
159 ARIA_REFLECT_ATTR(AriaRelevant, aria_relevant)
160 ARIA_REFLECT_ATTR(AriaRequired, aria_required)
161 ARIA_REFLECT_ATTR(AriaRoleDescription, aria_roledescription)
162 ARIA_REFLECT_ATTR(AriaRowCount, aria_rowcount)
163 ARIA_REFLECT_ATTR(AriaRowIndex, aria_rowindex)
164 ARIA_REFLECT_ATTR(AriaRowIndexText, aria_rowindextext)
165 ARIA_REFLECT_ATTR(AriaRowSpan, aria_rowspan)
166 ARIA_REFLECT_ATTR(AriaSelected, aria_selected)
167 ARIA_REFLECT_ATTR(AriaSetSize, aria_setsize)
168 ARIA_REFLECT_ATTR(AriaSort, aria_sort)
169 ARIA_REFLECT_ATTR(AriaValueMax, aria_valuemax)
170 ARIA_REFLECT_ATTR(AriaValueMin, aria_valuemin)
171 ARIA_REFLECT_ATTR(AriaValueNow, aria_valuenow)
172 ARIA_REFLECT_ATTR(AriaValueText, aria_valuetext)
174 void GetAttr(const nsAtom* aName, nsAString& aResult) const;
176 nsresult SetAttr(nsAtom* aName, const nsAString& aValue);
178 const AttrArray& GetAttrs() const { return mAttrs; }
180 DocGroup* GetDocGroup();
182 private:
183 ~ElementInternals() = default;
186 * Gets the attribute element for the given attribute.
187 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
189 Element* GetAttrElement(nsAtom* aAttr) const;
192 * Sets an attribute element for the given attribute.
193 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
195 void SetAttrElement(nsAtom* aAttr, Element* aElement);
197 // It's a target element which is a custom element.
198 RefPtr<HTMLElement> mTarget;
200 // The form that contains the target element.
201 // It's safe to use raw pointer because it will be reset via
202 // CustomElementData::Unlink when mTarget is released or unlinked.
203 HTMLFormElement* mForm;
205 // This is a pointer to the target element's closest fieldset parent if any.
206 // It's safe to use raw pointer because it will be reset via
207 // CustomElementData::Unlink when mTarget is released or unlinked.
208 HTMLFieldSetElement* mFieldSet;
210 // https://html.spec.whatwg.org/#face-submission-value
211 Nullable<OwningFileOrUSVStringOrFormData> mSubmissionValue;
213 // https://html.spec.whatwg.org/#face-state
214 // TODO: Bug 1734841 - Figure out how to support autocomplete for
215 // form-associated custom element.
216 Nullable<OwningFileOrUSVStringOrFormData> mState;
218 // https://html.spec.whatwg.org/#face-validation-message
219 nsString mValidationMessage;
221 // https://html.spec.whatwg.org/#face-validation-anchor
222 RefPtr<nsGenericHTMLElement> mValidationAnchor;
224 AttrArray mAttrs;
226 // Used to store the key to a form-associated custom element in the current
227 // session. Is empty until element has been upgraded.
228 nsCString mStateKey;
230 RefPtr<CustomStateSet> mCustomStateSet;
232 // A number for a form-associated custom element that is unique within its
233 // owner document. This is only set to a number for elements inserted into the
234 // document by the parser from the network. Otherwise, it is -1.
235 int32_t mControlNumber;
238 * Explicitly set attr-elements, see
239 * https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#explicitly-set-attr-element
241 nsTHashMap<RefPtr<nsAtom>, nsWeakPtr> mAttrElements;
244 } // namespace mozilla::dom
246 #undef ARIA_REFLECT_ATTR
248 #endif // mozilla_dom_ElementInternals_h