Skip to content

Commit

Permalink
fix 修正HTML转义Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jun 1, 2023
1 parent 7ed0a87 commit e457503
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.1.0.3</Version>
<Version>2.1.1.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
21 changes: 21 additions & 0 deletions XinjingdailyBot.Infrastructure/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,26 @@ public static string EscapeHtml(this string text)
return escapedText;
}
}

/// <summary>
/// HTML反转义
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string ReEscapeHtml(this string text)
{
if (string.IsNullOrEmpty(text))
{
return "";
}
else
{
var escapedText = text
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("&", "&amp;");
return escapedText;
}
}
}
}
31 changes: 13 additions & 18 deletions XinjingdailyBot.Service/Helper/TextHelperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Telegram.Bot.Types.Enums;
using XinjingdailyBot.Infrastructure;
using XinjingdailyBot.Infrastructure.Attribute;
using XinjingdailyBot.Infrastructure.Extensions;
using XinjingdailyBot.Infrastructure.Model;
using XinjingdailyBot.Interface.Bot.Common;
using XinjingdailyBot.Interface.Helper;
Expand Down Expand Up @@ -63,7 +64,7 @@ public string PureText(string? text)

public string HtmlUserLink(long userId, string userName, string userNick)
{
var nick = EscapeHtml(userNick);
var nick = EscapeHtml(userNick).ReEscapeHtml();

if (string.IsNullOrEmpty(userName))
{
Expand Down Expand Up @@ -208,7 +209,7 @@ public string MakePostText(NewPosts post, Users poster, ChannelOptions? channel)

if (!string.IsNullOrEmpty(post.Text))
{
var text = post.Text;
var text = post.Text.EscapeHtml().ReEscapeHtml();
sb.AppendLine(text);
}

Expand Down Expand Up @@ -244,14 +245,7 @@ public string ParseMessage(Message message)
return "";
}

if (entities == null)
{
return text;
}
else
{
return ParseMessage(entities, text);
}
return ParseMessage(entities, text).ReEscapeHtml();
}

/// <summary>
Expand All @@ -260,17 +254,18 @@ public string ParseMessage(Message message)
/// <param name="entities"></param>
/// <param name="text"></param>
/// <returns></returns>
private string ParseMessage(MessageEntity[] entities, string text)
private string ParseMessage(MessageEntity[]? entities, string text)
{
StringBuilder sb = new(text.Replace('<', '<').Replace('>', '>').Replace('&', '&'));

Dictionary<int, TagObjct> tagMap = new();
var sb = new StringBuilder(text.EscapeHtml());

int count = entities.Length;
if (entities == null)
{
return sb.ToString();
}

for (int i = 0; i < count; i++)
var tagMap = new Dictionary<int, TagObjct>();
foreach (var entity in entities)
{
var entity = entities[i];
string head;
string tail;

Expand All @@ -297,7 +292,7 @@ private string ParseMessage(MessageEntity[] entities, string text)
tail = "</tg-spoiler>";
break;
case MessageEntityType.TextLink:
head = $"<a href=\"{EscapeHtml(entity.Url)}\">";
head = $"<a href=\"{EscapeHtml(entity.Url).ReEscapeHtml()}\">";
tail = "</a>";
break;
case MessageEntityType.Code:
Expand Down

0 comments on commit e457503

Please sign in to comment.