fix(input): preserve native click state and capture release ordering

Keep shared native per-button presses valid through release callbacks,
retaining flat-tree targeting and synchronous DOM-removal invalidation.
Release capture after compatibility mouseup while preserving hover
reconciliation and the captured click target.

Correct custom reparent/reinsert expectations against preserved and fresh
Chromium observations, and cover removing only one chorded button target.
Remove the obsolete callback-layer release enum.

Validation: cargo fmt --all; full workspace/all-targets/all-features
clippy with -D warnings; nextest 22178 passed, 16 skipped, 32 threads,
zero retries. Chromium 145 reference: 240 cases and 2904 checks passed.
This commit is contained in:
ldm0
2026-11-21 20:03:33 +08:00
parent fd9291049b
commit c0a67bd510
5 changed files with 138 additions and 55 deletions
@@ -26,7 +26,7 @@ impl PageScriptHost {
self.mouse_input.presses.remove(&button);
}
pub(crate) fn clear_mouse_press(&mut self) {
pub(crate) fn clear_mouse_presses(&mut self) {
self.mouse_input.presses.clear();
}
@@ -1806,55 +1806,6 @@ impl ScriptVm {
mouse_down_allows_drag.set(false);
}
}
let captured_click_target = if event_name == "mouseup" {
unsafe { &*runtime_ptr }.pointer_capture_target_for_click(pointer_id)
} else {
None
};
if pointer_event_name == Some("pointerup") {
release_pointer_capture_after_pointer_end(
scope,
runtime_ptr,
pointer_id,
coordinates,
button,
buttons,
&pointer,
modifiers,
);
if captured_release {
let runtime = unsafe { &*runtime_ptr };
let released_hit = match input_surface_hit_test(
runtime,
runtime.document_handle(),
root_point,
false,
true,
) {
Ok(surface) => surface.input,
// Author callbacks can invalidate a published tree while
// this input is still dispatching. Retarget its saved
// native path without triggering an implicit render.
Err(moli_layout::LayoutError::NoLayoutSnapshot) => release_hit_path
.as_ref()
.and_then(|path| path.resolve(runtime))
.zip(hit)
.map(|(handle, hit)| crate::native_bridge::element::InputHit {
handle,
root_to_frame: hit.root_to_frame,
}),
Err(error) => return Err(error.into()),
};
hover_input.dispatch_capture_events(
scope,
runtime_ptr,
&mut pointer_hover,
&[],
released_hit,
);
released_mouse_hover = Some(pointer_hover.clone());
}
}
let suppress_current_mouse_event =
suppress_compat_mouse_events && can_suppress_compat_mouse_event(event_name);
let mouse_dispatch_handle = if pointer_event_name.is_some() {
@@ -1924,6 +1875,55 @@ impl ScriptVm {
}
}
}
let captured_click_target = if event_name == "mouseup" {
unsafe { &*runtime_ptr }.pointer_capture_target_for_click(pointer_id)
} else {
None
};
if pointer_event_name == Some("pointerup") {
release_pointer_capture_after_pointer_end(
scope,
runtime_ptr,
pointer_id,
coordinates,
button,
buttons,
&pointer,
modifiers,
);
if captured_release {
let runtime = unsafe { &*runtime_ptr };
let released_hit = match input_surface_hit_test(
runtime,
runtime.document_handle(),
root_point,
false,
true,
) {
Ok(surface) => surface.input,
// Author callbacks can invalidate a published tree while
// this input is still dispatching. Retarget its saved
// native path without triggering an implicit render.
Err(moli_layout::LayoutError::NoLayoutSnapshot) => release_hit_path
.as_ref()
.and_then(|path| path.resolve(runtime))
.zip(hit)
.map(|(handle, hit)| crate::native_bridge::element::InputHit {
handle,
root_to_frame: hit.root_to_frame,
}),
Err(error) => return Err(error.into()),
};
hover_input.dispatch_capture_events(
scope,
runtime_ptr,
&mut pointer_hover,
&[],
released_hit,
);
released_mouse_hover = Some(pointer_hover.clone());
}
}
if let Some((drag_start_handle, initial_data, start_coordinates)) = drag_start
&& !suppress_current_mouse_event
&& let Some(data_store) =
@@ -1993,7 +1993,7 @@ impl ScriptVm {
coordinates,
modifiers,
});
unsafe { &mut *runtime_ptr }.clear_mouse_press();
unsafe { &mut *runtime_ptr }.clear_mouse_presses();
let cancel_target = unsafe { &*runtime_ptr }
.active_pointer_capture_target(pointer_id)
.unwrap_or(pointer_dispatch_handle);
@@ -95,7 +95,16 @@ globalThis.__installClickTargetProbe = function (mode, button, inChild) {
if (mode === 'release-before-up') parent.releasePointerCapture(activePointerId);
}, finish(pointerType) {
const activation = clicks.filter(e => e.target !== 'control');
check('one click-like event', activation.length, 1);
const invalidatedPress = ['mouseup-reparent-down', 'mouseup-reparent-both', 'same-up-reparent'].includes(mode);
check('click-like event count', activation.length, invalidatedPress ? 0 : 1);
if (invalidatedPress) {
check('moving a connected press suppresses all activation', clicks.length, 0);
check('press is connected under its new parent', source.parentNode === other && source.isConnected, true);
const releases = rows.filter(event => event.type === 'mouseup');
check('compatibility release still dispatches', releases.length, 1);
check('pointerup precedes the compatibility release',
rows.findIndex(event => event.type === 'pointerup') < rows.indexOf(releases[0]), true);
}
if (activation.length) {
const click = activation[0];
check('activation target', click.target, expectedTarget);
@@ -46,7 +46,7 @@ globalThis.__installCompatMouseTargetProbe = function (mode, trigger, captured,
}
const source = element('source', holder);
source.style.cssText = 'position:fixed;left:50px;top:50px;width:100px;height:100px;background:#acf';
const expected = mode === 'remove-target' || mode === 'remove-and-throw' || mode === 'poison-native-relations' || mode === 'adopt-detached-document' ? branch :
const expected = mode === 'remove-target' || mode === 'remove-and-throw' || mode === 'poison-native-relations' || mode === 'adopt-detached-document' || mode === 'reparent-same-document' || mode === 'remove-reinsert' ? branch :
mode === 'remove-parent' ? parent :
mode.startsWith('shadow-host-') ? branch :
mode.startsWith('shadow-child-') ? host :
@@ -110,7 +110,16 @@ globalThis.__installCompatMouseTargetProbe = function (mode, trigger, captured,
check('pointer event at the original target', pointerEvents.length, 1);
if (pointerEvents.length) check('original pointer target preserved', pointerEvents[0].matchedSource, true);
check('listener mutation ran', mutated, true);
check('one compatibility mouse event', mouse.length, 1);
check('compatibility mouse event count', mouse.length, mode === 'remove-document-element' ? 0 : 1);
if (mode === 'remove-document-element') {
check('removed document element suppresses compatibility propagation',
observations.filter(row => row.type === mouseType).length, 0);
}
if (mode === 'reparent-same-document' || mode === 'remove-reinsert') {
check('mutation reconnected the source', source.isConnected, true);
check('compatibility dispatch does not return to the reinserted source',
mouse.some(event => event.matchedSource), false);
}
if (mode.startsWith('slotted-target')) {
check('compatibility event stays outside the former slot tree',
observations.filter(row => row.type === mouseType &&
@@ -118,7 +127,7 @@ globalThis.__installCompatMouseTargetProbe = function (mode, trigger, captured,
}
if (mouse.length) {
const event = mouse[0];
check('compatibility target from original path', event.matchedExpected, true);
check('compatibility target after synchronous native removal', event.matchedExpected, true);
check('mouse button', event.button, 0);
check('mouse buttons', event.buttons, trigger === 'up' ? 0 : 1);
check('mouse coordinates', [event.clientX,event.clientY], trigger === 'down' ? [80,80] : [85,80]);
@@ -450,3 +450,68 @@ fn mouse_click_keeps_the_capture_target_when_release_listeners_move_or_remove_it
}
}
}
#[test]
fn removing_one_chorded_press_preserves_the_other_buttons_activation() {
for (removed_button, removed_mask, kept_button, kept_mask) in
[(0, 1, 1, 4), (1, 4, 0, 1), (2, 2, 0, 1), (0, 1, 2, 2)]
{
let mut vm = scan_link_vm();
vm.eval(
"window.activations = []; for (const type of ['click', 'auxclick']) document.addEventListener(type, event => { activations.push([event.type, event.target.id, event.button, event.buttons, event.isTrusted]); event.preventDefault(); }); document.addEventListener('contextmenu', event => event.preventDefault());",
).expect("record all chorded activations");
let dispatch = |vm: &mut StandaloneScriptVmHarness, x, event, button, buttons| {
vm.dispatch_mouse_event_at_point_with_pointer_and_modifiers(
x,
20.0,
event,
button,
Some(buttons),
1,
0.0,
0.0,
crate::runtime::RendererPointerEventProperties {
pointer_id: 17,
pointer_type: "mouse".to_owned(),
..Default::default()
},
0,
false,
)
.expect("chorded coordinate input should dispatch");
};
dispatch(&mut vm, 20.0, "mousedown", removed_button, removed_mask);
dispatch(
&mut vm,
100.0,
"mousedown",
kept_button,
removed_mask | kept_mask,
);
vm.eval("document.getElementById('left').remove()")
.expect("remove only one pressed subtree");
vm.publish_layout_for_test()
.expect("publish the surviving release target");
dispatch(&mut vm, 100.0, "mouseup", removed_button, kept_mask);
dispatch(&mut vm, 100.0, "mouseup", kept_button, 0);
dispatch(&mut vm, 100.0, "mouseup", kept_button, 0);
let actual = vm
.eval("JSON.stringify(activations)")
.expect("read chorded activations");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&actual).unwrap(),
serde_json::json!([[
if kept_button == 0 {
"click"
} else {
"auxclick"
},
"right",
kept_button,
0,
true,
]]),
"removed button {removed_button}, surviving button {kept_button}"
);
}
}