diff --git a/packages/mjml-core/src/helpers/skeleton.js b/packages/mjml-core/src/helpers/skeleton.js index daf705cc3..2731d1c41 100644 --- a/packages/mjml-core/src/helpers/skeleton.js +++ b/packages/mjml-core/src/helpers/skeleton.js @@ -1,4 +1,4 @@ -import { reduce, negate, isNil } from 'lodash' +import { map, reduce, negate, isNil, isFunction } from 'lodash' import buildPreview from './preview' import { buildFontsTags } from './fonts' import buildMediaQueriesTags from './mediaQueries' @@ -15,7 +15,7 @@ export default function skeleton(options) { headRaw = [], preview, title = '', - style, + style = [], forceOWADesktop, inlineStyle, lang, @@ -71,11 +71,9 @@ export default function skeleton(options) { '', )} - ${ - style && style.length > 0 - ? `` - : '' - } + ${headRaw.filter(negate(isNil)).join('\n')}
@@ -35,23 +35,31 @@ const input = ` ` -const html = mjml(input).html +const { html } = mjml(input) const $ = cheerio.load(html) // should put the attributes at the right place -chai.expect( - $('.text div').map(function getAttr() { - return $(this).attr('data-id') - }).get(), - 'Custom attributes added on texts', -).to.eql(['42', '42']) +chai + .expect( + $('.text div') + .map(function getAttr() { + return $(this).attr('data-id') + }) + .get(), + 'Custom attributes added on texts', + ) + .to.eql(['42', '42']) -chai.expect( - $('.image td').map(function getAttr() { - return $(this).attr('data-name') - }).get(), - 'Custom attributes added on image', -).to.eql(['43']) +chai + .expect( + $('.image td') + .map(function getAttr() { + return $(this).attr('data-name') + }) + .get(), + 'Custom attributes added on image', + ) + .to.eql(['43']) // should not alter templating syntax, or move the content that is outside any tag (mj-raws) const expected = [ @@ -63,14 +71,8 @@ const expected = [ '{ end if }', '{ item + 1 }', ] -const indexes = expected.map(str => html.indexOf(str)) +const indexes = expected.map((str) => html.indexOf(str)) -chai.expect( - indexes, - 'Templating syntax unaltered', -).to.not.include(-1) +chai.expect(indexes, 'Templating syntax unaltered').to.not.include(-1) -chai.expect( - sortBy(indexes), - 'Mj-raws kept same positions', -).to.deep.eql(indexes) +chai.expect(sortBy(indexes), 'Mj-raws kept same positions').to.deep.eql(indexes) diff --git a/packages/mjml/test/index.js b/packages/mjml/test/index.js new file mode 100644 index 000000000..b98cf46e2 --- /dev/null +++ b/packages/mjml/test/index.js @@ -0,0 +1,2 @@ +require('./html-attributes.test') +require('./lazy-head-style.test') diff --git a/packages/mjml/test/lazy-head-style.test.js b/packages/mjml/test/lazy-head-style.test.js new file mode 100644 index 000000000..ad489a677 --- /dev/null +++ b/packages/mjml/test/lazy-head-style.test.js @@ -0,0 +1,46 @@ +const chai = require('chai') +const spies = require('chai-spies') +const mjml = require('../lib') + +chai.use(spies) + +const { + HeadComponent, + registerComponent, +} = require('../../mjml-core/lib/index') + +const addStyle = chai.spy( + (breakpoint) => ` + @media only screen and (max-width:${breakpoint}) { + h1 { + font-size: 20px; + } + } + `, +) + +class HeadComponentWithFunctionStyle extends HeadComponent { + handler() { + const { add } = this.context + add('style', addStyle) + } +} +HeadComponentWithFunctionStyle.componentName = 'mj-head-component-with-function-style' +HeadComponentWithFunctionStyle.endingTag = true +HeadComponentWithFunctionStyle.allowedAttributes = {} + + +registerComponent(HeadComponentWithFunctionStyle) + +mjml(` +