diff --git a/moli-core/tests/history_child/location_before_load.rs b/moli-core/tests/history_child/location_before_load.rs index bf3bf589f14..3eb6ec5f4e3 100644 --- a/moli-core/tests/history_child/location_before_load.rs +++ b/moli-core/tests/history_child/location_before_load.rs @@ -221,3 +221,95 @@ async fn main_location_navigation_pushes_after_load_and_document_open() -> Resul server.shutdown().await; Ok(()) } + +async fn popup_navigation_history(phase: &str, api: &str, replaces: bool) -> Result<()> { + let server = FixtureServer::spawn().await?; + let browser = Browser::new(AppConfig::default())?; + let destination = markup_url( + &server, + "", + ); + let source = format!( + r#""#, + serde_json::to_string(&destination)?.replace("", "<\\/script>"), + serde_json::to_string(api)?, + serde_json::to_string(phase)? + ); + let parent = format!( + r#""#, + serde_json::to_string(&markup_url(&server, &source))?.replace("", "<\\/script>") + ); + let result = tokio::time::timeout(Duration::from_secs(10), async { + let mut page = browser.fetch(&markup_url(&server, &parent)).await?; + let result = page + .evaluate_runtime_expression_with_await_async( + "finished.then(value => JSON.stringify(value))", + true, + ) + .await?; + page.evaluate_runtime_expression_with_await_async("popup.close()", false) + .await?; + Ok::<_, anyhow::Error>(result) + }) + .await??; + let result: serde_json::Value = serde_json::from_str( + result["value"] + .as_str() + .expect("popup navigation history result"), + )?; + assert_eq!( + result["after"].as_u64().unwrap() - result["before"].as_u64().unwrap(), + u64::from(!replaces), + "phase={phase}, api={api}, {result}" + ); + server.shutdown().await; + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn popup_location_navigation_replaces_until_load_callbacks_finish() -> Result<()> { + for phase in ["parser", "load"] { + for api in ["href", "assign", "document"] { + popup_navigation_history(phase, api, true).await?; + } + } + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn popup_location_navigation_pushes_after_complete_even_after_document_open() -> Result<()> { + for phase in ["after-load", "reopen"] { + for api in ["href", "assign", "document"] { + popup_navigation_history(phase, api, false).await?; + } + } + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn popup_window_open_self_before_load_keeps_push_history() -> Result<()> { + for phase in ["parser", "load"] { + popup_navigation_history(phase, "window-open", false).await?; + } + Ok(()) +} diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs index 01442b434ec..bc4e98c6c2e 100644 --- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs +++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs @@ -93,8 +93,13 @@ pub(crate) fn navigate_location_object<'s>( let owner = runtime_window_owner(scope, location); let replaces_unloaded_document = kind == LocationNavigationKind::Assign && location_document_is_before_load_complete(scope, owner) - && !context_host_ptr_for_navigation_owner(scope, owner) - .is_some_and(|host| unsafe { &*host }.protocol_user_gesture_activation()); + && !context_host_ptr_for_navigation_owner(scope, owner).is_some_and(|host| { + let host = unsafe { &*host }; + host.protocol_user_gesture_activation() + || host.window_has_transient_user_activation(location_navigation_initiator_scope( + scope, host, + )) + }); let kind = if replaces_unloaded_document { LocationNavigationKind::Replace } else { @@ -1039,14 +1044,7 @@ fn location_document_is_before_load_complete<'s>( return false; }; let host = unsafe { &*host_ptr }; - let document_handle = if runtime_window_is_global(scope, owner) { - Some(host.document_handle()) - } else { - child_browsing_context_handle_for_runtime_owner(scope, owner) - .and_then(|handle| host.frame_owner_current_child_snapshot(handle)) - .map(|snapshot| snapshot.document_handle) - }; - document_handle + super::window_accessors::window_document_handle(scope, owner, host) .is_some_and(|document| host.document_is_completely_loaded(document) == Some(false)) } @@ -1190,19 +1188,7 @@ fn location_navigation_initiator_url( scope: &mut v8::PinScope<'_, '_>, host: &JsContextHost, ) -> Option { - if let Some(popup_id) = crate::native_bridge::active_lightweight_popup_id(scope) { - return host.lightweight_popup_document_url(popup_id); - } - let dispatch_scope = scope - .get_incumbent_context() - .and_then(|context| host.window_execution_context_identity_for_access_check(context)) - .map(|identity| identity.dispatch_scope()) - .or_else(|| { - crate::context_bootstrap::current_child_browsing_context_handle_for_runtime_scope(scope) - .map(crate::native_bridge::OwnerDispatchScope::Child) - }) - .unwrap_or(crate::native_bridge::OwnerDispatchScope::Top); - match dispatch_scope { + match location_navigation_initiator_scope(scope, host) { crate::native_bridge::OwnerDispatchScope::Top => Some(host.document_url().clone()), crate::native_bridge::OwnerDispatchScope::Child(handle) => { host.child_browsing_context_current_url(handle) @@ -1213,6 +1199,24 @@ fn location_navigation_initiator_url( } } +fn location_navigation_initiator_scope( + scope: &mut v8::PinScope<'_, '_>, + host: &JsContextHost, +) -> crate::native_bridge::OwnerDispatchScope { + if let Some(popup_id) = crate::native_bridge::active_lightweight_popup_id(scope) { + return crate::native_bridge::OwnerDispatchScope::LightweightPopup(popup_id); + } + scope + .get_incumbent_context() + .and_then(|context| host.window_execution_context_identity_for_access_check(context)) + .map(|identity| identity.dispatch_scope()) + .or_else(|| { + crate::context_bootstrap::current_child_browsing_context_handle_for_runtime_scope(scope) + .map(crate::native_bridge::OwnerDispatchScope::Child) + }) + .unwrap_or(crate::native_bridge::OwnerDispatchScope::Top) +} + #[cfg(test)] mod meta_refresh_tests { use super::*; diff --git a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs index 9ec273d8ed9..f76fe413c64 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs @@ -163,6 +163,9 @@ impl JsContextHost { &self, document_handle: crate::document_runtime::DomHandle, ) -> Option { + if let Some(popup_id) = self.lightweight_popup_id_for_document_handle(document_handle) { + return self.lightweight_popup_document_is_completely_loaded(popup_id); + } let owner = if document_handle == self.document_handle() { self.current_main_document_task_owner()?.document_owner() } else { diff --git a/moli-renderer-v8/src/native_bridge/context_host/popups.rs b/moli-renderer-v8/src/native_bridge/context_host/popups.rs index dd7cc7e896e..a1e891efb47 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs @@ -405,6 +405,9 @@ struct LightweightPopupDocumentRecord { pending_load_event: Option, queued_load_event: Option, load_event_dispatched: bool, + // Location replacement remains in effect while load callbacks run, and + // document.open() does not make a previously loaded Document incomplete. + completely_loaded: bool, } struct LightweightPopupDocumentCommit { @@ -1028,6 +1031,7 @@ impl JsContextHost { pending_load_event: None, queued_load_event: None, load_event_dispatched: false, + completely_loaded: true, }, session_storage_store, })), @@ -1326,6 +1330,14 @@ impl JsContextHost { .is_some_and(|document| document.is_initial_empty_document) } + pub(in crate::native_bridge::context_host) fn lightweight_popup_document_is_completely_loaded( + &self, + popup_id: u64, + ) -> Option { + self.lightweight_popup_document_record(popup_id) + .map(|document| document.completely_loaded) + } + pub(crate) fn lightweight_popup_session_storage_store( &self, popup_id: u64, @@ -2150,6 +2162,7 @@ impl JsContextHost { pending_load_event: None, queued_load_event: None, load_event_dispatched: false, + completely_loaded: false, }, ); record.location_url = commit.location_url; @@ -4266,6 +4279,18 @@ impl JsContextHost { document.load_event_dispatched = true; } + pub(crate) fn note_lightweight_popup_load_event_finished( + &mut self, + owner: LightweightPopupDocumentOwner, + ) { + if !self.lightweight_popup_document_owner_is_current(owner) { + return; + } + self.lightweight_popup_document_record_mut(owner.popup_id()) + .expect("current popup load delivery must retain its Document record") + .completely_loaded = true; + } + pub(crate) fn discard_stale_lightweight_popup_load_event_task( &mut self, stale: LightweightPopupNavigationTaskToken, diff --git a/moli-renderer-v8/src/script_vm/popup_load_event.rs b/moli-renderer-v8/src/script_vm/popup_load_event.rs index a020e1655ac..bed99748ed1 100644 --- a/moli-renderer-v8/src/script_vm/popup_load_event.rs +++ b/moli-renderer-v8/src/script_vm/popup_load_event.rs @@ -33,6 +33,8 @@ impl ScriptVm { self.with_default_context_scope(|scope, host_ptr| { unsafe { &mut *host_ptr } .dispatch_lightweight_popup_load_event(scope, target.popup_id()); + unsafe { &mut *host_ptr } + .note_lightweight_popup_load_event_finished(target.document_owner()); Ok(()) }) } 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.rs index 0eb2e4a7c00..29d697a360d 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 @@ -1041,12 +1041,22 @@ async fn javascript_form_actions_execute_without_serializing_the_entry_list() { #[test] fn location_assign_before_complete_load_respects_user_activation() { - for activated in [false, true] { + for activation in ["none", "protocol", "input"] { + let activated = activation != "none"; let mut vm = new_storage_test_vm("https://location-before-load.test/source"); - if activated { + if activation == "protocol" { vm._context_host .borrow_mut() .begin_protocol_user_gesture_activation(); + } else if activation == "input" { + vm.eval("if (!document.documentElement) document.appendChild(document.createElement('html')); if (!document.body) document.documentElement.appendChild(document.createElement('body'));").unwrap(); + vm.dispatch_key_event("keydown", "x", "KeyX", "x", 0, false, false) + .unwrap(); + assert_eq!( + vm.eval("navigator.userActivation.isActive").unwrap(), + "true" + ); + assert!(!vm._context_host.borrow().protocol_user_gesture_activation()); } let navigation_type = vm .eval( @@ -1058,7 +1068,7 @@ fn location_assign_before_complete_load_respects_user_activation() { "#, ) .expect("Location navigation must expose its resolved history behavior"); - if activated { + if activation == "protocol" { vm._context_host .borrow_mut() .end_protocol_user_gesture_activation();