Skip to content

Commit 149796f

Browse files
committed
fix: screenshot api failing due to rounding errors
1 parent a4918e9 commit 149796f

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/phoenix/shell.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ async function _capturePageBinary(rectOrNodeOrSelector) {
300300
}
301301
const maxWidth = Math.ceil(window.innerWidth * boundsScale);
302302
const maxHeight = Math.ceil(window.innerHeight * boundsScale);
303-
if (rect.x + rect.width > maxWidth) {
304-
throw new Error("rect x + width exceeds window innerWidth");
305-
}
306-
if (rect.y + rect.height > maxHeight) {
307-
throw new Error("rect y + height exceeds window innerHeight");
303+
if (rect.x >= maxWidth || rect.y >= maxHeight) {
304+
throw new Error("rect origin leaves no capturable area");
308305
}
306+
307+
rect.width = Math.min(rect.width, maxWidth - rect.x);
308+
rect.height = Math.min(rect.height, maxHeight - rect.y);
309309
}
310310
if (window.__TAURI__) {
311311
const bytes = await window.__TAURI__.invoke('capture_page', { rect });

test/spec/Native-platform-test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,22 @@ define(function (require, exports, module) {
450450
).toBeRejectedWithError("rect width and height must be greater than 0");
451451
});
452452

453-
it("Should throw when rect exceeds window width bounds", async function () {
454-
await expectAsync(
455-
Phoenix.app.screenShotBinary({x: 0, y: 0, width: 999999, height: 100})
456-
).toBeRejectedWithError("rect x + width exceeds window innerWidth");
453+
it("Should clamp an oversize width rect and return a valid PNG", async function () {
454+
const bytes = await Phoenix.app.screenShotBinary({x: 0, y: 0, width: 999999, height: 100});
455+
expect(bytes instanceof Uint8Array).toBeTrue();
456+
expect(isPNG(bytes)).withContext("Result should be valid PNG data").toBeTrue();
457+
});
458+
459+
it("Should clamp an oversize height rect and return a valid PNG", async function () {
460+
const bytes = await Phoenix.app.screenShotBinary({x: 0, y: 0, width: 100, height: 999999});
461+
expect(bytes instanceof Uint8Array).toBeTrue();
462+
expect(isPNG(bytes)).withContext("Result should be valid PNG data").toBeTrue();
457463
});
458464

459-
it("Should throw when rect exceeds window height bounds", async function () {
465+
it("Should throw when rect origin leaves no capturable area", async function () {
460466
await expectAsync(
461-
Phoenix.app.screenShotBinary({x: 0, y: 0, width: 100, height: 999999})
462-
).toBeRejectedWithError("rect y + height exceeds window innerHeight");
467+
Phoenix.app.screenShotBinary({x: 999999, y: 0, width: 100, height: 100})
468+
).toBeRejectedWithError("rect origin leaves no capturable area");
463469
});
464470

465471
it("Should capture a screenshot of a DOM element", async function () {

0 commit comments

Comments
 (0)