package markup
import (
"strings"
"testing"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func attr(n *html.Node, key string) string {
for _, a := range n.Attr {
if a.Key == key {
return a.Val
}
}
return ""
}
func hasClass(n *html.Node, want string) bool {
for _, c := range strings.Fields(attr(n, "class")) {
if c == want {
return true
}
}
return false
}
func TestTableAlignmentSurvivesSanitizer(t *testing.T) {
doc := parseBody(t, "| a | b | c |\n|:--|:-:|--:|\n| 1 | 2 | 3 |\n")
got := []string{}
for _, th := range findAll(doc, atom.Th) {
got = append(got, attr(th, "align"))
}
if strings.Join(got, ",") != "left,center,right" {
t.Fatalf("th align = %v", got)
}
got = got[:0]
for _, td := range findAll(doc, atom.Td) {
got = append(got, attr(td, "align"))
}
if strings.Join(got, ",") != "left,center,right" {
t.Fatalf("td align = %v", got)
}
}
func TestTaskListItemsCarryState(t *testing.T) {
doc := parseBody(t, "- [ ] open\n- [x] done\n")
lis := findAll(doc, atom.Li)
if len(lis) != 2 || !hasClass(lis[0], "unchecked") || !hasClass(lis[1], "checked") {
t.Fatalf("li classes = %q %q", attr(lis[0], "class"), attr(lis[1], "class"))
}
if uls := findAll(doc, atom.Ul); len(uls) != 1 || !hasClass(uls[0], "md-tasks") {
t.Fatal("task-only list lacks md-tasks")
}
doc = parseBody(t, "- plain\n- [x] done\n")
if uls := findAll(doc, atom.Ul); hasClass(uls[0], "md-tasks") {
t.Fatal("mixed list must not carry md-tasks")
}
}
func TestTableColumnHints(t *testing.T) {
src := "| name | qty | note |\n|---|---|---|\n" +
"| a | 1 | " + strings.Repeat("x", 30) + " |\n" +
"| b | -2.5% | y |\n"
doc := parseBody(t, src)
rows := findAll(doc, atom.Tr)
if len(rows) != 3 {
t.Fatalf("rows = %d", len(rows))
}
cellsOf := func(tr *html.Node) []*html.Node {
var cs []*html.Node
for c := tr.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode {
cs = append(cs, c)
}
}
return cs
}
for _, tr := range rows {
cs := cellsOf(tr)
if !hasClass(cs[0], "md-short") || hasClass(cs[0], "md-num") {
t.Errorf("col 0 classes %q", attr(cs[0], "class"))
}
if !hasClass(cs[1], "md-short") || !hasClass(cs[1], "md-num") {
t.Errorf("col 1 classes %q", attr(cs[1], "class"))
}
if attr(cs[2], "class") != "" {
t.Errorf("col 2 classes %q", attr(cs[2], "class"))
}
}
}