-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_test.go
175 lines (141 loc) · 3.56 KB
/
misc_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
169
170
171
172
173
174
175
package premailer_test
import (
"bytes"
"testing"
"golang.org/x/net/html"
premailer "github.com/mailproto/go-premailer"
)
func inlineCSSNode(t *testing.T, body string) *html.Node {
pre, err := premailer.New([]byte(body))
if err != nil {
t.Error("couldn't parse for premailer", err)
}
inline, err := pre.ToInlineCSS()
if err != nil {
t.Error("couldn't convert to inline css", err)
}
doc, err := html.Parse(bytes.NewBuffer([]byte(inline)))
if err != nil {
t.Error("Resulting content was not html", err)
}
return doc
}
func checkElementStyle(t *testing.T, doc *html.Node, element, expectStyle string) {
eachElement(doc, func(n *html.Node) bool {
// Skip all non-matching elements
if n.Data != element {
return true
}
var hasStyle bool
for _, attr := range n.Attr {
if attr.Key == "style" {
if expectStyle == "" {
t.Errorf("Expected no style attribute, got: `%v`", attr.Val)
continue
}
if attr.Val != expectStyle {
t.Errorf("Wrong inlined style, want: `%v`, got: `%v`", expectStyle, attr.Val)
}
hasStyle = true
}
}
if expectStyle != "" && !hasStyle {
t.Error("style didn't get inlined properly")
}
return true
})
}
func TestStylesInTheBody(t *testing.T) {
doc := inlineCSSNode(t, `<html>
<body>
<style type="text/css"> p { color: red; } </style>
<p>Test</p>
</body>
</html>`)
checkElementStyle(t, doc, "p", "color: red")
}
// XXX: not sure why commented out styles should still be applied, but premailer does this
func TestCommentedOutStylesInTheBody(t *testing.T) {
doc := inlineCSSNode(t, ` <html>
<body>
<style type="text/css"> <!-- p { color: red; } --> </style>
<p>Test</p>
</body>
</html>`)
checkElementStyle(t, doc, "p", "color: red")
}
func TestNotApplyingStylesToTheHead(t *testing.T) {
doc := inlineCSSNode(t, `<html>
<head>
<title>Title</title>
<style type="text/css"> * { color: red; } </style>
</head>
<body>
<p><a>Test</a></p>
</body>
</html>`)
checkElementStyle(t, doc, "head", "")
checkElementStyle(t, doc, "title", "")
}
func TestMultipleIdenticalIDs(t *testing.T) {
doc := inlineCSSNode(t, `<html>
<head>
<style type="text/css"> #the_id { color: red; } </style>
</head>
<body>
<p id="the_id">Test</p>
<p id="the_id">Test</p>
</body>
</html>`)
checkElementStyle(t, doc, "p", "color: red")
}
func TestRemovingScripts(t *testing.T) {
p, err := premailer.New([]byte(`<html>
<head>
<script>script to be removed</script>
</head>
<body>
content
</body>
</html>`))
if err != nil {
t.Error("error creating premailer", err)
}
p.RemoveScripts = 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)
}
if e := findElement(doc, "script"); e != nil {
t.Errorf("Shouldn't have found a script tag, got: %v", e)
}
}
func TestDontRemoveScripts(t *testing.T) {
p, err := premailer.New([]byte(`<html>
<head>
<script>script to be removed</script>
</head>
<body>
content
</body>
</html>`))
if err != nil {
t.Error("error creating premailer", err)
}
p.RemoveScripts = 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)
}
if e := findElement(doc, "script"); e == nil {
t.Errorf("Should have found a script tag, but didn't")
}
}