html5_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in html5_validator.gemspec
4
+ gemspec
@@ -0,0 +1,68 @@
1
+ = html5_validator
2
+
3
+ * http://github.com/damian/html5_validator
4
+
5
+ == DESCRIPTION:
6
+
7
+ Ruby gem to test for valid HTML5 markup with RSpec. It also provides an interface validator.nu.
8
+
9
+ == INSTALL:
10
+
11
+ * gem install html5_validator
12
+
13
+ == MAIN USAGE:
14
+
15
+ describe TestController do
16
+ render_views
17
+
18
+ it "should be valid html5" do
19
+ get :new
20
+ response.body.should be_valid_html5
21
+ end
22
+ end
23
+
24
+ If your spec fails it outputs the validation error in your console.
25
+
26
+ == OTHER USAGE:
27
+
28
+ @validator = Html5Validator::Validator.new
29
+
30
+ # Validation a url
31
+ @validator.validate_uri('http://damiannicholson.com')
32
+
33
+ # Passing in an html string directly
34
+ @validator.validate_text(@html)
35
+
36
+ # Checking to see whether it's valid
37
+ @validator.valid?
38
+
39
+ # Viewing errors
40
+ @validator.errors
41
+
42
+
43
+ See more examples in the spec folder.
44
+
45
+ == LICENSE:
46
+
47
+ (The MIT License)
48
+
49
+ Copyright (c) 2011 Damian Nicholson
50
+
51
+ Permission is hereby granted, free of charge, to any person obtaining
52
+ a copy of this software and associated documentation files (the
53
+ 'Software'), to deal in the Software without restriction, including
54
+ without limitation the rights to use, copy, modify, merge, publish,
55
+ distribute, sublicense, and/or sell copies of the Software, and to
56
+ permit persons to whom the Software is furnished to do so, subject to
57
+ the following conditions:
58
+
59
+ The above copyright notice and this permission notice shall be
60
+ included in all copies or substantial portions of the Software.
61
+
62
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
63
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
64
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
65
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
66
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
67
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
68
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "html5_validator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "html5_validator"
7
+ s.version = Html5Validator::VERSION
8
+ s.authors = ["Damian Nicholson"]
9
+ s.email = ["damian.nicholson21@gmail.com"]
10
+ s.homepage = "http://github.com/damian/html5_validator"
11
+ s.summary = %q{Ruby gem to test for valid HTML5 markup with RSpec}
12
+ s.description = %q{Ruby gem to test for valid HTML5 markup with RSpec}
13
+
14
+ s.rubyforge_project = "html5_validator"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.add_dependency "json"
20
+ s.add_dependency "rest-client"
21
+ s.add_dependency "rspec", "~> 2.6.0"
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require
4
+ require 'json'
5
+ require 'rest_client'
6
+ require 'rspec'
7
+ require 'html5_validator/rspec'
8
+
9
+ module Html5Validator
10
+ class Validator
11
+ BASE_URI = 'http://html5.validator.nu'
12
+ HEADERS = { 'Content-Type' => 'text/html; charset=utf-8', 'Content-Encoding' => 'UTF-8' }
13
+ attr_reader :errors
14
+
15
+ def initialize(proxy = nil)
16
+ RestClient.proxy = proxy unless proxy.nil?
17
+ end
18
+
19
+ # Validate the markup of a String
20
+ def validate_text(text)
21
+ response = RestClient.post "#{BASE_URI}/?out=json", text, HEADERS
22
+ @json = JSON.parse(response.body)
23
+ @errors = retrieve_errors
24
+ end
25
+
26
+ # Validate the markup of a URI
27
+ def validate_uri(uri)
28
+ response = RestClient.get BASE_URI, :params => { :doc => uri, :out => 'json' }
29
+ @json = JSON.parse(response.body)
30
+ @errors = retrieve_errors
31
+ end
32
+
33
+ # TODO - Flesh out the file upload method
34
+ # Validate the markup of a file
35
+ def validate_file(file)
36
+ end
37
+
38
+ def inspect
39
+ @errors.map do |err|
40
+ "- Error: #{err['message']}"
41
+ end.join("\n")
42
+ end
43
+
44
+ def valid?
45
+ @errors.length == 0
46
+ end
47
+
48
+ private
49
+
50
+ def retrieve_errors
51
+ @json['messages'].select { |mssg| mssg['type'] == 'error' }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,12 @@
1
+ # Assert that the response is valid HTML5
2
+ RSpec::Matchers.define :be_valid_html5 do
3
+ validator = nil
4
+ match do |body|
5
+ validator = Html5Validator::Validator.new
6
+ validator.validate_text(body)
7
+ validator.valid?
8
+ end
9
+ failure_message_for_should do |actual|
10
+ validator.inspect
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Html5Validator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Hello world</title>
5
+ </head>
6
+ <body>
7
+ <!-- Missing closing section tag -->
8
+ <section>
9
+ </body>
10
+ </html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Hello world</title>
5
+ </head>
6
+ <body>
7
+ <section>
8
+ <a href="http://google.com">Google</a>
9
+ </section>
10
+ </body>
11
+ </html>
@@ -0,0 +1,96 @@
1
+ require 'html5_validator'
2
+
3
+ describe "Html5Validator" do
4
+
5
+ describe "when using the custom matcher" do
6
+ it "should not be valid html5 when supplied with invalid content" do
7
+ @html = File.open('spec/fixtures/invalid_html.html').read
8
+ @html.should_not be_valid_html5
9
+ end
10
+
11
+ it "should be valid html5 when supplied with valid content" do
12
+ @html = File.open('spec/fixtures/valid_html.html').read
13
+ @html.should be_valid_html5
14
+ end
15
+ end
16
+
17
+ before do
18
+ @validator = Html5Validator::Validator.new
19
+ end
20
+
21
+ it "should be an instance of Html5Validator::Validator" do
22
+ @validator.should be_an_instance_of Html5Validator::Validator
23
+ end
24
+
25
+ context "validating a uri" do
26
+ describe "when supplied with an invalid url" do
27
+
28
+ before do
29
+ @validator.validate_uri('http://google.co.uk')
30
+ end
31
+
32
+ it "should not be valid html5" do
33
+ @validator.valid?.should be_false
34
+ end
35
+
36
+ it "should return thirty-nine errors" do
37
+ @validator.errors.should have(39).items
38
+ end
39
+ end
40
+
41
+ describe "when supplied with a valid url" do
42
+
43
+ before do
44
+ @validator.validate_uri('http://damiannicholson.com')
45
+ end
46
+
47
+ it "should be valid html5" do
48
+ @validator.valid?.should be_true
49
+ end
50
+
51
+ it "should return zero errors" do
52
+ @validator.errors.should have(0).items
53
+ end
54
+ end
55
+ end
56
+
57
+ context "validating text" do
58
+ describe "when supplied with invalid html" do
59
+
60
+ before do
61
+ @html = File.open('spec/fixtures/invalid_html.html').read
62
+ @validator.validate_text(@html)
63
+ end
64
+
65
+ it "should not be valid html5" do
66
+ @validator.valid?.should be_false
67
+ end
68
+
69
+ it "should return two errors" do
70
+ @validator.errors.should have(2).items
71
+ end
72
+
73
+ it "should include an error complaining about the lack of a closing section tag" do
74
+ @validator.inspect.should include('Unclosed element')
75
+ end
76
+
77
+ end
78
+
79
+ describe "when supplied with valid html" do
80
+
81
+ before do
82
+ @html = File.open('spec/fixtures/valid_html.html').read
83
+ @validator.validate_text(@html)
84
+ end
85
+
86
+ it "should be valid html5" do
87
+ @validator.valid?.should be_true
88
+ end
89
+
90
+ it "should return no errors" do
91
+ @validator.errors.should have(0).items
92
+ end
93
+
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html5_validator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Damian Nicholson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rest-client
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 23
57
+ segments:
58
+ - 2
59
+ - 6
60
+ - 0
61
+ version: 2.6.0
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ description: Ruby gem to test for valid HTML5 markup with RSpec
65
+ email:
66
+ - damian.nicholson21@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files: []
72
+
73
+ files:
74
+ - .gitignore
75
+ - .rspec
76
+ - Gemfile
77
+ - README.rdoc
78
+ - Rakefile
79
+ - html5_validator.gemspec
80
+ - lib/html5_validator.rb
81
+ - lib/html5_validator/rspec.rb
82
+ - lib/html5_validator/version.rb
83
+ - spec/fixtures/invalid_html.html
84
+ - spec/fixtures/valid_html.html
85
+ - spec/html5_validator_spec.rb
86
+ homepage: http://github.com/damian/html5_validator
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project: html5_validator
115
+ rubygems_version: 1.8.10
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Ruby gem to test for valid HTML5 markup with RSpec
119
+ test_files:
120
+ - spec/fixtures/invalid_html.html
121
+ - spec/fixtures/valid_html.html
122
+ - spec/html5_validator_spec.rb