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_HTMLMenuItemElement_h
8 #define mozilla_dom_HTMLMenuItemElement_h
9 
10 #include "mozilla/Attributes.h"
11 #include "nsGenericHTMLElement.h"
12 
13 namespace mozilla {
14 
15 class EventChainPreVisitor;
16 
17 namespace dom {
18 
19 class Visitor;
20 
21 class HTMLMenuItemElement final : public nsGenericHTMLElement {
22  public:
23   using mozilla::dom::Element::GetText;
24 
25   HTMLMenuItemElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
26                       mozilla::dom::FromParser aFromParser);
27 
28   NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLMenuItemElement, menuitem)
29 
30   // nsISupports
31   NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLMenuItemElement,
32                                        nsGenericHTMLElement)
33 
34   void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
35   virtual nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
36 
37   virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
38 
39   virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
40                               const nsAString& aValue,
41                               nsIPrincipal* aMaybeScriptedPrincipal,
42                               nsAttrValue& aResult) override;
43 
44   virtual void DoneCreatingElement() override;
45 
46   virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
47 
GetType()48   uint8_t GetType() const { return mType; }
49 
50   /**
51    * Syntax sugar to make it easier to check for checked and checked dirty
52    */
IsChecked()53   bool IsChecked() const { return mChecked; }
IsCheckedDirty()54   bool IsCheckedDirty() const { return mCheckedDirty; }
55 
56   void GetText(nsAString& aText);
57 
58   // WebIDL
59 
60   void GetType(DOMString& aValue);
SetType(const nsAString & aType,ErrorResult & aError)61   void SetType(const nsAString& aType, ErrorResult& aError) {
62     SetHTMLAttr(nsGkAtoms::type, aType, aError);
63   }
64 
65   // nsAString needed for HTMLMenuElement
GetLabel(nsAString & aValue)66   void GetLabel(nsAString& aValue) {
67     if (!GetAttr(kNameSpaceID_None, nsGkAtoms::label, aValue)) {
68       GetText(aValue);
69     }
70   }
SetLabel(const nsAString & aLabel,ErrorResult & aError)71   void SetLabel(const nsAString& aLabel, ErrorResult& aError) {
72     SetHTMLAttr(nsGkAtoms::label, aLabel, aError);
73   }
74 
75   // nsAString needed for HTMLMenuElement
GetIcon(nsAString & aValue)76   void GetIcon(nsAString& aValue) {
77     GetURIAttr(nsGkAtoms::icon, nullptr, aValue);
78   }
SetIcon(const nsAString & aIcon,ErrorResult & aError)79   void SetIcon(const nsAString& aIcon, ErrorResult& aError) {
80     SetHTMLAttr(nsGkAtoms::icon, aIcon, aError);
81   }
82 
Disabled()83   bool Disabled() const { return GetBoolAttr(nsGkAtoms::disabled); }
SetDisabled(bool aDisabled,ErrorResult & aError)84   void SetDisabled(bool aDisabled, ErrorResult& aError) {
85     SetHTMLBoolAttr(nsGkAtoms::disabled, aDisabled, aError);
86   }
87 
Checked()88   bool Checked() const { return mChecked; }
89   void SetChecked(bool aChecked);
90 
GetRadiogroup(DOMString & aValue)91   void GetRadiogroup(DOMString& aValue) {
92     GetHTMLAttr(nsGkAtoms::radiogroup, aValue);
93   }
SetRadiogroup(const nsAString & aRadiogroup,ErrorResult & aError)94   void SetRadiogroup(const nsAString& aRadiogroup, ErrorResult& aError) {
95     SetHTMLAttr(nsGkAtoms::radiogroup, aRadiogroup, aError);
96   }
97 
DefaultChecked()98   bool DefaultChecked() const { return GetBoolAttr(nsGkAtoms::checked); }
SetDefaultChecked(bool aDefault,ErrorResult & aError)99   void SetDefaultChecked(bool aDefault, ErrorResult& aError) {
100     SetHTMLBoolAttr(nsGkAtoms::checked, aDefault, aError);
101   }
102 
103  protected:
104   virtual ~HTMLMenuItemElement();
105 
106   virtual JSObject* WrapNode(JSContext* aCx,
107                              JS::Handle<JSObject*> aGivenProto) override;
108 
109  protected:
110   virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
111                                 const nsAttrValue* aValue,
112                                 const nsAttrValue* aOldValue,
113                                 nsIPrincipal* aSubjectPrincipal,
114                                 bool aNotify) override;
115 
116   void WalkRadioGroup(Visitor* aVisitor);
117 
118   HTMLMenuItemElement* GetSelectedRadio();
119 
120   void AddedToRadioGroup();
121 
122   void InitChecked();
123 
124   friend class ClearCheckedVisitor;
125   friend class SetCheckedDirtyVisitor;
126 
ClearChecked()127   void ClearChecked() { mChecked = false; }
SetCheckedDirty()128   void SetCheckedDirty() { mCheckedDirty = true; }
129 
130  private:
131   uint8_t mType : 2;
132   bool mParserCreating : 1;
133   bool mShouldInitChecked : 1;
134   bool mCheckedDirty : 1;
135   bool mChecked : 1;
136 };
137 
138 }  // namespace dom
139 }  // namespace mozilla
140 
141 #endif  // mozilla_dom_HTMLMenuItemElement_h
142