Skip to content

Commit df7ec0e

Browse files
authored
Escape special chars (RCS1181) (#1534)
1 parent 48d201b commit df7ec0e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fix analyzer [RCS0056](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0056) ([PR](https://github.com/dotnet/roslynator/pull/1521))
1818
- Fix analyzer [RCS1181](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1181) ([PR](https://github.com/dotnet/roslynator/pull/1526))
1919
- Fix analyzer [RCS0005](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0005) ([PR](https://github.com/dotnet/roslynator/pull/1533))
20+
- Fix analyzer [RCS1181](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1181) ([PR](https://github.com/dotnet/roslynator/pull/1534))
2021

2122
## [4.12.5] - 2024-09-13
2223

src/Tests/Analyzers.Tests/RCS1181ConvertCommentToDocumentationCommentTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,37 @@ void M()
196196
");
197197
}
198198

199+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ConvertCommentToDocumentationComment)]
200+
public async Task Test_CommentContainsXmlSpecialChars()
201+
{
202+
await VerifyDiagnosticAndFixAsync("""
203+
namespace N
204+
{
205+
/// <summary>
206+
/// x
207+
/// </summary>
208+
class C
209+
{
210+
int P { get; set; } [|// Must be >= 0 & <= 5.|]
211+
}
212+
}
213+
""", """
214+
namespace N
215+
{
216+
/// <summary>
217+
/// x
218+
/// </summary>
219+
class C
220+
{
221+
/// <summary>
222+
/// Must be &gt;= 0 &amp; &lt;= 5.
223+
/// </summary>
224+
int P { get; set; }
225+
}
226+
}
227+
""");
228+
}
229+
199230
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ConvertCommentToDocumentationComment)]
200231
public async Task TestNoDiagnostic_DocumentationComment()
201232
{

src/Workspaces.Common/CSharp/Refactorings/ConvertCommentToDocumentationCommentRefactoring.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Immutable;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Net;
67
using System.Text.RegularExpressions;
78
using System.Threading;
89
using System.Threading.Tasks;
@@ -61,7 +62,10 @@ public static Task<Document> RefactorAsync(
6162

6263
Debug.Assert(trailingTrivia.Contains(trivia));
6364

64-
comments = ImmutableArray.Create(_leadingSlashesRegex.Replace(trivia.ToString(), ""));
65+
string commentText = _leadingSlashesRegex.Replace(trivia.ToString(), "");
66+
commentText = WebUtility.HtmlEncode(commentText);
67+
68+
comments = ImmutableArray.Create(commentText);
6569

6670
SyntaxToken newToken = token.WithTrailingTrivia(trailingTrivia.Skip(trailingTrivia.IndexOf(trivia) + 1));
6771

0 commit comments

Comments
 (0)