peterbe / premailer

Turns CSS blocks into style attributes
https://premailer.io
BSD 3-Clause "New" or "Revised" License
1.06k stars 188 forks source link

Contains word selector doesn't produce correct results #195

Open mikicz opened 6 years ago

mikicz commented 6 years ago

So I have a HTML email with styles similiar to:

<html>
<head>
<style>
.view a {
  color: green;
}

.view span[style~="color:"] a {
  color: inherit;
}
</style>

<body>
<div class="view">
   <span style="color: red">
     <a href="google.com">Link that should be red</a>
   </span>

   <a href="google.com">Link that should be green</a>
</div>
</body>
</html>

which should produce links of two different colors (open the HTML in a browser).

Using this library the HTML is transformed to the following HTML:

<html>
<head>
<style>.view span[style~="color:"] a {color:inherit}</style>

</head>
<body>
<div class="view">
   <span style="color: red">
     <a href="google.com" style="color:green">Link that should be red</a>
   </span>

   <a href="google.com" style="color:green">Link that should be green</a>
</div>
</body>
</html>

However, this snippet results in two links that are green. The problem is that the inlined color of the first link overrides the inherit from style element.

peterbe commented 6 years ago

Sorry to hear that. I have to admit that it's very unlikely that I'll be able to find the time to patch this. If you can take a stab at the code and figure out why it doesn't work, I can help you write and run a unit test.

mikicz commented 6 years ago

Quite understandable, don't worry. I worked around it by simply not having a default link color, just having the color of links always to inherit.

What should the correct behaviour be? Should the library recognise the ~= selector and apply it as other regular selectors?

peterbe commented 6 years ago

This library depends on cssselect which I suspect isn't powerful enough to understand a selector like span[style~="color:"]. Might be the reason.

mikicz commented 6 years ago

Ok. I might take a look into that sometimes, as I said, it's not really that important. Will submit a PR if I figure something out...