Skip to content

Commit

Permalink
fix: remove empty style tags
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Oct 12, 2022
1 parent c2af961 commit aae4813
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/transformers/removeInlinedSelectors.js
Expand Up @@ -71,7 +71,12 @@ const plugin = posthtmlOptions => tree => {
} catch {}
})

node.content = root.toString()
node.content = root.toString().trim()

// Remove <style> tag if it ends up empty after processing
if (node.content.length === 0) {
node.tag = false
}
}

return node
Expand Down
43 changes: 31 additions & 12 deletions test/test-transformers.js
Expand Up @@ -495,11 +495,10 @@ test('remove inlined selectors', async t => {
</body>
</html>`

const expectedWithOptions = `<!DOCTYPE html>
const expectedHTML = `<!DOCTYPE html>
<html>
<head>
<style>
.hover-text-blue:hover {
<style>.hover-text-blue:hover {
color: #00a8ff;
}
Expand All @@ -514,11 +513,8 @@ test('remove inlined selectors', async t => {
@media (max-width: 600px) {
.ignore {color: blue}
}
</style>
<style>
.keep {margin: 0}
</style>
}</style>
<style>.keep {margin: 0}</style>
</head>
<body>
<div no-value id="keepId" class="keep foo-class" style="color: red; display: inline">
Expand All @@ -529,6 +525,30 @@ test('remove inlined selectors', async t => {
</body>
</html>`

const html2 = `<!DOCTYPE html>
<html>
<head><style>
img {
border: 0;
vertical-align: middle
}
</style></head>
<body>
<img src="https://example.com/image.jpg" style="border: 0; vertical-align: middle">
</body>
</html>`

const expectedNoEmptyStyleTags = `<!DOCTYPE html>
<html>
<head></head>
<body>
<img src="https://example.com/image.jpg" style="border: 0; vertical-align: middle">
</body>
</html>`

const basic = await Maizzle.removeInlinedClasses(html)
const noEmptyStyle = await Maizzle.removeInlinedClasses(html2)

const withPostHTMLOptions = await Maizzle.removeInlinedClasses(html, {
build: {
posthtml: {
Expand All @@ -539,10 +559,9 @@ test('remove inlined selectors', async t => {
}
})

const basic = await Maizzle.removeInlinedClasses(html)

t.is(withPostHTMLOptions, expectedWithOptions)
t.is(basic, expectedWithOptions)
t.is(basic, expectedHTML)
t.is(withPostHTMLOptions, expectedHTML)
t.is(noEmptyStyle, expectedNoEmptyStyleTags)
})

test('remove inlined selectors (disabled)', async t => {
Expand Down

0 comments on commit aae4813

Please sign in to comment.