deCSS3 icon indicating copy to clipboard operation
deCSS3 copied to clipboard

Selectors

Open chriscoyier opened this issue 14 years ago • 11 comments

The reason I ultimately gave up on mine was that selectors are way harder to remove than properties and values. Ultimately it would be cool if this could negate selectors...

li:nth-last-child(2) {
     font-weight: bold;
}

so I guess it would find that, and either somehow remove it or go through each property and negate it:

li:nth-last-child(2) {
     font-weight: normal;
}

chriscoyier avatar Aug 23 '11 16:08 chriscoyier

seems like in this case we could just delete the whole rule, no?

SlexAxton avatar Aug 23 '11 17:08 SlexAxton

especially because 'normal' might not be the inherited style.

SlexAxton avatar Aug 23 '11 17:08 SlexAxton

/(.*?)\:nth-last-child\(\d\)\s*\{.*\}/ig or something similar would create the match, and then I could just remove the entire rule.

SlexAxton avatar Aug 23 '11 17:08 SlexAxton

That'd be ideal

chriscoyier avatar Aug 23 '11 17:08 chriscoyier

Here's a list of all the selectors by CSS version http://www.w3.org/TR/css3-selectors/#selectors

Quite a bit of regex going on... but I think we tackle each selector one by one. I'll maybe work on those this evening.

davatron5000 avatar Aug 23 '11 19:08 davatron5000

It may actually be easier than all that. Since we are going through, rule by rule, we could naiively do something like this:

if ( rule.match(/\:nth-last-child/g) ) {
   newRule = "";
}

BUT i think when you go through the rules that have multiple selectors, one of which is valid, another which isn't, it gets hairier.

li:nth-last-child(2),
li.last {
  border: bold;
}

So maybe we keep the rule but turn the pseudo selector into something nonsensical, but valid:

  rule.replace( /\:nth-last-child\(\d+\)/g, '.supermegafarts' );
li.supermegafarts,
li.last {
  border: bold;
}

SlexAxton avatar Aug 23 '11 19:08 SlexAxton

Important fact: if part of a selector is invalid, the whole thing becomes invalid.

li:nth-last-child(2),
strong {
   color: red;
}

In a browser that doesn't support :nth-list-child, strongs will NOT be red. That's a fact for every browser in the world that I know of, except IE 7. So not sure what the best plan would be there. But overall I think this becomes easier.

chriscoyier avatar Aug 23 '11 20:08 chriscoyier

oh, hmm. that actually might end up being harder now that i look at the code. :/ I'll see what I can do.

SlexAxton avatar Aug 23 '11 20:08 SlexAxton

OH EASY

Just replace the rule with something invalid! Then IE (if we supported it...) would technically have the right behavior, etc.

li:nth-last-child(2),
strong {
   color: red;
}

becomes

INVALIDSELECTOR:INVALID,
strong {
   color: red;
}

SlexAxton avatar Aug 23 '11 20:08 SlexAxton

That's perfect, and solves the IE 7 issue too.

chriscoyier avatar Aug 23 '11 20:08 chriscoyier

Although, obviously, the selector needs to be:

RAPTOR:PANCAKE

chriscoyier avatar Aug 23 '11 20:08 chriscoyier