Skip to content

Commit

Permalink
Switch to Nokogiri::HTML for html writer
Browse files Browse the repository at this point in the history
By switching to the HTML parser from the XML parser, we can better handle malformed html documents and save out something that at least marginally resembles the input HTML.
  • Loading branch information
hotgazpacho authored and kytrinyx committed Apr 5, 2014
1 parent f97c7a7 commit 05dc7f6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/approvals/writers/html_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def extension
end

def format(data)
Nokogiri::XML(data.to_s.strip,&:noblanks).to_xhtml(:indent => 2, :encoding => 'UTF-8')
Nokogiri::HTML(data.to_s.strip,&:noblanks).to_xhtml(:indent => 2, :encoding => 'UTF-8')
end

end
Expand Down
11 changes: 11 additions & 0 deletions spec/approvals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def hello.inspect
Approvals.verify html, :format => :html, :namer => namer
end

it "verifies a malformed html fragment" do
html = <<-HTML
<!DOCTYPE html>
<html>
<title>Hoi</title>
<script async defer src="http://foo.com/bar.js"></script>
<h1>yo</h1>
HTML
Approvals.verify html, :format => :html, :namer => namer
end

it "verifies xml" do
xml = "<xml char=\"kiddo\"><node><content name='beatrice' /></node><node aliases='5'><content /></node></xml>"
Approvals.verify xml, :format => :xml, :namer => namer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hoi</title>
<script async="" defer="defer" src="http://foo.com/bar.js"></script>
</head>
<body>
<h1>yo</h1>
</body>
</html>

0 comments on commit 05dc7f6

Please sign in to comment.