-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathinlineCSS.test.js
More file actions
255 lines (234 loc) · 7.3 KB
/
inlineCSS.test.js
File metadata and controls
255 lines (234 loc) · 7.3 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
import { describe, expect, test } from 'vitest'
import { inlineCSS } from '../../src/index.js'
import { cleanString } from '../../src/utils/string.js'
import { run as useTransformers } from '../../src/transformers/index.js'
const css = `
.w-1 {width: 4px}
.h-1 {height: 4px}
.foo {color: red}
.bar {cursor: pointer}
.hover\\:foo:hover {color: blue}
.bg-custom {background-image: url('https://picsum.photos/600/400') !important}
@media (max-width: 600px) {
.sm\\:text-center {text-align: center}
}
u + .body .gmail\\:hidden {
display: none;
}
`
const html = `
<style>${css}</style>
<p class="bar">test</p>
<table class="w-1 h-1 sm:text-center bg-custom">
<tr>
<td class="foo bar h-1 gmail:hidden">test</td>
</tr>
</table>`
describe.concurrent('Inline CSS', () => {
test('Invalid input', async () => {
expect(await inlineCSS()).toBe('')
expect(await inlineCSS('')).toBe('')
})
test('Sanity test', async () => {
const result = await inlineCSS(html, {
removeInlinedSelectors: true,
codeBlocks: {
RB: {
start: '<%',
end: '%>',
},
},
})
expect(cleanString(result)).toBe(cleanString(`
<style>
.hover\\:foo:hover {color: blue}
@media (max-width: 600px) {
.sm\\:text-center {text-align: center}
}
u + .body .gmail\\:hidden { display: none; }
</style>
<p style="cursor: pointer">test</p>
<table class="sm:text-center" style="width: 4px; height: 4px; background-image: url('https://picsum.photos/600/400')">
<tr>
<td class="gmail:hidden" style="height: 4px; color: red; cursor: pointer">test</td>
</tr>
</table>`))
})
test('Preserves user-defined selectors', async () => {
const result = await inlineCSS(`
<style>
.bar {margin: 0}
.variant\\:foo {color: blue}
</style>
<p class="bar">test</p>
<span class="variant:foo"></span>`,
{
removeInlinedSelectors: true,
safelist: ['foo', '.bar'],
})
expect(cleanString(result)).toBe(cleanString(`
<style>
.bar {margin: 0}
.variant\\:foo {color: blue}
</style>
<p class="bar" style="margin: 0">test</p>
<span class="variant:foo" style="color: blue"></span>`
))
})
test('Preserves inlined selectors', async () => {
const result = await inlineCSS(html, {
removeInlinedSelectors: false,
})
expect(cleanString(result)).toBe(cleanString(`
<style>
.w-1 {width: 4px}
.h-1 {height: 4px}
.foo {color: red}
.bar {cursor: pointer}
.hover\\:foo:hover {color: blue}
.bg-custom {background-image: url('https://picsum.photos/600/400') !important}
@media (max-width: 600px) {
.sm\\:text-center {text-align: center}
}
u + .body .gmail\\:hidden { display: none; }
</style>
<p class="bar" style="cursor: pointer">test</p>
<table class="w-1 h-1 sm:text-center bg-custom" style="width: 4px; height: 4px; background-image: url('https://picsum.photos/600/400')">
<tr>
<td class="foo bar h-1 gmail:hidden" style="height: 4px; color: red; cursor: pointer">test</td>
</tr>
</table>`))
})
test('Works with `customCSS` option', async () => {
expect(
cleanString(
await inlineCSS(
'<p class="bar" style="color: red"></p>',
{
customCSS: '.bar {display: flex;}'
}
)
)
).toBe('<p class="bar" style="display: flex; color: red;"></p>')
})
test('Works with `preferUnitlessValues` option disabled', async () => {
const result = cleanString(
await inlineCSS(`
<style>.m-0 {margin: 0px}</style>
<p class="m-0">test</p>`,
{
preferUnitlessValues: false, // default is true
}
)
)
expect(result).toBe(cleanString(`
<style></style>
<p style="margin: 0px">test</p>`))
})
test('`preferUnitlessValues` skips invalid inline CSS', async () => {
const result = cleanString(
await inlineCSS(`
<style>.m-0 {margin: 0px}</style>
<p class="m-0" style="color: #{{ $foo->theme }}">test</p>`
)
)
expect(result).toBe(cleanString(`
<style></style>
<p class="m-0" style="margin: 0px; color: #{{ $foo->theme }};">test</p>`))
})
test('Works with `excludedProperties` option', async () => {
expect(
cleanString(
await inlineCSS(`
<style>.bar {cursor: pointer; margin: 0}</style>
<p class="bar">test</p>`, {
removeInlinedSelectors: true,
excludedProperties: ['margin']
})
)
).toBe(cleanString(`
<style></style>
<p style="cursor: pointer">test</p>`))
})
test('Uses `applyWidthAttributes` and `applyHeightAttributes` by default', async () => {
expect(
await useTransformers('<style>.size-10px {width: 10px; height: 10px}</style><img class="size-10px">', {
css: { inline: { removeInlinedSelectors: true } },
}).then(({ html }) => html)
).toBe('<style></style><img style="width: 10px; height: 10px" width="10" height="10" alt>')
})
test('Does not inline <style> tags marked as "embedded"', async () => {
expect(
await inlineCSS(`
<style embed>.foo {color: red}</style>
<style data-embed>.foo {display: flex}</style>
<p class="foo">test</p>`)
).toBe(`
<style>.foo {color: red}</style>
<style>.foo {display: flex}</style>
<p class="foo">test</p>`
)
})
test('useTransformers context', async () => {
expect(
cleanString(
await useTransformers(html, {
attributes: { add: false },
css: { inline: { removeInlinedSelectors: true } },
}).then(({ html }) => html)
)
).toBe(cleanString(`
<style>
.hover-foo:hover {color: blue}
@media (max-width: 600px) {
.sm-text-center {text-align: center}
}
u + .body .gmail-hidden { display: none; }
</style>
<p style="cursor: pointer">test</p>
<table class="sm-text-center" style="width: 4px; height: 4px; background-image: url('https://picsum.photos/600/400')">
<tr>
<td class="gmail-hidden" style="height: 4px; color: red; cursor: pointer">test</td>
</tr>
</table>`))
})
test('Works with base64-encoded CSS values', async () => {
expect(
cleanString(
await inlineCSS(`
<style>
.base64 {
background-image: url("data:image/gif;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs=");
}
</style>
<p class="base64">test</p>`,
)
)
).toBe(cleanString(`
<style> </style>
<p style="background-image: url('data:image/gif;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs=')">test</p>`))
})
test('Works with pseudo-classes', async () => {
expect(
cleanString(
await inlineCSS(`
<style>
li::marker {color: blue}
ul > li {
color: red;
}
</style>
<ul>
<li>test</li>
</ul>`,
)
)
).toBe(cleanString(`
<style>
li::marker {color: blue}
</style>
<ul>
<li style="color: red">test</li>
</ul>`))
})
})