Skip to main content

script/dom/event/
customevent.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 js::context::JSContext;
7use js::jsapi::Heap;
8use js::jsval::JSVal;
9use js::rust::{HandleObject, HandleValue, MutableHandleValue};
10use script_bindings::reflector::reflect_dom_object_with_proto;
11use stylo_atoms::Atom;
12
13use crate::dom::bindings::codegen::Bindings::CustomEventBinding;
14use crate::dom::bindings::codegen::Bindings::CustomEventBinding::CustomEventMethods;
15use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::bindings::trace::RootedTraceableBox;
20use crate::dom::event::Event;
21use crate::dom::globalscope::GlobalScope;
22
23// https://dom.spec.whatwg.org/#interface-customevent
24#[dom_struct]
25pub(crate) struct CustomEvent {
26    event: Event,
27    #[ignore_malloc_size_of = "Defined in rust-mozjs"]
28    detail: Heap<JSVal>,
29}
30
31impl CustomEvent {
32    fn new_inherited() -> CustomEvent {
33        CustomEvent {
34            event: Event::new_inherited(),
35            detail: Heap::default(),
36        }
37    }
38
39    pub(crate) fn new_uninitialized(
40        cx: &mut JSContext,
41        global: &GlobalScope,
42    ) -> DomRoot<CustomEvent> {
43        Self::new_uninitialized_with_proto(cx, global, None)
44    }
45
46    fn new_uninitialized_with_proto(
47        cx: &mut JSContext,
48        global: &GlobalScope,
49        proto: Option<HandleObject>,
50    ) -> DomRoot<CustomEvent> {
51        reflect_dom_object_with_proto(cx, Box::new(CustomEvent::new_inherited()), global, proto)
52    }
53
54    fn new(
55        cx: &mut JSContext,
56        global: &GlobalScope,
57        proto: Option<HandleObject>,
58        type_: Atom,
59        bubbles: bool,
60        cancelable: bool,
61        detail: HandleValue,
62    ) -> DomRoot<CustomEvent> {
63        let ev = CustomEvent::new_uninitialized_with_proto(cx, global, proto);
64        ev.init_custom_event(type_, bubbles, cancelable, detail);
65        ev
66    }
67
68    fn init_custom_event(
69        &self,
70        type_: Atom,
71        can_bubble: bool,
72        cancelable: bool,
73        detail: HandleValue,
74    ) {
75        let event = self.upcast::<Event>();
76        if event.dispatching() {
77            return;
78        }
79
80        self.detail.set(detail.get());
81        event.init_event(type_, can_bubble, cancelable);
82    }
83}
84
85impl CustomEventMethods<crate::DomTypeHolder> for CustomEvent {
86    /// <https://dom.spec.whatwg.org/#dom-customevent-customevent>
87    fn Constructor(
88        cx: &mut JSContext,
89        global: &GlobalScope,
90        proto: Option<HandleObject>,
91        type_: DOMString,
92        init: RootedTraceableBox<CustomEventBinding::CustomEventInit>,
93    ) -> DomRoot<CustomEvent> {
94        let event = CustomEvent::new(
95            cx,
96            global,
97            proto,
98            Atom::from(type_),
99            init.parent.bubbles,
100            init.parent.cancelable,
101            init.detail.handle(),
102        );
103        event.upcast::<Event>().set_composed(init.parent.composed);
104        event
105    }
106
107    /// <https://dom.spec.whatwg.org/#dom-customevent-detail>
108    fn Detail(&self, mut retval: MutableHandleValue) {
109        retval.set(self.detail.get())
110    }
111
112    /// <https://dom.spec.whatwg.org/#dom-customevent-initcustomevent>
113    fn InitCustomEvent(
114        &self,
115        type_: DOMString,
116        can_bubble: bool,
117        cancelable: bool,
118        detail: HandleValue,
119    ) {
120        self.init_custom_event(Atom::from(type_), can_bubble, cancelable, detail)
121    }
122
123    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
124    fn IsTrusted(&self) -> bool {
125        self.event.IsTrusted()
126    }
127}