Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix !important added to table attributes #385

Merged
merged 1 commit into from
May 22, 2021
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
21 changes: 16 additions & 5 deletions lib/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function inlineDocument($, css, options) {
}

var important = value.match(/!important$/) !== null;
if (important && !options.preserveImportant) value = value.replace(/\s*!important$/, '');
if (important && !options.preserveImportant) value = removeImportant(value);
// adds line number and column number for the properties as "additionalPriority" to the
// properties because in CSS the position directly affect the priority.
var additionalPriority = [style[i].position.start.line, style[i].position.start.col];
Expand Down Expand Up @@ -289,13 +289,17 @@ function inlineDocument($, css, options) {
if (juiceClient[dimension + 'Elements'].indexOf(elName) > -1) {
for (var i in el.styleProps) {
if (el.styleProps[i].prop === dimension) {
if (el.styleProps[i].value.match(/px/)) {
var pxSize = el.styleProps[i].value.replace('px', '');
var value = el.styleProps[i].value;
if (options.preserveImportant) {
value = removeImportant(value);
}
if (value.match(/px/)) {
var pxSize = value.replace('px', '');
$(el).attr(dimension, pxSize);
return;
}
if (juiceClient.tableElements.indexOf(elName) > -1 && el.styleProps[i].value.match(/\%/)) {
$(el).attr(dimension, el.styleProps[i].value);
if (juiceClient.tableElements.indexOf(elName) > -1 && value.match(/\%/)) {
$(el).attr(dimension, value);
return;
}
}
Expand All @@ -319,6 +323,9 @@ function inlineDocument($, css, options) {
if (styleProps.indexOf(el.styleProps[i].prop) > -1) {
var prop = juiceClient.styleToAttribute[el.styleProps[i].prop];
var value = el.styleProps[i].value;
if (options.preserveImportant) {
value = removeImportant(value);
}
if (prop === 'background') {
value = extractBackgroundUrl(value);
}
Expand All @@ -332,6 +339,10 @@ function inlineDocument($, css, options) {
}
}

function removeImportant(value) {
return value.replace(/\s*!important$/, '')
}

function findVariableValue(el, variable) {
while (el) {
if (variable in el.styleProps) {
Expand Down
5 changes: 5 additions & 0 deletions test/cases/juice-content/important.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
span {
color: blue;
}
table {
background-color: black !important;
width: 100% !important;
}
</style>
</head>
<body>
<p>hi</p>
<span>there</span>
<p style="color: blue !important">again</p>
<table></table>
</body>
</html>
1 change: 1 addition & 0 deletions test/cases/juice-content/important.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<p style="color: red !important;">hi</p>
<span style="color: blue;">there</span>
<p style="color: blue !important;">again</p>
<table style="background-color: black !important; width: 100% !important;" width="100%" bgcolor="black"></table>
</body>
</html>