diff --git a/internal/forms/forms_test.go b/internal/forms/forms_test.go index bdd4795..4ef0024 100644 --- a/internal/forms/forms_test.go +++ b/internal/forms/forms_test.go @@ -3,6 +3,7 @@ package forms import ( "os" "path/filepath" + "regexp" "strings" "testing" ) @@ -100,6 +101,25 @@ func TestRenderEscapes(t *testing.T) { } } +// The script finds each form by the data attribute the markup sets, and asks +// for that form's token by the name in it. A rename once left them apart, and +// every form asked for the token of a form called "undefined". +func TestScriptReadsTheFormsAttribute(t *testing.T) { + f := load(t, contact) + h, err := f.Render("/f.js", "/f.css") + if err != nil { + t.Fatal(err) + } + m := regexp.MustCompile(`data-([a-z]+)-form="contact"`).FindStringSubmatch(string(h)) + if m == nil { + t.Fatalf("no data-…-form attribute in:\n%s", h) + } + js := string(Script) + if !strings.Contains(js, "form[data-"+m[1]+"-form]") || !strings.Contains(js, "form.dataset."+m[1]+"Form") { + t.Errorf("the script doesn't read data-%s-form, the attribute the form has", m[1]) + } +} + func TestSubjectIsPlain(t *testing.T) { for _, bad := range []string{`{{ range 100000000 }}x{{ end }}`, `{{ printf "%099999999d" 1 }}`, `{{ if .a }}x{{ end }}`, `{{ $x := .a }}`, `{{ .a.b }}`, `{{ template "x" }}`} { if _, err := Parse("contact", []byte("to: a@example.org\nsubject: '"+bad+"'\nfields:\n - name: a\n")); err == nil { diff --git a/internal/forms/static/hotdog-forms.js b/internal/forms/static/hotdog-forms.js index 3b2ed5f..3fa95f2 100644 --- a/internal/forms/static/hotdog-forms.js +++ b/internal/forms/static/hotdog-forms.js @@ -10,7 +10,7 @@ form.querySelectorAll('[aria-invalid]').forEach(function (el) { el.removeAttribute('aria-invalid'); }); } function token(form) { - return fetch('/_hotdog/forms/' + form.dataset.clForm + '/token', { credentials: 'omit' }) + return fetch('/_hotdog/forms/' + form.dataset.hotdogForm + '/token', { credentials: 'omit' }) .then(function (r) { if (!r.ok) throw new Error(r.status); return r.json(); }) .then(function (d) { form.querySelector('input[name="_t"]').value = d.token; }); }