package classes import "testing" func TestSanitizeHTML(t *testing.T) { const clean = `
Zobacz źródło.
` if got := SanitizeHTML(clean); got != clean { t.Fatalf("clean HTML changed: %q", got) } if got, want := SanitizeHTML(`a
a
Treść
Witaj świecie
\n"; got != want { t.Fatalf("content_html = %#v, want %q", got, want) } if attrs["content"] != "Witaj **świecie**" || attrs["title"] != "Witaj" { t.Fatalf("other attributes changed: %#v", attrs) } }) t.Run("content wins over a supplied content_html", func(t *testing.T) { attrs := map[string]any{"content": "Tekst", "content_html": payload} if err := FormatTranslation(attrs); err != nil { t.Fatal(err) } if got, want := attrs["content_html"], "Tekst
\n"; got != want { t.Fatalf("content_html = %#v, want %q", got, want) } }) t.Run("raw HTML in content is sanitized", func(t *testing.T) { attrs := map[string]any{"content": payload} if err := FormatTranslation(attrs); err != nil { t.Fatal(err) } assertNoActiveContent(t, attrs["content_html"].(string)) }) t.Run("content_html alone is sanitized", func(t *testing.T) { for _, attrs := range []map[string]any{ {"content_html": payload}, {"content": "", "content_html": payload}, {"content": " ", "content_html": payload}, {"content": nil, "content_html": payload}, } { if err := FormatTranslation(attrs); err != nil { t.Fatal(err) } got, ok := attrs["content_html"].(string) if !ok { t.Fatalf("content_html = %#v, want a string", attrs["content_html"]) } assertNoActiveContent(t, got) if want := `Treść
Lion's Mane, \"superior herb\" & inne
" attrs := map[string]any{"content_html": clean} if err := FormatTranslation(attrs); err != nil { t.Fatal(err) } if attrs["content_html"] != clean { t.Fatalf("content_html = %#v, want %q", attrs["content_html"], clean) } }) t.Run("non-string content_html is sanitized as text", func(t *testing.T) { attrs := map[string]any{"content_html": []any{`