Test HTML generated by HtmlAgility Pack valid

I want to make sure that the HTML that I generate using the HtmlAgility package is valid HTML5.

For example, the following creates an empty HTML5 document. I will have other, similary functions that will create more complex documents. I want the tests to verify that I don't blame anything, 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 can I create an "html5 validator" using the HtmlAgility Pack, where I could do something like this:

 Assert.IsTrue(IsValidHtml5(CreateEmptyHtmlDoc())); 

Thanks.

+4
source share
1 answer

w3c offers a free online validator. You can write a wrapper that sends your generated document for verification. Validate by Direct Input is likely to be the easiest option for automation. HTML5 validation is experimental. Check their terms of service if you do this on a scale.

http://validator.w3.org/#validate_by_input+with_options

validator.nu offers a similar service with the RESTful API.

+3
source

Source: https://habr.com/ru/post/1413625/


All Articles