From 149796fe0d20ab7638f2b96cf7494fb06f84bc05 Mon Sep 17 00:00:00 2001 From: Pluto Date: Thu, 28 May 2026 18:30:10 +0530 Subject: [PATCH] fix: screenshot api failing due to rounding errors --- src/phoenix/shell.js | 10 +++++----- test/spec/Native-platform-test.js | 20 +++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/phoenix/shell.js b/src/phoenix/shell.js index 36117b1af7..c85ca46241 100644 --- a/src/phoenix/shell.js +++ b/src/phoenix/shell.js @@ -300,12 +300,12 @@ async function _capturePageBinary(rectOrNodeOrSelector) { } const maxWidth = Math.ceil(window.innerWidth * boundsScale); const maxHeight = Math.ceil(window.innerHeight * boundsScale); - if (rect.x + rect.width > maxWidth) { - throw new Error("rect x + width exceeds window innerWidth"); - } - if (rect.y + rect.height > maxHeight) { - throw new Error("rect y + height exceeds window innerHeight"); + if (rect.x >= maxWidth || rect.y >= maxHeight) { + throw new Error("rect origin leaves no capturable area"); } + + rect.width = Math.min(rect.width, maxWidth - rect.x); + rect.height = Math.min(rect.height, maxHeight - rect.y); } if (window.__TAURI__) { const bytes = await window.__TAURI__.invoke('capture_page', { rect }); diff --git a/test/spec/Native-platform-test.js b/test/spec/Native-platform-test.js index 40c12c5ef4..683703dd17 100644 --- a/test/spec/Native-platform-test.js +++ b/test/spec/Native-platform-test.js @@ -450,16 +450,22 @@ define(function (require, exports, module) { ).toBeRejectedWithError("rect width and height must be greater than 0"); }); - it("Should throw when rect exceeds window width bounds", async function () { - await expectAsync( - Phoenix.app.screenShotBinary({x: 0, y: 0, width: 999999, height: 100}) - ).toBeRejectedWithError("rect x + width exceeds window innerWidth"); + it("Should clamp an oversize width rect and return a valid PNG", async function () { + const bytes = await Phoenix.app.screenShotBinary({x: 0, y: 0, width: 999999, height: 100}); + expect(bytes instanceof Uint8Array).toBeTrue(); + expect(isPNG(bytes)).withContext("Result should be valid PNG data").toBeTrue(); + }); + + it("Should clamp an oversize height rect and return a valid PNG", async function () { + const bytes = await Phoenix.app.screenShotBinary({x: 0, y: 0, width: 100, height: 999999}); + expect(bytes instanceof Uint8Array).toBeTrue(); + expect(isPNG(bytes)).withContext("Result should be valid PNG data").toBeTrue(); }); - it("Should throw when rect exceeds window height bounds", async function () { + it("Should throw when rect origin leaves no capturable area", async function () { await expectAsync( - Phoenix.app.screenShotBinary({x: 0, y: 0, width: 100, height: 999999}) - ).toBeRejectedWithError("rect y + height exceeds window innerHeight"); + Phoenix.app.screenShotBinary({x: 999999, y: 0, width: 100, height: 100}) + ).toBeRejectedWithError("rect origin leaves no capturable area"); }); it("Should capture a screenshot of a DOM element", async function () {