package seaglass_test
import (
"slices"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/seaglass"
)
// TestSanitizeByteStable pins D-10's byte-stability promise: HTML the policy
// keeps whole comes back byte for byte, with attributes in their source order,
// entities as written, whitespace and quoting untouched, and a zero report.
func TestSanitizeByteStable(t *testing.T) {
for _, html := range []string{
"",
"plain text",
"
",
} {
got, report := seaglass.SanitizeReport(html)
if got != html {
t.Errorf("SanitizeReport(%q) = %q, want the input byte for byte", html, got)
}
if report.Changed || report.ElementCount != 0 || report.AttributeCount != 0 || report.Comments != 0 || len(report.Elements) != 0 || len(report.Attributes) != 0 {
t.Errorf("SanitizeReport(%q) report = %+v, want zero", html, report)
}
if out := seaglass.Sanitize(html); out != html {
t.Errorf("Sanitize(%q) = %q", html, out)
}
if err := seaglass.Check(html); err != nil {
t.Errorf("Check(%q) = %v", html, err)
}
}
}
// TestSanitizeReport pins the removal report: element, attribute and comment
// names and counts, the ten-name cap with its exact counts, a repeated
// attribute, a style that lost a property, and the sanitized output itself.
func TestSanitizeReport(t *testing.T) {
for _, tc := range []struct {
name string
in, out string
elements []string
elemCount int
attributes []string
attrCount int
comments int
}{
{
name: "element, attribute and comments",
in: `
Hi
`,
out: `
Hi
`, elements: []string{"script"}, elemCount: 1, attributes: []string{"onclick"}, attrCount: 1, comments: 2,
},
{
name: "names are distinct in first-seen order, counts are exact",
in: `
1
2
`,
out: `
1
2
`, elements: []string{"script"}, elemCount: 2, attributes: []string{"onclick", "onmouseover"}, attrCount: 3,
},
{
name: "ten names then an ellipsis, the count stays exact",
in: `