Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 3.7 KiB
1package web
2
3// HTMX compiles an hx-trigger filter, hx-on handler, or js: value with
4// Function(), which the CSP forbids (script-src 'self'). A template that carries
5// one makes the browser log an EvalError and leaves the trigger or value failing
6// on the production instance, where tests rooted in the rendered HTML do not see
7// a console. The conditions those filters used to express belong in app.js
8// beside the request they guard.
9
10import (
11 "io/fs"
12 "regexp"
13 "strings"
14 "testing"
15)
16
17var (
18 htmxTrigger = regexp.MustCompile(`hx-trigger="([^"]*)"`)
19 htmxEval = regexp.MustCompile(`hx-(?:vals|headers)="(?:javascript:|js:)`)
20)
21
22func TestTemplatesCarryNoHTMXEval(t *testing.T) {
23 t.Parallel()
24 files, err := fs.Glob(templateFS, "templates/*.html")
25 if err != nil {
26 t.Fatal(err)
27 }
28 for _, name := range files {
29 body, err := fs.ReadFile(templateFS, name)
30 if err != nil {
31 t.Fatal(err)
32 }
33 source := string(body)
34 for _, match := range htmxTrigger.FindAllStringSubmatch(source, -1) {
35 if strings.Contains(match[1], "[") {
36 t.Errorf("%s has an hx-trigger filter (%q), which HTMX evaluates with Function()", name, match[1])
37 }
38 }
39 if strings.Contains(source, "hx-on:") || strings.Contains(source, "hx-on-") {
40 t.Errorf("%s has an hx-on handler, which HTMX evaluates with Function()", name)
41 }
42 if htmxEval.MatchString(source) {
43 t.Errorf("%s has a js: htmx value, which HTMX evaluates with Function()", name)
44 }
45 }
46}
47
48// A td or th that carries a class setting a non-table display stops being a
49// table cell. The browser wraps it in an anonymous cell, so the real cell draws
50// no border and the element's own border-bottom spans its text instead, which
51// breaks the row separator around it (the Releases channels column did this
52// with .label-row). A wrapper inside the cell keeps both: a td holding labels
53// is written <td><div class="label-row">…</div></td>.
54
55var (
56 cssRule = regexp.MustCompile(`(?s)([^{}]+)\{([^{}]*)\}`)
57 cssDisplay = regexp.MustCompile(`(?:^|[;\s])display:\s*([^;}]+)`)
58 cssClass = regexp.MustCompile(`\.[-\w]+`)
59 cellClass = regexp.MustCompile(`<(?:td|th)\s+class="([^"]*)"`)
60 selectorPart = regexp.MustCompile(`[\s>+~]+`)
61)
62
63// nonTableCellClasses maps each class the stylesheet gives a display other than
64// table-cell to that display. Only the subject of a selector counts, so
65// `.label-row .label` does not implicate .label.
66func nonTableCellClasses(t *testing.T) map[string]string {
67 t.Helper()
68 css, err := fs.ReadFile(staticFS, "static/vole.css")
69 if err != nil {
70 t.Fatal(err)
71 }
72 classes := map[string]string{}
73 for _, rule := range cssRule.FindAllStringSubmatch(string(css), -1) {
74 m := cssDisplay.FindStringSubmatch(rule[2])
75 if m == nil {
76 continue
77 }
78 display := strings.TrimSpace(m[1])
79 if strings.HasPrefix(display, "table") {
80 continue
81 }
82 for _, selector := range strings.Split(rule[1], ",") {
83 parts := selectorPart.Split(strings.TrimSpace(selector), -1)
84 subject := parts[len(parts)-1]
85 for _, class := range cssClass.FindAllString(subject, -1) {
86 classes[strings.TrimPrefix(class, ".")] = display
87 }
88 }
89 }
90 return classes
91}
92
93func TestTableCellsKeepTheirDisplay(t *testing.T) {
94 t.Parallel()
95 bad := nonTableCellClasses(t)
96 files, err := fs.Glob(templateFS, "templates/*.html")
97 if err != nil {
98 t.Fatal(err)
99 }
100 for _, name := range files {
101 body, err := fs.ReadFile(templateFS, name)
102 if err != nil {
103 t.Fatal(err)
104 }
105 for _, cell := range cellClass.FindAllStringSubmatch(string(body), -1) {
106 for _, class := range strings.Fields(cell[1]) {
107 if display, ok := bad[class]; ok {
108 t.Errorf("%s: a td or th carries class %q (%s), which stops it being a table cell; wrap that element in a div inside the cell", name, class, display)
109 }
110 }
111 }
112 }
113}
114