-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenizer_html_test.go
57 lines (51 loc) · 1.07 KB
/
tokenizer_html_test.go
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
package jargon_test
import (
"strings"
"testing"
"github.com/clipperhouse/jargon"
)
func TestTokenizeHTML(t *testing.T) {
h := `<html>
<p foo="bar">
Hi! Let's talk Ruby on Rails.
<!-- Ignore ASPNET MVC in comments -->
<script src="foo">var Nodejs = Reactjs;</script>
<style>p { margin-bottom:20px; } </style>
</p>
</html>
`
r := strings.NewReader(h)
tokens, err := jargon.TokenizeHTML(r).ToSlice()
if err != nil {
t.Error(err)
}
got := map[string]bool{}
for _, token := range tokens {
got[token.String()] = true
}
expected := []string{
// tags kept whole
`<p foo="bar">`,
"</p>",
// whitespace preserved
"\n",
// text nodes got tokenized
"Hi", "!",
"Ruby", "on", "Rails",
// comment kept whole
"<!-- Ignore ASPNET MVC in comments -->",
// contents of script not tokenized
`<script src="foo">`,
"var Nodejs = Reactjs;",
"</script>",
// contents of style not tokenized
"<style>",
"p { margin-bottom:20px; } ",
"</style>",
}
for _, e := range expected {
if !got[e] {
t.Errorf("Expected to find token %q, but did not.", e)
}
}
}