Files
servo/components/script/dom/compositionevent.rs
Martin Robinson 291e9e57cb script: Readily accept all event names as Atoms (#43066)
Accepting the name of the event as a `DOMString` means that when events
are constructed from within Servo (for instance via input event
handling), the static strings are converted to an owned `String`
wrapped in a `DOMString` and then finally converted to an `Atom` in
`Event`. This makes it so that all non-generated `Event` constructors
accept `Atom`, which can avoid a conversion entirely when the atom
already exists on the Stylo atoms list and eliminate one in-memory copy
in the case that it isn't on the atom list.

Eventually, all event names can be on the atom list and we can eliminate
all copies. This is a tiny optimization, but also makes the code much
friendlier everywhere.

Testing: This should not change behavior so should be covered by
existing test.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2026-03-06 19:25:33 +00:00

119 lines
3.5 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use js::rust::HandleObject;
use style::Atom;
use crate::dom::bindings::codegen::Bindings::CompositionEventBinding::{
self, CompositionEventMethods,
};
use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEvent_Binding::UIEventMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::{reflect_dom_object, reflect_dom_object_with_proto};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::uievent::UIEvent;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct CompositionEvent {
uievent: UIEvent,
data: DOMString,
}
impl CompositionEvent {
pub(crate) fn new_inherited() -> CompositionEvent {
CompositionEvent {
uievent: UIEvent::new_inherited(),
data: DOMString::new(),
}
}
pub(crate) fn new_uninitialized(window: &Window, can_gc: CanGc) -> DomRoot<CompositionEvent> {
reflect_dom_object(Box::new(CompositionEvent::new_inherited()), window, can_gc)
}
#[expect(clippy::too_many_arguments)]
pub(crate) fn new(
window: &Window,
event_type: Atom,
can_bubble: bool,
cancelable: bool,
view: Option<&Window>,
detail: i32,
data: DOMString,
can_gc: CanGc,
) -> DomRoot<CompositionEvent> {
Self::new_with_proto(
window, None, event_type, can_bubble, cancelable, view, detail, data, can_gc,
)
}
#[expect(clippy::too_many_arguments)]
fn new_with_proto(
window: &Window,
proto: Option<HandleObject>,
event_type: Atom,
can_bubble: bool,
cancelable: bool,
view: Option<&Window>,
detail: i32,
data: DOMString,
can_gc: CanGc,
) -> DomRoot<CompositionEvent> {
let ev = reflect_dom_object_with_proto(
Box::new(CompositionEvent {
uievent: UIEvent::new_inherited(),
data,
}),
window,
proto,
can_gc,
);
ev.uievent
.init_event(event_type, can_bubble, cancelable, view, detail);
ev
}
pub(crate) fn data(&self) -> &DOMString {
&self.data
}
}
impl CompositionEventMethods<crate::DomTypeHolder> for CompositionEvent {
/// <https://w3c.github.io/uievents/#dom-compositionevent-compositionevent>
fn Constructor(
window: &Window,
proto: Option<HandleObject>,
can_gc: CanGc,
event_type: DOMString,
init: &CompositionEventBinding::CompositionEventInit,
) -> Fallible<DomRoot<CompositionEvent>> {
let event = CompositionEvent::new_with_proto(
window,
proto,
event_type.into(),
init.parent.parent.bubbles,
init.parent.parent.cancelable,
init.parent.view.as_deref(),
init.parent.detail,
init.data.clone(),
can_gc,
);
Ok(event)
}
/// <https://w3c.github.io/uievents/#dom-compositionevent-data>
fn Data(&self) -> DOMString {
self.data.clone()
}
/// <https://dom.spec.whatwg.org/#dom-event-istrusted>
fn IsTrusted(&self) -> bool {
self.uievent.IsTrusted()
}
}