From 4ecc84bc9f0e336143939ceb475ca174d2a44828 Mon Sep 17 00:00:00 2001 From: BertMarti <107127310+BertMarti@users.noreply.github.com> Date: Mon, 26 Oct 2026 02:10:03 +0100 Subject: [PATCH 1/6] feat(web): selector de tema (Sistema/Claro/Oscuro) y Trusted Types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - theme.js en (sin parpadeo): data-theme, y theme-color forzados o restaurados; localStorage commitling:theme con lista cerrada (light, dark); «Sistema» borra la clave; todo con try/catch. - CSP: require-trusted-types-for 'script'; trusted-types commitling. La única política (generator.js) solo crea la URL de wasm_exec.js. - Tests: theme.test.js (Node), theme_test.go, escenarios de Trusted Types en generator.test.js y CSP. gallery.go/gallery_test.go incluyen ya la intro y el recuento de archivos de la especie cactus (commit siguiente). Interfaz hecha por GPT-5.6 Luna (delegado) y revisada y corregida por Claude (lectura de valores fuera de lista, test de almacenamiento); Trusted Types por Claude. Probado en Chromium: 0 violaciones. Co-Authored-By: Claude Opus 5.5 --- internal/gallery/gallery.go | 7 ++ internal/gallery/gallery_test.go | 17 ++-- internal/gallery/generator.js | 22 ++++- internal/gallery/generator_test.go | 9 +- internal/gallery/index.html.tmpl | 13 ++- internal/gallery/style.css | 9 +- internal/gallery/testdata/generator.test.js | 26 ++++++ internal/gallery/testdata/theme.test.js | 99 +++++++++++++++++++++ internal/gallery/theme.js | 92 +++++++++++++++++++ internal/gallery/theme_test.go | 49 ++++++++++ 10 files changed, 330 insertions(+), 13 deletions(-) create mode 100644 internal/gallery/testdata/theme.test.js create mode 100644 internal/gallery/theme.js create mode 100644 internal/gallery/theme_test.go diff --git a/internal/gallery/gallery.go b/internal/gallery/gallery.go index c542f7a..4cc0190 100644 --- a/internal/gallery/gallery.go +++ b/internal/gallery/gallery.go @@ -61,6 +61,9 @@ var generatorJS []byte //go:embed page.js var pageJS []byte +//go:embed theme.js +var themeJS []byte + // installJS is the workflow generator of «Instálalo en tu perfil»: plain // JavaScript, no wasm and no network (a test compares it with generate.Install). // @@ -202,6 +205,7 @@ func Build(dir, version string) (int, error) { intros := map[creature.Species]string{ creature.MossSprout: "La especie predeterminada: un brote de musgo con ojos que acaba siendo un árbol ancestral.", creature.Mushroom: "Una seta pequeña con ojos que empieza siendo una espora y acaba en un corro de setas. Se elige con species: mushroom.", + creature.Cactus: "Un cactus pequeño en su maceta que empieza siendo una semilla y acaba en un cactus centenario. Se elige con species: cactus.", } for _, sp := range creature.AllSpecies { // The default species keeps the file names of the first release. @@ -259,6 +263,9 @@ func Build(dir, version string) (int, error) { if err := write("page.js", pageJS); err != nil { return n, err } + if err := write("theme.js", themeJS); err != nil { + return n, err + } if err := write("install.js", installJS); err != nil { return n, err } diff --git a/internal/gallery/gallery_test.go b/internal/gallery/gallery_test.go index 7881f14..9ac58f6 100644 --- a/internal/gallery/gallery_test.go +++ b/internal/gallery/gallery_test.go @@ -37,10 +37,10 @@ func TestBuild(t *testing.T) { t.Fatal(err) } // Per species, 20 stage×mood cards and 4 accessory cards, light and - // dark, plus favicon, og.png, generator.js, page.js, install.js, style.css and index (the hero of the header is + // dark, plus favicon, og.png, generator.js, page.js, theme.js, install.js, style.css and index (the hero of the header is // one of the accessory cards), plus the compact badges: per species, one // per stage, light and dark. - if want := 2*(20+4)*2 + 7 + 2*5*2; n != want { + if want := 3*(20+4)*2 + 8 + 3*5*2; n != want { t.Fatalf("Build wrote %d files, want %d", n, want) } @@ -331,7 +331,7 @@ func TestPageColoursReachAA(t *testing.T) { } css := builtCSS(t) light := vars(regexp.MustCompile(`(?s):root\{(.*?)\}`).FindStringSubmatch(css)[1]) - darkOver := vars(regexp.MustCompile(`(?s)prefers-color-scheme:dark\)\{\s*:root\{(.*?)\}`).FindStringSubmatch(css)[1]) + darkOver := vars(regexp.MustCompile(`(?s)prefers-color-scheme:dark\)\{\s*:root:not\(\[data-theme=light\]\)\{(.*?)\}`).FindStringSubmatch(css)[1]) dark := map[string]string{} for k, v := range light { dark[k] = v @@ -659,6 +659,10 @@ func TestPageHasStrictCSP(t *testing.T) { "img-src 'self' blob: data:", "object-src 'none'", "base-uri 'self'", + // Trusted Types: no string reaches a DOM sink that runs code; the + // only policy is generator.js's «commitling», for wasm_exec.js. + "require-trusted-types-for 'script'", + "trusted-types commitling", } { if !strings.Contains(csp, want) { t.Errorf("CSP %q lacks %q", csp, want) @@ -677,8 +681,11 @@ func TestPageHasStrictCSP(t *testing.T) { t.Errorf("inline script %q would be blocked by the CSP", s) } } - if len(scripts) != 3 { - t.Errorf("scripts = %v, want page.js, generator.js and install.js", scripts) + if tt := regexp.MustCompile(`trusted-types ([^;]*)`).FindStringSubmatch(csp); tt == nil || strings.TrimSpace(tt[1]) != "commitling" { + t.Errorf("Trusted Types must allow only the policy commitling: %q", csp) + } + if len(scripts) != 4 { + t.Errorf("scripts = %v, want theme.js, page.js, generator.js and install.js", scripts) } // Styles come only from style.css: no