Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(renderer): dispatch form navigation events to target window
ldm0ed1c4f87d9b3
diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rsindex 70005f2e6..4f6dfd341 100644--- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs+++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs@@ -849,6 +849,7 @@ fn window_for_child_cross_document_location_navigation<'s>(pub(crate) fn dispatch_top_level_navigation_event_with_source_element<'s>(scope: &mut v8::PinScope<'s, '_>,+ owner: v8::Local<'s, v8::Object>,href: &str,navigation_type: &str,source_element: Option<v8::Local<'s, v8::Object>>,@@ -856,14 +857,11 @@ pub(crate) fn dispatch_top_level_navigation_event_with_source_element<'s>(user_initiated: bool,download_request: Option<&str>,) -> bool {- let global = scope.get_current_context().global(scope);- let Some(navigation) = super::navigation_window::window_navigation_for_holder(scope, global)+ let Some(navigation) = super::navigation_window::window_navigation_for_holder(scope, owner)else {return true;};- let current_href = global- .get(scope, v8str(scope, "location").into())- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())+ let current_href = window_location_for_holder(scope, owner).and_then(|location| location_href_slot(scope, location)).unwrap_or_default();let _ = cancel_active_navigation_event(scope, navigation);@@ -896,20 +894,18 @@ pub(crate) fn dispatch_top_level_navigation_event_with_source_element<'s>(pub(crate) fn dispatch_top_level_form_navigation_event<'s>(scope: &mut v8::PinScope<'s, '_>,+ owner: v8::Local<'s, v8::Object>,href: &str,navigation_type: &str,source_element: Option<v8::Local<'s, v8::Object>>,user_initiated: bool,form_data: v8::Local<'s, v8::Value>,) -> bool {- let global = scope.get_current_context().global(scope);- let Some(navigation) = super::navigation_window::window_navigation_for_holder(scope, global)+ let Some(navigation) = super::navigation_window::window_navigation_for_holder(scope, owner)else {return true;};- let current_href = global- .get(scope, v8str(scope, "location").into())- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())+ let current_href = window_location_for_holder(scope, owner).and_then(|location| location_href_slot(scope, location)).unwrap_or_default();let _ = cancel_active_navigation_event(scope, navigation);diff --git a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rsindex a533f399d..6b87a047d 100644--- a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs+++ b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs@@ -36,9 +36,10 @@ use super::super::{update_focus,};use super::targets::{- SpecialBrowsingContextTarget, form_navigation_target_document,- named_iframe_target_handle_for_navigation, navigate_hyperlink_source_browsing_context,- navigate_hyperlink_target_browsing_context, navigate_target_browsing_context,+ SpecialBrowsingContextTarget, browsing_context_window_for_dispatch_scope,+ form_navigation_target_document, named_iframe_target_handle_for_navigation,+ navigate_hyperlink_source_browsing_context, navigate_hyperlink_target_browsing_context,+ navigate_target_browsing_context,};fn array_like_length(scope: &mut v8::PinScope<'_, '_>, object: v8::Local<'_, v8::Object>) -> u32 {@@ -1964,11 +1965,13 @@ fn anchor_click_default_action(return None;}if target_name.is_none() || special_target == Some(SpecialBrowsingContextTarget::Current) {+ let window = navigation_owner_window_for_handle(scope, runtime_ptr, handle)?;let source_element = node_wrapper_from_handle(scope, handle);let can_intercept = url::Url::parse(&resolved).is_ok_and(|url| moli_url::same_origin(runtime.document_url(), &url));if !crate::context_bootstrap::dispatch_top_level_navigation_event_with_source_element(scope,+ window,&resolved,"push",source_element,@@ -2138,7 +2141,11 @@ fn navigation_owner_window_for_handle<'s>(.node(handle).and_then(Node::owner_document)?;if document_handle == runtime.document_handle() {- return Some(scope.get_current_context().global(scope));+ return browsing_context_window_for_dispatch_scope(+ scope,+ runtime_ptr,+ crate::native_bridge::OwnerDispatchScope::Top,+ );}if let Some(popup_id) = runtime.lightweight_popup_id_for_document_handle(document_handle) {return runtime.lightweight_popup_window(scope, popup_id);@@ -2366,9 +2373,17 @@ pub(in crate::native_bridge) fn navigate_form_target_browsing_context(}return navigated;}+ let Some(window) = browsing_context_window_for_dispatch_scope(+ scope,+ runtime_ptr,+ crate::native_bridge::OwnerDispatchScope::Top,+ ) else {+ return false;+ };let source_element = node_wrapper_from_handle(scope, form_handle);if !crate::context_bootstrap::dispatch_top_level_navigation_event_with_source_element(scope,+ window,resolved_url,"replace",source_element,diff --git a/moli-renderer-v8/src/native_bridge/element/activation/mod.rs b/moli-renderer-v8/src/native_bridge/element/activation/mod.rsindex cda4f3337..7c1972de8 100644--- a/moli-renderer-v8/src/native_bridge/element/activation/mod.rs+++ b/moli-renderer-v8/src/native_bridge/element/activation/mod.rs@@ -18,7 +18,8 @@ pub(crate) use targets::{navigate_named_iframe_target,};pub(in crate::native_bridge) use targets::{- form_navigation_target_document, named_iframe_target_handle_for_navigation,+ browsing_context_window_for_dispatch_scope, form_navigation_target_document,+ named_iframe_target_handle_for_navigation,};pub(in crate::native_bridge::element) use targets::{queue_deferred_named_iframe_target_navigation_from_document,diff --git a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs b/moli-renderer-v8/src/native_bridge/element/activation/targets.rsindex b6ecf3cc0..b853fec00 100644--- a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs+++ b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs@@ -437,7 +437,7 @@ fn hyperlink_javascript_url_allowed_by_csp(unsafe { &mut *runtime_ptr }.allows_inline_javascript_navigation_by_csp(scope, owner, &source)}-fn browsing_context_window_for_dispatch_scope<'s>(+pub(in crate::native_bridge) fn browsing_context_window_for_dispatch_scope<'s>(scope: &mut v8::PinScope<'s, '_>,runtime_ptr: *mut JsContextHost,dispatch_scope: crate::native_bridge::OwnerDispatchScope,@@ -445,7 +445,10 @@ fn browsing_context_window_for_dispatch_scope<'s>(let runtime = unsafe { &*runtime_ptr };match dispatch_scope {crate::native_bridge::OwnerDispatchScope::Top => {- Some(scope.get_current_context().global(scope))+ // A borrowed submission method can run in a different Window realm.+ let owner = runtime.current_window_execution_context_owner(dispatch_scope)?;+ let (_, context) = runtime.window_execution_context(scope, owner, dispatch_scope)?;+ Some(context.global(scope))}crate::native_bridge::OwnerDispatchScope::Child(handle) => {runtime.existing_child_browsing_context_window_wrapper(scope, handle)diff --git a/moli-renderer-v8/src/native_bridge/element/forms/submission.rs b/moli-renderer-v8/src/native_bridge/element/forms/submission.rsindex eda9a642c..a53dca966 100644--- a/moli-renderer-v8/src/native_bridge/element/forms/submission.rs+++ b/moli-renderer-v8/src/native_bridge/element/forms/submission.rs@@ -2,8 +2,8 @@ use super::*;use crate::blob;use crate::native_bridge::context_host::ChildBrowsingContextNavigationRequest;use crate::native_bridge::element::activation::{- SpecialBrowsingContextTarget, form_navigation_target_document,- named_iframe_target_handle_for_navigation,+ SpecialBrowsingContextTarget, browsing_context_window_for_dispatch_scope,+ form_navigation_target_document, named_iframe_target_handle_for_navigation,};use crate::native_bridge::element::{NodePublicEventDispatchOutcome, TextEditInputType, activate_default_submit_button_via_keyboard,@@ -979,6 +979,13 @@ fn submit_post_form_to_top_level_browsing_context(form_data_entries: &[(String, v8::Global<v8::Value>)],user_initiated: bool,) -> bool {+ let Some(window) = browsing_context_window_for_dispatch_scope(+ scope,+ runtime_ptr,+ crate::native_bridge::OwnerDispatchScope::Top,+ ) else {+ return false;+ };let Some(form_data) = form_data_object_from_entries(scope, form_data_entries) else {return false;};@@ -987,6 +994,7 @@ fn submit_post_form_to_top_level_browsing_context(let navigation_type = if user_initiated { "push" } else { "replace" };if !crate::context_bootstrap::dispatch_top_level_form_navigation_event(scope,+ window,resolved_url.as_str(),navigation_type,source_element,diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rsindex 1cf3e7806..35bdb91b4 100644--- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs@@ -1,5 +1,117 @@use super::*;+#[test]+fn form_navigation_events_use_resolved_top_window_across_realms() {+ for (depth, target) in [(1, "_top"), (1, "_PARENT"), (2, "_ToP")] {+ for method in ["get", "post"] {+ for api in ["submit", "requestSubmit"] {+ for use_child_realm in [true, false] {+ for cancel_top in [true, false] {+ let mut vm = new_parsed_test_vm(+ "https://form-navigation-target.test/source",+ "<!doctype html><body></body>",+ );+ let description = format!(+ "depth={depth} target={target} method={method} api={api} \+ child_realm={use_child_realm} cancel_top={cancel_top}"+ );+ let result = vm+ .eval(&format!(+ r#"+ (() => {{+ const seen = [];+ let source = document;+ for (let i = 0; i < {depth}; ++i) {{+ const frame = source.createElement('iframe');+ source.body.appendChild(frame);+ frame.srcdoc = '<body></body>';+ source = frame.contentDocument;+ frame.contentWindow.navigation.onnavigate = event => {{+ seen.push('child:' + i);+ event.preventDefault();+ }};+ frame.contentWindow.navigation.onnavigateerror = () => seen.push('child-error');+ }}+ const form = source.createElement('form');+ form.target = {target:?};+ form.method = {method:?};+ form.action = 'https://form-navigation-target.test/submitted';+ form.innerHTML = '<input name="value" value="b">';+ source.body.appendChild(form);+ navigation.onnavigate = event => {{+ seen.push([+ 'top',+ event.target === navigation,+ event.currentTarget === navigation,+ event.sourceElement === form,+ event.cancelable,+ event.navigationType,+ event.destination.url,+ {method:?} === 'get' ? event.formData === null : event.formData.get('value') === 'b'+ ]);+ event.signal.onabort = () => seen.push('top-abort');+ if ({cancel_top}) event.preventDefault();+ }};+ navigation.onnavigateerror = () => seen.push('top-error');+ const realm = {use_child_realm} ? source.defaultView : window;+ if ({use_child_realm} && realm.HTMLFormElement === HTMLFormElement)+ throw new Error('submission must use the child realm');+ realm.HTMLFormElement.prototype[{api:?}].call(form);+ return JSON.stringify(seen);+ }})()+ "#,+ ))+ .expect(&description);+ let expected_url = if method == "get" {+ "https://form-navigation-target.test/submitted?value=b"+ } else {+ "https://form-navigation-target.test/submitted"+ };+ let mut expected = vec![serde_json::json!([+ "top",+ true,+ true,+ true,+ true,+ "replace",+ expected_url,+ true+ ])];+ if cancel_top {+ expected.extend([+ serde_json::json!("top-abort"),+ serde_json::json!("top-error"),+ ]);+ }+ assert_eq!(+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),+ serde_json::json!(expected),+ "{description}"+ );+ let pending = vm.take_pending_location_navigation_with_seed();+ if cancel_top {+ assert!(pending.is_none(), "{description}");+ } else {+ let pending = pending.expect(&description);+ assert_eq!(pending.url.as_str(), expected_url, "{description}");+ assert_eq!(+ pending.request_method,+ method.to_ascii_uppercase(),+ "{description}"+ );+ assert_eq!(+ pending.request_body.as_deref(),+ (method == "post").then_some(b"value=b".as_slice()),+ "{description}"+ );+ }+ }+ }+ }+ }+ }+}+#[tokio::test]async fn named_element_navigation_prefers_its_source_frame_over_duplicate_names() {for action in ["anchor", "submit", "requestSubmit"] {