-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an html validation check by looking at Nokogiri errors
- Loading branch information
1 parent
c30c183
commit e413f18
Showing
12 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# encoding: utf-8 | ||
|
||
class Html < ::HTML::Proofer::Checks::Check | ||
|
||
# new html5 tags (source: http://www.w3schools.com/html/html5_new_elements.asp) | ||
HTML5_TAGS = %w(article aside bdi details dialog figcaption | ||
figure footer header main mark menuitem meter | ||
nav progress rp rt ruby section summary | ||
time wbr datalist keygen output color date | ||
datetime datetime-local email month number | ||
range search tel time url week canvas | ||
svg audio embed source track video) | ||
|
||
def run | ||
@html.errors.each do |e| | ||
|
||
# Nokogiri (or rather libxml2 underhood) only recognizes html4 tags, | ||
# so we need to skip errors caused by the new tags in html5 | ||
next if HTML5_TAGS.include? e.to_s[/Tag ([\w-]+) invalid/o, 1] | ||
|
||
self.add_issue(e.to_s) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<head> | ||
<div> | ||
</div> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<html> | ||
<myfancytag></myfancytag> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
<a href="/test>A test link</a> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
spec/html/proofer/fixtures/html/opening_and_ending_tag_mismatch.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
<p>The quick <strong>brown fox</p> | ||
jumped over the | ||
<p>lazy</strong> dog.</p> | ||
</body> | ||
</html> |
4 changes: 4 additions & 0 deletions
4
spec/html/proofer/fixtures/html/unescaped_ampersand_in_attribute.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<html> | ||
<body data-something="up&down"> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "spec_helper" | ||
|
||
describe "Html test" do | ||
it "ignores an invalid tag by default" do | ||
html = "#{FIXTURES_DIR}/html/invalid_tag.html" | ||
output = capture_stderr { HTML::Proofer.new(html).run } | ||
output.should == "" | ||
end | ||
|
||
it "fails for an invalid tag" do | ||
html = "#{FIXTURES_DIR}/html/invalid_tag.html" | ||
output = capture_stderr { HTML::Proofer.new(html, {:validate_html => true}).run } | ||
output.should match /Tag myfancytag invalid/ | ||
end | ||
|
||
it "fails for an unmatched end tag" do | ||
html = "#{FIXTURES_DIR}/html/unmatched_end_tag.html" | ||
output = capture_stderr { HTML::Proofer.new(html, {:validate_html => true}).run } | ||
output.should match /Unexpected end tag : div/ | ||
end | ||
|
||
it "fails for an unescaped ampersand in attribute" do | ||
html = "#{FIXTURES_DIR}/html/unescaped_ampersand_in_attribute.html" | ||
output = capture_stderr { HTML::Proofer.new(html, {:validate_html => true}).run } | ||
output.should match /htmlParseEntityRef: expecting ';'/ | ||
end | ||
|
||
it "fails for mismatch between opening and ending tag" do | ||
html = "#{FIXTURES_DIR}/html/opening_and_ending_tag_mismatch.html" | ||
output = capture_stderr { HTML::Proofer.new(html, {:validate_html => true}).run } | ||
output.should match /Opening and ending tag mismatch: p and strong/ | ||
end | ||
|
||
it "fails for div inside head" do | ||
html = "#{FIXTURES_DIR}/html/div_inside_head.html" | ||
output = capture_stderr { HTML::Proofer.new(html, {:validate_html => true}).run } | ||
output.should match /Unexpected end tag : head/ | ||
end | ||
|
||
it "fails for missing closing quotation mark in href" do | ||
html = "#{FIXTURES_DIR}/html/missing_closing_quotes.html" | ||
output = capture_stderr { HTML::Proofer.new(html, {:validate_html => true}).run } | ||
output.should match /Couldn't find end of Start Tag a/ | ||
end | ||
end |