-
Notifications
You must be signed in to change notification settings - Fork 0
/
premailer_test.go
168 lines (137 loc) · 3.92 KB
/
premailer_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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package premailer_test
import (
"bytes"
"testing"
"golang.org/x/net/html"
premailer "github.com/mailproto/go-premailer"
)
func TestRemoveIDs(t *testing.T) {
body := `<html> <head> <style type="text/css"> #remove { color:blue; } </style> </head>
<body>
<p id="remove"><a href="#keep">Test</a></p>
<div id="keep">Test</div>
</body> </html>`
p, err := premailer.New([]byte(body))
if err != nil {
t.Error("couldn't parse for premailer", err)
}
p.RemoveIDs = true
inline, err := p.ToInlineCSS()
if err != nil {
t.Error("error inlining css", err)
}
doc, err := html.Parse(bytes.NewBuffer([]byte(inline)))
if err != nil {
t.Error("Resulting content was not html", err)
}
remove := findElement(doc, "p")
for _, attr := range remove.Attr {
if attr.Key == "id" {
t.Error("id attribute not removed", remove.Attr)
}
}
link := findElement(doc, "a")
var linkHref string
for _, attr := range link.Attr {
if attr.Key == "href" {
linkHref = attr.Val
}
}
var hadID bool
keep := findElement(doc, "div")
for _, attr := range keep.Attr {
if attr.Key == "id" {
if attr.Val != linkHref[1:] {
t.Errorf("Expected ID to match href want: %v, got: %v", linkHref[1:], attr.Val)
}
hadID = true
}
}
if !hadID {
t.Error("Expected id value, got none on", keep)
}
}
func TestResetContentEditable(t *testing.T) {
body := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> <head> <style type="text/css"> #remove { color:blue; } </style> </head>
<body>
<div contenteditable="true" id="editable"> Test </div>
</body> </html>`
p, err := premailer.New([]byte(body))
if err != nil {
t.Error("couldn't parse for premailer", err)
}
p.ResetContentEditable = true
inline, err := p.ToInlineCSS()
if err != nil {
t.Error("error inlining css", err)
}
doc, err := html.Parse(bytes.NewBuffer([]byte(inline)))
if err != nil {
t.Error("Resulting content was not html", err)
}
node := findElement(doc, "div")
for _, attr := range node.Attr {
if attr.Key == "contenteditable" {
t.Error("contenteditable attribute not removed", node.Attr)
}
}
}
func TestRemoveComments(t *testing.T) {
body := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> <head> <style type="text/css"> #remove { color:blue; } </style> </head>
<body>
<!-- Link to example.com -->
<a href="http://example.com">Example</a>
</body> </html>`
p, err := premailer.New([]byte(body))
if err != nil {
t.Error("couldn't parse for premailer", err)
}
p.RemoveComments = true
inline, err := p.ToInlineCSS()
if err != nil {
t.Error("error inlining css", err)
}
doc, err := html.Parse(bytes.NewBuffer([]byte(inline)))
if err != nil {
t.Error("Resulting content was not html", err)
}
eachElement(doc, func(n *html.Node) bool {
if n.Type == html.CommentNode {
t.Error("Should have removed all comment nodes, found", n)
}
return true
})
}
func TestDontRemoveComments(t *testing.T) {
body := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> <head> <style type="text/css"> #remove { color:blue; } </style> </head>
<body>
<!-- Link to example.com -->
<a href="http://example.com">Example</a>
</body> </html>`
p, err := premailer.New([]byte(body))
if err != nil {
t.Error("couldn't parse for premailer", err)
}
p.RemoveComments = false
inline, err := p.ToInlineCSS()
if err != nil {
t.Error("error inlining css", err)
}
doc, err := html.Parse(bytes.NewBuffer([]byte(inline)))
if err != nil {
t.Error("Resulting content was not html", err)
}
var gotComment bool
eachElement(doc, func(n *html.Node) bool {
if n.Type == html.CommentNode {
gotComment = true
}
return true
})
if !gotComment {
t.Error("Expected comment, found none")
}
}