From f8d252b5f2dc71508df886c6fabe5e2ba3dd84c8 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Tue, 22 Sep 2026 09:15:55 +0800 Subject: [PATCH] fix(navigation): bind entries and events to the active document Bind Navigation objects to native Document identity and use the initial-empty lifecycle flag instead of the URL to disable entries. Preserve initialness through document.open(), and replace the Navigation object when javascript: creates a new Document without changing its URL. Keep underlying History and Location navigation working when Navigation entries and events are disabled. Reject explicit pushes from initial documents and preserve the required pending promises. Cover initial, srcdoc, javascript, and subsequently loaded blank documents using a fixture also compared against Chrome. Preserve fragment changes on a retained popup Document before its queued close task runs, while continuing to abort navigation when a navigate handler closes the window. --- moli-core/tests/history_child.rs | 8 +- .../context_bootstrap/location_navigation.rs | 46 ++----- .../context_bootstrap/navigation_bootstrap.rs | 10 +- .../navigation_callbacks/navigation.rs | 67 ++++----- .../navigation_cross_document.rs | 12 +- .../context_bootstrap/navigation_events.rs | 12 +- .../context_bootstrap/navigation_mutation.rs | 4 +- .../context_bootstrap/navigation_restore.rs | 39 +++++- .../context_bootstrap/navigation_result.rs | 4 + .../context_bootstrap/navigation_surface.rs | 5 +- .../navigation_surface/accessors.rs | 8 +- .../context_bootstrap/navigation_traversal.rs | 12 +- .../navigation_traversal_plan.rs | 10 +- .../context_bootstrap/navigation_window.rs | 98 +++++++++----- .../src/frame_owner_model/records.rs | 10 +- .../src/frame_owner_model/store.rs | 30 +++-- .../native_bridge/context_host/host_loads.rs | 2 +- .../navigation/element_navigation.rs | 127 ++++++++++++++++++ .../navigation/history_traversal.rs | 4 +- .../extracted/frame_navigation_and_history.rs | 38 ++++-- .../extracted/window_and_frame_surfaces.rs | 20 ++- .../src/script_vm/tests/dom_xhr/forms.rs | 14 +- .../tests/extracted/child_documents.rs | 2 +- .../fixtures/navigation-initial-document.js | 71 ++++++++++ 24 files changed, 476 insertions(+), 177 deletions(-) create mode 100644 moli-renderer-v8/tests/fixtures/navigation-initial-document.js diff --git a/moli-core/tests/history_child.rs b/moli-core/tests/history_child.rs index fcf7f19b724..7f27fb7f13c 100644 --- a/moli-core/tests/history_child.rs +++ b/moli-core/tests/history_child.rs @@ -327,7 +327,7 @@ async fn assert_child_location_navigation_stays_window_local( page.serialize_html_async() .await .unwrap() - .contains("data-child-current-entry-index=\"0\""), + .contains("data-child-current-entry-index=\"-1\""), "{}", page.serialize_html_async().await.unwrap() ); @@ -341,7 +341,7 @@ async fn assert_child_location_navigation_stays_window_local( page.serialize_html_async() .await .unwrap() - .contains(&format!("data-child-current-entry-url=\"{}\"", initial_url)), + .contains("data-child-current-entry-url=\"null\""), "{}", page.serialize_html_async().await.unwrap() ); @@ -5829,7 +5829,7 @@ async fn child_browsing_context_pending_navigation_keeps_committed_window_docume page.serialize_html_async() .await .unwrap() - .contains("data-current-entry-immediate=\"about:blank\"") + .contains("data-current-entry-immediate=\"null\"") ); assert!( page.serialize_html_async() @@ -5891,7 +5891,7 @@ async fn child_browsing_context_pending_navigation_keeps_committed_window_docume page.serialize_html_async() .await .unwrap() - .contains("data-current-entry-pending-microtask=\"about:blank\""), + .contains("data-current-entry-pending-microtask=\"null\""), "{}", page.serialize_html_async().await.unwrap() ); diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs index f77f44636f8..9b06c32dc40 100644 --- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs +++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs @@ -25,8 +25,7 @@ use super::navigation_events::{ }; use super::navigation_lifecycle::finish_navigation_error_events; use super::navigation_mutation::{ - apply_navigation_navigate_same_document, sync_local_document_front_from_window, - update_navigation_current_entry_for_same_document, + apply_navigation_navigate_same_document, update_navigation_current_entry_for_same_document, }; use super::navigation_reload::{NavigationReloadAdmission, navigation_reload_admission}; use super::navigation_result::{ @@ -39,11 +38,10 @@ use super::navigation_serialize::{ apply_current_document_referrer_policy_to_entry_snapshots, serialize_history_entries, }; use super::navigation_window::{ - child_browsing_context_handle_for_runtime_owner, navigation_document_can_update_current_entry, - navigation_document_has_opaque_origin, navigation_document_is_active, - navigation_unload_event_active, runtime_window_is_global, runtime_window_owner, - runtime_window_uses_top_level_history_model, url_is_about_blank_document, - window_history_for_holder, window_location_for_holder, + child_browsing_context_handle_for_runtime_owner, navigation_document_has_disabled_entries, + navigation_document_is_active, navigation_unload_event_active, runtime_window_is_global, + runtime_window_owner, runtime_window_uses_top_level_history_model, window_history_for_holder, + window_location_for_holder, }; use super::*; use crate::native_bridge::{LocationNavigationSource, NavigationHistoryEntrySeed}; @@ -554,31 +552,8 @@ fn navigate_location_object_with_options<'s>( unsafe { &*host_ptr }.current_lightweight_popup_document_owner(popup_id) }) }); - let opaque_origin = navigation_document_has_opaque_origin(scope, owner); - if !opaque_origin - && !runtime_window_is_global(scope, owner) - && current_url - .as_ref() - .is_some_and(url_is_about_blank_document) - && !navigation_document_can_update_current_entry(scope, owner) - { - sync_location_object(scope, location, resolved.as_str()); - sync_local_document_front_from_window(scope, owner); - if let Some(popup_id) = popup_id - && let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) - { - unsafe { &mut *host_ptr } - .dispatch_lightweight_popup_same_document_navigation_events( - scope, - popup_id, - owner, - ¤t_href, - resolved.as_str(), - ); - } - return; - } - let navigation = if opaque_origin { + let entries_disabled = navigation_document_has_disabled_entries(scope, owner); + let navigation = if entries_disabled { None } else { super::navigation_window::window_navigation_for_holder(scope, owner) @@ -643,8 +618,11 @@ fn navigate_location_object_with_options<'s>( { return; } + // Detect a close/removal from navigate handlers. Disabled entries have + // no such callback, and a popup's retained Document can still accept a + // fragment change before its queued close task retires the Document. if !location_has_relevant_document(scope, location) - || !navigation_document_is_active(scope, owner) + || (navigate_outcome.is_some() && !navigation_document_is_active(scope, owner)) { return; } @@ -738,7 +716,7 @@ fn navigate_location_object_with_options<'s>( .is_some_and(|outcome| outcome.intercepted); let protocol_navigation_type = if intercepted { "other" } else { "fragment" }; sync_location_object(scope, location, resolved.as_str()); - if opaque_origin { + if entries_disabled { apply_navigation_navigate_same_document( scope, owner, diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap/navigation_bootstrap.rs index 9758e6f2b28..6a177125a03 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_bootstrap.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_bootstrap.rs @@ -18,7 +18,9 @@ use super::navigation_surface::{ build_history_runtime_state, build_navigation_runtime_state, install_history_scroll_restoration_runtime_state, install_history_state_runtime_state, }; -use super::navigation_window::set_runtime_window_owner; +use super::navigation_window::{ + bind_navigation_document, navigation_has_current_document, set_runtime_window_owner, +}; use super::*; use crate::context_bootstrap::navigation_entry::wrappers as entry_wrappers; use crate::util::{get_private_value, set_private_value}; @@ -65,6 +67,7 @@ pub(crate) fn install_window_location_history_navigation_runtime_state<'s>( let navigation = build_navigation_runtime_state(scope, window, &initial_seed)?; set_runtime_window_owner(scope, navigation, owner); + bind_navigation_document(scope, navigation, owner); set_private_value(scope, window, WINDOW_NAVIGATION_SLOT, navigation.into()); if crate::native_bridge::lightweight_popup_id_from_window(scope, window).is_some() { super::session_history::initialize(scope, window, &initial_seed); @@ -130,10 +133,11 @@ pub(crate) fn reset_window_location_history_navigation_runtime_state<'s>( set_private_value(scope, window, WINDOW_HISTORY_SLOT, history.into()); let navigation = match window_runtime_object(scope, window, WINDOW_NAVIGATION_SLOT) { - Some(navigation) => navigation, - None => build_navigation_runtime_state(scope, window, &initial_seed)?, + Some(navigation) if navigation_has_current_document(scope, navigation) => navigation, + Some(_) | None => build_navigation_runtime_state(scope, window, &initial_seed)?, }; set_runtime_window_owner(scope, navigation, window); + bind_navigation_document(scope, navigation, window); set_navigation_current_entry(scope, navigation, current_entry); clear_active_cross_document_navigation_if_matches(scope, navigation, href); install_navigation_activation_runtime_state( diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_callbacks/navigation.rs b/moli-renderer-v8/src/context_bootstrap/navigation_callbacks/navigation.rs index 4a818b5a5df..8babfef5598 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_callbacks/navigation.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_callbacks/navigation.rs @@ -19,8 +19,9 @@ use super::super::navigation_lifecycle::{ settle_navigation_finished_resolved_immediately, settle_navigation_transition_finished_local, }; use super::super::navigation_window::{ - navigation_document_can_update_current_entry, navigation_document_has_disabled_entries, - navigation_document_is_active, navigation_unload_event_active, + navigation_can_update_current_entry, navigation_document_is_active, + navigation_document_is_initial_empty, navigation_has_current_document, + navigation_has_disabled_entries, navigation_unload_event_active, }; use super::*; use crate::util::{get_private_value, set_private_value}; @@ -39,7 +40,7 @@ pub(in crate::context_bootstrap) fn navigation_entries_callback<'s>( rv.set(v8::Array::new(scope, 0).into()); return; } - if navigation_document_has_disabled_entries(scope, owner) { + if navigation_has_disabled_entries(scope, navigation) { rv.set(v8::Array::new(scope, 0).into()); return; } @@ -111,9 +112,8 @@ pub(in crate::context_bootstrap) fn navigation_navigate_callback<'s>( return; }; let current_href = location_href_slot(scope, location).unwrap_or_default(); - let raw_url = parsed.url; let Some(next_url) = - resolve_navigation_navigate_target(scope, owner, ¤t_href, raw_url.clone()) + resolve_navigation_navigate_target(scope, owner, ¤t_href, parsed.url) else { rv.set( navigation_rejected_dom_exception_result(scope, "Invalid URL", "SyntaxError").into(), @@ -124,6 +124,19 @@ pub(in crate::context_bootstrap) fn navigation_navigate_callback<'s>( let Some(navigate_history_kind) = parse_navigation_navigate_history_kind(scope, options) else { return; }; + if navigation_document_is_initial_empty(scope, owner) + && matches!(navigate_history_kind, NavigationNavigateHistoryKind::Push) + { + rv.set( + navigation_rejected_dom_exception_result( + scope, + "Cannot push a navigation in the initial about:blank document", + "NotSupportedError", + ) + .into(), + ); + return; + } let cloned_navigation_state = match clone_navigation_state_arg_for_result(scope, options) { // navigate() serializes an omitted state as undefined. Keep it distinct // from navigation sources which use the default null destination state. @@ -133,7 +146,9 @@ pub(in crate::context_bootstrap) fn navigation_navigate_callback<'s>( return; } }; - if !navigation_document_is_active(scope, owner) { + if !navigation_has_current_document(scope, args.this()) + || !navigation_document_is_active(scope, owner) + { rv.set( navigation_rejected_dom_exception_result( scope, @@ -187,30 +202,6 @@ pub(in crate::context_bootstrap) fn navigation_navigate_callback<'s>( ); return; } - if current_href == "about:blank" - && raw_url.starts_with('#') - && matches!(navigate_history_kind, NavigationNavigateHistoryKind::Push) - { - rv.set( - navigation_rejected_dom_exception_result( - scope, - "Cannot push a same-document navigation in the initial about:blank document", - "NotSupportedError", - ) - .into(), - ); - return; - } - if current_href == "about:blank" - && raw_url.starts_with('#') - && matches!( - navigate_history_kind, - NavigationNavigateHistoryKind::Default - ) - { - rv.set(navigation_pending_result(scope).into()); - return; - } let navigation_info = options.and_then(|options| { options .get(scope, v8str(scope, "info").into()) @@ -229,11 +220,23 @@ pub(in crate::context_bootstrap) fn navigation_navigate_callback<'s>( return; }; let current_url = url::Url::parse(¤t_href).ok(); - let can_update_current_entry = navigation_document_can_update_current_entry(scope, owner); + let can_update_current_entry = navigation_can_update_current_entry(scope, args.this()); // History behavior chooses which entry changes, not whether the navigation // replaces the Document. Even an empty fragment is a fragment navigation. let fragment_navigation = next_url.fragment().is_some() && is_same_document_fragment_navigation(current_url.as_ref(), &next_url); + if !can_update_current_entry && fragment_navigation { + // Disabled Navigation entries suppress API tracking, not the underlying + // fragment navigation or its legacy History/Location behavior. + super::super::location_navigation::navigate_location_object( + scope, + location, + same_document_kind, + Some(next_url.to_string()), + ); + rv.set(navigation_pending_result(scope).into()); + return; + } if can_update_current_entry && fragment_navigation { let navigation_for_event = window_navigation_for_holder(scope, owner); let canceled_cross_document = if let Some(navigation) = navigation_for_event { @@ -1812,7 +1815,7 @@ pub(in crate::context_bootstrap) fn navigation_update_current_entry_callback<'s> let Some(navigation) = window_navigation_for_holder(scope, owner) else { return; }; - if !navigation_document_can_update_current_entry(scope, owner) { + if !navigation_can_update_current_entry(scope, args.this()) { throw_dom_exception( scope, "InvalidStateError", diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs b/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs index d3dd2c8d84a..ed21db496c4 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs @@ -15,8 +15,8 @@ use super::navigation_serialize::{ serialize_navigation_entry_object, }; use super::navigation_window::{ - child_browsing_context_handle_for_runtime_owner, runtime_window_is_global, - window_location_for_holder, + child_browsing_context_handle_for_runtime_owner, navigation_document_is_initial_empty, + runtime_window_is_global, window_location_for_holder, }; use super::*; use moli_page_types::{NavigationHistoryMutation, cross_document_navigation_seed}; @@ -49,16 +49,12 @@ pub(super) fn handle_navigation_navigate_cross_document<'s>( ); let current_href = window_location_for_holder(scope, owner) .and_then(|location| location_href_slot(scope, location)); - let current_entry_is_about_blank = entries - .iter() - .find(|entry| entry.history_index == current_index) - .is_some_and(|entry| entry.url == "about:blank"); let mutation = match navigate_history_kind { NavigationNavigateHistoryKind::Push => NavigationHistoryMutation::Push, NavigationNavigateHistoryKind::Replace => NavigationHistoryMutation::Replace, NavigationNavigateHistoryKind::Default => { - if current_href.as_deref() == Some(next_url.as_str()) - || (current_href.as_deref() == Some("about:blank") && current_entry_is_about_blank) + if navigation_document_is_initial_empty(scope, owner) + || current_href.as_deref() == Some(next_url.as_str()) { NavigationHistoryMutation::Replace } else { diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_events.rs b/moli-renderer-v8/src/context_bootstrap/navigation_events.rs index fc166d8334e..1a2795953a7 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_events.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_events.rs @@ -24,9 +24,9 @@ use super::navigation_lifecycle::{ use super::navigation_result::navigation_dom_exception; use super::navigation_window::{ child_browsing_context_handle_for_runtime_owner, navigation_document_is_active, - replace_navigation_unload_event_active, runtime_window_dispatch_scope, - runtime_window_is_global, runtime_window_owner, should_dispatch_hash_change, - window_location_for_holder, window_task_target_for_runtime_owner, + navigation_has_disabled_entries, replace_navigation_unload_event_active, + runtime_window_dispatch_scope, runtime_window_is_global, runtime_window_owner, + should_dispatch_hash_change, window_location_for_holder, window_task_target_for_runtime_owner, }; use super::*; use crate::context_bootstrap::navigation_entry::wrappers as entry_wrappers; @@ -663,6 +663,9 @@ pub(super) fn dispatch_navigation_currententrychange<'s>( from: Option>, navigation_type: Option<&str>, ) { + if navigation_has_disabled_entries(scope, navigation) { + return; + } let context = navigation .get_creation_context(scope) .unwrap_or_else(|| scope.get_current_context()); @@ -775,6 +778,9 @@ pub(super) fn dispatch_navigation_navigate_event_with_form_data_and_outcome<'s>( form_data: Option>, source_element: Option>, ) -> NavigationDispatchOutcome<'s> { + if navigation_has_disabled_entries(scope, navigation) { + return NavigationDispatchOutcome::proceed(); + } let context = navigation .get_creation_context(scope) .unwrap_or_else(|| scope.get_current_context()); diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs b/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs index df4e56d19fb..3120db4a187 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs @@ -32,9 +32,7 @@ mod document_front; mod local; mod same_document; -pub(super) use document_front::{ - sync_local_document_front_from_window, sync_same_document_navigation_commit, -}; +pub(super) use document_front::sync_same_document_navigation_commit; pub(crate) use local::apply_local_window_location_navigation; pub(super) use same_document::{ apply_navigation_navigate_same_document, update_navigation_current_entry_for_same_document, diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs b/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs index 462af9bec15..9b818e73ef6 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs @@ -9,7 +9,9 @@ use super::navigation_result::clear_active_cross_document_navigation_if_matches; use super::navigation_seed::{ build_current_navigation_entry_from_seed, build_history_entries_from_seed, }; -use super::navigation_window::{window_history_for_holder, window_navigation_for_holder}; +use super::navigation_window::{ + navigation_has_current_document, window_history_for_holder, window_navigation_for_holder, +}; use crate::context_bootstrap::navigation_entry::wrappers as entry_wrappers; use crate::native_bridge::NavigationHistoryEntrySeed; @@ -33,6 +35,7 @@ pub(crate) fn capture_inherited_history_for_browser_commit( navigation_index, url, position, + super::navigation_window::navigation_source_document(scope, owner), )?; super::session_history::capture_for_navigation(scope, owner, &mut seed); Some(seed) @@ -44,6 +47,7 @@ pub(crate) fn inherited_history_seed_for_browser_commit( navigation_index: u32, url: &url::Url, position: moli_session_history::SessionHistoryPosition, + source_document: moli_page_types::NavigationSourceDocument, ) -> Option { if !entries.iter().any(|entry| { entry @@ -60,9 +64,15 @@ pub(crate) fn inherited_history_seed_for_browser_commit( .any(|entry| entry.history_index == target && entry.url == url.as_str()) { if current == target { - moli_page_types::reload_navigation_seed(entries, current)? + moli_page_types::reload_navigation_seed(entries, current, source_document)? } else { - moli_page_types::traversal_navigation_seed_candidate(entries, current, target)?.seed + moli_page_types::traversal_navigation_seed_candidate( + entries, + current, + target, + source_document, + )? + .seed } } else { let mutation = if target == current && position.length() == entries.len() { @@ -80,6 +90,7 @@ pub(crate) fn inherited_history_seed_for_browser_commit( navigation_index, url, mutation, + source_document, ) }; Some(seed) @@ -128,6 +139,28 @@ pub(crate) fn install_navigation_entry_view_for_holder<'s>( if super::history_runtime::state::window_has_shared_history(scope, owner) { return; } + if let Some(navigation) = window_navigation_for_holder(scope, owner) + && !navigation_has_current_document(scope, navigation) + { + let Some(current) = entry_seed + .entries + .iter() + .find(|entry| entry.history_index == entry_seed.current_index) + else { + return; + }; + // A javascript: navigation can replace the Document without changing + // its URL. Do not install the new seed into the old Navigation object. + if super::navigation_bootstrap::reset_window_location_history_navigation_runtime_state( + scope, + owner, + ¤t.url, + ) + .is_err() + { + return; + } + } let Some(history) = window_history_for_holder(scope, owner) else { return; }; diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_result.rs b/moli-renderer-v8/src/context_bootstrap/navigation_result.rs index 99fa5cef571..5b8faf11e71 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_result.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_result.rs @@ -10,6 +10,7 @@ use super::navigation_lifecycle::{ pub(super) use super::navigation_lifecycle::{ perform_navigation_scroll_if_needed, reset_navigation_focus_if_unchanged, }; +use super::navigation_window::navigation_has_disabled_entries; use super::*; use crate::context_bootstrap::navigation_entry::wrappers as entry_wrappers; use crate::util::{get_private_value, set_private_value}; @@ -256,6 +257,9 @@ pub(super) fn queue_same_document_navigation_success<'s>( signal: Option>, fallback_scroll_href: Option<&str>, ) { + if navigation_has_disabled_entries(scope, navigation) { + return; + } queue_same_document_navigation_finished( scope, navigation, diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_surface.rs b/moli-renderer-v8/src/context_bootstrap/navigation_surface.rs index 3bbcfa32b3b..98732853087 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_surface.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_surface.rs @@ -15,8 +15,9 @@ use super::navigation_traversal::{ navigation_traverse_to_callback, pending_or_current_navigation_entry_index, }; use super::navigation_window::{ - child_browsing_context_handle_for_runtime_owner, navigation_document_has_disabled_entries, - navigation_document_is_active, runtime_window_owner, window_history_for_holder, + child_browsing_context_handle_for_runtime_owner, history_owner_if_fully_active, + navigation_document_is_active, navigation_has_disabled_entries, runtime_window_owner, + window_history_for_holder, }; use super::*; use anyhow::Result; diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_surface/accessors.rs b/moli-renderer-v8/src/context_bootstrap/navigation_surface/accessors.rs index 0610fe43648..b8c31919050 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_surface/accessors.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_surface/accessors.rs @@ -99,7 +99,7 @@ fn navigation_can_go_back_getter_function<'s>( rv.set_bool(false); return; } - if navigation_document_has_disabled_entries(scope, owner) { + if navigation_has_disabled_entries(scope, args.this()) { rv.set_bool(false); return; } @@ -122,7 +122,7 @@ fn navigation_can_go_forward_getter_function<'s>( rv.set_bool(false); return; } - if navigation_document_has_disabled_entries(scope, owner) { + if navigation_has_disabled_entries(scope, args.this()) { rv.set_bool(false); return; } @@ -181,7 +181,9 @@ fn navigation_current_entry_getter_function<'s>( rv.set(v8::null(scope).into()); return; } - if navigation_document_has_disabled_entries(scope, owner) { + if !navigation_error_event_active(scope, args.this()) + && navigation_has_disabled_entries(scope, args.this()) + { rv.set(v8::null(scope).into()); return; } diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal.rs index 9081fc3e152..a3dbf3c7b72 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal.rs @@ -37,9 +37,9 @@ use super::navigation_traversal_plan::{ navigation_delta_traversal_plan, navigation_index_traversal_plan, }; use super::navigation_window::{ - navigation_document_can_update_current_entry, navigation_document_is_active, - navigation_unload_event_active, runtime_window_owner, window_history_for_holder, - window_location_for_holder, window_navigation_for_holder, + navigation_can_update_current_entry, navigation_document_is_active, + navigation_has_current_document, navigation_unload_event_active, runtime_window_owner, + window_history_for_holder, window_location_for_holder, window_navigation_for_holder, }; use super::*; use crate::webidl; @@ -234,7 +234,9 @@ pub(super) fn navigation_reload_callback<'s>( return; } }; - if !navigation_document_is_active(scope, owner) { + if !navigation_has_current_document(scope, args.this()) + || !navigation_document_is_active(scope, owner) + { rv.set( navigation_rejected_invalid_state_result( scope, @@ -270,7 +272,7 @@ pub(super) fn navigation_reload_callback<'s>( ); return; } - if navigation_document_can_update_current_entry(scope, owner) + if navigation_can_update_current_entry(scope, args.this()) && let Some(navigation) = window_navigation_for_holder(scope, owner) { if crate::native_bridge::lightweight_popup_id_from_window(scope, owner).is_some() { diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs index 803f5ba511d..6f8ed69dde3 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs @@ -7,7 +7,7 @@ use super::navigation_projection::{ }; use super::navigation_traversal_execution::TraversalTarget; use super::navigation_window::{ - navigation_document_has_opaque_origin, navigation_document_is_active, runtime_window_owner, + navigation_document_is_active, navigation_has_disabled_entries, runtime_window_owner, window_history_for_holder, }; @@ -72,9 +72,9 @@ pub(super) fn navigation_delta_traversal_plan<'s>( "Cannot traverse a non-fully-active document", )); } - if navigation_document_has_opaque_origin(scope, owner) { + if navigation_has_disabled_entries(scope, navigation) { return Some(NavigationTraversalPlan::RejectInvalidState( - "Cannot traverse an opaque origin document", + "Cannot traverse a document with disabled navigation entries", )); } let history = window_history_for_holder(scope, owner)?; @@ -130,9 +130,9 @@ pub(super) fn navigation_index_traversal_plan<'s>( "Cannot traverse a non-fully-active document", )); } - if navigation_document_has_opaque_origin(scope, owner) { + if navigation_has_disabled_entries(scope, navigation) { return Some(NavigationTraversalPlan::RejectInvalidState( - "Cannot traverse an opaque origin document", + "Cannot traverse a document with disabled navigation entries", )); } let history = window_history_for_holder(scope, owner)?; diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_window.rs b/moli-renderer-v8/src/context_bootstrap/navigation_window.rs index c33919412d6..ae1cea6d46f 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_window.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_window.rs @@ -3,6 +3,49 @@ use super::*; use crate::util::{get_private_value, set_private_value}; const WINDOW_UNLOAD_EVENT_ACTIVE_SLOT: &str = "__lmWindowUnloadEventActive"; +const NAVIGATION_DOCUMENT_SLOT: &str = "__lmNavigationDocument"; + +pub(super) fn bind_navigation_document<'s>( + scope: &mut v8::PinScope<'s, '_>, + navigation: v8::Local<'s, v8::Object>, + owner: v8::Local<'s, v8::Object>, +) { + if let Some(document) = navigation_owner_document(scope, owner) { + let value = v8::BigInt::new_from_u64(scope, document.index() as u64); + set_private_value(scope, navigation, NAVIGATION_DOCUMENT_SLOT, value.into()); + } +} + +fn navigation_owner_document<'s>( + scope: &mut v8::PinScope<'s, '_>, + owner: v8::Local<'s, v8::Object>, +) -> Option { + let host_ptr = context_host_ptr_from_global_bridge(scope)?; + super::window_accessors::window_document_handle(scope, owner, unsafe { &*host_ptr }) +} + +pub(super) fn navigation_has_current_document<'s>( + scope: &mut v8::PinScope<'s, '_>, + navigation: v8::Local<'s, v8::Object>, +) -> bool { + let Some(document) = get_private_value(scope, navigation, NAVIGATION_DOCUMENT_SLOT) + .and_then(|value| dom_handle_from_marker_value(scope, value)) + else { + // Bootstrap may create the surface before registering the child realm. + return true; + }; + let owner = runtime_window_owner(scope, navigation); + navigation_owner_document(scope, owner) == Some(document) +} + +pub(super) fn navigation_has_disabled_entries<'s>( + scope: &mut v8::PinScope<'s, '_>, + navigation: v8::Local<'s, v8::Object>, +) -> bool { + let owner = runtime_window_owner(scope, navigation); + !navigation_has_current_document(scope, navigation) + || navigation_document_has_disabled_entries(scope, owner) +} pub(super) fn runtime_window_owner<'s>( scope: &mut v8::PinScope<'s, '_>, @@ -148,15 +191,6 @@ pub(super) fn window_task_target_for_runtime_owner<'s>( )) } -pub(super) fn navigation_document_is_initial_empty<'s>( - scope: &mut v8::PinScope<'s, '_>, - owner: v8::Local<'s, v8::Object>, -) -> bool { - runtime_window_is_global(scope, owner) - && context_host_ptr_from_global_bridge(scope) - .is_some_and(|host| unsafe { &*host }.main_document_is_initial_empty()) -} - pub(super) fn navigation_document_is_active<'s>( scope: &mut v8::PinScope<'s, '_>, owner: v8::Local<'s, v8::Object>, @@ -164,30 +198,32 @@ pub(super) fn navigation_document_is_active<'s>( navigation_document_is_live(scope, owner) } -pub(super) fn navigation_document_can_update_current_entry<'s>( +pub(super) fn navigation_can_update_current_entry<'s>( + scope: &mut v8::PinScope<'s, '_>, + navigation: v8::Local<'s, v8::Object>, +) -> bool { + let owner = runtime_window_owner(scope, navigation); + navigation_document_is_live(scope, owner) && !navigation_has_disabled_entries(scope, navigation) +} + +pub(super) fn navigation_document_is_initial_empty<'s>( scope: &mut v8::PinScope<'s, '_>, owner: v8::Local<'s, v8::Object>, ) -> bool { - if !navigation_document_is_live(scope, owner) { - return false; - } - if let Some(popup_id) = crate::native_bridge::lightweight_popup_id_from_window(scope, owner) { - return context_host_ptr_from_global_bridge(scope).is_some_and(|host_ptr| { - !unsafe { &*host_ptr }.lightweight_popup_current_document_is_initial_empty(popup_id) - }); - } - if runtime_window_is_global(scope, owner) { - return !navigation_document_is_initial_empty(scope, owner); - } - let Some(handle) = child_browsing_context_handle_for_runtime_owner(scope, owner) else { - return false; - }; let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else { return false; }; - unsafe { &*host_ptr } - .child_browsing_context_current_url(handle) - .is_some_and(|url| !url_is_about_blank_document(&url)) + let host = unsafe { &*host_ptr }; + if let Some(popup_id) = crate::native_bridge::lightweight_popup_id_from_window(scope, owner) { + return host.lightweight_popup_current_document_is_initial_empty(popup_id); + } + if runtime_window_is_global(scope, owner) { + return host.main_document_is_initial_empty(); + } + // Initialness belongs to the Document, not its URL: document.open() can + // change that URL, and a later navigation can create a non-initial blank. + child_browsing_context_handle_for_runtime_owner(scope, owner) + .is_some_and(|handle| host.child_current_document_is_initial_empty(handle)) } pub(super) fn navigation_document_has_disabled_entries<'s>( @@ -196,14 +232,6 @@ pub(super) fn navigation_document_has_disabled_entries<'s>( ) -> bool { navigation_document_has_opaque_origin(scope, owner) || navigation_document_is_initial_empty(scope, owner) - || crate::native_bridge::lightweight_popup_id_from_window(scope, owner).is_some_and( - |popup_id| { - context_host_ptr_from_global_bridge(scope).is_some_and(|host_ptr| { - unsafe { &*host_ptr } - .lightweight_popup_current_document_is_initial_empty(popup_id) - }) - }, - ) } pub(super) fn navigation_document_has_opaque_origin<'s>( diff --git a/moli-renderer-v8/src/frame_owner_model/records.rs b/moli-renderer-v8/src/frame_owner_model/records.rs index 56e41fbdb6c..a7dcfd5f46a 100644 --- a/moli-renderer-v8/src/frame_owner_model/records.rs +++ b/moli-renderer-v8/src/frame_owner_model/records.rs @@ -285,12 +285,18 @@ pub(crate) enum DocumentCreationKind { Navigation, Srcdoc, JavascriptUrl, - DocumentOpen, + DocumentOpen { is_initial_empty: bool }, } impl DocumentCreationKind { pub(crate) fn is_initial_empty(self) -> bool { - matches!(self, Self::InitialEmpty) + matches!( + self, + Self::InitialEmpty + | Self::DocumentOpen { + is_initial_empty: true + } + ) } } diff --git a/moli-renderer-v8/src/frame_owner_model/store.rs b/moli-renderer-v8/src/frame_owner_model/store.rs index a58f5452e36..ed41ba0c205 100644 --- a/moli-renderer-v8/src/frame_owner_model/store.rs +++ b/moli-renderer-v8/src/frame_owner_model/store.rs @@ -340,7 +340,13 @@ impl FrameOwnerStore { document_handle, url, base_url, - DocumentCreationKind::DocumentOpen, + DocumentCreationKind::DocumentOpen { + is_initial_empty: self + .documents + .get(&transition.retired_owner().document_id)? + .creation_kind + .is_initial_empty(), + }, )?; self.pending_main_document_owner_transitions .push_back(transition); @@ -426,15 +432,16 @@ impl FrameOwnerStore { retired_document.active_requests.clear(); let document_id = transition.current_owner().document_id; - let lifecycle_progress = if creation_kind == DocumentCreationKind::DocumentOpen { - self.new_loading_document_lifecycle_for_document_open( - DocumentLoadDeliveryKind::Main, - load_continuation, - completely_loaded, - ) - } else { - self.new_loading_document_lifecycle(DocumentLoadDeliveryKind::Main) - }; + let lifecycle_progress = + if matches!(creation_kind, DocumentCreationKind::DocumentOpen { .. }) { + self.new_loading_document_lifecycle_for_document_open( + DocumentLoadDeliveryKind::Main, + load_continuation, + completely_loaded, + ) + } else { + self.new_loading_document_lifecycle(DocumentLoadDeliveryKind::Main) + }; self.documents.insert( document_id, DocumentRecord { @@ -986,6 +993,7 @@ impl FrameOwnerStore { .lifecycle_progress .document_open_load_continuation(); let completely_loaded = retired_document.lifecycle_progress.is_completely_loaded(); + let is_initial_empty = retired_document.creation_kind.is_initial_empty(); retired_document.lifecycle = DocumentLifecycleState::Replaced; retired_document.lifecycle_progress.retire(); retired_document.active_requests.clear(); @@ -1004,7 +1012,7 @@ impl FrameOwnerStore { document_handle, url, base_url: base_url.clone(), - creation_kind: DocumentCreationKind::DocumentOpen, + creation_kind: DocumentCreationKind::DocumentOpen { is_initial_empty }, lifecycle: DocumentLifecycleState::Current, lifecycle_progress, active_requests: BTreeMap::new(), diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_loads.rs b/moli-renderer-v8/src/native_bridge/context_host/host_loads.rs index db432aaf13a..71224a4837b 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/host_loads.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/host_loads.rs @@ -602,7 +602,7 @@ impl PageScriptHost { let document_open_replacement = matches!( self.frame_owner_store .current_child_document_creation_kind(finish.child_handle), - Some(crate::frame_owner_model::DocumentCreationKind::DocumentOpen) + Some(crate::frame_owner_model::DocumentCreationKind::DocumentOpen { .. }) ); let parent_frame_id = self.protocol_child_frame_parent_id( finish 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 8a35180a03f..f144b551ba7 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 @@ -986,6 +986,133 @@ async fn named_element_navigation_prefers_its_source_frame_over_duplicate_names( } } +#[tokio::test] +async fn navigation_entries_and_events_follow_document_initialness() { + for mode in [ + "initial", + "initial-fragment", + "initial-history", + "initial-open", + "srcdoc", + "javascript", + "loaded-blank", + "loaded-blank-fragment", + "initial-navigate-fragment", + "initial-navigate-relative", + "initial-navigate-cross", + "initial-navigate-push", + "initial-anchor", + "loaded-blank-navigate-fragment", + "javascript-navigate-fragment", + ] { + let crossed = matches!( + mode, + "initial-navigate-relative" | "initial-navigate-cross" | "initial-anchor" + ); + let requested = if mode == "initial-navigate-relative" { + vec!["/parent"] + } else if crossed || mode.starts_with("loaded-") { + vec!["/child"] + } else { + vec![] + }; + let server = StaticHttpServer::spawn(requested.len()).await; + let parent_url = server.base_url().join("parent").unwrap(); + let loader = static_http_loader([]); + let mut vm = + new_storage_page_task_executor_test_vm_with_loader(parent_url.as_str(), &loader); + let script = include_str!("../../../../tests/fixtures/navigation-initial-document.js"); + vm.eval(&format!( + "{script}\nglobalThis.initialResult = 'pending';\n\ + initialNavigationProbe({mode:?}).then(\n\ + value => initialResult = value, error => initialResult = String(error));" + )) + .unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(initialResult !== 'pending')", + "true", + mode, + ) + .await; + let result: serde_json::Value = + serde_json::from_str(&vm.eval("JSON.stringify(initialResult)").unwrap()).unwrap(); + let initial = mode.starts_with("initial"); + let before_url = if mode == "srcdoc" { + "about:srcdoc" + } else { + "about:blank" + }; + let after_url = if mode.ends_with("-fragment") { + "about:blank#fragment" + } else { + match mode { + "initial-history" => "about:blank#second", + "initial-open" => "/parent", + "initial-navigate-relative" => "/parent#relative", + "initial-navigate-cross" | "initial-anchor" => "/child", + _ => before_url, + } + }; + let snapshot = |url: &str, entries: Vec<&str>, current: Option<&str>, back: bool| { + serde_json::json!({ + "url": url, "entries": entries, "current": current, "activation": initial, + "transition": true, "back": back, "forward": false, + }) + }; + let before = snapshot( + before_url, + if initial { vec![] } else { vec![before_url] }, + (!initial).then_some(before_url), + false, + ); + let fragment = !initial && mode.ends_with("-fragment"); + let after = snapshot( + after_url, + if initial { + vec![] + } else if fragment { + vec![before_url, after_url] + } else { + vec![after_url] + }, + (!initial).then_some(after_url), + fragment, + ); + let events = if initial { + vec![] + } else if fragment { + vec![ + "navigate", + "currententrychange", + "navigatesuccess", + "currententrychange", + ] + } else { + vec!["currententrychange"] + }; + let promises = if mode == "initial-navigate-push" { + vec!["committed:NotSupportedError", "finished:NotSupportedError"] + } else if fragment && mode.contains("-navigate-") { + vec!["committed:fulfilled", "finished:fulfilled"] + } else { + vec![] + }; + assert_eq!( + result, + serde_json::json!({ + "before": before, "after": after, + "update": if initial { serde_json::json!("InvalidStateError") } else { serde_json::json!(3) }, + "events": events, "promises": promises, "sameNavigation": !crossed, + "newCurrent": if crossed || !initial { Some(after_url) } else { None }, + }), + "{mode}" + ); + assert_eq!(server.finish_targets().await, requested, "{mode}"); + } +} + #[test] fn form_action_scheme_selects_query_mutation_or_post_resource() { for action in [ diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_traversal.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_traversal.rs index f0d6f395c90..af08db5596e 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_traversal.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_traversal.rs @@ -320,10 +320,10 @@ async fn reset_navigation_history_updates_prebootstrapped_child_default_realm() assert_eq!( vm.eval_in_child_default_context( child_context_id, - "JSON.stringify({ historyLength: history.length, navigationLength: navigation.entries().length, currentIndex: navigation.currentEntry.index })", + "JSON.stringify({ historyLength: history.length, navigationLength: navigation.entries().length, currentEntryNull: navigation.currentEntry === null })", ) .expect("materialized child reset history state should evaluate"), - r#"{"historyLength":1,"navigationLength":1,"currentIndex":0}"# + r#"{"historyLength":1,"navigationLength":0,"currentEntryNull":true}"# ); } diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs index 47892595558..216ab86566f 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs @@ -121,11 +121,19 @@ document.getElementById('frame').srcdoc = fn detached_iframe_navigation_entry_properties_are_invalidated() { let mut vm = new_storage_test_vm("https://detached-navigation-entry.test/page.html"); + vm.eval( + r#" +const frame = document.createElement('iframe'); +frame.srcdoc = '

committed child

'; +(document.body || document.documentElement || document).appendChild(frame); +"#, + ) + .expect("committed child setup should evaluate"); + vm.drain_pending_child_frame_work_for_test(); + let result = vm .eval( r#" -const frame = document.createElement('iframe'); -(document.body || document.documentElement || document).appendChild(frame); const entry = frame.contentWindow.navigation.currentEntry; const before = [ entry.sameDocument, @@ -316,13 +324,21 @@ fn navigation_runtime_state_ignores_proto_pollution_slots() { fn targeted_anchor_click_dispatches_navigate_on_named_child_window() { let mut vm = new_storage_html_test_vm("https://targeted-child-navigate.test/page.html"); - let result = vm - .eval( - r#" + vm.eval( + r#" const frame = document.createElement('iframe'); +frame.srcdoc = '

committed child

'; frame.name = 'target'; const root = document.body || document.documentElement || document; root.appendChild(frame); +"#, + ) + .expect("committed child setup should evaluate"); + vm.drain_pending_child_frame_work_for_test(); + + let result = vm + .eval( + r#" const link = document.createElement('a'); link.href = '/next.html'; link.target = 'target'; @@ -350,7 +366,7 @@ seen.join('|') ) .expect("targeted anchor click should dispatch child navigate"); - // The child inherits the parent's origin, but its about:blank URL cannot + // The child inherits the parent's origin, but its about:srcdoc URL cannot // be rewritten to an HTTPS URL, so this navigation cannot be intercepted. assert_eq!( result, @@ -724,7 +740,7 @@ const frame = document.querySelector('iframe'); return [ frame.contentWindow.location.href, frame.contentWindow.navigation.entries().length, - frame.contentWindow.navigation.currentEntry.url, + frame.contentWindow.navigation.currentEntry === null, frame.contentWindow.navigation.activation === null ].join('|') })() @@ -732,7 +748,7 @@ return [ ) .expect("no-src iframe navigation activation should evaluate"); - assert_eq!(result, "about:blank|1|about:blank|true"); + assert_eq!(result, "about:blank|0|true|true"); } #[test] fn no_src_iframe_initial_about_blank_load_is_synchronous_at_connection() { @@ -2579,14 +2595,14 @@ child.location.href = "about:blank#2"; [ child.location.href, child.navigation.entries().length, - child.navigation.currentEntry.url, + child.navigation.currentEntry === null, events.join(",") ].join("|") "##, ) .expect("initial about:blank fragment navigation should evaluate"); - assert_eq!(result, "about:blank#2|1|about:blank|"); + assert_eq!(result, "about:blank#2|0|true|"); } #[test] fn initial_about_blank_iframe_assign_replaces_history_after_same_document_update() { @@ -4217,7 +4233,7 @@ fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronous assert_eq!( result, - r##"{"initialJointLength":1,"observed":["load|about:blank|about:blank|about:blank|about:blank|true|1|1","load|about:blank#foo|about:blank#foo|about:blank#foo|about:blank#foo|true|1|1","load|about:blank?foo|about:blank?foo|about:blank?foo|about:blank?foo|true|1|1"]}"## + r##"{"initialJointLength":1,"observed":["load|about:blank|about:blank|about:blank||true|1|1","load|about:blank#foo|about:blank#foo|about:blank#foo||true|1|1","load|about:blank?foo|about:blank?foo|about:blank?foo||true|1|1"]}"## ); assert!( !vm.has_pending_child_navigation_commit_for_test(), diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs index 440187f3b11..d8272c3b170 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs @@ -954,16 +954,24 @@ fn child_webassembly_constructors_use_newtarget_child_realm_default_prototype() fn targeted_anchor_click_reports_same_document_hash_change_for_child_window() { let mut vm = new_storage_html_test_vm("https://targeted-child-hash.test/page.html"); - let result = vm - .eval( - r#" + vm.eval( + r#" const frame = document.createElement('iframe'); +frame.srcdoc = '

committed child

'; frame.name = 'target'; const root = document.body || document.documentElement || document; root.appendChild(frame); -frame.contentWindow.history.replaceState(null, '', 'about:blank#child'); +"#, + ) + .expect("committed child setup should evaluate"); + vm.drain_pending_child_frame_work_for_test(); + + let result = vm + .eval( + r#" +frame.contentWindow.history.replaceState(null, '', 'about:srcdoc#child'); const link = document.createElement('a'); -link.href = 'about:blank#next'; +link.href = 'about:srcdoc#next'; link.target = 'target'; root.appendChild(link); let seen = []; @@ -981,7 +989,7 @@ seen.join('|') ) .expect("targeted same-document anchor click should dispatch child navigate"); - assert_eq!(result, "true,true,about:blank#next,-1"); + assert_eq!(result, "true,true,about:srcdoc#next,-1"); } #[test] fn window_load_uses_original_event_after_global_constructors_are_deleted() { diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/forms.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/forms.rs index 6eb376a3b3f..20306ed2bcb 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/forms.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/forms.rs @@ -4811,10 +4811,10 @@ seen fn iframe_anchor_download_click_dispatches_child_navigate_event() { let mut vm = new_storage_test_vm("https://iframe-download-navigation.test/page.html"); - let result = vm - .eval( - r#" + vm.eval( + r#" const frame = document.createElement('iframe'); +frame.srcdoc = '

committed child

'; if (!document.documentElement) { document.appendChild(document.createElement('html')); } @@ -4822,6 +4822,14 @@ if (!document.body) { document.documentElement.appendChild(document.createElement('body')); } document.body.appendChild(frame); +"#, + ) + .expect("committed download child should evaluate"); + vm.drain_pending_child_frame_work_for_test(); + + let result = vm + .eval( + r#" let topNavigate = false; let childNavigate = false; navigation.onnavigate = () => { topNavigate = true; }; diff --git a/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs b/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs index 7370c56622f..4336fd1c834 100644 --- a/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs +++ b/moli-renderer-v8/src/script_vm/tests/extracted/child_documents.rs @@ -1369,7 +1369,7 @@ fn pending_initial_child_navigation_keeps_initial_empty_window_surface() { "#, ) .expect("pending child Window state should evaluate"), - "about:blank|about:blank|about:blank|complete", + "about:blank|null|about:blank|complete", "a planned target must not be projected as committed while the initial empty Document is still live" ); diff --git a/moli-renderer-v8/tests/fixtures/navigation-initial-document.js b/moli-renderer-v8/tests/fixtures/navigation-initial-document.js new file mode 100644 index 00000000000..9028644f4d7 --- /dev/null +++ b/moli-renderer-v8/tests/fixtures/navigation-initial-document.js @@ -0,0 +1,71 @@ +globalThis.initialNavigationProbe = async function(mode) { + const frame = document.createElement('iframe'); + frame.name = 'initial-navigation-target'; + const loaded = Promise.withResolvers(); + frame.onload = loaded.resolve; + if(mode === 'srcdoc') frame.srcdoc = '

loaded

'; + else if(mode.startsWith('javascript')) frame.src = "javascript:'

loaded

'"; + else if(mode.startsWith('loaded-')) frame.src = new URL('/child', location.href).href; + (document.body || document.documentElement || document).appendChild(frame); + await loaded.promise; + if(mode.startsWith('loaded-')) { + const blankLoaded = new Promise(resolve => frame.onload = resolve); + frame.src = 'about:blank'; + await blankLoaded; + } + // Run after load has returned, so history behavior is not affected by an + // in-progress load event. + await new Promise(resolve => setTimeout(resolve, 0)); + const child = frame.contentWindow; + const nav = child.navigation; + const events = []; + for(const type of ['navigate','currententrychange','navigatesuccess','navigateerror']) { + nav.addEventListener(type, () => events.push(type)); + } + const snapshot = () => ({ + url:child.location.href.replaceAll(location.origin, ''), + entries:nav.entries().map(entry => entry.url.replaceAll(location.origin, '')), + current:nav.currentEntry?.url.replaceAll(location.origin, '') ?? null, + activation:nav.activation === null, + transition:nav.transition === null, + back:nav.canGoBack, + forward:nav.canGoForward + }); + const before = snapshot(); + const promises = []; + if(mode.includes('-navigate-') || mode === 'initial-anchor') { + const crossDocument = mode.endsWith('-relative') || mode.endsWith('-cross') || mode === 'initial-anchor'; + const nextLoad = crossDocument ? new Promise(resolve => frame.onload = resolve) : null; + if(mode === 'initial-anchor') { + frame.name = 'initial-navigation-target'; + const link = document.createElement('a'); + link.href = new URL('/child', location.href).href; + link.target = frame.name; + (document.body || document.documentElement || document).appendChild(link); + link.click(); + } else { + const target = mode.endsWith('-relative') ? '#relative' : mode.endsWith('-cross') || mode.endsWith('-push') ? new URL('/child', location.href).href : 'about:blank#fragment'; + const result = nav.navigate(target, mode.endsWith('-push') ? {history:'push'} : {}); + for(const key of ['committed', 'finished']) result[key].then(() => promises.push(key + ':fulfilled'), error => promises.push(key + ':' + error.name)); + } + if(nextLoad) await nextLoad; + } else if(mode.endsWith('-fragment')) child.location.href = 'about:blank#fragment'; + else if(mode === 'initial-history') { + child.history.pushState({value:1}, '', 'about:blank#first'); + child.history.pushState({value:2}, '', 'about:blank#second'); + } else if(mode === 'initial-open') { + child.document.open(); + child.document.write('

written

'); + child.document.close(); + } + await new Promise(resolve => setTimeout(resolve, 20)); + const after = snapshot(); + let update; + try { + nav.updateCurrentEntry({state:{value:3}}); + update = nav.currentEntry?.getState().value; + } catch(error) { update = error.name; } + const result = {before,after,update,events,promises,sameNavigation:nav === child.navigation,newCurrent:child.navigation.currentEntry?.url.replaceAll(location.origin, "") ?? null}; + frame.remove(); + return result; +};