Class: Html::Test::Validator
- Inherits:
-
Object
- Object
- Html::Test::Validator
- Defined in:
- lib/validator.rb
Constant Summary collapse
- DEFAULT_W3C_URL =
For local validation you might change this to localhost/validator/htdocs/check
"http://validator.w3.org/check"
- DEFAULT_DTD =
File.join(File.dirname(__FILE__), 'DTD', 'xhtml1-strict.dtd')
- @@verbose =
verbose = true shows “validating …” verbose = false shows NOTHING
true
- @@revalidate_all =
revalidate_all = true will validate every call to a url revalidate_all = false will only validate the first call to a url
true
- @@tidy_ignore_list =
[]
- @@w3c_url =
DEFAULT_W3C_URL
- @@w3c_show_source =
Whether the W3C validator should show the HTML document being validated in its response. Set to 0 to disable.
"1"
Class Method Summary collapse
-
.dtd(document) ⇒ Object
Path to DTD file that the xmllint validator uses.
-
.tidy_errors(body) ⇒ Object
Validate an HTML document string using tidy.
-
.w3c_errors(body) ⇒ Object
Validate an HTML document string by going to the online W3C validator.
-
.xmllint_errors(body) ⇒ Object
Validate an HTML document string using the xmllint command line validator tool.
Class Method Details
.dtd(document) ⇒ Object
Path to DTD file that the xmllint validator uses
35 36 37 |
# File 'lib/validator.rb', line 35 def self.dtd(document) DEFAULT_DTD end |
.tidy_errors(body) ⇒ Object
Validate an HTML document string using tidy. Code excerpted from the rails_tidy plugin
41 42 43 44 45 46 47 48 |
# File 'lib/validator.rb', line 41 def self.tidy_errors(body) tidy = RailsTidy.tidy_factory tidy.clean(body) errors = tidy.errors.empty? ? nil : tidy.errors.delete_if { |e| tidy_ignore_list.select { |p| e =~ p }.size > 0 }.join("\n") tidy.release errors.blank? ? nil : errors end |
.w3c_errors(body) ⇒ Object
Validate an HTML document string by going to the online W3C validator. Credit for the original code goes to Scott Baron (htonl)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/validator.rb', line 52 def self.w3c_errors(body) response = Net::HTTP.post_form(URI.parse(w3c_url), {'ss'=>w3c_show_source, 'fragment'=>body}) status = response['x-w3c-validator-status'] if status != 'Valid' # Reference in the stylesheets response.body.sub!(%r{@import "./base.css"}, %Q{@import "#{File.dirname(w3c_url)}/base.css"}) response_file = find_unique_path(File.join(tmp_dir, "w3c_response.html")) # open(response_file, "w" ) { |f| f.puts(response.body) } # I was getting many errors like ... (in ruby 1.9.3 and rails 3) # Encoding::UndefinedConversionError: "\xE2" from ASCII-8BIT to UTF-8 # adding force_encoding('UTF-8') seems to fix this. open(response_file, "w" ) { |f| f.puts(response.body.force_encoding('UTF-8')) } "W3C status #{status}. Response from W3C was written to the file #{response_file}" else nil end end |
.xmllint_errors(body) ⇒ Object
Validate an HTML document string using the xmllint command line validator tool. Returns nil if validation passes and an error message otherwise. Original code taken from the book “Enterprise Integration with Ruby”
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/validator.rb', line 76 def self.xmllint_errors(body) error_file = create_tmp_file("xmllint_error") doc_file = command = nil if dtd(body) =~ /^doctype$/i # Use the DOCTYPE declaration doc_file = create_tmp_file("xmllint", body) command = "xmllint --noout --valid #{doc_file} &> #{error_file}" else # Override the DOCTYPE declaration doc_file = create_tmp_file("xmllint", body.sub(/<!DOCTYPE[^>]+>/m, "")) command = "xmllint --noout --dtdvalid #{dtd(body)} #{doc_file} &> #{error_file}" end system(command) status = $?.exitstatus if status == 0 return nil else failure_doc = File.join(tmp_dir, "xmllint_last_response.html") FileUtils.cp doc_file, failure_doc return ("command='#{command}'. HTML document at '#{failure_doc}'. " + IO.read(error_file)) end end |