Question

I've looked around all day yesterday for a working HTML validator that I could use to check the code generated by my rails application. So far, I've tried several different strategies such as RailsTidy, which no longer works as of Rails 1.9, tidy_ffi, which I've had a hard time "require"-ing when running in rails console, and the FireFox's Total Validator, which always gives me the same error regardless of what I'm checking.

My goal is to check my code while not having to upload anything to the internet. It would be most helpful to be able to run tests from the terminal (I'm using RedHat Linux) or to run tests on a running localhost server.

To save myself another day (or possibly more) of frustration, I've brought this question here and I'd like to know if anyone else has successfully worked with a validator for Ruby on Rails.

Was it helpful?

Solution

Try out my gem, html_acceptance. It uses HTML Tidy to do the validation under the hood. I put it out a while ago, but so far it has mostly gone unnoticed. It has been useful to me, and if you report issues/have feature requests I'll happily look at them.

In the samples, I have a custom RSpec matcher, so if you add that custom matcher, in your integration tests in spec/requests you can do:

page.should have_valid_html

The idea is you can use it within integration tests, and even if you get minor warnings/failures that you don't care about (TIDY complains about some IE specific hacks, for example), you can go in, accept them, and as long as the validation result is constant, the test will then pass.

Also, you need to have tidy on the path. So on OS X: port install htmltidy or Ubuntu sudo apt-get install tidy.

OTHER TIPS

I am working on an all-in-one HTML/CSS validation gem for Ruby On Rails apps. It's name is Headhunter.

From the docs:

Headhunter is an HTML and CSS validation tool that injects itself into your Rails feature tests and automagically checks all your generated HTML and CSS for validity.

In addition, it also looks out for unused (and therefore superfluous) CSS selectors.

All you have to do is add gem 'headhunter' to your Gemfile in test environment:

group :test do
  gem 'headhunter'
end

The rest is all done automagically for you, and you'll get a statistic for your application's validness at the end of your tests, like this:

Validated 42 HTML pages.
41 pages are valid.
1 page is invalid.
Open .validation/results.html to view full results.

Validated 1 stylesheets.
1 stylesheet is invalid.
  application.css:
  - Invalid css: line 1: Property bla doesn't exist

Found 23 CSS selectors.
20 selectors are in use.
3 selectors are not in use: a img, #flash.failure, input[type='file']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top