开发者

How to safely HTML encode content which may already be HTML encoded?

开发者 https://www.devze.com 2023-02-04 02:21 出处:网络
I need to HTML encode some text which may or may not already be HTML encoded (perhaps only partially). Is the following safe? Are there any characters/encodings that 开发者_JAVA百科could cause unexpec

I need to HTML encode some text which may or may not already be HTML encoded (perhaps only partially). Is the following safe? Are there any characters/encodings that 开发者_JAVA百科could cause unexpected behaviour?

HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text))

Thanks


Your logic "HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text))" is safe. There is a standard for HTML characters encoding. Take a look.


There is no built-in functionality. But I would use:

 return 
     HttpUtility.HtmlDecode(text)!=text ? text : HttpUtility.HtmlEncode(text);
0

精彩评论

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

关注公众号