Nacre/vendor/servo-script/dom/event/commandevent.rs
astar76 0ecfd0f573 S031: root due timers in vendored servo-script (R5-01 SIGSEGV), crash-adblock regression smoke
fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept
the functions of the timers still waiting. Heavy-web reruns and cargo test still pending.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 20:38:47 -06:00

128 lines
4.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::context::JSContext;
use js::rust::HandleObject;
use script_bindings::codegen::GenericBindings::NodeBinding::NodeMethods;
use script_bindings::inheritance::Castable;
use script_bindings::reflector::reflect_dom_object_with_proto;
use stylo_atoms::Atom;
use crate::dom::bindings::codegen::Bindings::CommandEventBinding;
use crate::dom::bindings::codegen::Bindings::CommandEventBinding::CommandEventMethods;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::element::Element;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::eventtarget::EventTarget;
use crate::dom::node::Node;
use crate::dom::types::Window;
#[dom_struct]
pub(crate) struct CommandEvent {
event: Event,
/// <https://html.spec.whatwg.org/multipage/#dom-commandevent-source>
source: Option<Dom<Element>>,
/// <https://html.spec.whatwg.org/multipage/#dom-commandevent-command>
command: DOMString,
}
impl CommandEvent {
pub(crate) fn new_inherited(source: Option<&Element>, command: DOMString) -> CommandEvent {
CommandEvent {
event: Event::new_inherited(),
source: source.map(Dom::from_ref),
command,
}
}
#[expect(clippy::too_many_arguments)]
fn new_with_proto(
cx: &mut JSContext,
window: &Window,
proto: Option<HandleObject>,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
source: Option<&Element>,
command: DOMString,
) -> DomRoot<CommandEvent> {
let event = Box::new(CommandEvent::new_inherited(source, command));
let event = reflect_dom_object_with_proto(cx, event, window, proto);
{
let event = event.upcast::<Event>();
event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
}
event
}
pub(crate) fn new(
cx: &mut JSContext,
window: &Window,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
source: Option<&Element>,
command: DOMString,
) -> DomRoot<CommandEvent> {
Self::new_with_proto(
cx, window, None, type_, bubbles, cancelable, source, command,
)
}
}
impl CommandEventMethods<crate::DomTypeHolder> for CommandEvent {
/// <https://html.spec.whatwg.org/multipage/#commandevent>
fn Constructor(
cx: &mut JSContext,
window: &Window,
proto: Option<HandleObject>,
type_: DOMString,
init: &CommandEventBinding::CommandEventInit,
) -> Fallible<DomRoot<CommandEvent>> {
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
Ok(CommandEvent::new_with_proto(
cx,
window,
proto,
Atom::from(type_),
bubbles,
cancelable,
init.source.as_deref(),
init.command.clone(),
))
}
/// <https://dom.spec.whatwg.org/#dom-event-istrusted>
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
/// <https://html.spec.whatwg.org/multipage/#dom-commandevent-command>
fn Command(&self) -> DOMString {
// The command attribute must return the value it was initialized to.
self.command.clone()
}
/// <https://html.spec.whatwg.org/multipage/#dom-commandevent-source>
fn GetSource(&self) -> Option<DomRoot<Element>> {
// The source getter steps are to return the result of retargeting source against this's currentTarget.
let source = self.source.as_ref()?;
if let Some(current_target) = self.event.GetCurrentTarget() {
let retargeted = source.upcast::<EventTarget>().retarget(&current_target);
return retargeted.downcast::<Element>().map(DomRoot::from_ref);
}
let document = source.upcast::<Node>().GetOwnerDocument().unwrap();
let retargeted = source
.upcast::<EventTarget>()
.retarget(document.upcast::<EventTarget>());
retargeted.downcast::<Element>().map(DomRoot::from_ref)
}
}