-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss_inline_test.exs
More file actions
334 lines (286 loc) · 8.86 KB
/
css_inline_test.exs
File metadata and controls
334 lines (286 loc) · 8.86 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
defmodule CSSInlineTest do
use ExUnit.Case, async: true
describe "inline/1" do
test "inlines basic CSS styles" do
html = """
<html>
<head>
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html)
assert result =~ ~r/<p[^>]*style="[^"]*color: ?red/
end
test "handles empty HTML" do
assert {:ok, result} = CSSInline.inline("")
assert is_binary(result)
end
test "handles HTML without styles" do
html = "<html><body><p>Hello</p></body></html>"
assert {:ok, result} = CSSInline.inline(html)
assert result =~ "Hello"
end
test "handles complex CSS selectors" do
html = """
<html>
<head>
<style>
.greeting { font-weight: bold; }
#main { margin: 10px; }
</style>
</head>
<body>
<p class="greeting">Hello</p>
<div id="main">Content</div>
</body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html)
assert result =~ ~r/font-weight: ?bold/
assert result =~ ~r/margin: ?10px/
end
test "returns an error when NIF encounters invalid inline styles" do
html = """
<html>
<head>
<style>h1 { background-color: blue; }</style>
</head>
<body>
<h1 style="@wrong {color: ---}">Hello world!</h1>
</body>
</html>
"""
assert {:error, _reason} = CSSInline.inline(html)
end
test "minifies CSS output" do
html = """
<html>
<head>
<style>
p {
color: red;
margin: 10px;
}
</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html)
# CSS should be minified (no extra whitespace)
assert result =~ "color:red" or result =~ "color: red"
end
end
describe "inline/2 with options" do
test "keep_style_tags: true preserves style tags" do
html = """
<html>
<head>
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html, keep_style_tags: true)
assert result =~ "<style>"
assert result =~ ~r/<p[^>]*style="[^"]*color: ?red/
end
test "keep_style_tags: false removes style tags (default)" do
html = """
<html>
<head>
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html, keep_style_tags: false)
refute result =~ "<style>"
end
test "inline_style_tags: false skips inlining from style tags" do
html = """
<html>
<head>
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html, inline_style_tags: false)
# The style should NOT be inlined
refute result =~ ~r/<p[^>]*style=/
end
test "minify_css: false preserves whitespace in CSS" do
html = """
<html>
<head>
<style>p { color: red; margin: 10px; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html, minify_css: false)
# With minify_css: false, there should be spaces around colons
assert result =~ "color: red"
end
test "keep_link_tags: true preserves link tags" do
html = """
<html>
<head>
<link rel="stylesheet" href="https://example.com/style.css">
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} =
CSSInline.inline(html, keep_link_tags: true, load_remote_stylesheets: false)
assert result =~ "<link"
end
test "load_remote_stylesheets: false skips external stylesheets" do
html = """
<html>
<head>
<link rel="stylesheet" href="https://example.com/nonexistent.css">
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
# With load_remote_stylesheets: false, this should succeed without trying to fetch
assert {:ok, result} = CSSInline.inline(html, load_remote_stylesheets: false)
assert result =~ ~r/<p[^>]*style="[^"]*color: ?red/
end
test "multiple options can be combined" do
html = """
<html>
<head>
<style>p { color: red; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
assert {:ok, result} = CSSInline.inline(html, keep_style_tags: true, minify_css: false)
assert result =~ "<style>"
assert result =~ "color: red"
end
end
describe "inline!/2 with options" do
test "returns inlined HTML on success" do
html = """
<html>
<head>
<style>p { color: blue; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
result = CSSInline.inline!(html)
assert result =~ ~r/<p[^>]*style="[^"]*color: ?blue/
end
test "accepts options" do
html = """
<html>
<head>
<style>p { color: blue; }</style>
</head>
<body><p>Hello</p></body>
</html>
"""
result = CSSInline.inline!(html, keep_style_tags: true)
assert result =~ "<style>"
end
test "raises on error" do
html = """
<html>
<head>
<style>h1 { color: blue; }</style>
</head>
<body>
<h1 style="@invalid {color: ---}">Hello!</h1>
</body>
</html>
"""
assert_raise RuntimeError, ~r/CSS inlining failed/, fn ->
CSSInline.inline!(html)
end
end
end
describe "!important handling" do
@email_html """
<html>
<head>
<style>
a[x-apple-data-detectors],
u + #body a {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}
.button a {
color: #ffffff !important;
font-size: 16px !important;
font-weight: bold !important;
}
</style>
</head>
<body id="body">
<u>
<div class="button">
<a href="https://example.com">Click Me</a>
</div>
</u>
</body>
</html>
"""
test "preserves !important when inlining styles" do
assert {:ok, result} = CSSInline.inline(@email_html)
assert result =~ ~r/style="[^"]*!important/
end
@production_opts [
load_remote_stylesheets: false,
keep_link_tags: true,
keep_style_tags: true
]
test "production settings: inlined selectors remain in <style> without remove_inlined_selectors" do
assert {:ok, result} = CSSInline.inline(@email_html, @production_opts)
assert result =~ ~r/style="[^"]*!important/,
"Inlined styles should preserve !important"
[_, style_content] = Regex.run(~r/<style[^>]*>(.*?)<\/style>/s, result)
assert style_content =~ "u + #body a",
"Non-inlinable selectors should remain in <style> tag"
assert style_content =~ ".button a",
"Without remove_inlined_selectors, inlined selectors remain in <style>"
end
test "production settings + remove_inlined_selectors strips inlined rules from <style>" do
{:ok, result} =
CSSInline.inline(@email_html, [remove_inlined_selectors: true] ++ @production_opts)
assert result =~ ~r/style="[^"]*color:[^"]*!important/,
"Inlined styles should preserve !important"
[_, style_content] = Regex.run(~r/<style[^>]*>(.*?)<\/style>/s, result)
assert style_content =~ "u + #body a",
"Non-inlinable selectors (complex email client overrides) must remain"
refute style_content =~ ".button a",
"Inlined selectors should be removed from <style> to prevent conflicts"
end
end
describe "regression tests" do
test "returns error for deeply nested HTML" do
html = File.read!("test/fixtures/deeply_nested.html")
assert {:error, :nesting_depth_exceeded} = CSSInline.inline(html)
end
test "accepts HTML near but under the nesting limit" do
divs = String.duplicate("<div>", 100)
closing = String.duplicate("</div>", 100)
html =
"<html><head><style>p{color:red}</style></head><body>#{divs}<p>ok</p>#{closing}</body></html>"
assert {:ok, result} = CSSInline.inline(html)
assert result =~ "ok"
end
end
end