script/dom/html/
htmloptgroupelement.rs1use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name};
7use js::rust::HandleObject;
8use script_bindings::str::DOMString;
9use stylo_dom::ElementState;
10
11use crate::dom::attr::Attr;
12use crate::dom::bindings::codegen::Bindings::HTMLOptGroupElementBinding::HTMLOptGroupElementMethods;
13use crate::dom::bindings::codegen::GenericBindings::NodeBinding::Node_Binding::NodeMethods;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::document::Document;
17use crate::dom::element::{AttributeMutation, Element};
18use crate::dom::html::htmlelement::HTMLElement;
19use crate::dom::html::htmloptionelement::HTMLOptionElement;
20use crate::dom::html::htmlselectelement::HTMLSelectElement;
21use crate::dom::node::{BindContext, Node, UnbindContext};
22use crate::dom::validation::Validatable;
23use crate::dom::validitystate::ValidationFlags;
24use crate::dom::virtualmethods::VirtualMethods;
25use crate::script_runtime::CanGc;
26
27#[dom_struct]
29pub(crate) struct HTMLOptGroupElement {
30 htmlelement: HTMLElement,
31}
32
33impl HTMLOptGroupElement {
34 fn new_inherited(
35 local_name: LocalName,
36 prefix: Option<Prefix>,
37 document: &Document,
38 ) -> HTMLOptGroupElement {
39 HTMLOptGroupElement {
40 htmlelement: HTMLElement::new_inherited_with_state(
41 ElementState::ENABLED,
42 local_name,
43 prefix,
44 document,
45 ),
46 }
47 }
48
49 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
50 pub(crate) fn new(
51 local_name: LocalName,
52 prefix: Option<Prefix>,
53 document: &Document,
54 proto: Option<HandleObject>,
55 can_gc: CanGc,
56 ) -> DomRoot<HTMLOptGroupElement> {
57 Node::reflect_node_with_proto(
58 Box::new(HTMLOptGroupElement::new_inherited(
59 local_name, prefix, document,
60 )),
61 document,
62 proto,
63 can_gc,
64 )
65 }
66
67 fn update_select_validity(&self, can_gc: CanGc) {
68 if let Some(select) = self.owner_select_element() {
69 select
70 .validity_state()
71 .perform_validation_and_update(ValidationFlags::all(), can_gc);
72 }
73 }
74
75 fn owner_select_element(&self) -> Option<DomRoot<HTMLSelectElement>> {
76 self.upcast::<Node>()
77 .GetParentNode()
78 .and_then(DomRoot::downcast)
79 }
80}
81
82impl HTMLOptGroupElementMethods<crate::DomTypeHolder> for HTMLOptGroupElement {
83 make_bool_getter!(Disabled, "disabled");
85
86 make_bool_setter!(SetDisabled, "disabled");
88
89 make_getter!(Label, "label");
91
92 make_setter!(SetLabel, "label");
94}
95
96impl VirtualMethods for HTMLOptGroupElement {
97 fn super_type(&self) -> Option<&dyn VirtualMethods> {
98 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
99 }
100
101 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
102 self.super_type()
103 .unwrap()
104 .attribute_mutated(attr, mutation, can_gc);
105 if attr.local_name() == &local_name!("disabled") {
106 let disabled_state = match mutation {
107 AttributeMutation::Set(None) => true,
108 AttributeMutation::Set(Some(_)) => {
109 return;
111 },
112 AttributeMutation::Removed => false,
113 };
114 let el = self.upcast::<Element>();
115 el.set_disabled_state(disabled_state);
116 el.set_enabled_state(!disabled_state);
117 let options = el
118 .upcast::<Node>()
119 .children()
120 .filter(|child| child.is::<HTMLOptionElement>())
121 .map(|child| DomRoot::from_ref(child.downcast::<HTMLOptionElement>().unwrap()));
122 if disabled_state {
123 for option in options {
124 let el = option.upcast::<Element>();
125 el.set_disabled_state(true);
126 el.set_enabled_state(false);
127 }
128 } else {
129 for option in options {
130 let el = option.upcast::<Element>();
131 el.check_disabled_attribute();
132 }
133 }
134 }
135 }
136
137 fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
138 if let Some(super_type) = self.super_type() {
139 super_type.bind_to_tree(context, can_gc);
140 }
141
142 self.update_select_validity(can_gc);
143 }
144
145 fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
146 self.super_type().unwrap().unbind_from_tree(context, can_gc);
147
148 if let Some(select) = context.parent.downcast::<HTMLSelectElement>() {
149 select
150 .validity_state()
151 .perform_validation_and_update(ValidationFlags::all(), can_gc);
152 }
153 }
154}