Permalink
Browse files

Renamed be_valid_xhtml.rb to be_valid_html5.rb, changed class/method …

…names

accordingly and made associated changes across spec files. Now set up to
validate against HTML5 rather than XHTML.

BeValidHtml5 class now uses hpricot for manipulating the screen scraped
data from the W3C validator. Now works with Ruby 1.9. Also fixed how the
temp files that BeValidHTML5 creates are named.
  • Loading branch information...
1 parent 5530153 commit e89405da6a4f8e64fa41ec0d23bba49baa40fd88 @gaelian gaelian committed Sep 5, 2010
@@ -1,14 +1,15 @@
# Paste me into spec_helper.rb, or save me somewhere else and require me in.
+require 'net/http'
+require 'digest/md5'
+require 'hpricot'
-class BeValidXhtml
- require 'net/http'
- require 'digest/md5'
+class BeValidHtml5
def initialize(options)
@fragment = options[:fragment]
end
- # Assert that markup (html/xhtml) is valid according the W3C validator web service.
+ # Assert that markup is valid according the W3C validator web service.
# By default, it validates the contents of @response.body, which is set after calling
# one of the get/post/etc helper methods. You can also pass it a string to be validated.
# Validation errors, if any, will be included in the output. The input fragment and
@@ -38,11 +39,11 @@ def initialize(options)
def matches?(response)
- fn = response.rendered.to_s
+ fn = response.rendered[:template].template_path
fragment = response.body
- fragment = wrap_with_xhtml_header(fragment) if @fragment
+ fragment = wrap_with_html5_header(fragment) if @fragment
return true if validity_checks_disabled?
- base_filename = cache_resource('markup',fragment,'html',fn)
+ base_filename = cache_resource('markup',fragment,fn)
return false unless base_filename
results_filename = base_filename + '-results.yml'
@@ -54,10 +55,11 @@ def matches?(response)
File.open(results_filename, 'w+') do |f| Marshal.dump(response, f) end
end
markup_is_valid = response['x-w3c-validator-status'] == 'Valid'
- @message = ''
unless markup_is_valid
fragment.split($/).each_with_index{|line, index| message << "#{'%04i' % (index+1)} : #{line}#{$/}"} if @@display_invalid_content
- @message << XmlSimple.xml_in(response.body)['messages'][0]['msg'].collect{ |m| "Invalid markup: line #{m['line']}: #{CGI.unescapeHTML(m['content'])}" }.join("\n")
+ @message = "Invalid markup:\n"
+ @elements = Hpricot.XML(response.body).search("li.msg_err > span.msg")
+ (@elements).each { |span| @message << CGI.unescapeHTML(span.inner_html) + "\n" }
end
if markup_is_valid
return true
@@ -66,15 +68,12 @@ def matches?(response)
end
end
- def wrap_with_xhtml_header(fragment)
+ def wrap_with_html5_header(fragment)
ret = <<-EOS
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE html PUBLIC
- "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
- "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<!DOCTYPE html>
+<html dir="ltr" lang="en-US">
<head>
+ <meta charset="utf-8">
<title>Test</title>
</head>
<body>
@@ -85,11 +84,11 @@ def wrap_with_xhtml_header(fragment)
end
def description
- "be valid xhtml"
+ "be valid html5"
end
def failure_message
- " expected xhtml to be valid, but validation produced these errors:\n #{@message}"
+ " expected html5 to be valid, but validation produced these errors:\n #{@message}"
end
def negative_failure_message
@@ -110,19 +109,19 @@ def file_to_multipart(key,filename,mime_type,content)
"Content-Transfer-Encoding: binary\r\nContent-Type: #{mime_type}\r\n\r\n#{content}\r\n"
end
- def cache_resource(base,resource,extension,fn)
- resource_md5 = MD5.md5(resource).to_s
+ def cache_resource(base,resource,fn)
+ resource_md5 = Digest::MD5.hexdigest(resource).to_s
file_md5 = nil
output_dir = "#{RAILS_ROOT}/tmp/#{base}"
base_filename = File.join(output_dir, fn)
- filename = base_filename + extension
+ filename = base_filename
parent_dir = File.dirname(filename)
FileUtils.mkdir_p(parent_dir) unless File.exists?(parent_dir)
File.open(filename, 'r') do |f|
- file_md5 = MD5.md5(f.read(f.stat.size)).to_s
+ file_md5 = Digest::MD5.hexdigest(f.read(f.stat.size)).to_s
end if File.exists?(filename)
if file_md5 != resource_md5
@@ -142,10 +141,10 @@ def http
end
-def be_valid_xhtml
- BeValidXhtml.new
+def be_valid_html5
+ BeValidhtml5.new
end
-def be_valid_xhtml_fragment
- BeValidXhtml.new(:fragment => true)
+def be_valid_html5_fragment
+ BeValidHtml5.new(:fragment => true)
end
View
@@ -5,7 +5,7 @@
require 'spec'
require 'spec/rails'
-require File.expand_path(File.dirname(__FILE__) + "/be_valid_xhtml")
+require File.expand_path(File.dirname(__FILE__) + "/be_valid_html5")
Spec::Runner.configure do |config|
# If you're not using ActiveRecord you should remove these
@@ -2,7 +2,7 @@
describe "/admin/comments/index.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/comments/show.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/dashboard/show.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/pages/index.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/pages/new.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/pages/show.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/posts/index.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/posts/new.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/posts/show.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -2,7 +2,7 @@
describe "/admin/sessions/new.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it "renders" do
@@ -2,7 +2,7 @@
describe "/admin/undo_items/index.html.erb" do
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'should render' do
@@ -19,7 +19,7 @@ def tag(name)
end
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it 'renders posts grouped by month' do
@@ -13,7 +13,7 @@
end
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it "should render a page" do
@@ -19,7 +19,7 @@
end
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it "should render list of posts" do
@@ -36,7 +36,7 @@
end
after(:each) do
- response.should be_valid_xhtml_fragment
+ response.should be_valid_html5_fragment
end
it "should render a post" do

0 comments on commit e89405d

Please sign in to comment.