css_parser icon indicating copy to clipboard operation
css_parser copied to clipboard

Algorithm for generating border shorthand is incorrect when individual side border declarations exist

Open willtcarey opened this issue 7 years ago • 2 comments

My client bought an email template that I'm trying to inline into our emails using premailer. The template has the following css in it

.bb {
  border-bottom: 1px solid; }

.bt,
.bb {
  border-color: #dfdfdf; }

After going through the expand_shorthand phase the declarations have properties like

"border-bottom-width" => { :value => "1px" },
"border-bottom-style" => { :value => "solid" },
"border-bottom-color" => { :value => "#dfdfdf"},
"border-top-color" => { :value => "#dfdfdf"},
"border-left-color" => { :value => "#dfdfdf"},
"border-right-color" => { :value => "#dfdfdf"}

Then it goes through the create_shorthands phase and when it runs create_dimensions_shorthand! all the border-color declarations are collapsed back into one property which is still correct.

"border-bottom-width" => { :value => "1px" },
"border-bottom-style" => { :value => "solid" },
"border-color" => { :value => "#dfdfdf"}

The problem occurs at the next phase when it runs create_border_shorthand!. It runs through the declarations checking for border-width border-style and border-color. And I do have a border color declaration so it picks that up and creates a shorthand out of it for border: #dfdfdf which then overrides the border-bottom-width and border-bottom-style declarations.

So my suggestion would be when creating border shorthands to first check if there are any individual side border styles set and if so bail out. An alternate way to fix this would be to ensure that the border-bottom-width and border-bottom-style styles are set after the border shorthand so they override the shorthand properties but I don't know if there's a good way to do that.

Do you have any thoughts?

willtcarey avatar Jun 29 '17 17:06 willtcarey

I have a similar problem

"border-left-style" => { :value => "solid" },
"border-width" => { :value => "1px"}

gets transformed into

"border-left-style" => { :value => "solid" },
"border" => { :value => "1px"}

But border and border-width aren't the same thing, as border rewrites width, style and color So in this case it's overwriting border-left-style (= we get no border while we should have one on the left)

So my suggestion would be when creating border shorthands to first check if there are any individual side border styles set and if so bail out. An alternate way to fix this would be to ensure that the border-bottom-width and border-bottom-style styles are set after the border shorthand so they override the shorthand properties but I don't know if there's a good way to do that.

We didn't have this issue in premailer 1.8.7 because the properties were sorted afterwards by name (which puts border before border-*) but this was removed in https://github.com/premailer/premailer/commit/d135e0bf040888aba3ac67d83a901d0ce58bfd92 Could be added back in Declarations#to_s to make sure specific css properties are set after more global properties, but if it was removed somewhere it's not to be added back elsewhere I suppose. It's probably also an easier fix than check the presence of individual side border property though :)

kalilz4485 avatar Feb 05 '21 13:02 kalilz4485

PR welcome, even if it's just a test that reproduces it 👍

grosser avatar Feb 05 '21 15:02 grosser