Whidbey support for HtmlHead, Part I

Note: this entry has moved.

Any current page that has more than a <title> element, i.e.:

 

<head>

     <title>Title One</title>

     <title>Titlo Two</title>

</head>

 

Is violating the HTML401 spec which says that only one <title> element is allowed.

 

But spec compliance aside, nothing stops current ASP.NET pages to specify such markup. The end result will be that both elements will be rendered and browsers (FireBird/Fox, poor-old IE, etc) will only take into account the first <title> found.

 

So far so good.

 

Now, while porting your pages to Whidbey you need to add a runat=”server” attribute to the <head> element in order for some of the built-in functionally to work.

 

Which one would you expect to render as the title? “Title One” or “Title Two”?

 

You know that the ASP.NET Team is pushing hard for zero breaking changes, so you would expect “Title One” to render, just in case you have some really ugly javascript code that depends on the page’s title.

 

But… if you try this against the March04 preview the browser will render “Title Two”…

 

So far not so good now.

 

If you look at the rendered markup you’ll notice that only a single <title> element is being rendered now, where on earth did the other <title> element went?

 

The answer lies down in the HtmlHead.AddParsedSubObject method which is coded in a way that when it detects a child of type HtmlTitle it saves a reference to it using a private member variable. It doesn’t check if it has already assigned an HtmlTitle already so when the second HtmlTitle child is processed it just overwrites the first one.

 

When it comes down to rendering the control has saved only one HtmlHead, and it is the last one processed thus we get a different <title> than the one we should get when running the page in v1.x bits.

 

I believe that modifying the HtmlHead.AddParsedSubObject to only save the first HtmlTitle child found and just ignoring any following childs of the same type would be a more friendly approach. (Of course what I really would like is to throw an exception yelling “Hey Joe, you have specified more than one <title> element, go read the spec!” but that could be considered a really breaker :-)  )

No Comments