1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package logic

import "testing"

func TestHtmlToText(t *testing.T) {
	for _, c := range []struct{ name, in, want string }{
		{"plain", "Just a title", "Just a title"},
		{"entities", "Tom &amp; Jerry &lt;3 &quot;x&quot;", `Tom & Jerry <3 "x"`},
		{"inline tags", "a <b>bold</b> and <a href=\"https://x\">link</a>", "a bold and link"},
		{"paragraphs", "<p>a</p><p>b</p>", "a\nb"},
		{"line break", "a<br>b<br/>c", "a\nb\nc"},
		{"list", "<ul><li>one</li><li>two</li></ul>", "one\ntwo"},
		{"cells", "<table><tr><td>a</td><td>b</td></tr></table>", "a b"},
		{"white space", "  a \n\t b  <p>  c  </p>  ", "a b\nc"},
		{"blank paragraphs", "<p>a</p><p> </p><p></p><p>b</p>", "a\nb"},
		{"script", "a<script>alert(1)</script>b", "ab"},
		{"style", "<style>p{color:red}</style>text", "text"},
		{"svg", "x<svg><text>drawn</text></svg>y", "xy"},
		{"comment", "a<!-- hidden -->b", "ab"},
		{"unclosed", "<p>a<div>b", "a\nb"},
		{"empty", "", ""},
	} {
		t.Run(c.name, func(t *testing.T) {
			if got := htmlToText(c.in); got != c.want {
				t.Errorf("htmlToText(%q) = %q, want %q", c.in, got, c.want)
			}
		})
	}
}