There comes a point in every developer’s life when your realise the problem isn’t your work, but the tools you’ve got to hand. Toolsmithery is an important part of the job, and so I spent a few hours yesterday crafting a tool useful to any front-end developer.
The result is the CSS Redundancy Checker.
When you’re writing HTML, over time, your CSS files begin to fill up a lot. If you’re working on a large project, you might even end up with several people contributing to the CSS file, not to mention refactoring each other’s work. The result is a directory full of HTML files, and a very large CSS file.
What tends to happen is that not ever selector in the CSS file actually applies to your HTML; many are rendered redundant by refactoring, or by changes in HTML. But when you’ve got a 70k+ CSS file, it’s not easy to check precisely which selectors aren’t in use any more.
Enter the CSS Redundancy Checker. It’s a very simple tool, really. You pass in a single css file, and either a directory of HTML files, or a .txt file listing URLs (one to a line). It then proceeds to look at each file in turn, and at the end, list all the selectors in your css file that aren’t used by any of the HTML files.
That’s it. I’m pretty sure it’s accurate, and it should work with most CSS files. Most of the magic isn’t down to me, but down to _why the lucky stiff‘s marvelous Hpricot HTML parser. The script itself is about 50 lines of reasonably tidy Ruby. You’ll need Ruby, and Hpricot, in order to run it. There’s more full documentation over at the Google Code site where I’m hosting it. Please add any issues there, or get in touch if you want to contribute.
Things it doesn’t do: listing line numbers of where the selectors are. I wrote that functionality on the train this morning, but I can’t find a way to make it 100% accurate, so thought it best to leave it out – inaccurate line numbers are of no use to anyone. If you can come up with a way that works, let me know. Also, at some point I might turn it into a Textmate command. All in good time, though.
The need for the tool came out of a large project we’re working on at NPG, but I felt it would be useful to pretty much any HTML developer. So I’ve released it to the world. Let me know what you think, and do spread the word. You can get it via svn checkout, for now:
svn checkout http://css-redundancy-checker.googlecode.com/svn/trunk/ css-redundancy-checker
Jude Robinson said... 1
And many thankyous to you, Mr Armitage. I’ve wanted this for years. < runs off to use on that big NPG project />
Rob Lewis said... 2
Great idea for a tool, and very useful – I don’t suppose anyone has been good enough to get it up and running on a public website so that we don’t have to try installing it ourselves?
If I get chance, I may try doing that myself, if that’s allowed in the MIT licence?
Chris Heilmann said... 3
That is pretty sweet, but what about CSS classes that get dynamically added via JavaScript for unobtrusive JS functionality? It would be great if you could feed an extra object tha t has all those in it to avoid deletion.
mikko said... 4
omg, I praise you!
Jesper Lind said... 5
Havn’t tried it yet but this seems great. I wish there was on online version so that it would would on projects using .NET and PHP.
Rob: Please write a comment here again if you manage to put the tool online.
Rob said... 6
Doesn’t work.
mbp:~/rails/css-checker rob$ ruby css-redunancy-checker.rb mrdoe.css urls.txt
/usr/local/lib/ruby/gems/1.8/gems/hpricot-0.6/lib/hpricot/builder.rb:11: warning: `&’ interpreted as argument prefix
Parsing all html files within urls.txt for selectors in mrdoe.css…
————-
Parsing http://www.mrdoe.com/
Usage: css-redundancy-checker.rb [cssfile] [directory of html files OR .txt file listing urls to use]
mbp:~/rails/css-checker rob$
Jonathan said... 7
Oh yes. Yes, yes, yes. Yes. That is a good thing. *ah* That’s like drinking chocolate milk. Did you also post this to the TxD recipes page?
Denil Wickrama said... 8
I knew someone would get around to building this someday. Very useful, Thanks!
redsil said... 9
Just what the doctor ordered. I wonder why no-one has thought of this before? Great idea. Sometimes css files become so bloated, but I’m concerned that deleting rules with have an adverse effect on some obscure page in the site that I’ve completely forgotten about.
I wonder if this functionality could be biult into something like Firebug–orphaned css.
Thanks for putting this up!
shaz said... 10
really great tool! I didn’t try it out, but if it works, much thanks to you for it!! :)
Marc said... 11
@Rob Have you tryed adding require ‘open-uri’ on a new line after require ‘hpricot’
This fixed the problem you are having for me. Is this a bug?
Works great now…
Tom said... 12
That’s a big bug, sorry. I orginally had open-uri in there. No idea how it got removed. If you checkout/update the code again, it should work.
Apologies for the problem. Please list any bugs on the Google site – it’s the best way for me to track them.
bart said... 13
I’m really surprised that Dreamweaver doesn’t have this built into their reports or in form of some sort of checker. TopStyle Pro has this functionality.
Tom said... 14
Hi, I just happened to need this sort of functionality today, but didn’t want to install Ruby and Hpricot on my work machine (it’s not entirely mine), so I wrote a Greasemonkey script that logs unused CSS classes to the Firebug console.
Yeah, it’s pretty specific, but I figure some other web developers would have the same toolkit (and I’m mostly just making it for myself). It’s Available on my blog. ( http://blog.workingidea.com/article/6/todays-gizmo if urls are blocked)
Mike Malone said... 15
Hey, I just put together an online redundancy checker using Tom’s code. You can check it out at http://services.immike.net/css-checker/
Let me know what you think.
Romit said... 16
Thanks! I will use now! :D
Larry Voyer said... 17
Great idea!
Tried to use http://services.immike.net/css-checker/
Seems to fall apart if you have .php pages
bummer
Lee Bryant said... 18
I am writing this from the future. Thank you. This was just what I told somebody *must* exist today, and I am glad they found it. It had to be you in retrospect ;-)
Matt said... 19
This *is* pretty slick. There’s Firefox extension that does this, as well: Dust-Me Selectors; I believe written by someone associated with SitePoint.
On a related note, does anyone know of a tool/script/extension that will do essentially the REVERSE of this? That is, scan a pile of markup and list all the class and ID attribute values that may appear? Perhaps for a single document, or a whole site tree?
I’m looking for something to hunt through a bunch of JSP code that I’m reticent to change by hand, but need to create CSS for. I’d like to discover all the class and ID values that are in there.
Matt said... 20
…oh, and I already know that the Web Developer Firefox extension will essentially *show* me the classes and IDs, but it’s a bear to look at, and it’d be nice if I could just get a list parsed out.
Anthony said... 21
For those who, like me, spent ages searching for the greasemonky version but found all the links were dead you can find it here http://userscripts.org/scripts/show/11074
Awsome work both of you!
Trackback: Ajaxian » CSS Redundancy Checker
Trackback: ArtLung Blog » Daily Links
Trackback: The CSS Redundancy Checker - Limpia tu CSS de estilos que no uses // menéame
Trackback: Flatline Web Design » CSS Redundancy Checker
Trackback: Chad’s dailies » Blog Archive » links for 2007-07-07
Trackback: test 07/07/2007 « Strange Kite
Trackback: Vector Ninja, HIYAH!!!! » Blog Archive » CSS Redundancy Checker
Trackback: CSSDeveloper.net » Blog Archive » The CSS Redundancy Checker
Trackback: Links For Monday 9th July 2007 at Digitalia
Trackback: OmgSaveMe » Blog Archive » CSS Redundancy Checker
Trackback: links for 2007-07-07 « toonz
Trackback: Infovore » CSS Redundancy Checker: jQuery edition
Trackback: CSS failu pÄrbaude » Latvijas interneta pods
Trackback: Der CSS-Checker reinigt aufgeblasene CSS-Dateien | Dr. Web Weblog
Trackback: CSS Checker - MacBeat
Trackback: ToolTipping: Infovore Releases CSS Redundancy Checker
Trackback: Extensive CSS List: 134 Top CSS Resources << Vandelay Website Design
Trackback: Nerdcore — links for 2007-07-12
Trackback: Links for the Weekend, 7-14-2007
Trackback: links for 2007-07-15 | MY Vast Right Wing Conspiracy
Trackback: The Programming and Management Blog » Developer Links for 15/07/2007
Trackback: Links for 7/15/07 [my NetNewsWire tabs]
Trackback: links for 2007-07-07
Trackback: bluno.org » CSS Redundancy Checker
Trackback: CSS Redundancy Checker - I’m Mike
Trackback: links for 2007-07-31 at The New Reader
Trackback: Проверка CSS на избыточноÑть. | Каждому Ñвоё...
Trackback: CSS Redundancy Checker at infotexture
Trackback: Finding Redundant CSS-Code | Erik's Lab
Trackback: Infovore » The CSS Redundancy Checker » dontcom.com - Personal website of Darren Wood
Trackback: » CSS Collection II 2008 CSS Concept: CSS can be just that easy..
Trackback: Dust-Me Selectors | What did you mean ?
Trackback: The CSS Redundancy Checker | What did you mean ?