oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "io/fs"
5 "net/http"
6 "net/http/httptest"
7 "os"
8 "path"
9 "path/filepath"
10 "regexp"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/estilo.git/theme"
15)
16
17// The chrome is set in the system UI face and downloads no font, so it
18// can never flash (Paul, 2026-10-25: "There should never be a flash of
19// unstyled text. Ever." estilo AGENTS.md row 9). No page links or
20// preloads a webfont: not estilo's retired Onest, and no font file at
21// all. estilo's v1 files stay mounted (estilo.go) so a page cached before
22// this still resolves its stylesheet; nothing may name them.
23//
24// This replaces TestOnestIsLinkedAndServed, which required the link.
25func TestNoPageLinksOrPreloadsAChromeWebfont(t *testing.T) {
26 // Every template, as source: whichever route renders a page, it is
27 // made of these.
28 pages := 0
29 root := "templates"
30 err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
31 if err != nil || d.IsDir() || filepath.Ext(p) != ".html" {
32 return err
33 }
34 b, err := os.ReadFile(p)
35 if err != nil {
36 return err
37 }
38 pages++
39 for _, why := range chromeWebfonts(string(b)) {
40 t.Errorf("%s %s", p, why)
41 }
42 return nil
43 })
44 if err != nil {
45 t.Fatal(err)
46 }
47 if pages < 10 {
48 t.Fatalf("read only %d templates under %s; the scan would pass vacuously", pages, root)
49 }
50
51 // And as served, on the page every visitor meets first.
52 app, _ := appWithRecorder(t)
53 rec := do(t, app, httptest.NewRequest(http.MethodGet, "/signin", nil))
54 if rec.Code != http.StatusOK {
55 t.Fatalf("GET /signin = %d", rec.Code)
56 }
57 for _, why := range chromeWebfonts(rec.Body.String()) {
58 t.Errorf("GET /signin %s", why)
59 }
60
61 // The retired files still answer, for pages cached before the change.
62 for _, name := range []string{"fonts.css", "onest-latin.woff2"} {
63 if rec := do(t, app, httptest.NewRequest(http.MethodGet, theme.FontPrefix+name, nil)); rec.Code != http.StatusOK {
64 t.Errorf("GET %s%s = %d; keep estilo's font handler mounted for old pages", theme.FontPrefix, name, rec.Code)
65 }
66 }
67}
68
69var (
70 linkTag = regexp.MustCompile(`(?is)<link\b[^>]*>`)
71 asFont = regexp.MustCompile(`(?i)\bas\s*=\s*["']?font\b`)
72 fontFile = regexp.MustCompile(`(?i)\.(woff2?|ttf|otf|eot)\b`)
73)
74
75// chromeWebfonts names each way html would download a font for the
76// chrome: estilo's font path at any version, a <link> that preloads a
77// font or names a font file, or a face declared inline.
78func chromeWebfonts(html string) []string {
79 var found []string
80 if dir := path.Dir(strings.TrimSuffix(theme.FontPrefix, "/")) + "/"; strings.Contains(html, dir) {
81 found = append(found, "names estilo's webfont path "+dir)
82 }
83 for _, tag := range linkTag.FindAllString(html, -1) {
84 if asFont.MatchString(tag) || fontFile.MatchString(tag) {
85 found = append(found, "links or preloads a font: "+tag)
86 }
87 }
88 if strings.Contains(strings.ToLower(html), "@font-face") {
89 found = append(found, "declares a font face inline")
90 }
91 return found
92}
93