From bbee57cbaf91dce3ec6fff4b10ea66d9c1c7b18d Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 20 Sep 2026 15:03:38 +0800 Subject: [PATCH] fix(navigation): track intercepted traversal transitions Carry transition resolvers through pending traversal commits and settlement, reusing precommit transitions and the shared navigation lifecycle. Keep committed reactions after currententrychange and intercept handlers, and finish empty handler lists asynchronously. Preserve rejection reasons and transitions started by completion callbacks. Add default protocol regressions and four unmodified upstream WPT variants. Serve their .mjs helper through the compat fixture server. --- moli-core/tests/wpt_compat.rs | 4 + .../page/tests/same_document_history.rs | 316 ++++++++++++++++++ .../events/subclasses/data.rs | 4 +- .../history_runtime/apply.rs | 2 +- .../history_runtime/traversal.rs | 228 ++++++------- .../navigation_activation.rs | 39 ++- .../navigation_callbacks/navigation.rs | 20 +- .../context_bootstrap/navigation_lifecycle.rs | 13 +- .../navigation_traversal_coordinator.rs | 35 +- .../src/native_bridge/history_traversal.rs | 1 + moli-wpt-compat/fixtures/wpt/manifest.toml | 72 ++++ .../back-same-document-intercept-reject.html | 55 +++ .../back-same-document-intercept.html | 48 +++ .../resources/helpers.mjs | 206 ++++++++++++ moli-wpt-compat/src/server.rs | 2 +- 15 files changed, 895 insertions(+), 150 deletions(-) create mode 100644 moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html create mode 100644 moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept.html create mode 100644 moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/resources/helpers.mjs diff --git a/moli-core/tests/wpt_compat.rs b/moli-core/tests/wpt_compat.rs index 9a4b9125fc8..65f074bf90e 100644 --- a/moli-core/tests/wpt_compat.rs +++ b/moli-core/tests/wpt_compat.rs @@ -176,6 +176,10 @@ wpt_compat_cases! { wpt_compat_case_upstream_navigation_api_precommit_handler_precommithandler_reload => "upstream-navigation-api-precommit-handler-precommithandler-reload", wpt_compat_case_upstream_navigation_api_precommit_handler_precommithandler_replace => "upstream-navigation-api-precommit-handler-precommithandler-replace", wpt_compat_case_upstream_navigation_api_precommit_handler_precommithandler_traverse => "upstream-navigation-api-precommit-handler-precommithandler-traverse", + wpt_compat_case_upstream_navigation_api_ordering_and_transition_back_same_document_intercept_reject_no_currententrychange => "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-reject-no-currententrychange", + wpt_compat_case_upstream_navigation_api_ordering_and_transition_back_same_document_intercept_reject_currententrychange => "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-reject-currententrychange", + wpt_compat_case_upstream_navigation_api_ordering_and_transition_back_same_document_intercept_no_currententrychange => "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-no-currententrychange", + wpt_compat_case_upstream_navigation_api_ordering_and_transition_back_same_document_intercept_currententrychange => "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-currententrychange", wpt_compat_case_navigation_currententrychange_ignores_page_tampered_dispatch_basic => "navigation-currententrychange-ignores-page-tampered-dispatch-basic", wpt_compat_case_navigation_update_current_entry_event_surface_basic => "navigation-update-current-entry-event-surface-basic", wpt_compat_case_navigation_cross_document_currententrychange_quietness_basic => "navigation-cross-document-currententrychange-quietness-basic", diff --git a/moli-protocol/src/domains/page/tests/same_document_history.rs b/moli-protocol/src/domains/page/tests/same_document_history.rs index 2f5e4274d0e..d45b5ad5bbb 100644 --- a/moli-protocol/src/domains/page/tests/same_document_history.rs +++ b/moli-protocol/src/domains/page/tests/same_document_history.rs @@ -182,6 +182,322 @@ impl SameDocumentPage { } } +#[tokio::test(flavor = "multi_thread")] +async fn intercepted_traversal_transitions_track_commit_and_completion() { + for precommit in [false, true] { + for mode in ["empty", "sync", "async", "reject", "undefined"] { + let mut page = SameDocumentPage::new().await; + page.run( + "history.pushState(null, '', '#back'); history.pushState(null, '', '#current'); void 0", + "#current", + ).await; + page.ctx.sent.clear(); + let script = r#" + (async () => { + const mode = MODE; + const precommit = PRECOMMIT; + const order = []; + const checks = []; + const from = navigation.currentEntry; + const error = mode === 'undefined' ? undefined : new Error('traversal failure'); + const fails = mode === 'reject' || mode === 'undefined'; + let transition; + let event; + let transitionCommitted; + let transitionFinished; + const capture = phase => { + order.push(phase); + const current = navigation.transition; + checks.push(current !== null && current.from === from && + current.navigationType === 'traverse' && current.to === event.destination); + if (!transition) { + transition = current; + transitionCommitted = transition?.committed.then(value => value === undefined); + transitionFinished = transition?.finished.then( + value => { order.push('transition finished'); return !fails && value === undefined; }, + reason => { order.push('transition finished'); return fails && reason === error; } + ); + } + checks.push(current === transition); + }; + navigation.addEventListener('navigate', e => { + event = e; + order.push('navigate'); + checks.push(navigation.transition === null); + const options = {}; + if (precommit) options.precommitHandler = () => { + capture('precommit'); + checks.push(location.hash === '#current'); + return new Promise(resolve => setTimeout(resolve, 0)); + }; + if (mode !== 'empty') options.handler = () => { + capture('handler'); + checks.push(location.hash === '#back'); + if (fails) return Promise.reject(error); + if (mode === 'async') return new Promise(resolve => setTimeout(resolve, 0)); + }; + e.intercept(options); + e.signal.addEventListener('abort', () => checks.push(fails && e.signal.reason === error)); + }, {once: true}); + navigation.addEventListener('currententrychange', () => capture('currententrychange'), {once: true}); + navigation.addEventListener('navigatesuccess', () => capture('success'), {once: true}); + navigation.addEventListener('navigateerror', e => { + capture('error'); + checks.push(fails && e.error === error); + }, {once: true}); + const result = navigation.back(); + const committed = result.committed.then(entry => { + capture('committed'); + return entry === navigation.currentEntry; + }); + const finished = result.finished.then( + entry => { order.push('finished'); return !fails && entry === navigation.currentEntry && navigation.transition === null; }, + reason => { order.push('finished'); return fails && reason === error && navigation.transition === null; } + ); + checks.push(await committed, await finished); + checks.push(await transitionCommitted, await transitionFinished); + return {order, checks}; + })() + "#.replace("MODE", &json!(mode).to_string()) + .replace("PRECOMMIT", if precommit { "true" } else { "false" }); + let result = page.evaluate(&script).await; + let mut expected = vec!["navigate"]; + if precommit { + expected.push("precommit"); + } + expected.push("currententrychange"); + if mode != "empty" { + expected.push("handler"); + } + expected.extend([ + "committed", + if matches!(mode, "reject" | "undefined") { + "error" + } else { + "success" + }, + "finished", + "transition finished", + ]); + assert_eq!( + result["order"], + json!(expected), + "{mode}, precommit={precommit}" + ); + assert!( + result["checks"] + .as_array() + .unwrap() + .iter() + .all(|value| value == true), + "{mode}, precommit={precommit}: {result}" + ); + page.assert_history(&["", "#back", "#current"], 1).await; + page.assert_commits(&[("#back", "other")]); + } + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn intercepted_traversal_transitions_settle_when_canceled() { + for phase in ["precommit", "handler"] { + for action in ["stop", "navigate"] { + let mut page = SameDocumentPage::new().await; + let script = r#"(async () => { + const phase = PHASE; + const action = ACTION; + history.pushState(null, '', '#back'); + history.pushState(null, '', '#current'); + await new Promise(resolve=>setTimeout(resolve,0)); + const from = navigation.currentEntry; + let ready; + const started = new Promise(resolve => ready = resolve); + let transition, signal, release; + const observed = {committed: 'pending', finished: 'pending', transitionCommitted: 'pending', transitionFinished: 'pending'}; + navigation.addEventListener('navigate', event => { + signal = event.signal; + const handler = () => { + transition = navigation.transition; + observed.transitionPresent = transition !== null; + observed.from = transition?.from === from; + transition?.committed?.then(() => observed.transitionCommitted = 'resolved', reason => observed.transitionCommitted = reason.name); + transition?.finished.then(() => observed.transitionFinished = 'resolved', reason => observed.transitionFinished = reason.name); + ready(); + return new Promise(resolve => release = resolve); + }; + event.intercept(phase === 'precommit' ? {precommitHandler: handler} : {handler}); + }, {once: true}); + const result = navigation.back(); + result.committed.then(() => observed.committed = 'resolved', reason => observed.committed = reason.name); + result.finished.then(() => observed.finished = 'resolved', reason => observed.finished = reason.name); + await started; + if (action === 'stop') window.stop(); + else await navigation.navigate('#nested').finished; + await result.finished.catch(() => {}); + release(); + await new Promise(resolve => setTimeout(resolve, 0)); + observed.currentTransition = navigation.transition; + observed.hash = location.hash; + observed.aborted = signal.aborted; + return observed; +})() +"# + .replace("PHASE", &json!(phase).to_string()) + .replace("ACTION", &json!(action).to_string()); + let result = page.evaluate(&script).await; + let committed = if phase == "precommit" { + "AbortError" + } else { + "resolved" + }; + let hash = if action == "navigate" { + "#nested" + } else if phase == "precommit" { + "#current" + } else { + "#back" + }; + assert_eq!( + result, + json!({ + "committed": committed, + "finished": "AbortError", + "transitionCommitted": committed, + "transitionFinished": "AbortError", + "transitionPresent": true, + "from": true, + "currentTransition": null, + "hash": hash, + "aborted": true, + }), + "phase={phase}, action={action}" + ); + if action == "stop" { + page.assert_history( + &["", "#back", "#current"], + if phase == "precommit" { 2 } else { 1 }, + ) + .await; + } else if phase == "precommit" { + page.assert_history(&["", "#back", "#current", "#nested"], 3) + .await; + } else { + page.assert_history(&["", "#back", "#nested"], 2).await; + } + } + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn intercepted_traversal_transitions_reject_before_commit() { + for undefined in [false, true] { + let mut page = SameDocumentPage::new().await; + page.run( + "history.pushState(null, '', '#current'); void 0", + "#current", + ) + .await; + page.ctx.sent.clear(); + let script = r#"(async () => { + const expected = UNDEFINED ? undefined : new Error('precommit failure'); + const from = navigation.currentEntry; + const checks = []; + let transition; + let handlerRan = false; + navigation.addEventListener('navigate', event => { + event.signal.addEventListener('abort', () => checks.push(event.signal.reason === expected)); + event.intercept({ + precommitHandler() { + transition = navigation.transition; + return new Promise((_, reject) => setTimeout(() => reject(expected), 0)); + }, + handler() { handlerRan = true; } + }); + }, {once: true}); + const result = navigation.back(); + const rejectedWithExpected = promise => promise.then(() => false, reason => reason === expected); + checks.push(...await Promise.all([result.committed, result.finished].map(rejectedWithExpected))); + checks.push(...await Promise.all([transition.committed, transition.finished].map(rejectedWithExpected))); + checks.push(!handlerRan, navigation.currentEntry === from, navigation.transition === null); + return checks; +})() +"#.replace("UNDEFINED", if undefined { "true" } else { "false" }); + let result = page.evaluate(&script).await; + assert_eq!( + result, + json!([true, true, true, true, true, true, true, true]), + "undefined={undefined}" + ); + page.assert_history(&["", "#current"], 1).await; + page.assert_commits(&[]); + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn intercepted_traversal_transitions_preserve_navigation_started_during_completion() { + for trigger in ["success", "error", "abort"] { + let mut page = SameDocumentPage::new().await; + page.run( + "history.pushState(null, '', '#back'); history.pushState(null, '', '#current'); void 0", + "#current", + ) + .await; + page.ctx.sent.clear(); + let script = r#"(async () => { + const trigger = TRIGGER; + const error = new Error('expected'); + const observed = {}; + let transition, nestedTransition, nested, release; + navigation.addEventListener('currententrychange', () => { + transition = navigation.transition; + transition?.finished.then(() => observed.oldFinished = 'resolved', reason => observed.oldFinished = reason === error ? 'expected' : reason.name); + }, {once: true}); + const startNested = () => { + navigation.addEventListener('navigate', e => e.intercept({handler() { + nestedTransition = navigation.transition; + return new Promise(resolve => release = resolve); + }}), {once: true}); + nested = navigation.navigate('#nested'); + nested.finished.then(() => observed.nestedFinished = true, reason => observed.nestedError = reason.name); + }; + navigation.addEventListener('navigate', e => { + if (trigger === 'abort') e.signal.addEventListener('abort', startNested, {once: true}); + e.intercept({handler() { + if (trigger !== 'success') return Promise.reject(error); + }}); + }, {once: true}); + if (trigger !== 'abort') navigation.addEventListener(trigger === 'success' ? 'navigatesuccess' : 'navigateerror', startNested, {once: true}); + const result = navigation.back(); + await result.finished.catch(() => {}); + await new Promise(resolve => setTimeout(resolve, 0)); + observed.preservedNewTransition = navigation.transition === nestedTransition && nestedTransition !== null; + observed.distinct = nestedTransition !== transition; + observed.newPending = !observed.nestedFinished; + release(); + await nested.finished.catch(() => {}); + await new Promise(resolve => setTimeout(resolve, 0)); + observed.cleared = navigation.transition === null; + return observed; +})() +"#.replace("TRIGGER", &json!(trigger).to_string()); + let result = page.evaluate(&script).await; + assert_eq!( + result, + json!({ + "oldFinished": if trigger == "success" { "resolved" } else { "expected" }, + "preservedNewTransition": true, + "distinct": true, + "newPending": true, + "nestedFinished": true, + "cleared": true, + }), + "trigger={trigger}" + ); + page.assert_history(&["", "#back", "#nested"], 2).await; + page.assert_commits(&[("#back", "other"), ("#nested", "other")]); + } +} + #[tokio::test(flavor = "multi_thread")] async fn same_document_commits_keep_navigation_api_and_browser_history_in_sync() { let mut page = SameDocumentPage::new().await; diff --git a/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs b/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs index 7f67a836d7d..b39631caf95 100644 --- a/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs +++ b/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs @@ -3,7 +3,7 @@ use moli_webapi::navigation_values::options::{ }; use super::*; -use crate::context_bootstrap::navigation_activation::install_navigation_transition; +use crate::context_bootstrap::navigation_activation::{NAVIGATE_EVENT_PRECOMMIT_TRANSITION_RESOLVER_SLOT, install_navigation_transition}; use crate::context_bootstrap::navigation_events::{ navigation_destination_url_value, navigation_scroll_event_is_active, set_navigation_destination_url, @@ -42,8 +42,6 @@ const NAVIGATE_EVENT_PRECOMMIT_TRANSITION_DESTINATION_SLOT: &str = "__lmNavigateEventPrecommitTransitionDestination"; const NAVIGATE_EVENT_PRECOMMIT_TRANSITION_TYPE_SLOT: &str = "__lmNavigateEventPrecommitTransitionType"; -const NAVIGATE_EVENT_PRECOMMIT_TRANSITION_RESOLVER_SLOT: &str = - "__lmNavigateEventPrecommitTransitionResolver"; const PRECOMMIT_CONTROLLER_EVENT_SLOT: &str = "__lmPrecommitControllerEvent"; const PRECOMMIT_CONTROLLER_BACKING_SLOT: &str = "__moliPrecommitControllerBacking"; const PRECOMMIT_CONTROLLER_ACTIVE_SLOT: &str = "__lmPrecommitControllerActive"; diff --git a/moli-renderer-v8/src/context_bootstrap/history_runtime/apply.rs b/moli-renderer-v8/src/context_bootstrap/history_runtime/apply.rs index 0f570c20e29..7a2a1f7e5e0 100644 --- a/moli-renderer-v8/src/context_bootstrap/history_runtime/apply.rs +++ b/moli-renderer-v8/src/context_bootstrap/history_runtime/apply.rs @@ -30,7 +30,7 @@ pub(in crate::context_bootstrap) struct AppliedHistoryEntry<'s> { previous_history_index: u32, history_index: u32, entry: v8::Local<'s, v8::Object>, - previous_entry: Option>, + pub(in crate::context_bootstrap) previous_entry: Option>, } pub(in crate::context_bootstrap) struct PreparedHistoryEntry<'s> { diff --git a/moli-renderer-v8/src/context_bootstrap/history_runtime/traversal.rs b/moli-renderer-v8/src/context_bootstrap/history_runtime/traversal.rs index a919bd63d4c..b1b60a29b0b 100644 --- a/moli-renderer-v8/src/context_bootstrap/history_runtime/traversal.rs +++ b/moli-renderer-v8/src/context_bootstrap/history_runtime/traversal.rs @@ -1,9 +1,15 @@ +use super::super::navigation_activation::{ + install_navigation_transition, precommit_transition_resolver_from_event, + resolve_navigation_transition_committed, +}; use super::super::navigation_entry::history_entries; use super::super::navigation_events::{ NavigationDispatchOutcome, dispatch_navigation_success, run_navigation_precommit_deferred_handlers, }; -use super::super::navigation_lifecycle::finish_navigation_error_events; +use super::super::navigation_lifecycle::{ + finish_navigation_error_events, settle_navigation_transition_finished_local, +}; use super::super::navigation_result::{ navigation_dom_exception, perform_navigation_scroll_if_needed, suppress_unhandled_rejection, }; @@ -16,6 +22,7 @@ use super::super::*; use super::apply::dispatch_history_entry_post_commit_events; use super::results::reject_pending_navigation_results; use crate::native_bridge::PendingHistoryTraversal; +use crate::script_cleanup::ScriptExecutionScope; use crate::util::{get_private_value, set_private_value}; use moli_webapi_declare::WebApiObject; @@ -27,6 +34,7 @@ const TRAVERSAL_INTERCEPT_VALUE_SLOT: &str = "__lmTraversalInterceptValue"; const TRAVERSAL_INTERCEPT_URL_SLOT: &str = "__lmTraversalInterceptUrl"; const TRAVERSAL_INTERCEPT_PROMISE_SLOT: &str = "__lmTraversalInterceptPromise"; const NAVIGATION_ACTIVE_TRAVERSAL_INTERCEPT_SLOT: &str = "__lmNavigationActiveTraversalIntercept"; +const TRAVERSAL_TRANSITION_RESOLVER_SLOT: &str = "__lmTraversalTransitionResolver"; #[derive(WebApiObject)] #[webapi(plain)] @@ -48,6 +56,19 @@ struct TraversalInterceptSettlementDataDeclaration<'scope> { #[webapi(slot = TRAVERSAL_INTERCEPT_URL_SLOT)] url: v8::Local<'scope, v8::String>, + + #[webapi(slot = TRAVERSAL_TRANSITION_RESOLVER_SLOT)] + transition_resolver: Option>, +} + +fn traversal_transition_resolver<'s>( + scope: &mut v8::PinScope<'s, '_>, + data: v8::Local<'s, v8::Value>, +) -> Option> { + let data = v8::Local::::try_from(data).ok()?; + get_private_value(scope, data, TRAVERSAL_TRANSITION_RESOLVER_SLOT) + .and_then(|value| v8::Local::::try_from(value).ok()) + .map(|object| unsafe { v8::Local::::cast_unchecked(object) }) } fn navigation_active_traversal_intercept<'s>( @@ -180,14 +201,35 @@ pub(in crate::context_bootstrap) fn prepare_history_participant<'s>( return None; } let navigation = window_navigation_for_holder(scope, applied.owner)?; - Some(set_active_traversal_intercept_settlement( - scope, + let transition_resolver = outcome + .precommit_event + .and_then(|event| precommit_transition_resolver_from_event(scope, event)) + .or_else(|| { + applied.previous_entry.and_then(|from| { + install_navigation_transition( + scope, + navigation, + from, + outcome.destination, + "traverse", + ) + }) + }); + let url = v8::String::new(scope, &applied.url)?; + let data = TraversalInterceptSettlementDataDeclaration { + active: true, navigation, - outcome.signal, + signal: outcome.signal, finished_resolvers, - applied.resolved_entry, - &applied.url, - )) + value: applied.resolved_entry, + url, + transition_resolver, + } + .bind(scope) + .expect("traversal intercept settlement data should bind"); + set_navigation_active_traversal_intercept(scope, navigation, data); + resolve_navigation_transition_committed(scope, navigation, v8::undefined(scope).into()); + Some(data) } pub(in crate::context_bootstrap) fn finish_history_participant<'s>( @@ -223,7 +265,7 @@ pub(in crate::context_bootstrap) fn finish_history_participant<'s>( dispatch_history_entry_post_commit_events(scope, applied, true); return; } - let Some(navigation) = window_navigation_for_holder(scope, applied.owner) else { + let Some(_navigation) = window_navigation_for_holder(scope, applied.owner) else { dispatch_history_entry_post_commit_events(scope, applied, true); resolve_resolver_array(scope, finished_resolvers, applied.resolved_entry); return; @@ -239,36 +281,28 @@ pub(in crate::context_bootstrap) fn finish_history_participant<'s>( }; suppress_intercept_result_unhandled_rejection(scope, result); dispatch_history_entry_post_commit_events(scope, applied, true); - if let Some(data) = settlement { - if !traversal_intercept_is_active(scope, data.into()) { - return; - } - set_traversal_intercept_inactive(scope, navigation, data.into()); + let Some(data) = settlement else { + resolve_resolver_array(scope, finished_resolvers, applied.resolved_entry); + return; + }; + if !traversal_intercept_is_active(scope, data.into()) { + return; } if let Some(error) = error { - finish_navigation_error_events(scope, navigation, error, &applied.url); - reject_resolver_array(scope, finished_resolvers, error, true); - if let Some(signal) = outcome.signal { - crate::native_bridge::abort::abort_signal(scope, signal, error); + finish_traversal_intercept(scope, data.into(), Some(error)); + } else { + // Even an empty handler list settles asynchronously after committed reactions. + let result = result.or_else(|| { + let resolver = v8::PromiseResolver::new(scope)?; + resolver.resolve(scope, v8::undefined(scope).into())?; + Some(resolver.get_promise(scope).into()) + }); + if result + .is_none_or(|result| !queue_pending_traversal_intercept_settlement(scope, data, result)) + { + finish_traversal_intercept(scope, data.into(), None); } - return; } - if let Some(result) = result - && queue_pending_traversal_intercept_settlement( - scope, - navigation, - outcome.signal, - finished_resolvers, - applied.resolved_entry, - &applied.url, - result, - ) - { - return; - } - perform_navigation_scroll_if_needed(scope, navigation, &applied.url, true); - dispatch_navigation_success(scope, navigation); - resolve_resolver_array(scope, finished_resolvers, applied.resolved_entry); } fn history_traversal_target_window<'s>( @@ -320,11 +354,7 @@ fn suppress_intercept_result_unhandled_rejection<'s>( fn queue_pending_traversal_intercept_settlement<'s>( scope: &mut v8::PinScope<'s, '_>, - navigation: v8::Local<'s, v8::Object>, - signal: Option>, - finished_resolvers: v8::Local<'s, v8::Array>, - resolved_value: v8::Local<'s, v8::Value>, - url: &str, + data: v8::Local<'s, v8::Object>, result: v8::Local<'s, v8::Value>, ) -> bool { let Some(result_object) = v8::Local::::try_from(result).ok() else { @@ -336,14 +366,6 @@ fn queue_pending_traversal_intercept_settlement<'s>( else { return false; }; - let data = set_active_traversal_intercept_settlement( - scope, - navigation, - signal, - finished_resolvers, - resolved_value, - url, - ); if let Ok(promise) = v8::Local::::try_from(result) { suppress_unhandled_rejection(scope, promise); set_private_value(scope, data, TRAVERSAL_INTERCEPT_PROMISE_SLOT, result); @@ -364,29 +386,6 @@ fn queue_pending_traversal_intercept_settlement<'s>( .is_some() } -fn set_active_traversal_intercept_settlement<'s>( - scope: &mut v8::PinScope<'s, '_>, - navigation: v8::Local<'s, v8::Object>, - signal: Option>, - finished_resolvers: v8::Local<'s, v8::Array>, - resolved_value: v8::Local<'s, v8::Value>, - url: &str, -) -> v8::Local<'s, v8::Object> { - let url = v8_string(scope, url).unwrap_or_else(|| v8::String::empty(scope)); - let data = TraversalInterceptSettlementDataDeclaration { - active: true, - navigation, - signal, - finished_resolvers, - value: resolved_value, - url, - } - .bind(scope) - .expect("traversal intercept settlement data should bind"); - set_navigation_active_traversal_intercept(scope, navigation, data); - data -} - fn traversal_intercept_data<'s>( scope: &mut v8::PinScope<'s, '_>, data: v8::Local<'s, v8::Value>, @@ -469,18 +468,8 @@ pub(in crate::context_bootstrap) fn cancel_active_history_traversal_intercept_se else { return false; }; - let Some((navigation, signal, finished_resolvers, _, url, _)) = - traversal_intercept_data(scope, data.into()) - else { - return false; - }; - set_traversal_intercept_inactive(scope, navigation, data.into()); let error = navigation_dom_exception(scope, "Navigation was canceled", "AbortError"); - if let Some(signal) = signal { - crate::native_bridge::abort::abort_signal(scope, signal, error); - } - finish_navigation_error_events(scope, navigation, error, &url); - reject_resolver_array(scope, finished_resolvers, error, true); + finish_traversal_intercept(scope, data.into(), Some(error)); true } @@ -489,31 +478,7 @@ fn traversal_intercept_fulfilled_callback<'s>( args: v8::FunctionCallbackArguments<'s>, _rv: v8::ReturnValue<'_, v8::Value>, ) { - let Some((navigation, signal, finished_resolvers, resolved_value, url, _)) = - traversal_intercept_data(scope, args.data()) - else { - return; - }; - set_traversal_intercept_inactive(scope, navigation, args.data()); - let owner = runtime_window_owner(scope, navigation); - if !navigation_document_is_active(scope, owner) { - let error = navigation_dom_exception(scope, "Navigation was canceled", "AbortError"); - if let Some(signal) = signal { - crate::native_bridge::abort::abort_signal(scope, signal, error); - } - let top_owner = runtime_top_window_owner(scope, owner); - let filename = window_location_for_holder(scope, top_owner) - .and_then(|location| { - super::super::location_runtime::location_href_slot(scope, location) - }) - .unwrap_or(url); - finish_navigation_error_events(scope, navigation, error, &filename); - reject_resolver_array(scope, finished_resolvers, error, true); - return; - } - perform_navigation_scroll_if_needed(scope, navigation, &url, true); - dispatch_navigation_success(scope, navigation); - resolve_resolver_array(scope, finished_resolvers, resolved_value); + finish_traversal_intercept(scope, args.data(), None); } fn traversal_intercept_rejected_callback<'s>( @@ -521,21 +486,56 @@ fn traversal_intercept_rejected_callback<'s>( args: v8::FunctionCallbackArguments<'s>, _rv: v8::ReturnValue<'_, v8::Value>, ) { - let Some((navigation, signal, finished_resolvers, _, url, promise)) = - traversal_intercept_data(scope, args.data()) - else { + let Some((_, _, _, _, _, promise)) = traversal_intercept_data(scope, args.data()) else { return; }; - set_traversal_intercept_inactive(scope, navigation, args.data()); let error = promise .filter(|promise| promise.state() == v8::PromiseState::Rejected) .map(|promise| promise.result(scope)) .unwrap_or_else(|| args.get(0)); - if let Some(signal) = signal { - crate::native_bridge::abort::abort_signal(scope, signal, error); + finish_traversal_intercept(scope, args.data(), Some(error)); +} + +fn finish_traversal_intercept<'s>( + scope: &mut v8::PinScope<'s, '_>, + data: v8::Local<'s, v8::Value>, + error: Option>, +) { + let Some((navigation, signal, finished_resolvers, resolved_value, url, _)) = + traversal_intercept_data(scope, data) + else { + return; + }; + let _execution = ScriptExecutionScope::enter(scope); + let transition_resolver = traversal_transition_resolver(scope, data); + set_traversal_intercept_inactive(scope, navigation, data); + let owner = runtime_window_owner(scope, navigation); + let active = navigation_document_is_active(scope, owner); + let error = error.or_else(|| { + (!active).then(|| navigation_dom_exception(scope, "Navigation was canceled", "AbortError")) + }); + if let Some(error) = error { + if let Some(signal) = signal { + crate::native_bridge::abort::abort_signal(scope, signal, error); + } + let filename = if active { + url + } else { + let top_owner = runtime_top_window_owner(scope, owner); + window_location_for_holder(scope, top_owner) + .and_then(|location| { + super::super::location_runtime::location_href_slot(scope, location) + }) + .unwrap_or(url) + }; + finish_navigation_error_events(scope, navigation, error, &filename); + reject_resolver_array(scope, finished_resolvers, error, true); + } else { + perform_navigation_scroll_if_needed(scope, navigation, &url, true); + dispatch_navigation_success(scope, navigation); + resolve_resolver_array(scope, finished_resolvers, resolved_value); } - finish_navigation_error_events(scope, navigation, error, &url); - reject_resolver_array(scope, finished_resolvers, error, true); + settle_navigation_transition_finished_local(scope, navigation, transition_resolver, error); } pub(in crate::context_bootstrap) fn resolve_resolver_array<'s>( diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_activation.rs b/moli-renderer-v8/src/context_bootstrap/navigation_activation.rs index 69e3728ed3f..209738a9fd4 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_activation.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_activation.rs @@ -17,6 +17,9 @@ use moli_webapi::navigation_values::{ }; use moli_webapi_declare::WebApiObject; +pub(super) const NAVIGATE_EVENT_PRECOMMIT_TRANSITION_RESOLVER_SLOT: &str = + "__lmNavigateEventPrecommitTransitionResolver"; + #[derive(WebApiObject)] #[webapi(plain)] struct NavigationTransitionSettleDataDeclaration<'scope> { @@ -28,6 +31,9 @@ struct NavigationTransitionSettleDataDeclaration<'scope> { #[webapi(slot = NAVIGATION_TRANSITION_SETTLE_ERROR_SLOT)] error: Option>, + + #[webapi(slot = NAVIGATION_TRANSITION_SETTLE_REJECTED_SLOT)] + rejected: bool, } pub(super) fn current_entry_matches_activation_snapshot<'s>( @@ -186,6 +192,19 @@ pub(super) fn resolve_navigation_transition_committed<'s>( } } +pub(super) fn precommit_transition_resolver_from_event<'s>( + scope: &mut v8::PinScope<'s, '_>, + event: v8::Local<'s, v8::Object>, +) -> Option> { + event_private_value( + scope, + event, + NAVIGATE_EVENT_PRECOMMIT_TRANSITION_RESOLVER_SLOT, + ) + .and_then(|value| v8::Local::::try_from(value).ok()) + .map(|object| unsafe { v8::Local::::cast_unchecked(object) }) +} + pub(super) fn reject_navigation_transition_committed<'s>( scope: &mut v8::PinScope<'s, '_>, navigation: v8::Local<'s, v8::Object>, @@ -207,6 +226,8 @@ const NAVIGATION_TRANSITION_SETTLE_NAVIGATION_SLOT: &str = "__lmNavigationTransitionSettleNavigation"; const NAVIGATION_TRANSITION_SETTLE_RESOLVER_SLOT: &str = "__lmNavigationTransitionSettleResolver"; const NAVIGATION_TRANSITION_SETTLE_ERROR_SLOT: &str = "__lmNavigationTransitionSettleError"; +const NAVIGATION_TRANSITION_SETTLE_REJECTED_SLOT: &str = "__lmNavigationTransitionSettleRejected"; + fn take_navigation_transition_committed_resolver<'s>( scope: &mut v8::PinScope<'s, '_>, navigation: v8::Local<'s, v8::Object>, @@ -225,6 +246,7 @@ pub(super) fn schedule_settle_navigation_transition<'s>( navigation, resolver, error, + rejected: error.is_some(), } .bind(scope) .expect("navigation transition settle data should bind"); @@ -232,7 +254,9 @@ pub(super) fn schedule_settle_navigation_transition<'s>( .data(data.into()) .build(scope) else { - clear_navigation_transition(scope, navigation); + if navigation_transition_matches_resolver(scope, navigation, resolver) { + clear_navigation_transition(scope, navigation); + } return; }; enqueue_navigation_lifecycle_microtask(scope, callback); @@ -261,9 +285,11 @@ fn settle_navigation_transition_callback<'s>( if navigation_transition_matches_resolver(scope, navigation, resolver) { clear_navigation_transition(scope, navigation); } - if let Some(error) = get_private_value(scope, data, NAVIGATION_TRANSITION_SETTLE_ERROR_SLOT) - .filter(|value| !value.is_undefined()) + if get_private_value(scope, data, NAVIGATION_TRANSITION_SETTLE_REJECTED_SLOT) + .is_some_and(|value| value.is_true()) { + let error = get_private_value(scope, data, NAVIGATION_TRANSITION_SETTLE_ERROR_SLOT) + .unwrap_or_else(|| v8::undefined(scope).into()); let _ = resolver.reject(scope, error); } else { let _ = resolver.resolve(scope, v8::undefined(scope).into()); @@ -354,6 +380,13 @@ pub(super) fn set_navigation_current_entry( mod tests { use super::*; + fn navigation_transition_constructor<'s>( + _scope: &mut v8::PinScope<'s, '_>, + _args: v8::FunctionCallbackArguments<'s>, + _rv: v8::ReturnValue<'_, v8::Value>, + ) { + } + #[test] fn navigation_transition_and_promises_use_navigation_creation_realm() { crate::ensure_v8_for_test(); 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 c467bf91a5e..00e9cadaa26 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_callbacks/navigation.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_callbacks/navigation.rs @@ -4,8 +4,8 @@ use moli_webapi::navigation_values::options::{ use super::super::navigation_activation::{ clear_navigation_transition, install_navigation_transition, - navigation_transition_matches_resolver, reject_navigation_transition_committed, - resolve_navigation_transition_committed, + navigation_transition_matches_resolver, precommit_transition_resolver_from_event, + reject_navigation_transition_committed, resolve_navigation_transition_committed, }; use super::super::navigation_events::dispatch_popstate_event; use super::super::navigation_lifecycle::{ @@ -26,9 +26,6 @@ use crate::webidl; use moli_url_policy::{LocalFileNavigationAccess, route_navigation_url}; use moli_webapi_declare::WebApiObject; -const NAVIGATE_EVENT_PRECOMMIT_TRANSITION_RESOLVER_SLOT: &str = - "__lmNavigateEventPrecommitTransitionResolver"; - pub(in crate::context_bootstrap) fn navigation_entries_callback<'s>( scope: &mut v8::PinScope<'s, '_>, args: v8::FunctionCallbackArguments<'s>, @@ -1035,19 +1032,6 @@ fn cancel_precommit_commit_attempt<'s>( } } -fn precommit_transition_resolver_from_event<'s>( - scope: &mut v8::PinScope<'s, '_>, - event: v8::Local<'s, v8::Object>, -) -> Option> { - crate::context_bootstrap::event_private_value( - scope, - event, - NAVIGATE_EVENT_PRECOMMIT_TRANSITION_RESOLVER_SLOT, - ) - .and_then(|value| v8::Local::::try_from(value).ok()) - .map(|object| unsafe { v8::Local::::cast_unchecked(object) }) -} - fn set_pending_precommit_commit_active<'s>( scope: &mut v8::PinScope<'s, '_>, navigation: v8::Local<'s, v8::Object>, diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_lifecycle.rs b/moli-renderer-v8/src/context_bootstrap/navigation_lifecycle.rs index 1854edeff26..d6b63398a84 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_lifecycle.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_lifecycle.rs @@ -1,6 +1,6 @@ use super::navigation_activation::{ - clear_navigation_transition, resolve_navigation_transition_committed, - schedule_settle_navigation_transition, + clear_navigation_transition, navigation_transition_matches_resolver, + resolve_navigation_transition_committed, schedule_settle_navigation_transition, }; use super::navigation_events::{ NAVIGATE_EVENT_SCROLL_AFTER_TRANSITION_SLOT, NAVIGATE_EVENT_SCROLL_CALLED_SLOT, @@ -178,9 +178,6 @@ pub(super) fn settle_navigation_transition_finished<'s>( transition_resolver: Option>, error: Option>, ) { - if transition_resolver.is_some() { - clear_navigation_transition(scope, navigation); - } if let Some(transition_resolver) = transition_resolver { let transition_resolver = v8::Local::new(scope, transition_resolver); settle_navigation_transition_finished_local( @@ -198,10 +195,10 @@ pub(super) fn settle_navigation_transition_finished_local<'s>( transition_resolver: Option>, error: Option>, ) { - if transition_resolver.is_some() { - clear_navigation_transition(scope, navigation); - } if let Some(transition_resolver) = transition_resolver { + if navigation_transition_matches_resolver(scope, navigation, transition_resolver) { + clear_navigation_transition(scope, navigation); + } schedule_settle_navigation_transition(scope, navigation, transition_resolver, error); } } diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs index abe2126bae0..2b51b8ffbcd 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs @@ -1,6 +1,10 @@ //! Admit, reserve and commit complete traversal plans, including zero or one participant. use super::history_runtime::{apply, results, traversal}; +use super::navigation_activation::{ + navigation_transition_matches_resolver, precommit_transition_resolver_from_event, + reject_navigation_transition_committed, +}; use super::navigation_entry::{history_entries, history_index, navigation_current_entry}; use super::navigation_events::{ NavigationDispatchOutcome, dispatch_beforeunload_for_runtime_owner, @@ -64,6 +68,9 @@ impl TraversalParticipantOutcome { fn capture(scope: &mut v8::PinScope<'_, '_>, outcome: &NavigationDispatchOutcome<'_>) -> Self { Self { intercepted: outcome.intercepted, + destination: outcome + .destination + .map(|value| v8::Global::new(scope, value)), signal: outcome.signal.map(|value| v8::Global::new(scope, value)), event: outcome .precommit_event @@ -80,6 +87,10 @@ impl TraversalParticipantOutcome { fn local<'s>(&self, scope: &mut v8::PinScope<'s, '_>) -> NavigationDispatchOutcome<'s> { let mut outcome = NavigationDispatchOutcome::proceed(); outcome.intercepted = self.intercepted; + outcome.destination = self + .destination + .as_ref() + .map(|value| v8::Local::new(scope, value)); outcome.signal = self .signal .as_ref() @@ -323,6 +334,21 @@ fn settle_aborted_admission<'s>( }); results::reject_pending_navigation_results(scope, &admission.results, error); for participant in &admission.participants { + let navigation = participant + .navigation + .as_ref() + .map(|value| v8::Local::new(scope, value)); + let transition_resolver = participant.outcome.event.as_ref().and_then(|event| { + let event = v8::Local::new(scope, event); + precommit_transition_resolver_from_event(scope, event) + }); + if let Some(navigation) = navigation + && transition_resolver.is_some_and(|resolver| { + navigation_transition_matches_resolver(scope, navigation, resolver) + }) + { + reject_navigation_transition_committed(scope, navigation, error); + } let Some(signal) = participant .outcome .signal @@ -338,8 +364,7 @@ fn settle_aborted_admission<'s>( continue; } crate::native_bridge::abort::abort_signal(scope, signal, error); - if let Some(navigation) = &participant.navigation { - let navigation = v8::Local::new(scope, navigation); + if let Some(navigation) = navigation { let filename = if canceled { let owner = super::navigation_window::runtime_window_owner(scope, navigation); window_location_for_holder(scope, owner) @@ -353,6 +378,12 @@ fn settle_aborted_admission<'s>( super::navigation_lifecycle::finish_navigation_error_events( scope, navigation, error, &filename, ); + super::navigation_lifecycle::settle_navigation_transition_finished_local( + scope, + navigation, + transition_resolver, + Some(error), + ); } } } diff --git a/moli-renderer-v8/src/native_bridge/history_traversal.rs b/moli-renderer-v8/src/native_bridge/history_traversal.rs index f70dd540bbb..33c19d0d99e 100644 --- a/moli-renderer-v8/src/native_bridge/history_traversal.rs +++ b/moli-renderer-v8/src/native_bridge/history_traversal.rs @@ -32,6 +32,7 @@ impl HistoryTraversalId { #[derive(Default)] pub(crate) struct TraversalParticipantOutcome { pub(crate) intercepted: bool, + pub(crate) destination: Option>, pub(crate) signal: Option>, pub(crate) event: Option>, pub(crate) intercept_result: Option>, diff --git a/moli-wpt-compat/fixtures/wpt/manifest.toml b/moli-wpt-compat/fixtures/wpt/manifest.toml index d2e8a4780a7..512e4d75bca 100644 --- a/moli-wpt-compat/fixtures/wpt/manifest.toml +++ b/moli-wpt-compat/fixtures/wpt/manifest.toml @@ -22013,3 +22013,75 @@ source_path = "webmcp/imperative/unregister-during-executeTool.https.html" source_commit = "2d184faab9fb11e1070fb1eeafebf291f6e85923" tags = ["webmcp", "chromium", "upstream-wpt", "imperative"] notes = "Unmodified WPT imported from Chromium third_party/blink/web_tests/external/wpt at the recorded commit." + +[[test]] +id = "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-reject-no-currententrychange" +upstream = "web-platform-tests/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html?no-currententrychange" +upstream_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +local_path = "upstream/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html" +type = "testharness" +global = "window" +status = "pass" +wait_until = "load" +timeout_ms = 5000 +suite = "broad" +source = "upstream-wpt" +source_path = "navigation-api/ordering-and-transition/back-same-document-intercept-reject.html" +source_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +tags = ["navigation-api", "ordering-and-transition", "navigation", "history"] +notes = "Unmodified upstream test for intercepted traversal transition lifetime and event/Promise ordering, including this explicit query variant." +query = "no-currententrychange" + +[[test]] +id = "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-reject-currententrychange" +upstream = "web-platform-tests/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html?currententrychange" +upstream_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +local_path = "upstream/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html" +type = "testharness" +global = "window" +status = "pass" +wait_until = "load" +timeout_ms = 5000 +suite = "broad" +source = "upstream-wpt" +source_path = "navigation-api/ordering-and-transition/back-same-document-intercept-reject.html" +source_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +tags = ["navigation-api", "ordering-and-transition", "navigation", "history"] +notes = "Unmodified upstream test for intercepted traversal transition lifetime and event/Promise ordering, including this explicit query variant." +query = "currententrychange" + +[[test]] +id = "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-no-currententrychange" +upstream = "web-platform-tests/navigation-api/ordering-and-transition/back-same-document-intercept.html?no-currententrychange" +upstream_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +local_path = "upstream/navigation-api/ordering-and-transition/back-same-document-intercept.html" +type = "testharness" +global = "window" +status = "pass" +wait_until = "load" +timeout_ms = 5000 +suite = "broad" +source = "upstream-wpt" +source_path = "navigation-api/ordering-and-transition/back-same-document-intercept.html" +source_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +tags = ["navigation-api", "ordering-and-transition", "navigation", "history"] +notes = "Unmodified upstream test for intercepted traversal transition lifetime and event/Promise ordering, including this explicit query variant." +query = "no-currententrychange" + +[[test]] +id = "upstream-navigation-api-ordering-and-transition-back-same-document-intercept-currententrychange" +upstream = "web-platform-tests/navigation-api/ordering-and-transition/back-same-document-intercept.html?currententrychange" +upstream_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +local_path = "upstream/navigation-api/ordering-and-transition/back-same-document-intercept.html" +type = "testharness" +global = "window" +status = "pass" +wait_until = "load" +timeout_ms = 5000 +suite = "broad" +source = "upstream-wpt" +source_path = "navigation-api/ordering-and-transition/back-same-document-intercept.html" +source_commit = "db95fafd1fcef8428805e41eb5705d444e8c67ce" +tags = ["navigation-api", "ordering-and-transition", "navigation", "history"] +notes = "Unmodified upstream test for intercepted traversal transition lifetime and event/Promise ordering, including this explicit query variant." +query = "currententrychange" diff --git a/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html b/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html new file mode 100644 index 00000000000..f848c61eccd --- /dev/null +++ b/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept-reject.html @@ -0,0 +1,55 @@ + + + + + + + diff --git a/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept.html b/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept.html new file mode 100644 index 00000000000..f66e1dfba8f --- /dev/null +++ b/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/back-same-document-intercept.html @@ -0,0 +1,48 @@ + + + + + + + diff --git a/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/resources/helpers.mjs b/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/resources/helpers.mjs new file mode 100644 index 00000000000..22752e4cfed --- /dev/null +++ b/moli-wpt-compat/fixtures/wpt/upstream/navigation-api/ordering-and-transition/resources/helpers.mjs @@ -0,0 +1,206 @@ +const variants = new Set((new URLSearchParams(location.search)).keys()); + +export function hasVariant(name) { + return variants.has(name); +} + +export class Recorder { + #events = []; + #errors = []; + #navigationAPI; + #domExceptionConstructor; + #location; + #skipCurrentChange; + #finalExpectedEvent; + #finalExpectedEventCount; + #currentFinalEventCount = 0; + + #readyToAssertResolve; + #readyToAssertPromise = new Promise(resolve => { this.#readyToAssertResolve = resolve; }); + + constructor({ window = self, skipCurrentChange = false, finalExpectedEvent, finalExpectedEventCount = 1 }) { + assert_equals(typeof finalExpectedEvent, "string", "Must pass a string for finalExpectedEvent"); + + this.#navigationAPI = window.navigation; + this.#domExceptionConstructor = window.DOMException; + this.#location = window.location; + + this.#skipCurrentChange = skipCurrentChange; + this.#finalExpectedEvent = finalExpectedEvent; + this.#finalExpectedEventCount = finalExpectedEventCount; + } + + setUpNavigationAPIListeners() { + this.#navigationAPI.addEventListener("navigate", e => { + this.record("navigate"); + + e.signal.addEventListener("abort", () => { + this.recordWithError("AbortSignal abort", e.signal.reason); + }); + }); + + this.#navigationAPI.addEventListener("navigateerror", e => { + this.recordWithError("navigateerror", e.error); + + this.#navigationAPI.transition?.finished.then( + () => this.record("transition.finished fulfilled"), + err => this.recordWithError("transition.finished rejected", err) + ); + }); + + this.#navigationAPI.addEventListener("navigatesuccess", () => { + this.record("navigatesuccess"); + + this.#navigationAPI.transition?.finished.then( + () => this.record("transition.finished fulfilled"), + err => this.recordWithError("transition.finished rejected", err) + ); + }); + + if (!this.#skipCurrentChange) { + this.#navigationAPI.addEventListener("currententrychange", () => this.record("currententrychange")); + } + } + + setUpResultListeners(result, suffix = "") { + + result.committed.then( + () => this.record(`committed fulfilled${suffix}`), + err => this.recordWithError(`committed rejected${suffix}`, err) + ); + + result.finished.then( + () => this.record(`finished fulfilled${suffix}`), + err => this.recordWithError(`finished rejected${suffix}`, err) + ); + + this.#navigationAPI.transition?.committed?.then( + () => this.record(`transition.committed fulfilled${suffix}`), + err => this.recordWithError(`transition.committed rejected${suffix}`, err) + ); + } + + record(name) { + const transitionProps = this.#navigationAPI.transition === null ? null : { + from: this.#navigationAPI.transition.from, + navigationType: this.#navigationAPI.transition.navigationType + }; + + this.#events.push({ name, location: this.#location.hash, transitionProps }); + + if (name === this.#finalExpectedEvent && ++this.#currentFinalEventCount === this.#finalExpectedEventCount) { + this.#readyToAssertResolve(); + } + } + + recordWithError(name, errorObject) { + this.record(name); + this.#errors.push({ name, errorObject }); + } + + get readyToAssert() { + return this.#readyToAssertPromise; + } + + // Usage: + // recorder.assert([ + // /* event name, location.hash value, navigation.transition properties */ + // ["currententrychange", "", null], + // ["committed fulfilled", "#1", { from, navigationType }], + // ... + // ]); + // + // The array format is to avoid repitition at the call site, but I recommend + // you document it like above. + // + // This will automatically also assert that any error objects recorded are + // equal to each other. Use the other assert functions to check the actual + // contents of the error objects. + assert(expectedAsArray) { + if (this.#skipCurrentChange) { + expectedAsArray = expectedAsArray.filter(expected => expected[0] !== "currententrychange"); + } + // TODO: Remove once https://github.com/whatwg/html/pull/10919 is merged. + if (!('committed' in NavigationTransition.prototype)) { + expectedAsArray = expectedAsArray.filter(expected => { + return !expected[0].includes("transition.committed fulfilled") && + !expected[0].includes("transition.committed rejected") + }); + } + + // Doing this up front gives nicer error messages because + // assert_array_equals is nice. + const recordedNames = this.#events.map(e => e.name); + const expectedNames = expectedAsArray.map(e => e[0]); + assert_array_equals(recordedNames, expectedNames); + + for (let i = 0; i < expectedAsArray.length; ++i) { + const recorded = this.#events[i]; + const expected = expectedAsArray[i]; + + assert_equals( + recorded.location, + expected[1], + `event ${i} (${recorded.name}): location.hash value` + ); + + if (expected[2] === null) { + assert_equals( + recorded.transitionProps, + null, + `event ${i} (${recorded.name}): navigation.transition expected to be null` + ); + } else { + assert_not_equals( + recorded.transitionProps, + null, + `event ${i} (${recorded.name}): navigation.transition expected not to be null` + ); + assert_equals( + recorded.transitionProps.from, + expected[2].from, + `event ${i} (${recorded.name}): navigation.transition.from` + ); + assert_equals( + recorded.transitionProps.navigationType, + expected[2].navigationType, + `event ${i} (${recorded.name}): navigation.transition.navigationType` + ); + } + } + + if (this.#errors.length > 1) { + for (let i = 1; i < this.#errors.length; ++i) { + assert_equals( + this.#errors[i].errorObject, + this.#errors[0].errorObject, + `error objects must match: error object for ${this.#errors[i].name} did not match the one for ${this.#errors[0].name}` + ); + } + } + } + + assertErrorsAreAbortErrors() { + assert_greater_than( + this.#errors.length, + 0, + "No errors were recorded but assertErrorsAreAbortErrors() was called" + ); + + // Assume assert() has been called so all error objects are the same. + const { errorObject } = this.#errors[0]; + assert_throws_dom("AbortError", this.#domExceptionConstructor, () => { throw errorObject; }); + } + + assertErrorsAre(expectedErrorObject) { + assert_greater_than( + this.#errors.length, + 0, + "No errors were recorded but assertErrorsAre() was called" + ); + + // Assume assert() has been called so all error objects are the same. + const { errorObject } = this.#errors[0]; + assert_equals(errorObject, expectedErrorObject); + } +} diff --git a/moli-wpt-compat/src/server.rs b/moli-wpt-compat/src/server.rs index 0f73428696d..79184f4ae2e 100644 --- a/moli-wpt-compat/src/server.rs +++ b/moli-wpt-compat/src/server.rs @@ -1389,7 +1389,7 @@ fn wpt_fixture_content_type(path: &StdPath, fixture_path: &str) -> Option<&'stat Some("css") => Some("text/css; charset=utf-8"), Some("htm") => Some("text/html; charset=utf-8"), Some("html") => Some("text/html; charset=utf-8"), - Some("js") => Some("application/javascript"), + Some("js" | "mjs") => Some("application/javascript"), Some("json") => Some("application/json"), Some("txt") => Some("text/plain; charset=utf-8"), Some("xml") => Some("application/xml"),