Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

refactor: the scale-0 clip rule is enforced by every producer instead of the one consumer that needs it (PIN-142)

Luigi Agosti6a223c6b7678
diff --git a/internal/bridge/screenshot.go b/internal/bridge/screenshot.go
index 052f065c..59844d3c 100644
--- a/internal/bridge/screenshot.go
+++ b/internal/bridge/screenshot.go
@@ -110,13 +110,10 @@ type ScreenshotOpts struct {
func scaledScreenshotClip(opts ScreenshotOpts, viewportWidth, viewportHeight, documentWidth, documentHeight float64) *page.Viewport {
if opts.Scale <= 0 || opts.Scale == 1 {
- return opts.Clip
+ return cdptk.NormalizedViewport(opts.Clip)
}
if opts.Clip != nil {
- clip := *opts.Clip
- if clip.Scale == 0 {
- clip.Scale = 1
- }
+ clip := *cdptk.NormalizedViewport(opts.Clip)
clip.Scale *= opts.Scale
return &clip
}
@@ -172,22 +169,19 @@ func captureScreenshotWithoutActivation(ctx context.Context, shot *page.CaptureS
// top-level capture call has no scale parameter outside of clip, so this is
// the only way to apply a render-time rescale.
func CaptureScreenshot(ctx context.Context, opts ScreenshotOpts) ([]byte, error) {
- clip := opts.Clip
- if opts.Scale > 0 && opts.Scale != 1 {
- // Known issue: two back-to-back /capture?scale=<n!=1> on the same
- // tab without nav between can hang on the second call. Workaround:
- // nav between captures (see e2e cli/capture-basic.sh).
- viewportWidth, viewportHeight := opts.ViewportWidth, opts.ViewportHeight
- documentWidth, documentHeight := 0.0, 0.0
- if clip == nil {
- if opts.BeyondViewport {
- documentWidth, documentHeight = fetchDocumentSize(ctx)
- } else if viewportWidth == 0 || viewportHeight == 0 {
- viewportWidth, viewportHeight = fetchViewportSize(ctx)
- }
+ // Known issue: two back-to-back /capture?scale=<n!=1> on the same
+ // tab without nav between can hang on the second call. Workaround:
+ // nav between captures (see e2e cli/capture-basic.sh).
+ viewportWidth, viewportHeight := opts.ViewportWidth, opts.ViewportHeight
+ documentWidth, documentHeight := 0.0, 0.0
+ if opts.Scale > 0 && opts.Scale != 1 && opts.Clip == nil {
+ if opts.BeyondViewport {
+ documentWidth, documentHeight = fetchDocumentSize(ctx)
+ } else if viewportWidth == 0 || viewportHeight == 0 {
+ viewportWidth, viewportHeight = fetchViewportSize(ctx)
}
- clip = scaledScreenshotClip(opts, viewportWidth, viewportHeight, documentWidth, documentHeight)
}
+ clip := scaledScreenshotClip(opts, viewportWidth, viewportHeight, documentWidth, documentHeight)
fromSurface := cdptk.CaptureFromSurface(opts.BeyondViewport, clip)
diff --git a/internal/bridge/screenshot_test.go b/internal/bridge/screenshot_test.go
index 7e46ab94..b11edd6e 100644
--- a/internal/bridge/screenshot_test.go
+++ b/internal/bridge/screenshot_test.go
@@ -9,6 +9,7 @@ import (
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
+ "github.com/pinchtab/pinchtab/internal/cdptk"
)
type screenshotExecutor struct {
@@ -174,3 +175,38 @@ func TestScaledScreenshotClip(t *testing.T) {
}
})
}
+
+// CDP discards a scale-0 clip and returns the whole viewport, no error. The chrome path
+// once shipped exactly that defect, so every path a clip takes to CDP must leave the
+// conversion with a non-zero Scale — including the no-rescale early return, which used to
+// hand the caller's clip through verbatim.
+func TestEveryCapturePathNormalisesAZeroScaleClip(t *testing.T) {
+ unset := func() *page.Viewport {
+ return &page.Viewport{X: 40, Y: 60, Width: 120, Height: 60}
+ }
+ for _, tc := range []struct {
+ name string
+ clip *page.Viewport
+ optsScale float64
+ wantScale float64
+ }{
+ {name: "no rescale requested", clip: unset(), optsScale: 0, wantScale: 1},
+ {name: "explicit native rescale", clip: unset(), optsScale: 1, wantScale: 1},
+ {name: "real rescale multiplies the native default", clip: unset(), optsScale: 2, wantScale: 2},
+ {name: "real rescale multiplies an explicit scale", clip: &page.Viewport{X: 40, Y: 60, Width: 120, Height: 60, Scale: 1.5}, optsScale: 2, wantScale: 3},
+ {name: "Bridge.CaptureScreenshot converts through cdptk first", clip: cdptk.ClipViewport(&cdptk.ScreenshotClip{X: 40, Y: 60, Width: 120, Height: 60}), optsScale: 0, wantScale: 1},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got := scaledScreenshotClip(ScreenshotOpts{Clip: tc.clip, Scale: tc.optsScale}, 0, 0, 0, 0)
+ if got == nil {
+ t.Fatal("expected a clip")
+ }
+ if got.Scale != tc.wantScale {
+ t.Fatalf("scale = %v, want %v — a scale-0 clip reaching CDP is silently discarded", got.Scale, tc.wantScale)
+ }
+ if got.X != 40 || got.Y != 60 || got.Width != 120 || got.Height != 60 {
+ t.Fatalf("geometry changed: %+v", got)
+ }
+ })
+ }
+}
diff --git a/internal/cdptk/screenshot.go b/internal/cdptk/screenshot.go
index 1b2b2d92..44f6b01c 100644
--- a/internal/cdptk/screenshot.go
+++ b/internal/cdptk/screenshot.go
@@ -141,27 +141,32 @@ func CaptureFromSurface(beyondViewport bool, clip *page.Viewport) bool {
return beyondViewport || clip != nil
}
-// ClipViewport converts a ScreenshotClip into the page.Viewport CDP takes, and is the one
-// place the non-zero-scale rule is applied. A nil clip converts to nil, so callers keep
-// the nil-clip fast path CaptureFromSurface describes.
-//
-// Scale 0 means native — the contract ScreenshotOpts states and normalises. CDP does not:
-// it discards a scale-0 clip and returns the whole viewport, no error, exactly as it
-// discards a clip passed with fromSurface=false. Same symptom, same silence, so the two
-// rules belong to the same conversion rather than to whichever call site remembered.
+// NormalizedViewport owns the non-zero-scale rule for a clip already in CDP's own type.
+// Scale 0 means native. CDP does not agree: it discards a scale-0 clip and returns the
+// whole viewport, no error, exactly as it discards a clip passed with fromSurface=false.
+// Same symptom, same silence, so the rule is applied where the value meets CDP rather
+// than at whichever producer remembered. A nil viewport stays nil, keeping the nil-clip
+// fast path CaptureFromSurface describes.
+func NormalizedViewport(vp *page.Viewport) *page.Viewport {
+ if vp == nil || vp.Scale != 0 {
+ return vp
+ }
+ out := *vp
+ out.Scale = 1
+ return &out
+}
+
+// ClipViewport converts a ScreenshotClip into the page.Viewport CDP takes, routed
+// through NormalizedViewport so no conversion path can hand CDP a scale-0 clip.
func ClipViewport(clip *ScreenshotClip) *page.Viewport {
if clip == nil {
return nil
}
- scale := clip.Scale
- if scale == 0 {
- scale = 1
- }
- return &page.Viewport{
+ return NormalizedViewport(&page.Viewport{
X: clip.X,
Y: clip.Y,
Width: clip.Width,
Height: clip.Height,
- Scale: scale,
- }
+ Scale: clip.Scale,
+ })
}