HtmlEncode 为何要 encode 大于 0xa0 的A SCII 字符呢?

jmcooler 2015-02-03 11:32:23
HtmlEncode 通常 encode 下列字符:&, ', ", <, > 和空格
这倒好理解,因为这些字符与 html 本身的标签和属性相冲突

但是为何它要 encode 大于 0xa0 之后的 ASCII 字符呢?
.NET 源码为证:
public static unsafe void HtmlEncode(string value, TextWriter output)
{
if (value != null)
{
if (output == null)
{
throw new ArgumentNullException("output");
}
int num = IndexOfHtmlEncodingChars(value, 0);
if (num == -1)
{
output.Write(value);
}
else
{
int num2 = value.Length - num;
fixed (char* str = ((char*) value))
{
char* chPtr2 = str;
while (num-- > 0)
{
chPtr2++;
output.Write(chPtr2[0]);
}
while (num2-- > 0)
{
chPtr2++;
char ch = chPtr2[0];
if (ch <= '>')
{
switch (ch)
{
case '&':
{
output.Write("&");
continue;
}
case '\'':
{
output.Write("'");
continue;
}
case '"':
{
output.Write(""");
continue;
}
case '<':
{
output.Write("<");
continue;
}
case '>':
{
output.Write(">");
continue;
}
}
output.Write(ch);
continue;
}
if ((ch >= '\x00a0') && (ch < 'Ā')) //我说的是这个地方
{
output.Write("&#");
output.Write(((int) ch).ToString(NumberFormatInfo.InvariantInfo));
output.Write(';');
}
else
{
output.Write(ch);
}
}
}
}
}
}
...全文
158 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
md5e 2015-02-03
  • 打赏
  • 举报
回复
应该是中文字符或别的什么字符
winnowc 2015-02-03
  • 打赏
  • 举报
回复
这些字符(U+00A0到U+00FF)属于 Latin-1 Supplement 区间。对于html,它们都有自己的名字,属于常用字符(比如U+00A0就是 ),但是不是所有字符编码都能够支持它。 比如中国的编码标准gb2312就不支持这个区间内的很多字符(中日韩或者说CJK国家/地区的默认编码都是如此)。如果html页面使用gb2312编码,而这些字符没有经过encode,那么会变成'?'(问号,U+003F)或者其它gb2312能表示的字符。 对于拉丁语系的地区,他们有通用的iso-8859-1编码,这个编码自然是支持 Latin-1 Supplement 的,或者说本来就是源头(它也是html5之前的版本的默认编码,html5默认使用utf8编码),也就完全不需要这样处理。可以说这个处理正是考虑到了非拉丁语系地区的编码。 换句话说,这样处理可以保证就算html页面使用gb2312编码,里面也可以"嵌入"原本gb2312不支持的Latin-1 Supplement字符,在浏览器里就能够正常显示它原本表示的那个字符。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧