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/. */
6 
7 #ifndef mozilla_dom_ElementInternals_h
8 #define mozilla_dom_ElementInternals_h
9 
10 #include "js/TypeDecls.h"
11 #include "mozilla/dom/ElementInternalsBinding.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsIConstraintValidation.h"
14 #include "nsIFormControl.h"
15 #include "nsWrapperCache.h"
16 
17 class nsINodeList;
18 class nsGenericHTMLElement;
19 
20 namespace mozilla {
21 
22 class ErrorResult;
23 
24 namespace dom {
25 
26 class HTMLElement;
27 class HTMLFieldSetElement;
28 class HTMLFormElement;
29 class ShadowRoot;
30 class ValidityState;
31 
32 class ElementInternals final : public nsIFormControl,
33                                public nsIConstraintValidation,
34                                public nsWrapperCache {
35  public:
36   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
37   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(ElementInternals,
38                                                          nsIFormControl)
39 
40   explicit ElementInternals(HTMLElement* aTarget);
41 
42   nsISupports* GetParentObject();
43 
44   virtual JSObject* WrapObject(JSContext* aCx,
45                                JS::Handle<JSObject*> aGivenProto) override;
46 
47   // WebIDL
48   ShadowRoot* GetShadowRoot() const;
49   void SetFormValue(const Nullable<FileOrUSVStringOrFormData>& aValue,
50                     const Optional<Nullable<FileOrUSVStringOrFormData>>& aState,
51                     ErrorResult& aRv);
52   mozilla::dom::HTMLFormElement* GetForm(ErrorResult& aRv) const;
53   void SetValidity(const ValidityStateFlags& aFlags,
54                    const Optional<nsAString>& aMessage,
55                    const Optional<NonNull<nsGenericHTMLElement>>& aAnchor,
56                    ErrorResult& aRv);
57   bool GetWillValidate(ErrorResult& aRv) const;
58   ValidityState* GetValidity(ErrorResult& aRv);
59   void GetValidationMessage(nsAString& aValidationMessage,
60                             ErrorResult& aRv) const;
61   bool CheckValidity(ErrorResult& aRv);
62   bool ReportValidity(ErrorResult& aRv);
63   already_AddRefed<nsINodeList> GetLabels(ErrorResult& aRv) const;
64   nsGenericHTMLElement* GetValidationAnchor(ErrorResult& aRv) const;
65 
66   // nsIFormControl
GetFieldSet()67   mozilla::dom::HTMLFieldSetElement* GetFieldSet() override {
68     return mFieldSet;
69   }
GetForm()70   mozilla::dom::HTMLFormElement* GetForm() const override { return mForm; }
71   void SetForm(mozilla::dom::HTMLFormElement* aForm) override;
72   void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) override;
73   NS_IMETHOD Reset() override;
74   NS_IMETHOD SubmitNamesValues(mozilla::dom::FormData* aFormData) override;
AllowDrop()75   bool AllowDrop() override { return true; }
76 
SetFieldSet(mozilla::dom::HTMLFieldSetElement * aFieldSet)77   void SetFieldSet(mozilla::dom::HTMLFieldSetElement* aFieldSet) {
78     mFieldSet = aFieldSet;
79   }
80 
81   void UpdateFormOwner();
82   void UpdateBarredFromConstraintValidation();
83 
84   void Unlink();
85 
86  private:
87   ~ElementInternals() = default;
88 
89   // It's a target element which is a custom element.
90   // It's safe to use raw pointer because it will be reset via
91   // CustomElementData::Unlink when mTarget is released or unlinked.
92   HTMLElement* mTarget;
93 
94   // The form that contains the target element.
95   // It's safe to use raw pointer because it will be reset via
96   // CustomElementData::Unlink when mTarget is released or unlinked.
97   HTMLFormElement* mForm;
98 
99   // This is a pointer to the target element's closest fieldset parent if any.
100   // It's safe to use raw pointer because it will be reset via
101   // CustomElementData::Unlink when mTarget is released or unlinked.
102   HTMLFieldSetElement* mFieldSet;
103 
104   // https://html.spec.whatwg.org/#face-submission-value
105   Nullable<OwningFileOrUSVStringOrFormData> mSubmissionValue;
106 
107   // https://html.spec.whatwg.org/#face-state
108   // TODO: Bug 1734841 - Figure out how to support form restoration or
109   //       autocomplete for form-associated custom element
110   Nullable<OwningFileOrUSVStringOrFormData> mState;
111 
112   // https://html.spec.whatwg.org/#face-validation-message
113   nsString mValidationMessage;
114 
115   // https://html.spec.whatwg.org/#face-validation-anchor
116   RefPtr<nsGenericHTMLElement> mValidationAnchor;
117 };
118 
119 }  // namespace dom
120 }  // namespace mozilla
121 
122 #endif  // mozilla_dom_ElementInternals_h
123