script/dom/
inputevent.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 embedder_traits::Cursor;
7use euclid::Point2D;
8use js::rust::HandleObject;
9use style::Atom;
10use style_traits::CSSPixel;
11
12use crate::dom::bindings::codegen::Bindings::InputEventBinding::{self, InputEventMethods};
13use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEvent_Binding::UIEventMethods;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::datatransfer::DataTransfer;
19use crate::dom::node::Node;
20use crate::dom::staticrange::StaticRange;
21use crate::dom::uievent::UIEvent;
22use crate::dom::window::Window;
23use crate::script_runtime::CanGc;
24
25#[dom_struct]
26pub(crate) struct InputEvent {
27    uievent: UIEvent,
28    data: Option<DOMString>,
29    is_composing: bool,
30    input_type: DOMString,
31}
32
33impl InputEvent {
34    #[expect(clippy::too_many_arguments)]
35    pub(crate) fn new(
36        window: &Window,
37        proto: Option<HandleObject>,
38        event_type: Atom,
39        can_bubble: bool,
40        cancelable: bool,
41        view: Option<&Window>,
42        detail: i32,
43        data: Option<DOMString>,
44        is_composing: bool,
45        input_type: DOMString,
46        can_gc: CanGc,
47    ) -> DomRoot<InputEvent> {
48        let event = reflect_dom_object_with_proto(
49            Box::new(InputEvent {
50                uievent: UIEvent::new_inherited(),
51                data,
52                is_composing,
53                input_type,
54            }),
55            window,
56            proto,
57            can_gc,
58        );
59        event
60            .uievent
61            .init_event(event_type, can_bubble, cancelable, view, detail);
62        event
63    }
64}
65
66impl InputEventMethods<crate::DomTypeHolder> for InputEvent {
67    /// <https://w3c.github.io/uievents/#dom-inputevent-inputevent>
68    fn Constructor(
69        window: &Window,
70        proto: Option<HandleObject>,
71        can_gc: CanGc,
72        event_type: DOMString,
73        init: &InputEventBinding::InputEventInit,
74    ) -> Fallible<DomRoot<InputEvent>> {
75        let event = InputEvent::new(
76            window,
77            proto,
78            event_type.into(),
79            init.parent.parent.bubbles,
80            init.parent.parent.cancelable,
81            init.parent.view.as_deref(),
82            init.parent.detail,
83            init.data.clone(),
84            init.isComposing,
85            init.inputType.clone(),
86            can_gc,
87        );
88        Ok(event)
89    }
90
91    /// <https://w3c.github.io/uievents/#dom-inputevent-data>
92    fn GetData(&self) -> Option<DOMString> {
93        self.data.clone()
94    }
95
96    /// <https://w3c.github.io/uievents/#dom-inputevent-iscomposing>
97    fn IsComposing(&self) -> bool {
98        self.is_composing
99    }
100
101    /// <https://w3c.github.io/uievents/#dom-inputevent-inputtype>
102    fn InputType(&self) -> DOMString {
103        self.input_type.clone()
104    }
105
106    /// <https://w3c.github.io/input-events/#dom-inputevent-datatransfer>
107    fn GetDataTransfer(&self) -> Option<DomRoot<DataTransfer>> {
108        // TODO: Populate dataTransfer for contenteditable
109        None
110    }
111
112    /// <https://w3c.github.io/input-events/#dom-inputevent-gettargetranges>
113    fn GetTargetRanges(&self) -> Vec<DomRoot<StaticRange>> {
114        // TODO: Populate targetRanges for contenteditable
115        Vec::new()
116    }
117
118    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
119    fn IsTrusted(&self) -> bool {
120        self.uievent.IsTrusted()
121    }
122}
123
124/// A [`HitTestResult`] that is the result of doing a hit test based on a less-fine-grained
125/// `PaintHitTestResult` against our current layout.
126pub(crate) struct HitTestResult {
127    pub node: DomRoot<Node>,
128    pub cursor: Cursor,
129    pub point_in_node: Point2D<f32, CSSPixel>,
130    pub point_in_frame: Point2D<f32, CSSPixel>,
131    pub point_relative_to_initial_containing_block: Point2D<f32, CSSPixel>,
132}