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 pub(crate) fn new(
50 local_name: LocalName,
51 prefix: Option<Prefix>,
52 document: &Document,
53 proto: Option<HandleObject>,
54 can_gc: CanGc,
55 ) -> DomRoot<HTMLOptGroupElement> {
56 Node::reflect_node_with_proto(
57 Box::new(HTMLOptGroupElement::new_inherited(
58 local_name, prefix, document,
59 )),
60 document,
61 proto,
62 can_gc,
63 )
64 }
65
66 fn update_select_validity(&self, can_gc: CanGc) {
67 if let Some(select) = self.owner_select_element() {
68 select
69 .validity_state(can_gc)
70 .perform_validation_and_update(ValidationFlags::all(), can_gc);
71 }
72 }
73
74 fn owner_select_element(&self) -> Option<DomRoot<HTMLSelectElement>> {
75 self.upcast::<Node>()
76 .GetParentNode()
77 .and_then(DomRoot::downcast)
78 }
79}
80
81impl HTMLOptGroupElementMethods<crate::DomTypeHolder> for HTMLOptGroupElement {
82 make_bool_getter!(Disabled, "disabled");
84
85 make_bool_setter!(SetDisabled, "disabled");
87
88 make_getter!(Label, "label");
90
91 make_setter!(SetLabel, "label");
93}
94
95impl VirtualMethods for HTMLOptGroupElement {
96 fn super_type(&self) -> Option<&dyn VirtualMethods> {
97 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
98 }
99
100 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
101 self.super_type()
102 .unwrap()
103 .attribute_mutated(attr, mutation, can_gc);
104 if attr.local_name() == &local_name!("disabled") {
105 let disabled_state = match mutation {
106 AttributeMutation::Set(None, _) => true,
107 AttributeMutation::Set(Some(_), _) => {
108 return;
110 },
111 AttributeMutation::Removed => false,
112 };
113 let el = self.upcast::<Element>();
114 el.set_disabled_state(disabled_state);
115 el.set_enabled_state(!disabled_state);
116 let options = el
117 .upcast::<Node>()
118 .children()
119 .filter(|child| child.is::<HTMLOptionElement>())
120 .map(|child| DomRoot::from_ref(child.downcast::<HTMLOptionElement>().unwrap()));
121 if disabled_state {
122 for option in options {
123 let el = option.upcast::<Element>();
124 el.set_disabled_state(true);
125 el.set_enabled_state(false);
126 }
127 } else {
128 for option in options {
129 let el = option.upcast::<Element>();
130 el.check_disabled_attribute();
131 }
132 }
133 }
134 }
135
136 fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
137 if let Some(super_type) = self.super_type() {
138 super_type.bind_to_tree(context, can_gc);
139 }
140
141 self.update_select_validity(can_gc);
142 }
143
144 fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
145 self.super_type().unwrap().unbind_from_tree(context, can_gc);
146
147 if let Some(select) = context.parent.downcast::<HTMLSelectElement>() {
148 select
149 .validity_state(can_gc)
150 .perform_validation_and_update(ValidationFlags::all(), can_gc);
151 }
152 }
153}