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