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 
7 #ifndef mozilla_dom_HTMLFieldSetElement_h
8 #define mozilla_dom_HTMLFieldSetElement_h
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/ConstraintValidation.h"
12 #include "mozilla/dom/ValidityState.h"
13 #include "nsGenericHTMLElement.h"
14 
15 namespace mozilla {
16 class ErrorResult;
17 class EventChainPreVisitor;
18 namespace dom {
19 class FormData;
20 
21 class HTMLFieldSetElement final : public nsGenericHTMLFormControlElement,
22                                   public ConstraintValidation {
23  public:
24   using ConstraintValidation::GetValidationMessage;
25   using ConstraintValidation::SetCustomValidity;
26 
27   explicit HTMLFieldSetElement(
28       already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
29 
30   NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLFieldSetElement, fieldset)
31 
32   // nsISupports
33   NS_DECL_ISUPPORTS_INHERITED
34 
35   // nsIContent
36   void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
37   virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
38                                 const nsAttrValue* aValue,
39                                 const nsAttrValue* aOldValue,
40                                 nsIPrincipal* aSubjectPrincipal,
41                                 bool aNotify) override;
42 
43   virtual void InsertChildBefore(nsIContent* aChild, nsIContent* aBeforeThis,
44                                  bool aNotify, ErrorResult& aRv) override;
45   virtual void RemoveChildNode(nsIContent* aKid, bool aNotify) override;
46 
47   // nsGenericHTMLElement
48   virtual bool IsDisabledForEvents(WidgetEvent* aEvent) override;
49 
50   // nsIFormControl
51   NS_IMETHOD Reset() override;
SubmitNamesValues(FormData * aFormData)52   NS_IMETHOD SubmitNamesValues(FormData* aFormData) override { return NS_OK; }
53   virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
54 
GetFirstLegend()55   const nsIContent* GetFirstLegend() const { return mFirstLegend; }
56 
57   void AddElement(nsGenericHTMLFormElement* aElement);
58 
59   void RemoveElement(nsGenericHTMLFormElement* aElement);
60 
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLFieldSetElement,nsGenericHTMLFormControlElement)61   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLFieldSetElement,
62                                            nsGenericHTMLFormControlElement)
63 
64   // WebIDL
65   bool Disabled() const { return GetBoolAttr(nsGkAtoms::disabled); }
SetDisabled(bool aValue,ErrorResult & aRv)66   void SetDisabled(bool aValue, ErrorResult& aRv) {
67     SetHTMLBoolAttr(nsGkAtoms::disabled, aValue, aRv);
68   }
69 
GetName(nsAString & aValue)70   void GetName(nsAString& aValue) { GetHTMLAttr(nsGkAtoms::name, aValue); }
71 
SetName(const nsAString & aValue,ErrorResult & aRv)72   void SetName(const nsAString& aValue, ErrorResult& aRv) {
73     SetHTMLAttr(nsGkAtoms::name, aValue, aRv);
74   }
75 
76   void GetType(nsAString& aType) const;
77 
78   nsIHTMLCollection* Elements();
79 
80   // XPCOM WillValidate is OK for us
81 
82   // XPCOM Validity is OK for us
83 
84   // XPCOM GetValidationMessage is OK for us
85 
86   // XPCOM CheckValidity is OK for us
87 
88   // XPCOM SetCustomValidity is OK for us
89 
90   virtual EventStates IntrinsicState() const override;
91 
92   /*
93    * This method will update the fieldset's validity.  This method has to be
94    * called by fieldset elements whenever their validity state or status
95    * regarding constraint validation changes.
96    *
97    * @note If an element becomes barred from constraint validation, it has to
98    * be considered as valid.
99    *
100    * @param aElementValidityState the new validity state of the element
101    */
102   void UpdateValidity(bool aElementValidityState);
103 
104  protected:
105   virtual ~HTMLFieldSetElement();
106 
107   virtual JSObject* WrapNode(JSContext* aCx,
108                              JS::Handle<JSObject*> aGivenProto) override;
109 
110  private:
111   /**
112    * Notify all elements (in mElements) that the first legend of the fieldset
113    * has now changed.
114    */
115   void NotifyElementsForFirstLegendChange(bool aNotify);
116 
117   // This function is used to generate the nsContentList (listed form elements).
118   static bool MatchListedElements(Element* aElement, int32_t aNamespaceID,
119                                   nsAtom* aAtom, void* aData);
120 
121   // listed form controls elements.
122   RefPtr<nsContentList> mElements;
123 
124   // List of elements which have this fieldset as first fieldset ancestor.
125   nsTArray<nsGenericHTMLFormElement*> mDependentElements;
126 
127   nsIContent* mFirstLegend;
128 
129   /**
130    * Number of invalid and candidate for constraint validation
131    * elements in the fieldSet the last time UpdateValidity has been called.
132    *
133    * @note Should only be used by UpdateValidity() and IntrinsicState()!
134    */
135   int32_t mInvalidElementsCount;
136 };
137 
138 }  // namespace dom
139 }  // namespace mozilla
140 
141 #endif /* mozilla_dom_HTMLFieldSetElement_h */
142