From 0641866f4184c93dac73ebd88301a625d0418578 Mon Sep 17 00:00:00 2001 From: Paul Fernandez Date: Thu, 13 May 2021 20:52:16 -0700 Subject: [PATCH] Fix for #356 - preserveImportant: true adds !important to table attributes --- lib/inline.js | 21 ++++++++++++++++----- test/cases/juice-content/important.html | 5 +++++ test/cases/juice-content/important.out | 1 + 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/inline.js b/lib/inline.js index 866ee74..5a3c4cc 100644 --- a/lib/inline.js +++ b/lib/inline.js @@ -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]; @@ -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; } } @@ -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); } @@ -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) { diff --git a/test/cases/juice-content/important.html b/test/cases/juice-content/important.html index 0bd2bdf..2c05f93 100644 --- a/test/cases/juice-content/important.html +++ b/test/cases/juice-content/important.html @@ -7,11 +7,16 @@ span { color: blue; } + table { + background-color: black !important; + width: 100% !important; + }

hi

there

again

+
diff --git a/test/cases/juice-content/important.out b/test/cases/juice-content/important.out index 214d21c..8412b7c 100644 --- a/test/cases/juice-content/important.out +++ b/test/cases/juice-content/important.out @@ -6,5 +6,6 @@

hi

there

again

+