diff --git a/lib/inline.js b/lib/inline.js index af24fb9..083b8c5 100644 --- a/lib/inline.js +++ b/lib/inline.js @@ -491,6 +491,15 @@ module.exports = function makeJuiceClient(juiceClient) { function juiceDocument($, options) { options = utils.getDefaultOptions(options); + + // Track data-embed style elements before getStylesData removes the attribute + var embedStyleElements = new Set(); + if (options.removeInlinedSelectors && !options.removeStyleTags) { + $('style[data-embed]').each(function() { + embedStyleElements.add(this); + }); + } + var css = extractCssFromDocument($, options); css += '\n' + options.extraCss; @@ -498,13 +507,13 @@ module.exports = function makeJuiceClient(juiceClient) { // If removeInlinedSelectors is enabled, update style tags to remove inlined rules if (inlinedSelectors && !options.removeStyleTags) { - updateStyleTags($, inlinedSelectors, options); + updateStyleTags($, inlinedSelectors, options, embedStyleElements); } return $; } - function updateStyleTags($, inlinedSelectors, options) { + function updateStyleTags($, inlinedSelectors, options, embedStyleElements) { var stylesList = $('style'); stylesList.each(function() { var styleElement = this; @@ -514,7 +523,8 @@ module.exports = function makeJuiceClient(juiceClient) { return; } - if ($(styleElement).attr('data-embed') !== undefined) { + // Skip data-embed style elements (attribute may have been removed by getStylesData) + if (embedStyleElements && embedStyleElements.has(styleElement)) { return; } diff --git a/test/juice.test.js b/test/juice.test.js index 2b9eee0..108e2ef 100644 --- a/test/juice.test.js +++ b/test/juice.test.js @@ -406,6 +406,16 @@ it('removeInlinedSelectors', function() { assert.ok(result.indexOf('
Test
', + { removeStyleTags: false, removeInlinedSelectors: true } + ); + assert.ok(result.indexOf('style="color: blue;"') > -1, 'div styles should be inlined'); + assert.ok(result.indexOf('') > -1, 'data-embed style tag content should be completely untouched'); + assert.ok(result.indexOf('data-embed') === -1, 'data-embed attribute should be removed'); +}); + it('/* juice ignore */ (entire file)', function () { var css = '/* juice ignore */\nbody { color: red; }\n.test { color: blue; }'; var html = '
Hello
';