Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace <br />, <br/>, and <br> in XML comments with Environment.Newline #2899

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static string Humanize(string text)
.HumanizeCodeTags()
.HumanizeMultilineCodeTags()
.HumanizeParaTags()
.HumanizeBrTags() // must be called after HumanizeParaTags() so that it replaces any additional <br> tags
.DecodeXml();
}

Expand Down Expand Up @@ -108,6 +109,11 @@ private static string HumanizeParaTags(this string text)
return ParaTag().Replace(text, (match) => "<br>" + match.Groups["display"].Value);
}

private static string HumanizeBrTags(this string text)
{
return BrTag().Replace(text, m => Environment.NewLine);
}

private static string DecodeXml(this string text)
{
return WebUtility.HtmlDecode(text);
Expand All @@ -118,6 +124,7 @@ private static string DecodeXml(this string text)
private const string MultilineCodeTagPattern = @"<code>(?<display>.+?)</code>";
private const string ParaTagPattern = @"<para>(?<display>.+?)</para>";
private const string HrefPattern = @"<see href=\""(.*)\"">(.*)<\/see>";
private const string BrPattern = @"(<br ?\/?>)"; // handles <br>, <br/>, <br />

#if NET7_0_OR_GREATER
[GeneratedRegex(RefTagPattern)]
Expand All @@ -134,18 +141,23 @@ private static string DecodeXml(this string text)

[GeneratedRegex(HrefPattern)]
private static partial Regex HrefTag();

[GeneratedRegex(BrPattern)]
private static partial Regex BrTag();
#else
private static readonly Regex _refTag = new(RefTagPattern);
private static readonly Regex _codeTag = new(CodeTagPattern);
private static readonly Regex _multilineCodeTag = new(MultilineCodeTagPattern, RegexOptions.Singleline);
private static readonly Regex _paraTag = new(ParaTagPattern, RegexOptions.Singleline);
private static readonly Regex _hrefTag = new(HrefPattern);
private static readonly Regex _brTag = new(BrPattern);

private static Regex RefTag() => _refTag;
private static Regex CodeTag() => _codeTag;
private static Regex MultilineCodeTag() => _multilineCodeTag;
private static Regex ParaTag() => _paraTag;
private static Regex HrefTag() => _hrefTag;
private static Regex BrTag() => _brTag;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ Misplaced Tab Indentation
[InlineData(@"<paramref name=""param1"" /> does something", "param1 does something")]
[InlineData("<c>DoWork</c> is a method in <c>TestClass</c>.", "`DoWork` is a method in `TestClass`.")]
[InlineData("<code>DoWork</code> is a method in <code>\nTestClass\n</code>.", "```DoWork``` is a method in ```\nTestClass\n```.")]
[InlineData("<para>This is a paragraph</para>.", "<br>This is a paragraph.")]
[InlineData("<para>This is a paragraph</para>.", "\r\nThis is a paragraph.")]
[InlineData("GET /Todo?iscomplete=true&amp;owner=mike", "GET /Todo?iscomplete=true&owner=mike")]
[InlineData(@"Returns a <see langword=""null""/> item.", "Returns a null item.")]
[InlineData(@"<see href=""https://www.iso.org/iso-4217-currency-codes.html"">ISO currency code</see>", "[ISO currency code](https://www.iso.org/iso-4217-currency-codes.html)")]
[InlineData("First line.<br />Second line.<br/>Third line.<br>Fourth line.", "First line.\r\nSecond line.\r\nThird line.\r\nFourth line.")]
public void Humanize_HumanizesInlineTags(
string input,
string expectedOutput)
Expand Down