Skip to content

Commit dbfe251

Browse files
committed
fix: Escape certain characters for attribute values
1 parent c955807 commit dbfe251

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
99
### Fixed
1010

1111
- Added support in `FakeNavigationManager` to handle umlauts.
12+
- Fixed a bug where attribute values did not get escaped. Reported by [@brettwinters](https://github.com/brettwinters). Fixed by [@linkdotnet](https://github.com/linkdotnet).
1213

1314
## [1.13.5] - 2022-12-16
1415

src/bunit.web/Rendering/Internal/Htmlizer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Globalization;
77
using System.Text;
88
using System.Text.Encodings.Web;
9+
using System.Text.Unicode;
910
using Bunit.Rendering;
1011

1112
namespace Bunit;
@@ -270,7 +271,7 @@ private static int RenderAttributes(
270271
result.Append(frame.AttributeName);
271272
result.Append('=');
272273
result.Append('"');
273-
result.Append(value);
274+
result.Append(Escape(value));
274275
result.Append('"');
275276
break;
276277
default:
@@ -281,6 +282,11 @@ private static int RenderAttributes(
281282
return position + maxElements;
282283
}
283284

285+
private static string Escape(string value) =>
286+
value
287+
.Replace("&", "&", StringComparison.OrdinalIgnoreCase)
288+
.Replace("\"", """, StringComparison.OrdinalIgnoreCase);
289+
284290
private sealed class HtmlRenderingContext
285291
{
286292
private readonly RenderTreeFrameDictionary frames;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<p style="@Escaped">@Escaped</p>
22

33
@code {
4-
private string Escaped => "url('')";
4+
5+
[Parameter]
6+
public string Escaped { get; set; }
7+
58
}

tests/bunit.web.tests/BlazorE2E/ComponentRenderingTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,13 @@ public async Task CanHandleRemovedParentObjectsAsync()
691691
}
692692

693693
[Fact]
694-
public void EscapableCharactersDontGetEncoded()
694+
public void SomeEscapableCharactersDontGetEncoded()
695695
{
696-
var cut = RenderComponent<ComponentWithEscapableCharacters>();
696+
const string input = "url('\"&')";
697+
var cut = RenderComponent<ComponentWithEscapableCharacters>(
698+
p => p.Add(s => s.Escaped, input));
697699

698-
cut.Markup.ShouldBe("<p style=\"url('')\">url('')</p>");
700+
cut.Markup.ShouldBe("<p style=\"url('&quot;&amp;')\">url('\"&')</p>");
699701
}
700702

701703
[Fact]

0 commit comments

Comments
 (0)