Skip to main content

script/dom/event/
textevent.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 script_bindings::cell::DomRefCell;
8use script_bindings::codegen::GenericBindings::UIEventBinding::UIEventMethods;
9use script_bindings::domstring::TracedDOMString;
10use script_bindings::inheritance::Castable;
11use script_bindings::reflector::reflect_dom_object;
12use script_bindings::str::DOMString;
13
14use crate::dom::bindings::codegen::Bindings::TextEventBinding::TextEventMethods;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::event::Event;
17use crate::dom::uievent::UIEvent;
18use crate::dom::window::Window;
19
20#[dom_struct]
21/// <https://w3c.github.io/uievents/#textevent>
22pub(crate) struct TextEvent {
23    uievent: UIEvent,
24    data: DomRefCell<TracedDOMString>,
25}
26
27impl TextEvent {
28    pub(crate) fn new_inherited() -> TextEvent {
29        TextEvent {
30            uievent: UIEvent::new_inherited(),
31            data: DomRefCell::new(DOMString::new().traced()),
32        }
33    }
34
35    pub(crate) fn new_uninitialized(cx: &mut JSContext, window: &Window) -> DomRoot<TextEvent> {
36        reflect_dom_object(cx, Box::new(TextEvent::new_inherited()), window)
37    }
38}
39
40impl TextEventMethods<crate::DomTypeHolder> for TextEvent {
41    /// <https://w3c.github.io/uievents/event-algo.html#dom-textevent-inittextevent>
42    fn InitTextEvent(
43        &self,
44        type_: DOMString,
45        bubbles: bool,
46        cancelable: bool,
47        view: Option<&Window>,
48        data: DOMString,
49    ) {
50        // 1. If this’s dispatch flag is set, then return.
51        if self.upcast::<Event>().dispatching() {
52            return;
53        }
54
55        // 2. Initialize a UIEvent with this, type and eventTarget
56        // 3. Set this.bubbles = bubbles
57        // 4. Set this.cancelable = cancelable
58        // 5. Set this.view = view
59        // note: The bubbles/cancelable/view should be parameters to "Initialize a UIEvent" instead of being set twice.
60        self.uievent
61            .init_event(type_.into(), bubbles, cancelable, view, 0);
62
63        // 6. Set this.data = data
64        *self.data.borrow_mut() = data.traced();
65    }
66
67    fn Data(&self) -> DOMString {
68        self.data.borrow().clone().root()
69    }
70
71    fn IsTrusted(&self) -> bool {
72        self.uievent.IsTrusted()
73    }
74}