Is the ampersand the only character that should be encoded in an HTML attribute?
It's well known that this won't pass validation:
<a href="http://domain.com/search?q=whatever&lang=en"></a>
Because the ampersand should be &
. Here's a direct link to the validation fail.
This guy lists a bunch of characters that should be encoded, but he's wrong. If you encode the first "/" in http://
the href won't work.
In ASP.NET, is there a helper method already built to handle this? Stuff like Server.UrlEncode and HtmlEncode obviously don't work - those are for different purposes.
I can build my own simple extension method (like .ToAttributeView()
) which does a simple string replace.
The purpose of escaping characters is so that they won't be processed as arguments. So you actually don't want to encode the entire url, just the values you are passing via the querystring. For example:
The url you showed is actually a perfectly valid url that will pass validation. However, the browser will interpret the
&
symbols as a break between parameters in the querystring. So your querystring:Will actually be translated by the recipient as two parameters:
For your url to work you just need to ensure that your values are being encoded:
Edit: The common problems page from the W3C you linked to is talking about edge cases when urls are rendered in html and the
&
is followed by text that could be interpreted as an entity reference (©
for example). Here is a test in jsfiddle showing the url:http://jsfiddle.net/YjPHA/1/
In Chrome and FireFox the links works correctly, but IE renders
©
as ©, breaking the link. I have to admit I've never had a problem with this in the wild (it would only affect those entity references which don't require a semicolon, which is a pretty small subset).To ensure you're safe from this bug you can HTML encode any of your URLS you render to the page and you should be fine. If you're using ASP.NET the
HttpUtility.HtmlEncode
method should work just fine.In HTML attribute values, if you want ", '&' and a non-breaking space as a result, you should (as an author who is clear about intent) have ", & and in the markup.
For " though, you don't have to use " if you use single quotes to encase your attribute values.
For HTML text nodes, in addition to the above, if you want < and > as a result, you should use < and >. (I'd even use these in attribute values too.)
For hfnames and hfvalues (and directory names in the path) for URIs, I'd used Javascript's encodeURIComponent() (on a utf-8 page when encoding for use on a utf-8 page).
If I understand the question correctly, I believe this is what you want.
Other than standard URI encoding of the values, & is the only character related to HTML entities that you have to worry about simply because this is the character that begins every HTML entity. Take for example the following URL:
Even though there aren't trailing semi-colons, since < is the entity for < and > is the entity for >, some old browsers would translate this URL to:
So you need to specify & as & to prevent this from occurring for links within an HTML parsed document.
You do not need HTML escapement here:
According to the HTML5 spec: http://www.w3.org/TR/html5/tokenization.html#character-reference-in-attribute-value-state
&lang=
should be parsed as non-recognized character reference and value of the attribute should be used as it is:http://domain.com/search?q=whatever&lang=en
For the reference: added question to HTML5 WG: http://lists.w3.org/Archives/Public/public-html/2011Sep/0163.html