Bug 1655413 [wpt PR 24763] - Make CSP default-src without 'unsafe-eval' block eval...
[gecko.git] / dom / html / HTMLOptionElement.h
blob710ce2458176ef3567e25bf16ceb84ee937ea961
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 {
15 namespace dom {
17 class HTMLSelectElement;
19 class HTMLOptionElement final : public nsGenericHTMLElement {
20 public:
21 explicit HTMLOptionElement(
22 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
24 static already_AddRefed<HTMLOptionElement> Option(
25 const GlobalObject& aGlobal, const nsAString& aText,
26 const Optional<nsAString>& aValue, bool aDefaultSelected, bool aSelected,
27 ErrorResult& aError);
29 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLOptionElement, option)
31 // nsISupports
32 NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLOptionElement, nsGenericHTMLElement)
34 using mozilla::dom::Element::GetText;
36 bool Selected() const { return mIsSelected; }
37 void SetSelected(bool aValue);
39 void SetSelectedChanged(bool aValue) { mSelectedChanged = aValue; }
41 virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
42 int32_t aModType) const override;
44 virtual nsresult BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName,
45 const nsAttrValueOrString* aValue,
46 bool aNotify) override;
47 virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
48 const nsAttrValue* aValue,
49 const nsAttrValue* aOldValue,
50 nsIPrincipal* aSubjectPrincipal,
51 bool aNotify) override;
53 void SetSelectedInternal(bool aValue, bool aNotify);
55 /**
56 * This callback is called by an optgroup on all its option elements whenever
57 * its disabled state is changed so that option elements can know their
58 * disabled state might have changed.
60 void OptGroupDisabledChanged(bool aNotify);
62 /**
63 * Check our disabled content attribute and optgroup's (if it exists) disabled
64 * state to decide whether our disabled flag should be toggled.
66 void UpdateDisabledState(bool aNotify);
68 virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
69 virtual void UnbindFromTree(bool aNullParent = true) override;
71 // nsIContent
72 virtual EventStates IntrinsicState() const override;
74 virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
76 nsresult CopyInnerTo(mozilla::dom::Element* aDest);
78 bool Disabled() const { return GetBoolAttr(nsGkAtoms::disabled); }
80 void SetDisabled(bool aValue, ErrorResult& aRv) {
81 SetHTMLBoolAttr(nsGkAtoms::disabled, aValue, aRv);
84 HTMLFormElement* GetForm();
86 void GetRenderedLabel(nsAString& aLabel) {
87 if (!GetAttr(kNameSpaceID_None, nsGkAtoms::label, aLabel) ||
88 aLabel.IsEmpty()) {
89 GetText(aLabel);
93 void GetLabel(DOMString& aLabel) {
94 if (!GetAttr(kNameSpaceID_None, nsGkAtoms::label, aLabel)) {
95 GetText(aLabel);
98 void SetLabel(const nsAString& aLabel, ErrorResult& aError) {
99 SetHTMLAttr(nsGkAtoms::label, aLabel, aError);
102 bool DefaultSelected() const {
103 return HasAttr(kNameSpaceID_None, nsGkAtoms::selected);
105 void SetDefaultSelected(bool aValue, ErrorResult& aRv) {
106 SetHTMLBoolAttr(nsGkAtoms::selected, aValue, aRv);
109 void GetValue(nsAString& aValue) {
110 if (!GetAttr(kNameSpaceID_None, nsGkAtoms::value, aValue)) {
111 GetText(aValue);
114 void SetValue(const nsAString& aValue, ErrorResult& aRv) {
115 SetHTMLAttr(nsGkAtoms::value, aValue, aRv);
118 void GetText(nsAString& aText);
119 void SetText(const nsAString& aText, ErrorResult& aRv);
121 int32_t Index();
123 protected:
124 virtual ~HTMLOptionElement();
126 virtual JSObject* WrapNode(JSContext* aCx,
127 JS::Handle<JSObject*> aGivenProto) override;
130 * Get the select content element that contains this option, this
131 * intentionally does not return nsresult, all we care about is if
132 * there's a select associated with this option or not.
134 HTMLSelectElement* GetSelect();
136 bool mSelectedChanged;
137 bool mIsSelected;
139 // True only while we're under the SetOptionsSelectedByIndex call when our
140 // "selected" attribute is changing and mSelectedChanged is false.
141 bool mIsInSetDefaultSelected;
144 } // namespace dom
145 } // namespace mozilla
147 #endif // mozilla_dom_HTMLOptionElement_h__