From 9bbca36f0b411d3f4e2122e2dc2bcbb030f807b6 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 5 Nov 2026 18:25:51 +0800 Subject: [PATCH] fix(focus): preserve navigation reset after automatic focus fixup Treat disabled, hidden, and inert element focus fixup like removal when tracking focus changes during navigation. Clear the automatic change before blur handlers run so an author focus or blur decision still suppresses reset. Add a production page-task regression covering automatic fixup and author focus changes, including a focus-then-blur callback. --- .../mutation_commands/tree/focus.rs | 2 +- .../src/native_bridge/context_host/focus.rs | 17 ++--- .../src/native_bridge/element/focus.rs | 3 + .../browser_api/navigation/navigate_event.rs | 70 +++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) diff --git a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/focus.rs b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/focus.rs index 73fb1d27bee..09acdcc1bb3 100644 --- a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/focus.rs +++ b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/focus.rs @@ -90,7 +90,7 @@ impl DocumentRuntime { ) { let host = unsafe { &mut *host_ptr }; if let Some((document, area)) = plan.document_area { - host.clear_document_focused_area_on_removal(document, area); + host.clear_document_focused_area_for_fixup(document, area); } let Some(active) = &plan.active_element else { return; diff --git a/moli-renderer-v8/src/native_bridge/context_host/focus.rs b/moli-renderer-v8/src/native_bridge/context_host/focus.rs index 6b5d88626c4..cafd963f005 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/focus.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/focus.rs @@ -9,7 +9,7 @@ pub(super) struct DocumentFocusChangeState { focused_area: Option, sequential_starting_point: Option, epoch: u64, - last_change_was_removal: bool, + last_change_was_automatic_fixup: bool, } impl PageScriptHost { @@ -78,7 +78,7 @@ impl PageScriptHost { for (document, area) in documents { // A retained iframe handle must not revive its former parent's // focus when script inserts the iframe again after removal. - self.clear_document_focused_area_on_removal(document, area); + self.clear_document_focused_area_for_fixup(document, area); } } @@ -153,7 +153,7 @@ impl PageScriptHost { }) } - pub(crate) fn clear_document_focused_area_on_removal( + pub(crate) fn clear_document_focused_area_for_fixup( &mut self, document: DomHandle, area: DomHandle, @@ -163,9 +163,10 @@ impl PageScriptHost { { state.focused_area = None; state.epoch = state.epoch.wrapping_add(1); - // Removal clears "focus changed during ongoing navigation", even - // when an earlier focus() call changed the area during the transition. - state.last_change_was_removal = true; + // Automatic fixup clears "focus changed during ongoing navigation", + // even after an earlier focus() call. Later author focus changes + // still suppress the navigation's focus reset. + state.last_change_was_automatic_fixup = true; } } @@ -196,7 +197,7 @@ impl PageScriptHost { pub(crate) fn focus_changed_since(&self, document: DomHandle, epoch: u64) -> bool { self.document_focus_changes .get(&document) - .is_some_and(|state| state.epoch != epoch && !state.last_change_was_removal) + .is_some_and(|state| state.epoch != epoch && !state.last_change_was_automatic_fixup) } pub(crate) fn note_document_focused_area( @@ -211,7 +212,7 @@ impl PageScriptHost { if state.focused_area != area { state.focused_area = area; state.epoch = state.epoch.wrapping_add(1); - state.last_change_was_removal = false; + state.last_change_was_automatic_fixup = false; } } diff --git a/moli-renderer-v8/src/native_bridge/element/focus.rs b/moli-renderer-v8/src/native_bridge/element/focus.rs index 6ffc6a10d7d..db20712551c 100644 --- a/moli-renderer-v8/src/native_bridge/element/focus.rs +++ b/moli-renderer-v8/src/native_bridge/element/focus.rs @@ -580,6 +580,9 @@ pub(crate) fn apply_document_focus_fixup( scope.thread_safe_handle(), SCRIPT_TURN_WATCHDOG_TIMEOUT, ); + // Clear the automatic change before dispatching events so a blur handler's + // author focus change can still suppress navigation focus reset. + unsafe { &mut *runtime_ptr }.clear_document_focused_area_for_fixup(document, active); update_focus(scope, runtime_ptr, None); crate::script_cleanup::perform_callback_cleanup_checkpoint(scope); if watchdog.disarm() == V8ExecutionWatchdogOutcome::TimedOut { diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs index 08cb4a6b5c8..e09e3dfd45b 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs @@ -1,5 +1,75 @@ use super::*; +#[tokio::test] +async fn automatic_focus_fixup_preserves_navigation_reset_and_author_focus() { + for (change, before, on_blur, expected) in [ + ("a.disabled=true", "", "", "b"), + ("group.disabled=true", "", "", "b"), + ("a.hidden=true", "", "", "b"), + ("group.inert=true", "", "", "b"), + ("a.disabled=true", "", "c.focus()", "c"), + ("a.disabled=true", "", "c.focus();c.blur()", "BODY"), + ("a.disabled=true", "c.focus()", "", "c"), + ] { + let loader = static_http_loader([]); + let mut vm = new_storage_page_task_executor_test_vm_with_loader( + "https://navigation-focus-fixup.test/", + &loader, + ); + vm.eval(r#" + const root=document.documentElement||document.appendChild(document.createElement('html')); + const body=document.body||root.appendChild(document.createElement('body')); + body.innerHTML='
'; + const a=document.getElementById('a'),b=document.getElementById('b'),c=document.getElementById('c'),group=document.getElementById('group'); + a.focus(); + globalThis.fixupSetupReady=false; + requestAnimationFrame(()=>requestAnimationFrame(()=>{fixupSetupReady=true})); + "#).unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(fixupSetupReady)", + "true", + "consume ordinary autofocus while author focus is retained", + ) + .await; + assert_eq!(vm.eval("document.activeElement.id").unwrap(), "a"); + vm.eval(&format!(r##" + let release; + const held=new Promise(resolve=>{{release=resolve}}); + navigation.addEventListener('navigate',event=>event.intercept({{handler:()=>held}}),{{once:true}}); + globalThis.fixupNavigationDone=false; + globalThis.fixupNavigationResult='pending'; + globalThis.fixupBlurCount=0; + navigation.navigate('#next').finished.then(()=>{{ + fixupNavigationResult=document.activeElement.id||document.activeElement.tagName; + fixupNavigationDone=true; + }}); + a.addEventListener('blur',()=>{{ + fixupBlurCount++; + {on_blur}; + setTimeout(release,0); + }},{{once:true}}); + {before}; + {change}; + "##)).unwrap(); + advance_page_task_executor_until_eval_equals( + &mut vm, + &loader, + "String(fixupNavigationDone)", + "true", + "finish the intercepted navigation after focus fixup and its blur handlers", + ) + .await; + assert_eq!(vm.eval("String(fixupBlurCount)").unwrap(), "1"); + assert_eq!( + vm.eval("fixupNavigationResult").unwrap(), + expected, + "change={change}; before={before}; on_blur={on_blur}", + ); + } +} + #[tokio::test] async fn removal_reenables_navigation_focus_reset_until_another_focus_change() { for (action, expected) in [