script/dom/html/
htmloptgroupelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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/// <https://html.spec.whatwg.org/multipage/#htmloptgroupelement>
28#[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    // https://html.spec.whatwg.org/multipage/#dom-optgroup-disabled
83    make_bool_getter!(Disabled, "disabled");
84
85    // https://html.spec.whatwg.org/multipage/#dom-optgroup-disabled
86    make_bool_setter!(SetDisabled, "disabled");
87
88    // https://html.spec.whatwg.org/multipage/#dom-optgroup-label
89    make_getter!(Label, "label");
90
91    // https://html.spec.whatwg.org/multipage/#dom-optgroup-label
92    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                    // Option group was already disabled.
109                    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}