Skip to content

Lightpanda: the headless browser designed for AI and automation

Always honor screenshot clip

Karl Seguine2c16be63aad
diff --git a/src/browser/screenshot.zig b/src/browser/screenshot.zig
index 394b4c3f5..f0b067057 100644
--- a/src/browser/screenshot.zig
+++ b/src/browser/screenshot.zig
@@ -693,6 +693,38 @@ test "browser.screenshot: fixed height, clip and scale" {
try testing.expectEqual(40, std.mem.readInt(u32, aw.written()[20..24], .big));
}
+test "browser.screenshot: a clip past the viewport extends the strip" {
+ defer testing.test_session.closeAllPages();
+ const frame = try testing.createFrame();
+ frame.url = "http://localhost/";
+ const doc = frame.window._document;
+ const div = try doc.createElement("div", null, frame);
+ try Frame.parse.htmlAsChildren(frame, div.asNode(), "<p>one</p><p>two</p><p>three</p><p>four</p><p>five</p><p>six</p>");
+
+ const full = try contentHeight(div.asNode(), 300, frame);
+ try testing.expectEqual(true, full > 100);
+
+ var aw: std.Io.Writer.Allocating = .init(testing.allocator);
+ defer aw.deinit();
+ _ = try png(div.asNode(), .{
+ .width = 300,
+ .height = 100,
+ .clip = .{ .x = 0, .y = 0, .width = 300, .height = @floatFromInt(full) },
+ }, &aw.writer, frame);
+ try testing.expectEqual(300, std.mem.readInt(u32, aw.written()[16..20], .big));
+ try testing.expectEqual(full, std.mem.readInt(u32, aw.written()[20..24], .big));
+
+ // Never past the content, so an absurd probe resolves to the full page
+ // instead of a 1e8-tall raster.
+ aw.clearRetainingCapacity();
+ _ = try png(div.asNode(), .{
+ .width = 300,
+ .height = 100,
+ .clip = .{ .x = 0, .y = 0, .width = 300, .height = 1e8 },
+ }, &aw.writer, frame);
+ try testing.expectEqual(full, std.mem.readInt(u32, aw.written()[20..24], .big));
+}
+
test "browser.screenshot: raster is bounded" {
defer testing.test_session.closeAllPages();
const frame = try testing.createFrame();
diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rs
index 97256fe38..d65cdaaf2 100644
--- a/src/rust/render/lib.rs
+++ b/src/rust/render/lib.rs
@@ -432,12 +432,14 @@ impl Renderer {
}
// Rasterize the requested strip (viewport or full page), then crop.
- // A clip never extends the strip: Chrome intersects it with the page
- // too, and callers probe with absurd heights (Playwright's fullPage
- // clip is 1e8 tall).
- // Width and height are both client-controlled (viewport, clip scale),
- // so bound the buffer.
- let out_h = if opts.height == 0 { content_h } else { opts.height };
+ // A clip reaching past the strip extends it, but never past the
+ // content.
+ let mut out_h = if opts.height == 0 { content_h } else { opts.height };
+ if opts.clip_w > 0.0 {
+ let reach = (opts.clip_y as f64 + opts.clip_h as f64).ceil();
+ // A NaN reach clamps to NaN and casts to 0, leaving out_h alone.
+ out_h = out_h.max(reach.clamp(0.0, content_h as f64) as u32);
+ }
let pw = ((opts.width as f64 * scale as f64).ceil() as u64).clamp(1, MAX_RASTER_DIM);
let ph = ((out_h as f64 * scale as f64).ceil() as u64)
.clamp(1, MAX_RASTER_DIM)