package classes import "testing" func TestCheckHTML(t *testing.T) { cases := []struct { name string in string ok bool }{ {"empty", "", true}, {"heading and link", `

Cordyceps

Plain link

`, true}, {"table", `
A
1
`, true}, {"polish date label", `

Data: 12 maja 2024

`, true}, {"metadata text", `

Some metadata: here

`, true}, {"quotes, apostrophes and nbsp", `

Co to jest Lion's Mane? 

"superior herb" & inne

`, true}, {"pasted chat transcript", `

x

`, true}, {"script", ``, false}, {"iframe", ``, false}, {"object", ``, false}, {"embed", ``, false}, {"event handler", ``, false}, {"event handler without a space", `

`, false}, {"slash-separated svg onload", "
\n\n
", false}, {"javascript url", ``, false}, {"numeric entity colon", `x`, false}, {"named entity colon", `x`, false}, {"entity inside the scheme", `x`, false}, {"tab inside the scheme", "x", false}, {"vbscript url mixed case", ``, false}, {"data url", ``, false}, {"base", ``, false}, {"meta refresh", ``, false}, {"form", `
`, false}, {"style element", ``, false}, {"comment", `

a

`, false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { err := CheckHTML(tc.in) if tc.ok && err != nil { t.Fatalf("CheckHTML(%q) = %v, want nil", tc.in, err) } if !tc.ok && err == nil { t.Fatalf("CheckHTML(%q) = nil, want an error", tc.in) } }) } // Every stored-XSS payload FormatHTML strips is refused by CheckHTML. for _, tc := range unsafePayloads { if tc.src[0] != '<' { continue // markdown, not HTML } t.Run("payload "+tc.name, func(t *testing.T) { if err := CheckHTML(tc.src); err == nil { t.Fatalf("CheckHTML(%q) = nil, want an error", tc.src) } }) } }