You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
W3C Validators is a Ruby wrapper for the World Wide Web Consortium's online
validation services.
It supports the nu validator, the feed validator and the CSS validator.
Installation
gem install w3c_validators
Usage
There are three main validator classes available, the W3CValidators::NuValidator
(used for HTML), the W3CValidators::FeedValidator and the W3CValidators::CSSValidator.
Warning: The W3CValidators::MarkupValidator also exist but is not anymore the preferred
way to check HTML document. Indeed, it is working fine for non-HTML5 documents,
but it is broken when you test an HTML5 document due to W3C redirection. W3CValidators::NuValidator
should be used instead for standard cases.
Each validator has offers three different validation methods.
validate_text methods take a string
validate_file methods take a path to a file or an IO object
validate_uri methods take a published URL
In addition, the W3CValidators::MarkupValidator has a validate_uri_quickly method, which
performs a HEAD request against the markup validation service. The Results
of this call give an error count but no error details.
Using a local validator
Each of the three validators allows you to specify a custom path to the
validator. You can set your own validator like this:
require'w3c_validators'includeW3CValidators@validator=MarkupValidator.new# override the DOCTYPE@validator.set_doctype!(:html32)# turn on debugging messages@validator.set_debug!(true)file=File.dirname(__FILE__) + '/fixtures/markup.html'results=@validator.validate_file(fp)ifresults.errors.length > 0results.errors.eachdo |err|
putserr.to_sendelseputs'Valid!'endputs'Debugging messages'results.debug_messages.eachdo |key,value|
puts"#{key}: #{value}"end
Examples with Ruby Frameworks
# you can easily incorporate this in your ruby based frameworks:
# Gemfile
group :test do
gem 'w3c_validators'
end
# And in your relevant test file:
require 'w3c_validators'
class FoosControllerTest < ActionDispatch::IntegrationTest
setup do
@validator = W3CValidators::NuValidator.new
end
test "index" do
get foos_url
assert_equal 0, @validator.validate_text(response.body).errors.length
end
end
# granted it's not perfect, but hopefully that will at least get you going
# you might want to customise things so that it delivers a particular output in case an error shows up.
Tests
Run unit tests using rake test. Note that there is a one second delay
between each call to the W3C's validators per their request.