HTMLOptionElement.h (4349B)
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_HTMLOptionElement_h__ 8 #define mozilla_dom_HTMLOptionElement_h__ 9 10 #include "mozilla/dom/HTMLFormElement.h" 11 #include "nsGenericHTMLElement.h" 12 13 namespace mozilla::dom { 14 15 class HTMLSelectElement; 16 17 class HTMLOptionElement final : public nsGenericHTMLElement { 18 public: 19 explicit HTMLOptionElement( 20 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo); 21 22 static already_AddRefed<HTMLOptionElement> Option( 23 const GlobalObject& aGlobal, const nsAString& aText, 24 const Optional<nsAString>& aValue, bool aDefaultSelected, bool aSelected, 25 ErrorResult& aError); 26 27 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLOptionElement, option) 28 29 // nsISupports 30 NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLOptionElement, nsGenericHTMLElement) 31 32 using mozilla::dom::Element::GetCharacterDataBuffer; 33 34 bool Selected() const { return State().HasState(ElementState::CHECKED); } 35 void SetSelected(bool aValue); 36 37 void SetSelectedChanged(bool aValue) { mSelectedChanged = aValue; } 38 39 nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute, 40 AttrModType aModType) const override; 41 42 void BeforeSetAttr(int32_t aNamespaceID, nsAtom* aName, 43 const nsAttrValue* aValue, bool aNotify) override; 44 void AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, 45 const nsAttrValue* aValue, const nsAttrValue* aOldValue, 46 nsIPrincipal* aSubjectPrincipal, bool aNotify) override; 47 48 void SetSelectedInternal(bool aValue, bool aNotify); 49 50 /** 51 * This callback is called by an optgroup on all its option elements whenever 52 * its disabled state is changed so that option elements can know their 53 * disabled state might have changed. 54 */ 55 void OptGroupDisabledChanged(bool aNotify); 56 57 /** 58 * Check our disabled content attribute and optgroup's (if it exists) disabled 59 * state to decide whether our disabled flag should be toggled. 60 */ 61 void UpdateDisabledState(bool aNotify); 62 63 nsresult BindToTree(BindContext&, nsINode& aParent) override; 64 void UnbindFromTree(UnbindContext&) override; 65 66 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override; 67 68 nsresult CopyInnerTo(mozilla::dom::Element* aDest); 69 70 bool Disabled() const { return GetBoolAttr(nsGkAtoms::disabled); } 71 72 void SetDisabled(bool aValue, ErrorResult& aRv) { 73 SetHTMLBoolAttr(nsGkAtoms::disabled, aValue, aRv); 74 } 75 76 HTMLFormElement* GetForm(); 77 78 void GetRenderedLabel(nsAString& aLabel) { 79 if (!GetAttr(nsGkAtoms::label, aLabel) || aLabel.IsEmpty()) { 80 GetText(aLabel); 81 } 82 } 83 84 void GetLabel(nsAString& aLabel) { 85 if (!GetAttr(nsGkAtoms::label, aLabel)) { 86 GetText(aLabel); 87 } 88 } 89 void SetLabel(const nsAString& aLabel, ErrorResult& aError) { 90 SetHTMLAttr(nsGkAtoms::label, aLabel, aError); 91 } 92 93 bool DefaultSelected() const { return HasAttr(nsGkAtoms::selected); } 94 void SetDefaultSelected(bool aValue, ErrorResult& aRv) { 95 SetHTMLBoolAttr(nsGkAtoms::selected, aValue, aRv); 96 } 97 98 void GetValue(nsAString& aValue) { 99 if (!GetAttr(nsGkAtoms::value, aValue)) { 100 GetText(aValue); 101 } 102 } 103 void SetValue(const nsAString& aValue, ErrorResult& aRv) { 104 SetHTMLAttr(nsGkAtoms::value, aValue, aRv); 105 } 106 107 void GetText(nsAString& aText); 108 void SetText(const nsAString& aText, ErrorResult& aRv); 109 110 int32_t Index(); 111 /** 112 * Get the select content element that contains this option, this 113 * intentionally does not return nsresult, all we care about is if 114 * there's a select associated with this option or not. 115 */ 116 HTMLSelectElement* GetSelect() const; 117 118 protected: 119 virtual ~HTMLOptionElement(); 120 121 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 122 123 bool mSelectedChanged = false; 124 125 // True only while we're under the SetOptionsSelectedByIndex call when our 126 // "selected" attribute is changing and mSelectedChanged is false. 127 bool mIsInSetDefaultSelected = false; 128 }; 129 130 } // namespace mozilla::dom 131 132 #endif // mozilla_dom_HTMLOptionElement_h__