fix(layout): adapt WPT input to explicit published snapshots

This commit is contained in:
ldm0
2026-11-24 14:34:55 +08:00
parent e5cf16f9a6
commit af3609a0ad
13 changed files with 129 additions and 24 deletions
+25
View File
@@ -148,6 +148,31 @@ impl<N> FrozenLayoutTree<N>
where
N: Copy + Debug + Eq + Hash,
{
/// Retains a published viewport for an embedded Document without a root
/// element. The document identity is its source, and no box can receive
/// input or provide element geometry until a later layout is published.
pub fn empty_viewport(source_root: N, viewport: LayoutViewport) -> Self {
Self::new(
source_root,
viewport,
LayoutPoint::ZERO,
LayoutSize::new(viewport.css_width as f32, viewport.css_height as f32),
LayoutOutputBoxId::from_index(0),
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
FrozenCoordinateSpace {
owner: None,
local_to_viewport: super::model::LayoutTransform2D::IDENTITY,
local_to_viewport_ignoring_css_transforms:
super::model::LayoutTransform2D::IDENTITY,
},
Vec::new(),
Vec::new(),
)
}
/// Source root whose complete layout projection this member tree owns.
pub const fn source_root(&self) -> N {
self.source_root
@@ -14,6 +14,7 @@ async fn activate(
))
.await;
if matches!(mode, "trusted" | "location" | "nested-click") {
page.command("Page.captureScreenshot", json!({})).await;
for (kind, buttons) in [("mousePressed", 1), ("mouseReleased", 0)] {
page.command("Input.dispatchMouseEvent", json!({
"type": kind, "x": 20, "y": 20, "button": "left", "buttons": buttons, "clickCount": 1,
@@ -162,6 +162,7 @@ async fn navigation_focus_reset_respects_document_policy_and_manual_focus() {
.await;
// Establish the parent's focus with real input even though its
// policy also denies script-initiated focus without activation.
page.command("Page.captureScreenshot", json!({})).await;
for (kind, buttons) in [("mousePressed", 1), ("mouseReleased", 0)] {
page.command("Input.dispatchMouseEvent", json!({
"type": kind, "x": 310, "y": 10, "button": "left", "buttons": buttons, "clickCount": 1,
@@ -201,6 +202,7 @@ async fn navigation_focus_reset_preserves_trusted_initiation_after_activation_ex
"{PROBE}\nsetupNavigationFocusProbe(true, \"'none'\")"
))
.await;
page.command("Page.captureScreenshot", json!({})).await;
for (kind, buttons) in [("mousePressed", 1), ("mouseReleased", 0)] {
page.command("Input.dispatchMouseEvent", json!({
"type": kind, "x": 20, "y": 20, "button": "left", "buttons": buttons, "clickCount": 1,
+7 -1
View File
@@ -250,7 +250,13 @@ impl EmbeddedFrameRenderer<DomHandle> for NativeEmbeddedFrameRenderer<'_> {
.dom()
.document_element_handle_for_document(document)
else {
return Ok(None);
return Ok(Some(moli_layout::EmbeddedFrameSnapshot::new(
moli_layout::FrozenLayoutTree::empty_viewport(document, viewport),
self.capture_paint.then(|| {
moli_layout::PaintSnapshot::new(viewport, moli_layout::PaintColor::TRANSPARENT)
}),
Vec::new(),
)));
};
let mut services = self
.embedded_document_services
@@ -775,12 +775,13 @@ impl PageScriptHost {
let root = self
.dom_host()
.dom()
.document_element_handle_for_document(document);
.document_element_handle_for_document(document)
.unwrap_or(document);
let state = self.document_layout_state.borrow();
state
.latest_layout(document)
.or_else(|| root.and_then(|root| state.latest_layout_for_root(root)))
.filter(|tree| Some(tree.source_root()) == root)
.or_else(|| state.latest_layout_for_root(root))
.filter(|tree| tree.source_root() == root)
.map(inspect)
}
@@ -5918,12 +5918,7 @@ fn lightweight_popup_window_opener_getter<'s>(
return;
};
match unsafe { &*host_ptr }.lightweight_popup_opener_window(scope, popup_id) {
Some(opener) => rv.set(
super::child_frame_runtime::CrossOriginWindowAccessor::project_related_window(
scope,
opener.into(),
),
),
Some(opener) => rv.set(opener.into()),
None => rv.set_null(),
}
}
@@ -242,17 +242,17 @@ fn input_surface_hit_test_in_tree(
.dom_host()
.dom()
.document_element_handle_for_document(child_document);
let Some(child_root) = child_root else {
let child_tree = tree
.embedded_frame_tree(target)
.ok_or(LayoutError::NoLayoutSnapshot)?;
if child_root.unwrap_or(child_document) != child_tree.source_root() {
return Err(LayoutError::NoLayoutSnapshot);
}
if child_root.is_none() {
return Ok(InputSurfaceHit {
viewport_document: Some(child_document),
..InputSurfaceHit::default()
});
};
let child_tree = tree
.embedded_frame_tree(target)
.ok_or(LayoutError::NoLayoutSnapshot)?;
if child_root != child_tree.source_root() {
return Err(LayoutError::NoLayoutSnapshot);
}
let child_hit = input_surface_hit_test_in_tree(
runtime,
@@ -18,6 +18,7 @@ navigator.clipboard.writeText('seed');'ready'
}
fn activated_copy_eval(vm: &mut StandaloneScriptVmHarness, script: &str) -> String {
vm.publish_layout_for_test().unwrap();
vm._context_host
.borrow_mut()
.begin_protocol_user_gesture_activation();
@@ -351,6 +351,7 @@ fn close_watcher_mouse_and_touch_activate_at_their_respective_input_phases() {
setup_close_watcher_input_document(&mut vm);
vm.eval("record('first').oncancel = e => e.preventDefault()")
.unwrap();
vm.publish_layout_for_test().unwrap();
if touch {
vm.dispatch_touch_event_at_point(10.0, 11.0, "touchstart", false)
.unwrap();
@@ -15,7 +15,6 @@ fn viewport_input_vm() -> StandaloneScriptVmHarness {
..Default::default()
}))
.unwrap();
vm.force_fresh_layout_reads_for_test();
vm.eval(
r#"
if (!document.documentElement) document.appendChild(document.createElement('html'));
@@ -37,6 +36,7 @@ fn viewport_input_vm() -> StandaloneScriptVmHarness {
}
fn click_viewport(vm: &mut StandaloneScriptVmHarness, x: f64, y: f64) {
vm.publish_layout_for_test().unwrap();
for (event, button, buttons) in [("mousemove", -1, 0), ("mousedown", 0, 1), ("mouseup", 0, 0)] {
vm.dispatch_mouse_event_at_point(x, y, event, button, Some(buttons), 0.0, 0.0)
.unwrap();
@@ -240,6 +240,7 @@ fn viewport_root_input_wheel_keeps_child_target_and_chains_at_boundaries() {
)
.unwrap();
vm.eval(setup).unwrap();
vm.publish_layout_for_test().unwrap();
vm.dispatch_mouse_event_at_point(80.0, 80.0, "wheel", -1, Some(0), 0.0, 120.0)
.unwrap();
let actual = vm
@@ -256,3 +257,46 @@ fn viewport_root_input_wheel_keeps_child_target_and_chains_at_boundaries() {
);
}
}
#[test]
fn viewport_root_input_requires_republication_when_an_empty_child_gains_a_root() {
let mut vm = viewport_input_vm();
vm.eval(
r#"
globalThis.frame = document.createElement('iframe');
frame.style.cssText = 'position:fixed;left:40px;top:40px;width:200px;height:50px;border:4px solid black';
document.body.appendChild(frame);
frame.contentDocument.documentElement.remove();
recordViewportClicks(frame.contentWindow, 'child');
"#,
)
.unwrap();
click_viewport(&mut vm, 80.0, 80.0);
assert_eq!(vm.eval("viewportClicks.length").unwrap(), "0");
vm.eval(
r#"
const child = frame.contentDocument;
const root = child.createElement('html');
root.style.cssText = 'margin:0;padding:0;height:0';
child.appendChild(root);
"#,
)
.unwrap();
let before = vm.layout_pass_observability_for_test().1;
let error = vm
.dispatch_mouse_event_at_point(80.0, 80.0, "mousedown", 0, Some(1), 0.0, 0.0)
.unwrap_err();
assert_eq!(
error.downcast_ref::<moli_layout::LayoutError>(),
Some(&moli_layout::LayoutError::NoLayoutSnapshot)
);
assert_eq!(vm.layout_pass_observability_for_test().1, before);
assert_eq!(vm.pressed_mouse_buttons, 0);
assert!(vm.pending_mouse_press.is_none());
click_viewport(&mut vm, 80.0, 80.0);
assert_eq!(
vm.eval("JSON.stringify(viewportClicks)").unwrap(),
r#"[["child","HTML",36,36,true]]"#
);
}
@@ -660,6 +660,7 @@ html, body { margin: 0; }
<div id="under"></div><div id="container"><div id="target"></div></div>"#,
);
vm.publish_layout_for_test().unwrap();
let result = vm
.eval(
r#"
@@ -677,13 +678,32 @@ html, body { margin: 0; }
container.inert = false;
values.push(top() === target);
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
return values.join('|');
})()
"#,
)
.expect("dynamic inert point-query probe should evaluate");
assert_eq!(result, "true|under|under,html|true");
vm.eval(
r#"
globalThis.svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('style', 'position:fixed;inset:0;width:100px;height:100px');
const rect = document.createElementNS(svg.namespaceURI, 'rect');
rect.setAttribute('width', '100');
rect.setAttribute('height', '100');
svg.appendChild(rect);
document.body.appendChild(svg);
"#,
)
.unwrap();
vm.publish_layout_for_test().unwrap();
let result = vm
.eval(
r#"(() => {
const top = () => document.elementFromPoint(1, 1);
const target = document.getElementById('target');
const values = [];
const svgHit = top();
svg.setAttribute('inert', '');
values.push(top() === svgHit);
@@ -699,7 +719,7 @@ html, body { margin: 0; }
)
.expect("dynamic inert point-query probe should evaluate");
assert_eq!(result, "true|under|under,html|true|true|true");
assert_eq!(result, "true|true");
}
#[test]
@@ -1926,6 +1926,8 @@ fn dom_rect_list_has_readonly_indexed_semantics_and_rejects_structured_clone() {
"<!doctype html><body><div id='node'>text</div></body>",
);
vm.publish_layout_for_test().unwrap();
let result = vm
.eval(
r#"
@@ -154,7 +154,7 @@ fn native_element_click_initializes_layout_once() {
}
#[test]
fn native_element_click_refreshes_stale_layout_after_dom_and_style_changes() {
fn native_element_click_reads_explicitly_republished_layout_after_dom_and_style_changes() {
for mutation in [
"target.style.left='300px'",
"const veil=document.createElement('div');veil.style.cssText='position:fixed;inset:0;z-index:999';document.body.appendChild(veil)",
@@ -190,11 +190,18 @@ fn native_element_click_refreshes_stale_layout_after_dom_and_style_changes() {
"(()=>{{const target=document.getElementById('target');{mutation}}})()"
))
.expect("mutate live DOM/style before preparing another click");
let _ = vm.prepare_element_click(target);
assert_eq!(
vm.layout_pass_observability_for_test().1,
before + 1,
"click preparation must not publish changed DOM or style"
);
publish_layout_for_test(&mut vm);
let prepared = vm.prepare_element_click(target);
let prepared_passes = vm.layout_pass_observability_for_test().1;
assert!(
prepared_passes > before + 1,
"a changed DOM or style must refresh the shared geometry snapshot: {mutation}"
"explicit publication must refresh the shared geometry snapshot: {mutation}"
);
if mutation != "target.style.left='300px'" {
assert!(
@@ -242,8 +249,8 @@ fn native_element_click_refreshes_stale_layout_after_dom_and_style_changes() {
}
let passes = vm.layout_pass_observability_for_test().1 - before;
assert_eq!(
passes, 1,
"the entire mouse sequence must reuse one snapshot: {mutation}"
passes, 2,
"the entire mouse sequence must reuse the republished snapshot: {mutation}"
);
let events = vm.eval("JSON.stringify([clicks,events])").unwrap();
if let Some((existing_passes, existing_events)) = &reference {