Skip to content
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
45 changes: 45 additions & 0 deletions PolyPilot.Tests/DiffParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,49 @@ public void Parse_MultipleFiles_ParsesAll()
Assert.Equal("a.cs", files[0].FileName);
Assert.Equal("b.cs", files[1].FileName);
}

[Fact]
public void Parse_SpecialHtmlCharacters_PreservedInContent()
{
// Verify the parser preserves raw HTML characters as-is.
// DiffView relies on Blazor's @() auto-encoding, so the parser
// must never pre-encode content.
var diff = """
diff --git a/template.html b/template.html
--- a/template.html
+++ b/template.html
@@ -1,3 +1,3 @@
<div class="container">
- <span title="old">old &amp; value</span>
+ <span title="new">new &amp; value</span>
</div>
""";
var files = DiffParser.Parse(diff);
var lines = files[0].Hunks[0].Lines;

// Parser must pass through <, >, ", & verbatim — DiffView's @() handles encoding
Assert.Equal("<div class=\"container\">", lines[0].Content);
Assert.Equal(" <span title=\"old\">old &amp; value</span>", lines[1].Content);
Assert.Equal(" <span title=\"new\">new &amp; value</span>", lines[2].Content);
Assert.Equal("</div>", lines[3].Content);
}

[Fact]
public void Parse_AngleBracketsInCode_NotEncoded()
{
// Verify generic type parameters with <> are preserved as-is
var diff = """
diff --git a/f.cs b/f.cs
--- a/f.cs
+++ b/f.cs
@@ -1,2 +1,2 @@
-List<string> items = new List<string>();
+Dictionary<string, int> items = new Dictionary<string, int>();
""";
var files = DiffParser.Parse(diff);
var lines = files[0].Hunks[0].Lines;

Assert.Equal("List<string> items = new List<string>();", lines[0].Content);
Assert.Equal("Dictionary<string, int> items = new Dictionary<string, int>();", lines[1].Content);
}
}
8 changes: 2 additions & 6 deletions PolyPilot/Components/DiffView.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@using PolyPilot.Models
@using System.Web

@if (Files.Count == 0)
{
Expand Down Expand Up @@ -48,9 +47,9 @@ else
{
<tr>
<td class="diff-ln @(row.Left?.Type == DiffLineType.Removed ? "ln-del" : "")">@(row.Left?.OldLineNo?.ToString() ?? "")</td>
<td class="diff-code @(row.Left == null ? "diff-blank" : row.Left.Type == DiffLineType.Removed ? "diff-del" : "diff-ctx")"><pre>@(row.Left != null ? HtmlEncode(row.Left.Content) : "")</pre></td>
<td class="diff-code @(row.Left == null ? "diff-blank" : row.Left.Type == DiffLineType.Removed ? "diff-del" : "diff-ctx")"><pre>@(row.Left?.Content ?? "")</pre></td>
<td class="diff-ln @(row.Right?.Type == DiffLineType.Added ? "ln-add" : "")">@(row.Right?.NewLineNo?.ToString() ?? "")</td>
<td class="diff-code @(row.Right == null ? "diff-blank" : row.Right.Type == DiffLineType.Added ? "diff-add" : "diff-ctx")"><pre>@(row.Right != null ? HtmlEncode(row.Right.Content) : "")</pre></td>
<td class="diff-code @(row.Right == null ? "diff-blank" : row.Right.Type == DiffLineType.Added ? "diff-add" : "diff-ctx")"><pre>@(row.Right?.Content ?? "")</pre></td>
</tr>
}
}
Expand All @@ -70,7 +69,4 @@ else
{
Files = DiffParser.Parse(RawDiff);
}

private static string HtmlEncode(string s) =>
System.Web.HttpUtility.HtmlEncode(s);
}
1 change: 0 additions & 1 deletion PolyPilot/Models/DiffParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using System.Web;

namespace PolyPilot.Models;

Expand Down