inline-css icon indicating copy to clipboard operation
inline-css copied to clipboard

Selector specificity not working for !important rules

Open arty-name opened this issue 9 years ago • 8 comments

When two selectors match the same element, and both have !important, then the selector specificity is ignored and the last rule wins:

inlineCss('<style>p a {color: red!important} a{color: black!important}</style>  <p><a>test', {url: '/'}).then(s=>console.log(s))
// '<p><a style="color: black;">test</a></p>'
inlineCss('<style>a{color: black!important} p a {color: red!important}</style>  <p><a>test', {url: '/'}).then(s=>console.log(s))
// '<p><a style="color: red;">test</a></p>'

However for the browsers it’s different. No matter which rule goes first, if both have !important in them, the more specific wins.

arty-name avatar Mar 14 '16 16:03 arty-name

Not sure what the issue is here.

jonkemp avatar Apr 06 '16 15:04 jonkemp

The first example should also output color: red

arty-name avatar Apr 06 '16 15:04 arty-name

Please be more general in what you think this module do in these cases.

jonkemp avatar Apr 06 '16 15:04 jonkemp

In general terms: while making styles inline inline-css should calculate selector specificity according to CSS specification, as browsers do. Currently it errs when two rules contain !important and match the same element. In that case inline-css thinks that the last rule should win. This is wrong: more specific rule should win.

arty-name avatar Apr 06 '16 15:04 arty-name

What if they are equal?

jonkemp avatar Apr 06 '16 15:04 jonkemp

If they are equal in specificity, then the last rule indeed wins.

arty-name avatar Apr 06 '16 15:04 arty-name

Ok, thanks for your feedback!

jonkemp avatar Apr 06 '16 15:04 jonkemp

One more thing. Today I've found out, that source of style is also ignored with regards to important. For the following HTML:

<style>
p {
  color: red !important;
}
</style>
<p style="color: blue !important">test</p>

the output will be:

<p style="color: red !important">test</p>

while it should be:

<p style="color: blue !important">test</p>

bandrzejczak avatar Jul 19 '16 12:07 bandrzejczak