Is it okay to use HTML entities in attributes?

2020-07-11 06:36发布

问题:

I have been using slim, and suddenly noticed that it escapes everything by default. So the anchor tag looks something like this:

<a href="&#47;users&#47;lyann&#47;followers">
<img class="user-image" src="http:&#47;&#47;adasdasdasd.cloudfront.net&#47;users&# 47;2011&#47;05&#47;24&#47;4asdasd&#47;asdasd.jpg" />

Is it okay for the href and src attributes to be escaped like this? Are there any other implications? All browsers seems to render it without a problem, though.

回答1:

Yes, it's perfectly fine. Character references are valid inside attributes, too, and will be treated as character references just the same.

For reference, see:

  • A description of character references (they may be found within text)
  • A description of text