Skip to content

Commit a30d177

Browse files
authored
Merge pull request #1469 from maizzle/fix-pseudos
Ensure pseudo-classes preserved when inlining CSS
2 parents 04cdccb + fc5cf2e commit a30d177

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/transformers/inline.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,18 @@ export async function inline(html = '', options = {}) {
142142

143143
// For each rule in the CSS block we're parsing
144144
root.walkRules(rule => {
145+
// Create a set of selectors
145146
const { selector } = rule
146147

147-
selectors.add({
148-
name: selector,
149-
prop: get(rule.nodes[0], 'prop')
150-
})
151-
148+
// Add the selector to the set as long as it's not a pseudo selector
149+
if (!/(^|[^\\])::?[\w-]+/.test(selector)) {
150+
selectors.add({
151+
name: selector,
152+
prop: get(rule.nodes[0], 'prop')
153+
})
154+
}
152155
// Preserve pseudo selectors
153-
if ([':hover', ':active', ':focus', ':visited', ':link', ':before', ':after'].some(i => selector.includes(i))) {
156+
else {
154157
options.safelist.add(selector)
155158
}
156159

test/transformers/inlineCSS.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,29 @@ describe.concurrent('Inline CSS', () => {
214214
<style> </style>
215215
<p style="background-image: url('data:image/gif;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs=')">test</p>`))
216216
})
217+
218+
test('Works with pseudo-classes', async () => {
219+
expect(
220+
cleanString(
221+
await inlineCSS(`
222+
<style>
223+
li::marker {color: blue}
224+
225+
ul > li {
226+
color: red;
227+
}
228+
</style>
229+
<ul>
230+
<li>test</li>
231+
</ul>`,
232+
)
233+
)
234+
).toBe(cleanString(`
235+
<style>
236+
li::marker {color: blue}
237+
</style>
238+
<ul>
239+
<li style="color: red">test</li>
240+
</ul>`))
241+
})
217242
})

0 commit comments

Comments
 (0)