[Modify] Replace it
This commit is contained in:
parent
34dd9c9f60
commit
a793b5be21
@ -907,111 +907,7 @@ namespace WebSocketSharp.Net
|
|||||||
if (s.Length == 0)
|
if (s.Length == 0)
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
var entity = new StringBuilder ();
|
return htmlDecode (s);
|
||||||
var output = new StringBuilder ();
|
|
||||||
|
|
||||||
// 0 -> nothing,
|
|
||||||
// 1 -> right after '&'
|
|
||||||
// 2 -> between '&' and ';' but no '#'
|
|
||||||
// 3 -> '#' found after '&' and getting numbers
|
|
||||||
var state = 0;
|
|
||||||
|
|
||||||
var number = 0;
|
|
||||||
var haveTrailingDigits = false;
|
|
||||||
foreach (var c in s) {
|
|
||||||
if (state == 0) {
|
|
||||||
if (c == '&') {
|
|
||||||
entity.Append (c);
|
|
||||||
state = 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
output.Append (c);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '&') {
|
|
||||||
state = 1;
|
|
||||||
if (haveTrailingDigits) {
|
|
||||||
entity.Append (number.ToString (CultureInfo.InvariantCulture));
|
|
||||||
haveTrailingDigits = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
output.Append (entity.ToString ());
|
|
||||||
entity.Length = 0;
|
|
||||||
entity.Append ('&');
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == 1) {
|
|
||||||
if (c == ';') {
|
|
||||||
state = 0;
|
|
||||||
output.Append (entity.ToString ());
|
|
||||||
output.Append (c);
|
|
||||||
entity.Length = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
number = 0;
|
|
||||||
if (c != '#')
|
|
||||||
state = 2;
|
|
||||||
else
|
|
||||||
state = 3;
|
|
||||||
|
|
||||||
entity.Append (c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (state == 2) {
|
|
||||||
entity.Append (c);
|
|
||||||
if (c == ';') {
|
|
||||||
var key = entity.ToString ();
|
|
||||||
var entities = getEntities ();
|
|
||||||
if (key.Length > 1 && entities.ContainsKey (key.Substring (1, key.Length - 2)))
|
|
||||||
key = entities[key.Substring (1, key.Length - 2)].ToString ();
|
|
||||||
|
|
||||||
output.Append (key);
|
|
||||||
state = 0;
|
|
||||||
entity.Length = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (state == 3) {
|
|
||||||
if (c == ';') {
|
|
||||||
if (number > 65535) {
|
|
||||||
output.Append ("&#");
|
|
||||||
output.Append (number.ToString (CultureInfo.InvariantCulture));
|
|
||||||
output.Append (";");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
output.Append ((char) number);
|
|
||||||
}
|
|
||||||
|
|
||||||
state = 0;
|
|
||||||
entity.Length = 0;
|
|
||||||
haveTrailingDigits = false;
|
|
||||||
}
|
|
||||||
else if (Char.IsDigit (c)) {
|
|
||||||
number = number * 10 + ((int) c - '0');
|
|
||||||
haveTrailingDigits = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
state = 2;
|
|
||||||
if (haveTrailingDigits) {
|
|
||||||
entity.Append (number.ToString (CultureInfo.InvariantCulture));
|
|
||||||
haveTrailingDigits = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
entity.Append (c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entity.Length > 0)
|
|
||||||
output.Append (entity.ToString ());
|
|
||||||
else if (haveTrailingDigits)
|
|
||||||
output.Append (number.ToString (CultureInfo.InvariantCulture));
|
|
||||||
|
|
||||||
return output.ToString ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void HtmlDecode (string s, TextWriter output)
|
public static void HtmlDecode (string s, TextWriter output)
|
||||||
|
Loading…
Reference in New Issue
Block a user