escape URL regex

This commit is contained in:
Nicolas Constant 2021-02-11 00:45:55 -05:00
parent d660dc990c
commit cd36c62935
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
2 changed files with 21 additions and 2 deletions

View file

@ -64,7 +64,7 @@ namespace BirdsiteLive.Domain.Tools
secondPart = truncatedUrl.Substring(30);
}
messageContent = Regex.Replace(messageContent, m.ToString(),
messageContent = Regex.Replace(messageContent, Regex.Escape(m.ToString()),
$@"{m.Groups[1]}<a href=""{url}"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">{protocol}</span><span class=""ellipsis"">{firstPart}</span><span class=""invisible"">{secondPart}</span></a>");
}

View file

@ -217,6 +217,25 @@ namespace BirdsiteLive.Domain.Tests.Tools
#endregion
}
[TestMethod]
public void Extract_UrlRegexChars_Test()
{
#region Stubs
var message = @"🐣 juniors & tech(http://tech.guru maker)";
#endregion
#region Mocks
var logger = new Mock<ILogger<StatusExtractor>>();
#endregion
var service = new StatusExtractor(_settings, logger.Object);
var result = service.Extract(message);
#region Validations
Assert.AreEqual(@"🐣 juniors & tech(<a href=""http://tech.guru"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">tech.guru</span><span class=""invisible""></span></a> maker)", result.content);
#endregion
}
[TestMethod]
public void Extract_SingleHashTag_Test()
{
@ -345,7 +364,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span>"));
#endregion
}
[TestMethod]
public void Extract_SingleMentionTag_RT_Test()
{