script/dom/html/
htmldialogelement.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, ns};
7use js::rust::HandleObject;
8
9use crate::dom::bindings::cell::DomRefCell;
10use crate::dom::bindings::codegen::Bindings::HTMLDialogElementBinding::HTMLDialogElementMethods;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::document::Document;
15use crate::dom::element::Element;
16use crate::dom::eventtarget::EventTarget;
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::node::{Node, NodeTraits};
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct HTMLDialogElement {
23    htmlelement: HTMLElement,
24    return_value: DomRefCell<DOMString>,
25}
26
27impl HTMLDialogElement {
28    fn new_inherited(
29        local_name: LocalName,
30        prefix: Option<Prefix>,
31        document: &Document,
32    ) -> HTMLDialogElement {
33        HTMLDialogElement {
34            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
35            return_value: DomRefCell::new(DOMString::new()),
36        }
37    }
38
39    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
40    pub(crate) fn new(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44        proto: Option<HandleObject>,
45        can_gc: CanGc,
46    ) -> DomRoot<HTMLDialogElement> {
47        Node::reflect_node_with_proto(
48            Box::new(HTMLDialogElement::new_inherited(
49                local_name, prefix, document,
50            )),
51            document,
52            proto,
53            can_gc,
54        )
55    }
56}
57
58impl HTMLDialogElementMethods<crate::DomTypeHolder> for HTMLDialogElement {
59    // https://html.spec.whatwg.org/multipage/#dom-dialog-open
60    make_bool_getter!(Open, "open");
61
62    // https://html.spec.whatwg.org/multipage/#dom-dialog-open
63    make_bool_setter!(SetOpen, "open");
64
65    // https://html.spec.whatwg.org/multipage/#dom-dialog-returnvalue
66    fn ReturnValue(&self) -> DOMString {
67        let return_value = self.return_value.borrow();
68        return_value.clone()
69    }
70
71    // https://html.spec.whatwg.org/multipage/#dom-dialog-returnvalue
72    fn SetReturnValue(&self, return_value: DOMString) {
73        *self.return_value.borrow_mut() = return_value;
74    }
75
76    /// <https://html.spec.whatwg.org/multipage/#dom-dialog-show>
77    fn Show(&self, can_gc: CanGc) {
78        let element = self.upcast::<Element>();
79
80        // Step 1 TODO: Check is modal flag is false
81        if element.has_attribute(&local_name!("open")) {
82            return;
83        }
84
85        // TODO: Step 2 If this has an open attribute, then throw an "InvalidStateError" DOMException.
86
87        // Step 3
88        element.set_bool_attribute(&local_name!("open"), true, can_gc);
89
90        // TODO: Step 4 Set this's previously focused element to the focused element.
91
92        // TODO: Step 5 Let hideUntil be the result of running topmost popover ancestor given this, null, and false.
93
94        // TODO: Step 6 If hideUntil is null, then set hideUntil to this's node document.
95
96        // TODO: Step 7 Run hide all popovers until given hideUntil, false, and true.
97
98        // TODO(Issue #32702): Step 8 Run the dialog focusing steps given this.
99    }
100
101    // https://html.spec.whatwg.org/multipage/#dom-dialog-close
102    fn Close(&self, return_value: Option<DOMString>, can_gc: CanGc) {
103        let element = self.upcast::<Element>();
104        let target = self.upcast::<EventTarget>();
105
106        // Step 1 & 2
107        if element
108            .remove_attribute(&ns!(), &local_name!("open"), can_gc)
109            .is_none()
110        {
111            return;
112        }
113
114        // Step 3
115        if let Some(new_value) = return_value {
116            *self.return_value.borrow_mut() = new_value;
117        }
118
119        // TODO: Step 4 implement pending dialog stack removal
120
121        // Step 5
122        self.owner_global()
123            .task_manager()
124            .dom_manipulation_task_source()
125            .queue_simple_event(target, atom!("close"));
126    }
127}