Skip to content

Commit

Permalink
Fix for URL parsing bug with parentheses #375
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffputz committed Nov 21, 2024
1 parent 53079ef commit a49f7dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ public void UrlWithBangParsesCorrectly()
var result = service.ForumCodeToHtml("(and [url=\"https://groups.google.com/forum/#!original/rec.roller-coaster/iwTIvU2IXKI/hKB_D9uRbaEJ\"]'millennium' is spelled with two n's[/url] regardless of whether the new one starts in 2000 or 2001.)");
Assert.Equal("<p>(and <a href=\"https://groups.google.com/forum/#!original/rec.roller-coaster/iwTIvU2IXKI/hKB_D9uRbaEJ\" target=\"_blank\">'millennium' is spelled with two n's</a> regardless of whether the new one starts in 2000 or 2001.)</p>", result);
}

[Fact]
public void UrlWithParenthesesParsesCorrectly()
{
var service = GetService();
var result = service.ForumCodeToHtml("(and [url=\"https://blahblah.com/test(test)test\"]'millennium' is spelled with two n's[/url] regardless of whether the new one starts in 2000 or 2001.)");
Assert.Equal("<p>(and <a href=\"https://blahblah.com/test(test)test\" target=\"_blank\">'millennium' is spelled with two n's</a> regardless of whether the new one starts in 2000 or 2001.)</p>", result);
}

[Fact]
public void DontParagraphAnEmptyString()
Expand All @@ -404,4 +412,13 @@ public void DontParagraphAnEmptyString()
var result = service.CleanForumCodeToHtml(string.Empty);
Assert.Equal(result, string.Empty);
}

[Fact]
public void UrlWithParenthesesParsed()
{
var service = GetService();
_settings.AllowImages = true;
var result = service.ForumCodeToHtml("blah https://blahblah.com/test(test)test blah");
Assert.Equal("<p>blah <a href=\"https://blahblah.com/test(test)test\" target=\"_blank\">https://blahblah.com/test(test)test</a> blah</p>", result);
}
}
9 changes: 9 additions & 0 deletions src/PopForums.Test/Services/TextParsingServiceOtherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ public void ForumCodeToHtmlReturnsEmptyInsteadOfParaTags()
var result = service.ForumCodeToHtml("");
Assert.Equal(String.Empty, result);
}

[Fact]
public void ParsedUrlWithParenthesesUnparsed()
{
var service = GetService();
_settings.AllowImages = true;
var result = service.HtmlToClientHtml("<p>blah <a href=\"https://blahblah.com/test(test)test\" target=\"_blank\">https://blahblah.com/test(test)test</a> blah</p>");
Assert.Equal("<p>blah <a href=\"https://blahblah.com/test(test)test\">https://blahblah.com/test(test)test</a> blah</p>", result);
}
}
8 changes: 4 additions & 4 deletions src/PopForums/Services/TextParsingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public TextParsingService(ISettingsManager settingsManager)
private readonly ISettingsManager _settingsManager;

public static string[] AllowedCloseableTags = { "b", "i", "code", "pre", "ul", "ol", "li", "url", "quote", "img" };
private static readonly Regex TagPattern = new Regex(@"\[[\w""\?=&/;\+%\*\:~,\!\.\-\$\|@#]+\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex TagPattern = new Regex(@"\[[\w""\?=&/;\+%\*\:~,\!\.\-\$\|@#\(\)]+\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex TagID = new Regex(@"\[/?(\w+)\=*.*?\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex ProtocolPattern = new Regex(@"(?<![\]""\>=/\w])(((news|(ht|f)tp(s?))\://)[\w\-\*]+(\.[\w\-/~\*]+)*/?)([\w\?=&/;\+%\*\:~,\.\-\$\|@#])*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex WwwPattern = new Regex(@"(?<!(\]|""|//))(?<=\s|^)(w{3}(\.[\w\-/~\*]+)*/?)([\?\w=&;\+%\*\:~,\-\$\|@#])*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex ProtocolPattern = new Regex(@"(?<![\]""\>=/\w])(((news|(ht|f)tp(s?))\://)[\w\-\*]+(\.[\w\-/~\*]+)*/?)([\w\?=&/;\+%\*\:~,\.\-\$\|@#\(\)])*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex WwwPattern = new Regex(@"(?<!(\]|""|//))(?<=\s|^)(w{3}(\.[\w\-/~\*]+)*/?)([\?\w=&;\+%\*\:~,\-\$\|@#\(\)])*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex EmailPattern = new Regex(@"(?<=\s|\])(?<!(mailto:|""\]))([\w\.\-_']+)@(([\w\-]+\.)+[\w\-]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex YouTubePattern = new Regex(@"(?<![\]""\>=])(((http(s?))\://)[w*\.]*(youtu\.be|youtube\.com+))([\w\?=&/;\+%\*\:~,\.\-\$\|@#])*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex YouTubePattern = new Regex(@"(?<![\]""\>=])(((http(s?))\://)[w*\.]*(youtu\.be|youtube\.com+))([\w\?=&/;\+%\*\:~,\.\-\$\|@#\(\)])*", RegexOptions.Compiled | RegexOptions.IgnoreCase);

/// <summary>
/// Converts forum code from the browser to HTML for storage. This method wraps <see cref="CleanForumCode(string)"/> and <see cref="ForumCodeToHtml(string)"/>, and censors the text.
Expand Down

0 comments on commit a49f7dd

Please sign in to comment.