Refactor tag replacement in HtmlUtil

The revision refactors the replacement of invalid italic tags in the HtmlUtil FixInvalidItalicTags method. The separate string replace statements were replaced with loops that iterate over arrays of predefined invalid tag variations. This change leads to cleaner, more readable, and maintainable code.

Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-05-11 14:23:05 +01:00
parent 40c253a081
commit cfa0aece78

View File

@ -633,6 +633,14 @@ namespace Nikse.SubtitleEdit.Core.Common
return false;
}
private static readonly string[] BeginTagVariations = { "< i >", "< i>", "<i >", "< I >", "< I>", "<I >", "<i<", "<I<", "<I>" };
private static readonly string[] EndTagVariations =
{
"< / i >", "< /i>", "</ i>", "< /i >", "</i >", "</ i >",
"< / i>", "</I>", "< / I >", "< /I>", "</ I>", "< /I >", "</I >", "</ I >", "< / I>", "</i<", "</I<", "</I>"
};
public static string FixInvalidItalicTags(string input)
{
@ -651,36 +659,15 @@ namespace Nikse.SubtitleEdit.Core.Common
const string beginTag = "<i>";
const string endTag = "</i>";
foreach (var beginTagVariation in BeginTagVariations)
{
text = text.Replace(beginTagVariation, beginTag);
}
text = text.Replace("< i >", beginTag);
text = text.Replace("< i>", beginTag);
text = text.Replace("<i >", beginTag);
text = text.Replace("< I >", beginTag);
text = text.Replace("< I>", beginTag);
text = text.Replace("<I >", beginTag);
text = text.Replace("<i<", beginTag);
text = text.Replace("<I<", beginTag);
text = text.Replace("< / i >", endTag);
text = text.Replace("< /i>", endTag);
text = text.Replace("</ i>", endTag);
text = text.Replace("< /i >", endTag);
text = text.Replace("</i >", endTag);
text = text.Replace("</ i >", endTag);
text = text.Replace("< / i>", endTag);
text = text.Replace("</I>", endTag);
text = text.Replace("< / I >", endTag);
text = text.Replace("< /I>", endTag);
text = text.Replace("</ I>", endTag);
text = text.Replace("< /I >", endTag);
text = text.Replace("</I >", endTag);
text = text.Replace("</ I >", endTag);
text = text.Replace("< / I>", endTag);
text = text.Replace("</i<", endTag);
text = text.Replace("</I<", endTag);
text = text.Replace("<I>", beginTag);
text = text.Replace("</I>", endTag);
foreach (var endTagVariation in EndTagVariations)
{
text = text.Replace(endTagVariation, endTag);
}
text = text.Replace("</i> <i>", "_@_");
text = text.Replace(" _@_", "_@_");