mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 00:02:21 +00:00
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.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -9,7 +9,7 @@ pub(super) struct DocumentFocusChangeState {
|
||||
focused_area: Option<DomHandle>,
|
||||
sequential_starting_point: Option<SequentialFocusStartingPoint>,
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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='<fieldset id=group><button id=a autofocus></button></fieldset><button id=b autofocus></button><button id=c></button>';
|
||||
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 [
|
||||
|
||||
Reference in New Issue
Block a user