Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectors #9

Open
chriscoyier opened this issue Aug 23, 2011 · 11 comments
Open

Selectors #9

chriscoyier opened this issue Aug 23, 2011 · 11 comments

Comments

@chriscoyier
Copy link

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;
}
@SlexAxton
Copy link
Collaborator

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

@SlexAxton
Copy link
Collaborator

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

@SlexAxton
Copy link
Collaborator

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

@chriscoyier
Copy link
Author

That'd be ideal

@davatron5000
Copy link
Owner

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.

@SlexAxton
Copy link
Collaborator

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;
}

@chriscoyier
Copy link
Author

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.

@SlexAxton
Copy link
Collaborator

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

@SlexAxton
Copy link
Collaborator

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;
}

@chriscoyier
Copy link
Author

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

@chriscoyier
Copy link
Author

Although, obviously, the selector needs to be:

RAPTOR:PANCAKE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants