A Ruby wrapper for the World Wide Web Consortium’s online validation services.
Clone or download
doc75 Merge pull request #42 from doc75/travis_update
add ruby 2.5 test + bump version to 1.3.4
Latest commit 054cc7c Aug 14, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib add ruby 2.5 test + bump version to 1.3.4 Aug 13, 2018
test Use https for css validator Aug 10, 2018
.gitignore Fix Gemfile.lock on gitignore Aug 10, 2018
.travis.yml add ruby 2.5 test + bump version to 1.3.4 Aug 13, 2018
CHANGELOG add ruby 2.5 test + bump version to 1.3.4 Aug 13, 2018
Gemfile fix version issue in validator Oct 27, 2016
LICENSE No commit message Nov 9, 2007
README.md update version to 1.3.1 Dec 18, 2016
Rakefile
w3c_validators.gemspec change pre-req for json gem from ~> 2.0 to >= 1.8 to relax constraint Jan 21, 2017

README.md

W3C Validators Gem README

Build Status

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:

  validator = NuValidator.new(:validator_uri => 'http://localhost/check')

Using a proxy server

You can use a proxy server by passing in its information in the contructor.

  validator = NuValidator.new(:proxy_host => 'proxy.example.com',
                              :proxy_port => 80,
                              :proxy_user => 'optional',
                              :proxy_pass => 'optional')

Examples

Example #1: Nu validator, local file

  require 'w3c_validators'
  
  include W3CValidators

  @validator = NuValidator.new
  
  file = File.dirname(__FILE__) + '/fixtures/valid_html5.html'
  results = @validator.validate_file(fp)

  if results.errors.length > 0
    results.errors.each do |err|
      puts err.to_s
    end
  else
    puts 'Valid!'
  end

Example #2: Feed validator, remote file

  require 'w3c_validators'
  
  include W3CValidators

  @validator = FeedValidator.new

  results = @validator.validate_uri('http://example.com/feed.xml')

  if results.errors.length > 0
    results.errors.each do |err|
      puts err.to_s
    end
  else
    puts 'Valid!'
  end

Example #3: CSS validator, text fragment

  require 'w3c_validators'
  
  include W3CValidators

  @validator = CSSValidator.new

  results = @validator.validate_text('body { margin: 0px; }')

  if results.errors.length > 0
    results.errors.each do |err|
      puts err.to_s
    end
  else
    puts 'Valid!'
  end

Example #4: Markup validator, local file

  require 'w3c_validators'
  
  include W3CValidators

  @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)

  if results.errors.length > 0
    results.errors.each do |err|
      puts err.to_s
    end
  else
    puts 'Valid!'
  end
  
  puts 'Debugging messages'
  
  results.debug_messages.each do |key, value|
    puts "#{key}: #{value}"
  end

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.

Credits and code

Source is available on GitHub

Written by Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2007.

Thanks to Ryan King for creating the 0.9.2 update.

Thanks to Ryan King, Jonathan Julian and Sylvain LaFleur for creating the 0.9.3 update.

Thanks to James Rosen and Roman Shterenzon for creating the 1.0.1 update.