Merge pull request #252 from alfaproject/html_decode

Use native HtmlDecode
This commit is contained in:
Nikolaj Olsson 2014-09-16 22:16:57 +02:00
commit 037b372da9
7 changed files with 15 additions and 325 deletions

View File

@ -423,7 +423,7 @@ namespace Nikse.SubtitleEdit.Forms
startIndex++;
int endIndex = result.IndexOf("</span>", startIndex);
string translatedText = result.Substring(startIndex, endIndex - startIndex);
string test = Utilities.HtmlDecode(translatedText);
string test = WebUtility.HtmlDecode(translatedText);
sb.Append(test);
startIndex = result.IndexOf("<span title=", startIndex);
}

View File

@ -16332,8 +16332,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (!update.Text.Contains(Environment.NewLine))
update.Text = update.Text.Replace("\n", Environment.NewLine);
// update.Text = HttpUtility.HtmlDecode(update.Text).Replace("<br />", Environment.NewLine);
update.Text = Utilities.HtmlDecode(update.Text).Replace("<br />", Environment.NewLine);
update.Text = WebUtility.HtmlDecode(update.Text).Replace("<br />", Environment.NewLine);
}
if (update.User.Ip != _networkSession.CurrentUser.Ip || update.User.UserName != _networkSession.CurrentUser.UserName)
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace Nikse.SubtitleEdit.Logic.Networking
@ -108,7 +109,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking
DateTime updateTime;
Subtitle = new Subtitle();
foreach (var sequence in _seWs.GetSubtitle(sessionKey, out tempFileName, out updateTime))
Subtitle.Paragraphs.Add(new Paragraph(Utilities.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds));
Subtitle.Paragraphs.Add(new Paragraph(WebUtility.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds));
FileName = tempFileName;
OriginalSubtitle = new Subtitle();
@ -116,7 +117,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking
if (sequences != null)
{
foreach (var sequence in sequences)
OriginalSubtitle.Paragraphs.Add(new Paragraph(Utilities.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds));
OriginalSubtitle.Paragraphs.Add(new Paragraph(WebUtility.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds));
}
SessionId = sessionKey;
@ -169,7 +170,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking
if (sequences != null)
{
foreach (var sequence in sequences)
Subtitle.Paragraphs.Add(new Paragraph(Utilities.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds));
Subtitle.Paragraphs.Add(new Paragraph(WebUtility.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds));
}
return Subtitle;
}
@ -182,7 +183,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking
var sequences = _seWs.GetSubtitle(SessionId, out FileName, out _seWsLastUpdate);
foreach (var sequence in sequences)
{
Paragraph p = new Paragraph(Utilities.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds);
Paragraph p = new Paragraph(WebUtility.HtmlDecode(sequence.Text).Replace("<br />", Environment.NewLine), sequence.StartMilliseconds, sequence.EndMilliseconds);
Subtitle.Paragraphs.Add(p);
}
Subtitle.Renumber(1);

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
@ -79,7 +80,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
sb.Append(line.Substring(i, 1));
}
p.Text = sb.ToString().Trim();
p.Text = Utilities.HtmlDecode(p.Text);
p.Text = WebUtility.HtmlDecode(p.Text);
p.Text = ConvertJavaSpecialCharacters(p.Text);
subtitle.Paragraphs.Add(p);
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
@ -354,7 +355,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
tmp.StartsWith("<s", StringComparison.Ordinal) ||
tmp.StartsWith("</", StringComparison.Ordinal)))
{
total.Append(Utilities.HtmlDecode(partial.ToString()));
total.Append(WebUtility.HtmlDecode(partial.ToString()));
partial = new StringBuilder();
tagOn = true;
total.Append('<');
@ -373,12 +374,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
total.Append(text.Substring(i, 1));
}
}
total.Append(Utilities.HtmlDecode(partial.ToString()));
total.Append(WebUtility.HtmlDecode(partial.ToString()));
text = total.ToString();
}
else
{
text = Utilities.HtmlDecode(text);
text = WebUtility.HtmlDecode(text);
}
string cleanText = text;

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
@ -204,7 +205,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
foreach (Paragraph p2 in subtitle.Paragraphs)
p2.Text = Utilities.HtmlDecode(p2.Text);
p2.Text = WebUtility.HtmlDecode(p2.Text);
subtitle.Renumber(1);
}

View File

@ -3382,319 +3382,6 @@ namespace Nikse.SubtitleEdit.Logic
return sb.ToString();
}
/// <summary>
/// HTML-decodes a string
/// </summary>
/// <param name="text">Text string to encode</param>
/// <returns>HTML-decoded text</returns>
public static string HtmlDecode(string text)
{
if (text == null)
return string.Empty;
StringBuilder sb = new StringBuilder(text.Length);
int len = text.Length;
int i = 0;
while (i < len)
{
char c = text[i];
int nextSemiColon = text.IndexOf(';', i + 1);
if (c == '&' && nextSemiColon > 0 && nextSemiColon <= i + 8)
{
string code = text.Substring(i + 1, nextSemiColon - (i + 1));
i += code.Length + 2;
switch (code) // http://www.html-entities.com/ + http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
{
case "lt":
sb.Append('<');
break;
case "gt":
sb.Append('>');
break;
case "quot":
sb.Append('"');
break;
case "amp":
sb.Append('&');
break;
case "apos":
sb.Append('\'');
break;
case "nbsp":
sb.Append(' ');
break;
case "ndash":
sb.Append('');
break;
case "mdash":
sb.Append('—');
break;
case "iexcl":
sb.Append('¡');
break;
case "iquest":
sb.Append('¿');
break;
case "ldquo":
sb.Append('“');
break;
case "rdquo":
sb.Append('”');
break;
case "lsquo":
sb.Append('');
break;
case "rsquo":
sb.Append('');
break;
case "laquo":
sb.Append('«');
break;
case "raquo":
sb.Append('»');
break;
case "cent":
sb.Append('¢');
break;
case "copy":
sb.Append('©');
break;
case "divide":
sb.Append('÷');
break;
case "micro":
sb.Append('µ');
break;
case "middot":
sb.Append('·');
break;
case "para":
sb.Append('¶');
break;
case "plusmn":
sb.Append('±');
break;
case "euro":
sb.Append('€');
break;
case "pound":
sb.Append('£');
break;
case "reg":
sb.Append('®');
break;
case "sect":
sb.Append('§');
break;
case "trade":
sb.Append('™');
break;
case "yen":
sb.Append('¥');
break;
case "aacute":
sb.Append('á');
break;
case "Aacute":
sb.Append('Á');
break;
case "agrave":
sb.Append('à');
break;
case "Agrave":
sb.Append('À');
break;
case "acirc":
sb.Append('â');
break;
case "Acirc":
sb.Append('Â');
break;
case "aring":
sb.Append('å');
break;
case "Aring":
sb.Append('Å');
break;
case "atilde":
sb.Append('ã');
break;
case "Atilde":
sb.Append('Ã');
break;
case "auml":
sb.Append('ä');
break;
case "Auml":
sb.Append('Ä');
break;
case "aelig":
sb.Append('æ');
break;
case "AElig":
sb.Append('Æ');
break;
case "ccedil":
sb.Append('ç');
break;
case "Ccedil":
sb.Append('Ç');
break;
case "eacute":
sb.Append('é');
break;
case "Eacute":
sb.Append('É');
break;
case "egrave":
sb.Append('è');
break;
case "Egrave":
sb.Append('È');
break;
case "ecirc":
sb.Append('ê');
break;
case "Ecirc":
sb.Append('Ê');
break;
case "euml":
sb.Append('ë');
break;
case "Euml":
sb.Append('Ë');
break;
case "iacute":
sb.Append('í');
break;
case "Iacute":
sb.Append('Í');
break;
case "igrave":
sb.Append('ì');
break;
case "Igrave":
sb.Append('Ì');
break;
case "icirc":
sb.Append('î');
break;
case "Icirc":
sb.Append('Î');
break;
case "iuml":
sb.Append('ï');
break;
case "Iuml":
sb.Append('Ï');
break;
case "ntilde":
sb.Append('ñ');
break;
case "Ntilde":
sb.Append('Ñ');
break;
case "oacute":
sb.Append('ó');
break;
case "Oacute":
sb.Append('Ó');
break;
case "ograve":
sb.Append('ò');
break;
case "Ograve":
sb.Append('Ò');
break;
case "ocirc":
sb.Append('ô');
break;
case "Ocirc":
sb.Append('Ô');
break;
case "oslash":
sb.Append('ø');
break;
case "Oslash":
sb.Append('Ø');
break;
case "otilde":
sb.Append('õ');
break;
case "Otilde":
sb.Append('Õ');
break;
case "ouml":
sb.Append('ö');
break;
case "Ouml":
sb.Append('Ö');
break;
case "szlig":
sb.Append('ß');
break;
case "uacute":
sb.Append('ú');
break;
case "Uacute":
sb.Append('Ú');
break;
case "ugrave":
sb.Append('ù');
break;
case "Ugrave":
sb.Append('Ù');
break;
case "ucirc":
sb.Append('û');
break;
case "Ucirc":
sb.Append('Û');
break;
case "uuml":
sb.Append('ü');
break;
case "Uuml":
sb.Append('Ü');
break;
case "yuml":
sb.Append('ÿ');
break;
case "":
sb.Append("");
break;
default:
code = code.TrimStart('#');
if (code.StartsWith('x') || code.StartsWith('X'))
{
code = code.TrimStart('x');
code = code.TrimStart('X');
try
{
int value = Convert.ToInt32(code, 16);
sb.Append(Convert.ToChar(value));
}
catch
{
}
}
else if (IsInteger(code))
{
sb.Append(Convert.ToChar(int.Parse(code)));
}
break;
}
}
else
{
sb.Append(c);
i++;
}
}
return sb.ToString();
}
/// <summary>
/// UrlEncodes a string without the requirement for System.Web
/// </summary>