Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(layout): default unpublished document client size to viewport

ldm0aaf76669ff2c
diff --git a/moli-cdp-smoke/README.md b/moli-cdp-smoke/README.md
index 66464fb7f..1bc566d4e 100644
--- a/moli-cdp-smoke/README.md
+++ b/moli-cdp-smoke/README.md
@@ -249,14 +249,15 @@ Covered well:
asynchronous request cancellation, and suppression of asynchronous events and
timers while a synchronous request owns the main thread.
- The focused `layout-screenshot` group drives a real raw WebSocket session through target creation/attachment, fixed viewport, lifecycle-gated navigation, DevTools-style PNG capture (`quality: 100`), paint/layout mutations, page clips, `captureBeyondViewport`, and the Chromium DevTools node-screenshot chain (`DOM.getBoxModel` + `Page.getLayoutMetrics` + page clip). Its Moli-only TreeScope fixture also captures 104 open/closed Shadow Roots, nested roots, and 24 roots in an iframe twice, requiring stable pixels and completion within the protocol timeout. The group covers Moli's generation-gated 1 FPS JPEG screencast as well: initial-frame delivery, clean-state frame suppression without ACK backpressure, 400x300 scaling, metadata/session routing, mutation freshness, stop cleanup, and a separately restarted default-Mock boundary without `--layout`. The screenshot surface sequence can also run against Chromium as a coarse reference; the TreeScope, fixed-1-FPS, and default-Mock branches are Moli-only.
-- The default `playwright-compat` group takes viewport and clipped screenshots
- through `page.screenshot()` on fresh pages, without a raw CDP capture first.
+- The default `playwright-compat` group takes viewport, clipped, and full-page
+ screenshots through `page.screenshot()` on fresh pages, without a raw CDP capture first.
This covers Playwright's `Page.getLayoutMetrics` preflight before layout has
been published. DPR/resize and concurrent-page screenshot cases likewise use
Playwright directly, so a fixture cannot hide a broken first screenshot.
- A first `fullPage: true` screenshot still requires published DOM geometry:
- Playwright reads the document's scroll/offset/client dimensions before its
- metrics request, and those dimensions remain zero until layout is published.
+ In Moli, before the first layout publication, the document's client dimensions default
+ to the configured viewport, so a first `fullPage: true` capture covers one
+ viewport. The next full-page capture uses the published content dimensions;
+ the smoke test checks both captures on a page taller than the viewport.
- The default raw `action-window` group holds Moli's on-demand input policy at
the public CDP boundary. Three acknowledged `Input.dispatchMouseEvent`
wheel commands remain delayed until one fixed one-second deadline, preserve
diff --git a/moli-cdp-smoke/moli_cdp_smoke/groups/playwright_compat.py b/moli-cdp-smoke/moli_cdp_smoke/groups/playwright_compat.py
index eccfc73eb..cdc9ec7c4 100644
--- a/moli-cdp-smoke/moli_cdp_smoke/groups/playwright_compat.py
+++ b/moli-cdp-smoke/moli_cdp_smoke/groups/playwright_compat.py
@@ -35,6 +35,12 @@ async def _verify_playwright_first_screenshot(state: SmokeState) -> None:
for name, options, size in [
("viewport", {}, (320, 240)),
("clip", {"clip": {"x": 10, "y": 15, "width": 60, "height": 50}}, (60, 50)),
+ ("full-page", {"full_page": True}, (320, 240)),
+ (
+ "full-page-clip",
+ {"full_page": True, "clip": {"x": 10, "y": 15, "width": 60, "height": 50}},
+ (60, 50),
+ ),
]:
page = await state.context.new_page()
try:
@@ -48,9 +54,22 @@ async def _verify_playwright_first_screenshot(state: SmokeState) -> None:
image = decode_png(await page.screenshot(timeout=10_000, **options))
assert_equal((image.width, image.height), size, f"first Playwright {name} screenshot size")
assert_equal(image.pixel(5, 5), (20, 30, 40, 255), f"first Playwright {name} screenshot pixel")
+ if name == "full-page":
+ # The first capture publishes layout. The next full-page
+ # request must use the real content extent, not the fallback.
+ full_image = decode_png(await page.screenshot(full_page=True, timeout=10_000))
+ assert_equal(full_image.height, 900, "published Playwright full-page screenshot height")
+ assert_equal(
+ full_image.pixel(5, 880),
+ (20, 30, 40, 255),
+ "published Playwright full-page bottom pixel",
+ )
finally:
await page.close()
- state.record("playwright_first_screenshot_without_published_layout", {"modes": ["viewport", "clip"]})
+ state.record(
+ "playwright_first_screenshot_without_published_layout",
+ {"modes": ["viewport", "clip", "full-page", "full-page-clip"]},
+ )
async def _with_fresh_page(state: SmokeState, body: Callable[[Any, Any], Awaitable[None]]) -> None:
diff --git a/moli-renderer-v8/src/native_bridge/element/geometry/metrics.rs b/moli-renderer-v8/src/native_bridge/element/geometry/metrics.rs
index 82143c2ba..5e09f7d36 100644
--- a/moli-renderer-v8/src/native_bridge/element/geometry/metrics.rs
+++ b/moli-renderer-v8/src/native_bridge/element/geometry/metrics.rs
@@ -256,11 +256,14 @@ fn node_box_metric_from_object<'s>(
metric: &str,
) -> Result<i32, moli_layout::LayoutError> {
if let Ok((runtime_ptr, handle)) = node_runtime_and_handle_from_object(scope, object) {
- let metrics = read_element_metrics(unsafe { &*runtime_ptr }, handle)?;
+ let runtime = unsafe { &*runtime_ptr };
+ let metrics = read_element_metrics(runtime, handle)?;
return Ok(metrics
.as_ref()
.map(|metrics| layout_box_metric(metrics, metric))
- .unwrap_or(0));
+ .unwrap_or_else(|| {
+ unpublished_document_client_metric(runtime, handle, metric).unwrap_or(0)
+ }));
}
let Ok((runtime_ptr, handle)) = node_runtime_and_handle_from_object_or_detached(scope, object)
else {
@@ -280,6 +283,44 @@ fn node_box_metric_from_object<'s>(
}
}
+fn unpublished_document_client_metric(
+ runtime: &JsContextHost,
+ handle: DomHandle,
+ metric: &str,
+) -> Option<i32> {
+ if !matches!(metric, "clientWidth" | "clientHeight")
+ || !runtime.layout_policy().uses_real_layout()
+ || !runtime.dom_host().is_connected(handle)
+ {
+ return None;
+ }
+ let document = runtime.layout_document_for_source(handle)?;
+ if runtime
+ .dom_host()
+ .dom()
+ .document_element_handle_for_document(document)
+ != Some(handle)
+ || (document != runtime.document_handle()
+ && runtime
+ .child_browsing_context_host_for_document_handle(document)
+ .is_none())
+ || runtime
+ .with_latest_layout_tree_for_document(document, |_| ())
+ .is_some()
+ {
+ return None;
+ }
+ // Before the first visual publication, use the configured viewport as the
+ // document's client size. Content bounds and element boxes still need a
+ // snapshot; a published zero-sized box must not acquire this fallback.
+ let viewport = runtime.layout_viewport_for_document(document);
+ Some(rounded_layout_value(if metric == "clientWidth" {
+ viewport.css_width as f32
+ } else {
+ viewport.css_height as f32
+ }))
+}
+
fn layout_box_metric(metrics: &moli_layout::LayoutElementMetrics<DomHandle>, metric: &str) -> i32 {
rounded_layout_value(match metric {
"clientWidth" => metrics.client_size.width,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index 8de1c77bb..4fe88bccb 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -1382,6 +1382,58 @@ fn painted_overlay_wins_over_scrollbar_and_corner_consumes_input() {
);
}
+#[test]
+fn document_client_size_defaults_to_viewport_without_publishing_layout() {
+ let mut vm = new_parsed_test_vm(
+ "https://document-client-viewport.test/",
+ "<!doctype html><style>html,body{margin:0}main{width:80px;height:900px}</style><main></main>",
+ );
+ let initial_passes = vm.layout_pass_observability_for_test().1;
+ let query = r#"JSON.stringify([
+ document.documentElement.clientWidth, document.documentElement.clientHeight,
+ document.documentElement.offsetWidth, document.documentElement.scrollHeight,
+ document.body.clientWidth, document.querySelector('main').clientHeight,
+ document.querySelector('main').getBoundingClientRect().width
+ ])"#;
+ for (width, height, dpr) in [(320, 240, 1.0), (480, 360, 2.0)] {
+ vm.set_viewport_surface(Some(crate::protocol_types::ViewportSurface {
+ inner_width: width,
+ inner_height: height,
+ device_pixel_ratio: dpr,
+ ..Default::default()
+ }))
+ .unwrap();
+ assert_eq!(
+ vm.eval(query).unwrap(),
+ format!("[{width},{height},0,0,0,0,0]")
+ );
+ assert_eq!(vm.layout_pass_observability_for_test().1, initial_passes);
+ }
+
+ publish_layout_for_test(&mut vm);
+ let published = vm.eval(query).unwrap();
+ let published_metrics: serde_json::Value = serde_json::from_str(&published).unwrap();
+ assert_eq!(published_metrics[0], 465); // The viewport now has a scrollbar.
+ assert_eq!(published_metrics[1], 360);
+ assert_eq!(published_metrics[3], 900);
+ assert_eq!(published_metrics[6], 80);
+ let published_passes = vm.layout_pass_observability_for_test().1;
+
+ vm.set_viewport_surface(Some(crate::protocol_types::ViewportSurface {
+ inner_width: 640,
+ inner_height: 480,
+ ..Default::default()
+ }))
+ .unwrap();
+ assert_eq!(vm.eval(query).unwrap(), published);
+ assert_eq!(vm.layout_pass_observability_for_test().1, published_passes);
+
+ vm.eval("document.documentElement.style.display = 'none'")
+ .unwrap();
+ publish_layout_for_test(&mut vm);
+ assert_eq!(vm.eval(query).unwrap(), "[0,0,0,0,0,0,0]");
+}
+
#[test]
fn body_overflow_defines_viewport_scrolling_and_default_root_stable_gutters() {
let mut vm = new_storage_test_vm("https://viewport-overflow-policy.test/");