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/. */
7 #ifndef mozilla_dom_HTMLOptionElement_h__
8 #define mozilla_dom_HTMLOptionElement_h__
10 #include "mozilla/Attributes.h"
11 #include "nsGenericHTMLElement.h"
12 #include "mozilla/dom/HTMLFormElement.h"
14 namespace mozilla::dom
{
16 class HTMLSelectElement
;
18 class HTMLOptionElement final
: public nsGenericHTMLElement
{
20 explicit HTMLOptionElement(
21 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
23 static already_AddRefed
<HTMLOptionElement
> Option(
24 const GlobalObject
& aGlobal
, const nsAString
& aText
,
25 const Optional
<nsAString
>& aValue
, bool aDefaultSelected
, bool aSelected
,
28 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLOptionElement
, option
)
31 NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLOptionElement
, nsGenericHTMLElement
)
33 using mozilla::dom::Element::GetText
;
35 bool Selected() const { return State().HasState(ElementState::CHECKED
); }
36 void SetSelected(bool aValue
);
38 void SetSelectedChanged(bool aValue
) { mSelectedChanged
= aValue
; }
40 nsChangeHint
GetAttributeChangeHint(const nsAtom
* aAttribute
,
41 int32_t aModType
) const override
;
43 void BeforeSetAttr(int32_t aNamespaceID
, nsAtom
* aName
,
44 const nsAttrValue
* aValue
, bool aNotify
) override
;
45 void AfterSetAttr(int32_t aNameSpaceID
, nsAtom
* aName
,
46 const nsAttrValue
* aValue
, const nsAttrValue
* aOldValue
,
47 nsIPrincipal
* aSubjectPrincipal
, bool aNotify
) override
;
49 void SetSelectedInternal(bool aValue
, bool aNotify
);
52 * This callback is called by an optgroup on all its option elements whenever
53 * its disabled state is changed so that option elements can know their
54 * disabled state might have changed.
56 void OptGroupDisabledChanged(bool aNotify
);
59 * Check our disabled content attribute and optgroup's (if it exists) disabled
60 * state to decide whether our disabled flag should be toggled.
62 void UpdateDisabledState(bool aNotify
);
64 nsresult
BindToTree(BindContext
&, nsINode
& aParent
) override
;
65 void UnbindFromTree(UnbindContext
&) override
;
67 nsresult
Clone(dom::NodeInfo
*, nsINode
** aResult
) const override
;
69 nsresult
CopyInnerTo(mozilla::dom::Element
* aDest
);
71 bool Disabled() const { return GetBoolAttr(nsGkAtoms::disabled
); }
73 void SetDisabled(bool aValue
, ErrorResult
& aRv
) {
74 SetHTMLBoolAttr(nsGkAtoms::disabled
, aValue
, aRv
);
77 HTMLFormElement
* GetForm();
79 void GetRenderedLabel(nsAString
& aLabel
) {
80 if (!GetAttr(nsGkAtoms::label
, aLabel
) || aLabel
.IsEmpty()) {
85 void GetLabel(nsAString
& aLabel
) {
86 if (!GetAttr(nsGkAtoms::label
, aLabel
)) {
90 void SetLabel(const nsAString
& aLabel
, ErrorResult
& aError
) {
91 SetHTMLAttr(nsGkAtoms::label
, aLabel
, aError
);
94 bool DefaultSelected() const { return HasAttr(nsGkAtoms::selected
); }
95 void SetDefaultSelected(bool aValue
, ErrorResult
& aRv
) {
96 SetHTMLBoolAttr(nsGkAtoms::selected
, aValue
, aRv
);
99 void GetValue(nsAString
& aValue
) {
100 if (!GetAttr(nsGkAtoms::value
, aValue
)) {
104 void SetValue(const nsAString
& aValue
, ErrorResult
& aRv
) {
105 SetHTMLAttr(nsGkAtoms::value
, aValue
, aRv
);
108 void GetText(nsAString
& aText
);
109 void SetText(const nsAString
& aText
, ErrorResult
& aRv
);
114 virtual ~HTMLOptionElement();
116 JSObject
* WrapNode(JSContext
*, JS::Handle
<JSObject
*> aGivenProto
) override
;
119 * Get the select content element that contains this option, this
120 * intentionally does not return nsresult, all we care about is if
121 * there's a select associated with this option or not.
123 HTMLSelectElement
* GetSelect();
125 bool mSelectedChanged
= false;
127 // True only while we're under the SetOptionsSelectedByIndex call when our
128 // "selected" attribute is changing and mSelectedChanged is false.
129 bool mIsInSetDefaultSelected
= false;
132 } // namespace mozilla::dom
134 #endif // mozilla_dom_HTMLOptionElement_h__