Reputation: 3483
I want to make sure the HTML that I generate with the HtmlAgility pack is valid HTML5.
For example, the following creates an empty HTML5 document. I will have other, simliar functions that will create more complex documents. I want tests that verify that I didn't screw something up and the resulting HTML is well formed and valid.
public static String CreateEmptyHtmlDoc()
{
var hd = new HtmlDocument();
var doctype = hd.DocumentNode.SelectSingleNode("/comment()[starts-with(.,'<!DOCTYPE')]");
if (doctype == null)
doctype = hd.DocumentNode.PrependChild(hd.CreateComment());
doctype.InnerHtml = "";
hd.DocumentNode.AppendChild(
HtmlNode.CreateNode(
"<HTML><HEAD><META CHARSET=\"UTF-8\"><TITLE></TITLE></HEAD><BODY></BODY></HTML>"));
return hd.DocumentNode.OuterHtml;
}
How would I build a "html5 validator" using HtmlAgility Pack where I could do something like this:
Assert.IsTrue(IsValidHtml5(CreateEmptyHtmlDoc()));
Thanks.
Upvotes: 4
Views: 759
Reputation: 150148
w3c offers a free online validator. You could write a wrapper that submits your generated document for validation. Validate by Direct Input would probably be the easiest option to automate. HTML5 validation is experimental. Check their terms of service if you do this at scale.
http://validator.w3.org/#validate_by_input+with_options
validator.nu offers a similar service with a RESTful API.
Upvotes: 3