I'm looking for a library that can extract common css properties and add a class to the elements instead (I want to do this programatically, this is generated html, and I want to optimize it further by removing extra bytes). For instance I have a bunch of defined styles for elements:

```
#element1 {
    font-weight:bold;
    background-image:url("some_url");
}
#element2 {
    font-style:italic;
}
#element3 {
    font-weight:bold;
    font-style:italic;
    background-image:url("some_url");
}
#element4 {
    background-image:url("some_url");
}

............ (crapload of elements)

```

that gets folded into:

```
.bold {}
.italic {}
.backgroundimage {}

```

or even better:

```
.bold-and-italic {}
.italic-with-bg {}
etc etc

```

Is there a library out there that already does this? If there isn't, I could just go through and refcount all the properties, but is there a more optimal algorithm here? It would be simple to do one property per folded class, but I believe it would get more complicated doing one class with multiple properties ... What kind of data structure should be used here?