From e4575036a324910d9fdaaf894857ecb0e668d31f Mon Sep 17 00:00:00 2001 From: chr_ Date: Thu, 1 Jun 2023 10:57:36 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E6=AD=A3HTML=E8=BD=AC=E4=B9=89B?= =?UTF-8?q?ug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 2 +- .../Extensions/StringExtension.cs | 21 +++++++++++++ .../Helper/TextHelperService.cs | 31 ++++++++----------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index e1cf22a2..b9f996a5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 2.1.0.3 + 2.1.1.0 diff --git a/XinjingdailyBot.Infrastructure/Extensions/StringExtension.cs b/XinjingdailyBot.Infrastructure/Extensions/StringExtension.cs index dc888822..6bfc16d9 100644 --- a/XinjingdailyBot.Infrastructure/Extensions/StringExtension.cs +++ b/XinjingdailyBot.Infrastructure/Extensions/StringExtension.cs @@ -25,5 +25,26 @@ public static string EscapeHtml(this string text) return escapedText; } } + + /// + /// HTML反转义 + /// + /// + /// + public static string ReEscapeHtml(this string text) + { + if (string.IsNullOrEmpty(text)) + { + return ""; + } + else + { + var escapedText = text + .Replace("<", "<") + .Replace(">", ">") + .Replace("&", "&"); + return escapedText; + } + } } } diff --git a/XinjingdailyBot.Service/Helper/TextHelperService.cs b/XinjingdailyBot.Service/Helper/TextHelperService.cs index 59314ccb..49b04d07 100644 --- a/XinjingdailyBot.Service/Helper/TextHelperService.cs +++ b/XinjingdailyBot.Service/Helper/TextHelperService.cs @@ -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; @@ -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)) { @@ -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); } @@ -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(); } /// @@ -260,17 +254,18 @@ public string ParseMessage(Message message) /// /// /// - private string ParseMessage(MessageEntity[] entities, string text) + private string ParseMessage(MessageEntity[]? entities, string text) { - StringBuilder sb = new(text.Replace('<', '<').Replace('>', '>').Replace('&', '&')); - - Dictionary 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(); + foreach (var entity in entities) { - var entity = entities[i]; string head; string tail; @@ -297,7 +292,7 @@ private string ParseMessage(MessageEntity[] entities, string text) tail = ""; break; case MessageEntityType.TextLink: - head = $""; + head = $""; tail = ""; break; case MessageEntityType.Code: