Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(events): dispatch host events through native node dispatch

ldm084157b28a69d
diff --git a/moli-renderer-v8/src/detached_event_target.rs b/moli-renderer-v8/src/detached_event_target.rs
index 3fa634af5..cccea822f 100644
--- a/moli-renderer-v8/src/detached_event_target.rs
+++ b/moli-renderer-v8/src/detached_event_target.rs
@@ -1,4 +1,4 @@
-use super::util::{call_script_visible_function, v8_string, v8str};
+use super::util::v8_string;
use moli_webapi_declare::WebApiObject;
#[derive(WebApiObject)]
@@ -26,9 +26,9 @@ fn build_focus_event<'s>(
related_target: Option<v8::Local<'s, v8::Value>>,
bubbles: bool,
) -> Option<v8::Local<'s, v8::Object>> {
- let global = scope.get_current_context().global(scope);
- let constructor = global.get(scope, v8str(scope, "FocusEvent").into())?;
- let constructor = v8::Local::<v8::Function>::try_from(constructor).ok()?;
+ let constructor =
+ crate::context_bootstrap::ensure_intrinsic_interface_constructor(scope, "FocusEvent")
+ .ok()?;
let init = DetachedFocusEventInitDeclaration::new(
bubbles,
related_target.unwrap_or_else(|| v8::null(scope).into()),
@@ -47,9 +47,8 @@ fn build_simple_event<'s>(
cancelable: bool,
composed: bool,
) -> Option<v8::Local<'s, v8::Object>> {
- let global = scope.get_current_context().global(scope);
- let constructor = global.get(scope, v8str(scope, "Event").into())?;
- let constructor = v8::Local::<v8::Function>::try_from(constructor).ok()?;
+ let constructor =
+ crate::context_bootstrap::ensure_intrinsic_interface_constructor(scope, "Event").ok()?;
let init = DetachedSimpleEventInitDeclaration::new(bubbles, cancelable, composed)
.bind(scope)
.ok()?;
@@ -69,20 +68,7 @@ pub(super) fn dispatch_detached_simple_event<'s>(
let Some(event) = build_simple_event(scope, event_type, bubbles, cancelable, composed) else {
return true;
};
- let Some(dispatch_event) = target
- .get(scope, v8str(scope, "dispatchEvent").into())
- .and_then(|value| v8::Local::<v8::Function>::try_from(value).ok())
- else {
- return true;
- };
- call_script_visible_function(
- scope,
- dispatch_event,
- target.into(),
- &[event.into()],
- "detached dispatchEvent",
- )
- .is_none_or(|value| value.boolean_value(scope))
+ dispatch_node_event(scope, target, event)
}
pub(super) fn dispatch_detached_focus_event<'s>(
@@ -95,18 +81,22 @@ pub(super) fn dispatch_detached_focus_event<'s>(
let Some(event) = build_focus_event(scope, event_type, related_target, bubbles) else {
return true;
};
- let Some(dispatch_event) = target
- .get(scope, v8str(scope, "dispatchEvent").into())
- .and_then(|value| v8::Local::<v8::Function>::try_from(value).ok())
+ dispatch_node_event(scope, target, event)
+}
+
+fn dispatch_node_event<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ target: v8::Local<'s, v8::Object>,
+ event: v8::Local<'s, v8::Object>,
+) -> bool {
+ let Ok((runtime_ptr, handle)) =
+ crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, target)
else {
return true;
};
- call_script_visible_function(
- scope,
- dispatch_event,
- target.into(),
- &[event.into()],
- "detached dispatchEvent",
- )
- .is_none_or(|value| value.boolean_value(scope))
+ // Host dispatch must not call the author-visible dispatchEvent method.
+ // An override could intercept the event, and that script-call boundary is
+ // not part of native event dispatch or its selected-task completion.
+ crate::native_bridge::element::dispatch_public_event(scope, runtime_ptr, handle, event)
+ .allows_default()
}
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/child_document_lifecycle.rs b/moli-renderer-v8/src/runtime/page_vm/tests/child_document_lifecycle.rs
index 7d2b240ab..978cc6a79 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/child_document_lifecycle.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/child_document_lifecycle.rs
@@ -56,6 +56,10 @@ async fn child_document_lifecycle_body_leaves_reactions_for_selected_completion(
let (mut page_vm, _resource_source, _owner_wake_rx) =
page_vm_with_bound_task_sources_and_owner_wake(&loader, document_url);
install_child_document_lifecycle_fixture(&mut page_vm, "lifecycle-body").await?;
+ page_vm.vm_mut().eval(r#"
+ Object.defineProperty(document.getElementById("lifecycle-body").contentDocument,
+ "dispatchEvent", {get() { throw new Error("host event used dispatchEvent"); }});
+ "#)?;
let body = page_vm
.run_page_child_document_lifecycle_body_for_test()