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

CSS selectors merging #29

Closed
GoalSmashers opened this issue Nov 18, 2012 · 14 comments
Closed

CSS selectors merging #29

GoalSmashers opened this issue Nov 18, 2012 · 14 comments
Assignees
Milestone

Comments

@GoalSmashers
Copy link
Contributor

Raised by @XhmikosR

Another thing I was wondering, would it be very hard for clean-css to merge the selectors when they are the same? For example:

foo { font: Sans-serif; }
foo { font-weight: bold; }

can become:

foo {
    font: Sans-serif;
    font-weight: bold;
}
@XhmikosR
Copy link
Contributor

@GoalSmashers: Thanks, for opening a new issue for this; I should have done it myself already.

The selectors merging can be improved even further when it's possible to use the shorthand notation like in this case:

border-color: blue;
border-style: inset;
border-width: 1px;

can be

border: 1px inset blue;

EDIT: Refs #134.

@GoalSmashers
Copy link
Contributor Author

That could be even more tricky than selectors merging!
But thanks for raising it.

On Nov 18, 2012, at 10:07 AM, XhmikosR wrote:

@GoalSmashers: Thanks, for opening a new issue for this; I should have done it myself already.

The selectors merging can be even further improved when it's possible to use the shorthand notation like in this case:

border-color: blue;
border-style: inset;
border-width: 1px;

can be

border: 1px inset blue;

Reply to this email directly or view it on GitHub.

@thezoggy
Copy link

https://github.com/css/csso
you can see how they do their merging: https://github.com/css/csso/blob/master/lib/compressor.js

@GoalSmashers
Copy link
Contributor Author

Thanks @thezoggy. They go CSS parsing way whereas clean-css does all via RegExp replacements. And that's a totally different story.

@thezoggy
Copy link

pretty impressive all the things it can do if you look at their test examples.. wonder if that approach makes sense or if there is a lexical top down approach (cant recall the proper term) that would be much more flexible and extensible. at least the color safe optimizations is worth referencing :).

@GoalSmashers
Copy link
Contributor Author

It is much more powerful method but at the price of being much slower (by factor of 10), as it requires much more computational effort to build a parsed tree.

If we get to the point of building selectors merging I am sure it would be a quite different approach.

Thanks for pointing out more safe color optimizations!

@XhmikosR
Copy link
Contributor

In my opinion, this should be a higher priority. Many times it's much easier to specify a specific property instead of extending the current one. So the example from my first comment is very common. And this approach would really offer much greater compression.

I hope you can reconsider this. :)

@GoalSmashers
Copy link
Contributor Author

Not before 1.0 as there are more important things than this.

I wonder if it'd provide more than ~1% improvement in size though.

@XhmikosR
Copy link
Contributor

Well, you shouldn't have in mind well organized css files...

@CatmanIX
Copy link

CatmanIX commented Mar 3, 2013

I would also suggest merging selectors with the same properties + values. Such as:

a {margin: 3px}
div {margin: 3px}

should become

a, div {margin: 3px}

I know from testing myself, after already using clean-css on some of my projects and then using CSStidy just to merge selectors in this fashion, i can get 40-50% size improvements. I'd say it's well worth the time to try implementing this.

@GoalSmashers
Copy link
Contributor Author

Thanks @CatmanIX! Once we get into attributes merging we'll definitely take a look into selectors merging too.

@justinmarsan
Copy link

Keep in mind that when a selector becomes too long, IE8 will ignore it (issue i'm running I'm having right now with compass-generated sprites) in which case doing the opposite (splitting long aggregated selectors) would actually help. Maybe a safe mode or something like that.

@GoalSmashers
Copy link
Contributor Author

Thanks for pointing that out. At first it will for sure be an optional feature.

@GoalSmashers
Copy link
Contributor Author

Fixed in 1459d7f
Follow up in #134

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

5 participants