Sunday, December 17, 2006

I'm working on making my CSS stylesheets validate completely with the w3c css validator. This means no errors and no warning messages.

I'm using the excellent assert_valid_asset plugin for Ruby on Rails to do my validation. It's really easy to use, just go:

assert_valid_css_files 'lmg'

Substituting your stylesheet name for 'lmg'.

However, it currently just looks for error messages. I added the following little snippet to look for warnings as well:

# Check for warnings
warning_messages = []
REXML::XPath.each(, "//div[@id='warnings']/div/ul/li") do |element|
warning_messages << element.to_s.gsub(/>[^<]+>/,' ').gsub(/\n/,' ').gsub(/\s+/, ' ')
if warning_messages.length > 0
warning_message = warning_messages.join("\n")
flunk("CSS Validation warning:\n#{warning_message}")