[Modify] Polish it
This commit is contained in:
parent
b530875b85
commit
91fa2dc7b6
@ -930,59 +930,49 @@ namespace WebSocketSharp.Net
|
|||||||
output.Write (HtmlDecode (s));
|
output.Write (HtmlDecode (s));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// HTML-encodes a <see cref="string"/> and returns the encoded <see cref="string"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A <see cref="string"/> that represents the encoded string.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="s">
|
|
||||||
/// A <see cref="string"/> to encode.
|
|
||||||
/// </param>
|
|
||||||
public static string HtmlEncode (string s)
|
public static string HtmlEncode (string s)
|
||||||
{
|
{
|
||||||
if (s == null || s.Length == 0)
|
if (s == null || s.Length == 0)
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
var needEncode = false;
|
var buff = new StringBuilder ();
|
||||||
foreach (var c in s) {
|
|
||||||
if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
|
|
||||||
needEncode = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!needEncode)
|
|
||||||
return s;
|
|
||||||
|
|
||||||
var output = new StringBuilder ();
|
|
||||||
foreach (var c in s) {
|
foreach (var c in s) {
|
||||||
if (c == '&') {
|
if (c == '&') {
|
||||||
output.Append ("&");
|
buff.Append ("&");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else if (c == '"') {
|
|
||||||
output.Append (""");
|
if (c == '"') {
|
||||||
|
buff.Append (""");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else if (c == '<') {
|
|
||||||
output.Append ("<");
|
if (c == '<') {
|
||||||
|
buff.Append ("<");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else if (c == '>') {
|
|
||||||
output.Append (">");
|
if (c == '>') {
|
||||||
|
buff.Append (">");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else if (c > 159) {
|
|
||||||
|
if (c > 159) {
|
||||||
// MS starts encoding with &# from 160 and stops at 255.
|
// MS starts encoding with &# from 160 and stops at 255.
|
||||||
// We don't do that. One reason is the 65308/65310 unicode
|
// We do not do that. One reason is the 65308/65310 unicode
|
||||||
// characters that look like '<' and '>'.
|
// characters that look like '<' and '>'.
|
||||||
output.Append ("&#");
|
buff.Append ("&#");
|
||||||
output.Append (((int) c).ToString (CultureInfo.InvariantCulture));
|
buff.Append (((int) c).ToString (CultureInfo.InvariantCulture));
|
||||||
output.Append (";");
|
buff.Append (";");
|
||||||
}
|
|
||||||
else {
|
continue;
|
||||||
output.Append (c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buff.Append (c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return output.ToString ();
|
return buff.ToString ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user