Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions browser/canvas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package browser

import "github.com/go-rod/rod"

func snapshotHTML(page *rod.Page) (string, error) {
if err := snapshotCanvasPixels(page); err != nil {
return "", err
}
return page.HTML()
}

func snapshotCanvasPixels(page *rod.Page) error {
_, err := page.Eval(`() => {
const maxCanvasSnapshotPixels = 4096 * 4096;
for (const canvas of document.querySelectorAll("canvas")) {
try {
if (canvas.width === 0 || canvas.height === 0) continue;
if (canvas.width * canvas.height > maxCanvasSnapshotPixels) continue;
const src = canvas.toDataURL("image/png");
if (!src.startsWith("data:image/png")) continue;
const img = document.createElement("img");
for (const attr of canvas.attributes) img.setAttribute(attr.name, attr.value);
img.setAttribute("src", src);
if (!img.hasAttribute("alt")) img.setAttribute("alt", "");
if (!img.hasAttribute("width")) img.setAttribute("width", String(canvas.width));
if (!img.hasAttribute("height")) img.setAttribute("height", String(canvas.height));
const rect = canvas.getBoundingClientRect();
if (!img.style.width && rect.width > 0) img.style.width = rect.width + "px";
if (!img.style.height && rect.height > 0) img.style.height = rect.height + "px";
canvas.replaceWith(img);
} catch (_) {}
}
}`)
return err
}
2 changes: 1 addition & 1 deletion browser/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (p *Pool) Render(ctx context.Context, rawURL string) (RenderResult, error)
settle(page, p.opts.Settle)
}

html, err := page.HTML()
html, err := snapshotHTML(page)
if err != nil {
return RenderResult{}, fmt.Errorf("serialise %s: %w", rawURL, err)
}
Expand Down
54 changes: 54 additions & 0 deletions browser/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package browser

import (
"context"
"encoding/base64"
"errors"
"image/png"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -126,6 +128,58 @@ func TestRenderCapturesFinalDOM(t *testing.T) {
}
}

func TestRenderPreservesCanvasPixels(t *testing.T) {
if testing.Short() {
t.Skip("render test drives Chrome; skipped under -short")
}
if _, ok := LookChrome(); !ok {
t.Skip("no Chrome/Chromium found; skipping render test")
}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte(`<!doctype html><html><body>
<canvas id="graph" width="32" height="16"></canvas>
<script>
const ctx = document.getElementById("graph").getContext("2d");
ctx.fillStyle = "#ff0000";
ctx.fillRect(0, 0, 32, 16);
</script>
</body></html>`))
}))
defer srv.Close()

p := New(Options{Headless: true, Workers: 1, Settle: 300 * time.Millisecond, RenderTimeout: 20 * time.Second})
defer func() { _ = p.Close() }()

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

res, err := p.Render(ctx, srv.URL)
if err != nil {
t.Fatalf("render: %v", err)
}
if !strings.Contains(res.HTML, `src="data:image/png;base64,`) {
t.Fatalf("rendered HTML does not include a canvas PNG snapshot:\n%s", res.HTML)
}
data, ok := strings.CutPrefix(res.HTML[strings.Index(res.HTML, `src="data:image/png;base64,`):], `src="data:image/png;base64,`)
if !ok {
t.Fatalf("rendered HTML does not include a canvas PNG snapshot:\n%s", res.HTML)
}
encoded, _, ok := strings.Cut(data, `"`)
if !ok {
t.Fatalf("canvas PNG snapshot is not quoted correctly:\n%s", res.HTML)
}
img, err := png.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(encoded)))
if err != nil {
t.Fatalf("decode canvas PNG snapshot: %v", err)
}
r, g, b, a := img.At(0, 0).RGBA()
if r < 0xff00 || g > 0x0100 || b > 0x0100 || a < 0xff00 {
t.Fatalf("canvas PNG first pixel = rgba(%#x, %#x, %#x, %#x); want opaque red", r, g, b, a)
}
}

func TestIsHTML(t *testing.T) {
cases := []struct {
ct string
Expand Down
3 changes: 2 additions & 1 deletion pack/appdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"image/png"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestBuildAppDir(t *testing.T) {
if string(tr[:8]) != trailerMagic {
t.Error("AppRun missing the trailer")
}
if info, _ := os.Stat(apprun); info.Mode().Perm()&0o111 == 0 {
if info, _ := os.Stat(apprun); runtime.GOOS != "windows" && info.Mode().Perm()&0o111 == 0 {
t.Error("AppRun is not executable")
}

Expand Down