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