Skip to main content

script/dom/event/
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::context::JSContext;
9use js::rust::HandleObject;
10use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
11use style::Atom;
12use style_traits::CSSPixel;
13
14use crate::dom::bindings::codegen::Bindings::InputEventBinding::{self, InputEventMethods};
15use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEvent_Binding::UIEventMethods;
16use crate::dom::bindings::error::Fallible;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::datatransfer::DataTransfer;
20use crate::dom::node::Node;
21use crate::dom::staticrange::StaticRange;
22use crate::dom::uievent::UIEvent;
23use crate::dom::window::Window;
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        cx: &mut JSContext,
37        window: &Window,
38        proto: Option<HandleObject>,
39        event_type: Atom,
40        can_bubble: bool,
41        cancelable: bool,
42        view: Option<&Window>,
43        detail: i32,
44        data: Option<DOMString>,
45        is_composing: bool,
46        input_type: DOMString,
47    ) -> DomRoot<InputEvent> {
48        let event = reflect_dom_object_with_proto_and_cx(
49            Box::new(InputEvent {
50                uievent: UIEvent::new_inherited(),
51                data,
52                is_composing,
53                input_type,
54            }),
55            window,
56            proto,
57            cx,
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        cx: &mut JSContext,
70        window: &Window,
71        proto: Option<HandleObject>,
72        event_type: DOMString,
73        init: &InputEventBinding::InputEventInit,
74    ) -> Fallible<DomRoot<InputEvent>> {
75        let event = InputEvent::new(
76            cx,
77            window,
78            proto,
79            event_type.into(),
80            init.parent.parent.bubbles,
81            init.parent.parent.cancelable,
82            init.parent.view.as_deref(),
83            init.parent.detail,
84            init.data.clone(),
85            init.isComposing,
86            init.inputType.clone(),
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}