UPDATE: Refactored and tweaked the validation method below by popular request!
I know this might make some people groan - but that's OK - I'm used to it. As Eilon Lipton always tells me:
You're a PM dude - you're not supposed to code
Which is true! And to get the PM as Haack tradition alive, here is my latest attempt to completely devastate my reputation as a coder. Have at me, I love it.
Let's Put an X in Front Of HTML Too
The world's gone X-Crazy (XBox, OS X, ASPX pages, ActiveX...) and it seems that nothing's cool anymore unless there's an X associated with it. Maybe I can regain some of the rep I'm about to lose by changing my name to "RobX".
XRob?
Testing For Compliance
One of the main things that I get hammered for (with respect to the MVC Toolkit) is the lack of XHTML Compliance. I tried to pay as much attention as I could to it but... well some things just slip through. I read somewhere that my slip ups (particularly with respect to the method on the form tag not being in quotes was
...yet again evidence that Microsoft could care less [sic] about the HTML spec
On the contrary it was me, being lame.
I know what you're thinking - "can't you test for that somehow?" and up until now it meant copy/pasting a whole mess of HTML up to w3.org to run through the validator. But as of today I decided to let my Mr. Hack take over and created some code to ping w3.org automatically.
The Code
For my compliance Unit Tests (you wouldn't want to do this for every test, for obvious reasons) I'm creating a Select box, comme ci:
[TestMethod]
public void Select_BindToIntegerArray() {
int[] numbers = { 1, 2, 3 };
string select = SelectBuilder.Select("test", numbers,"","",0,false,null,null);
//validate it
Assert.IsTrue(XHTMLValidator.ValidateFragment(select));
}
UPDATE: Duncan Smart (?) refactored this function yet again - thanks!
And I'm calling on my new hacked up wunderclass- the XHTMLValidator. Here's the code - have at me and make it hurt:
public static bool IsValidXhtml(string htmlFragment) { NameValueCollection values = new NameValueCollection(); values["fragment"] = htmlFragment; values["prefill"] = "1"; values["prefill_doctype"] = "xhtml10"; WebClient webClient = new WebClient(); string postResult = Encoding.UTF8.GetString( webClient.UploadValues("http://validator.w3.org/check", values) ); //lame check - but it works bool isValid = postResult.Contains("Congratulations"); return isValid; }
Yes, I know. But you know what - it's an automatic way to make sure that my tags are compliant :).
If You're Still Here...
I'm also going to use the HTML Agility Pack that's up on CodePlex to make sure all the other bits that are supposed to be present in the generated HTML are indeed there. This is a really cool project for checking your HTML out - from there site:
This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).
Hope someone finds this helpful...
