开发者

Can I rely on an unescaped ampersand displaying correctly in a textarea?

开发者 https://www.devze.com 2023-03-06 20:13 出处:网络
I\'ve just notice a routine in our code that doesn\'t proper开发者_如何转开发ly escape an ampersand in a textarea:

I've just notice a routine in our code that doesn't proper开发者_如何转开发ly escape an ampersand in a textarea:

<textarea>Arts & Crafts</textarea>

What surprises me is that this works in every browser I check. Is there an unwritten rule about ampersands inside textareas? If so, is it a rule I can depend on? Or is this just a convenience that browsers provide to make sure your faulty page renders and there are no guarantees whatsoever?


Most browsers will try to fix errors made in the page. The ampersand cannot mean anything else than an ampersand, so it is shown as such. Alternatives would be to skip the ampersand, or not to render the page at all, but all vendors have thought this fallback to be the most logical one.

It will give you problems when the text is for example <textarea>Use &egrave; to show an è</textarea>, because this will render &egrave; as an è too. So in this case the ampersand must be escaped.

It is however one of the very few errors that are solved the same way in any browser. It is best not to make any error. That will give you the greatest chance on succes. ;)


It's just a convenience; a standalone & is invalid HTML and XHTML.

0

精彩评论

暂无评论...
验证码 换一张
取 消