Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,9 @@ export default function makeJuiceClient(juiceClient) {
try {
declarations = utils.parseCSS(cssText, { strict: true })[0][1];
} catch (err) {
// Template tags between declarations (`{{#if x}} color: red; {{/if}}`)
// aren't valid CSS, so keep the attribute as-is. The inlined styles
// go in front of it, so its declarations still win.
if (!/JUICE_CODE_BLOCK_\d+_/.test(styleAttributeValue)) {
throw err;
}
// An attribute that isn't valid CSS (template tags between declarations,
// encoded quotes, generated junk...) is kept as-is, like email clients
// do. The inlined styles go in front of it, so its declarations still win.
el.rawStyleAttribute = styleAttributeValue.trim();
}
if (declarations) {
Expand Down
18 changes: 12 additions & 6 deletions test/juice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ it('test that preserved text order is stable', function() {
});

it('can handle style attributes with html entities', function () {
// Throws without decodeStyleAttributes: true
expect(() => {
juice('<style type="text/css">div {color: red;}</style><div style="font-family:&quot;Open Sans&quot;, sans-serif;"></div>');
}).toThrow();
// Without decodeStyleAttributes, the attribute can't be parsed, so it's kept as-is
expect(
juice('<style type="text/css">div {color: red;}</style><div style="font-family:&quot;Open Sans&quot;, sans-serif;"></div>')
).toBe('<div style="color: red; font-family:&quot;Open Sans&quot;, sans-serif;"></div>');

// Expected results with decodeStyleAttributes: true
expect(
Expand Down Expand Up @@ -1062,8 +1062,14 @@ describe('code blocks inside style attributes', function() {
.toBe('<div class="somediv" style="color: blue; border-top: 1px solid {{color}};"></div>');
});

it('still throws on invalid style attributes without template tags', function() {
expect(() => juice(css + '<div class="somediv" style="font-family: &quot;Inter&quot;"></div>')).toThrow(/Unknown word/);
it('keeps other style attributes that are not valid CSS as they are too', function() {
expect(juice(css + '<div class="somediv" style="font-family: &quot;Inter&quot;"></div>'))
.toBe('<div class="somediv" style="color: blue; font-family: &quot;Inter&quot;"></div>');
});

it('does not throw on generated junk in style attributes (issue #582)', function() {
expect(juice('<style>p { color: red; }</style><p style="undefined;;;undefined">x</p><span style="undefined">y</span>'))
.toBe('<p style="color: red; undefined;;;undefined">x</p><span style="undefined">y</span>');
});
});

Expand Down
Loading