-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mjml-core): prevent empty style tags in head (#2682)
- Loading branch information
1 parent
988819d
commit 49535a0
Showing
4 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isFunction } from 'lodash' | ||
|
||
export function buildStyleFromComponents( | ||
breakpoint, | ||
componentsHeadStyles, | ||
headStylesObject, | ||
) { | ||
const headStyles = Object.values(headStylesObject) | ||
|
||
if (componentsHeadStyles.length === 0 && headStyles.length === 0) { | ||
return '' | ||
} | ||
|
||
return ` | ||
<style type="text/css">${[...componentsHeadStyles, ...headStyles].reduce( | ||
(result, styleFunction) => `${result}\n${styleFunction(breakpoint)}`, | ||
'', | ||
)} | ||
</style>` | ||
} | ||
|
||
export function buildStyleFromTags(breakpoint, styles) { | ||
if (styles.length === 0) { | ||
return '' | ||
} | ||
|
||
return ` | ||
<style type="text/css">${styles.reduce( | ||
(result, style) => | ||
`${result}\n${isFunction(style) ? style(breakpoint) : style}`, | ||
'', | ||
)} | ||
</style>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const chai = require('chai') | ||
const { load } = require('cheerio') | ||
const skeleton = require('../lib/helpers/skeleton') | ||
|
||
// The conditional style tag for Outlook does not get parsed by cheerio, | ||
// so each outputStyleCount excludes it | ||
const testValues = [ | ||
{ | ||
options: {}, | ||
outputStyleCount: 1, | ||
}, | ||
{ | ||
options: { | ||
componentsHeadStyle: [ | ||
() => '.custom-component-1 .custom-child { background: red; }', | ||
], | ||
}, | ||
outputStyleCount: 2, | ||
}, | ||
{ | ||
options: { | ||
headStyle: { | ||
'custom-component': () => | ||
'.custom-component .custom-child { background: orange; }', | ||
}, | ||
}, | ||
outputStyleCount: 2, | ||
}, | ||
{ | ||
options: { | ||
componentsHeadStyle: [ | ||
() => '.custom-component-1 .custom-child { background: yellow; }', | ||
], | ||
headStyle: { | ||
'custom-component': () => | ||
'.custom-component .custom-child { background: green; }', | ||
}, | ||
}, | ||
outputStyleCount: 2, | ||
}, | ||
{ | ||
options: { | ||
style: ['#title { background: blue; }'], | ||
}, | ||
outputStyleCount: 2, | ||
}, | ||
{ | ||
options: { | ||
componentsHeadStyle: [ | ||
() => '.custom-component-1 .custom-child { background: purple; }', | ||
], | ||
headStyle: { | ||
'custom-component': () => | ||
'.custom-component .custom-child { background: black; }', | ||
}, | ||
style: [() => '#title { background: white; }'], | ||
}, | ||
outputStyleCount: 3, | ||
}, | ||
] | ||
|
||
testValues.forEach((testUnit) => { | ||
const { options, outputStyleCount } = testUnit | ||
|
||
const $ = load(skeleton(options)) | ||
|
||
chai | ||
.expect($('head style').get().length, 'Unexpected number of style tags') | ||
.to.equal(outputStyleCount) | ||
}) |