-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_test.go
More file actions
107 lines (103 loc) · 1.96 KB
/
html_test.go
File metadata and controls
107 lines (103 loc) · 1.96 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package htmlstrip
import (
"strings"
"testing"
)
func TestHTML(t *testing.T) {
// isplit is a split point to break up the input html to be parsed in two parts.
for isplit := 0; isplit < 311; isplit++ {
for i, ht := range htmlTests {
sb := &strings.Builder{}
w := &Writer{W: sb}
var err error
if len(ht.html) >= isplit {
inpart0 := ht.html[:isplit]
_, err = w.Write([]byte(inpart0))
if err != nil {
panic(err)
}
inpart1 := ht.html[isplit:]
_, err = w.Write([]byte(inpart1))
if err != nil {
panic(err)
}
expect := strings.TrimSpace(ht.plain)
got := strings.TrimSpace(sb.String())
if got != expect {
t.Fatalf("htmlTests[%d] isplit=%d failed;\n input[0]: `%s`\n input[1]: `%s`\n expected: `%s`\n got: `%s`",
i, isplit, inpart0, inpart1, expect, got)
}
}
}
}
}
type htmlTest struct {
html, plain string
}
var htmlTests = []htmlTest{
htmlTest{
"foo<b>bar</b><br>f<c/>o<d />o<span id=a>bar</span>",
"foobar\nfoobar",
},
htmlTest{
"hello<!----cruel---->world",
"helloworld",
},
htmlTest{
"<!-- nothing to see here -- or here -> not even here",
"",
},
htmlTest{
"foo<!--<!-->!<!-->bar",
"foo!bar",
},
htmlTest{
"<div><p><p><script><!--</script>-->hey</script>hi",
"hi",
},
htmlTest{
"<!DOCTYPE html><html><p>foo</p><p>bar</p></html>",
"foo\nbar",
},
htmlTest{
"hello&world !",
"hello&world !",
},
htmlTest{
"<broken<tags<span>stuff",
"<broken<tagsstuff",
},
htmlTest{
"foo < bar",
"foo < bar",
},
htmlTest{
"foo&bar;baz",
"foo&bar;baz",
},
htmlTest{
"&SuperLongEntityThatWillNotTurnIntoAnythingSpecial;",
"&SuperLongEntityThatWillNotTurnIntoAnythingSpecial;",
},
htmlTest{
"&entity interruptus;",
"&entity interruptus;",
},
htmlTest{
`<!DOCTYPE html>
<html>
<head>
<title>hi</title>
<style>.foo { color: black; }</style>
</head>
<body>
<p>Hi!</p>
</body>
</html>`,
"Hi!",
},
htmlTest{
"",
"",
},
}