Skip to content

Commit 29d8a16

Browse files
Add back IHtmlString to System.Web.HttpUtility (#85673)
* Add back IHtmlString to System.Web.HttpUtility * Update src/libraries/System.Web.HttpUtility/tests/HttpUtility/HttpUtilityTest.cs Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> --------- Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
1 parent 27e725d commit 29d8a16

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

src/libraries/System.Web.HttpUtility/ref/System.Web.HttpUtility.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ public static void HtmlEncode(string? s, System.IO.TextWriter output) { }
6565
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("str")]
6666
public static string? UrlPathEncode(string? str) { throw null; }
6767
}
68+
public partial interface IHtmlString
69+
{
70+
string ToHtmlString();
71+
}
6872
}

src/libraries/System.Web.HttpUtility/src/System.Web.HttpUtility.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
7+
<Compile Include="System\Web\IHtmlString.cs" />
78
<Compile Include="System\Web\HttpUtility.cs" />
89
<Compile Include="System\Web\Util\HttpEncoder.cs" />
910
<Compile Include="System\Web\Util\HttpEncoderUtility.cs" />

src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ public static NameValueCollection ParseQueryString(string query, Encoding encodi
143143

144144
[return: NotNullIfNotNull(nameof(value))]
145145
public static string? HtmlEncode(object? value) =>
146-
value == null ? null : HtmlEncode(Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty);
146+
value switch
147+
{
148+
null => null,
149+
IHtmlString ihs => ihs.ToHtmlString() ?? string.Empty,
150+
_ => HtmlEncode(Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty),
151+
};
147152

148153
public static void HtmlEncode(string? s, TextWriter output) => HttpEncoder.HtmlEncode(s, output);
149154

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Web
5+
{
6+
/// <summary>Represents an HTML-encoded string that should not be encoded again.</summary>
7+
public interface IHtmlString
8+
{
9+
/// <summary>Returns an HTML-encoded string.</summary>
10+
/// <returns>An HTML-encoded string.</returns>
11+
string ToHtmlString();
12+
}
13+
}

src/libraries/System.Web.HttpUtility/tests/HttpUtility/HttpUtilityTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ public void HtmlEncode_TextWriter_null()
284284
});
285285
}
286286

287+
[Fact]
288+
public void HtmlEncode_IHtmlString_UseToHtmlString()
289+
{
290+
Assert.Equal(string.Empty, HttpUtility.HtmlEncode(new ActionHtmlString(() => null)));
291+
Assert.Equal(string.Empty, HttpUtility.HtmlEncode(new ActionHtmlString(() => string.Empty)));
292+
Assert.Equal("<", HttpUtility.HtmlEncode(new ActionHtmlString(() => "<")));
293+
Assert.Throws<FormatException>(() => HttpUtility.HtmlEncode(new ActionHtmlString(() => throw new FormatException())));
294+
}
295+
296+
private sealed class ActionHtmlString(Func<string> toHtmlString) : IHtmlString
297+
{
298+
public string ToHtmlString() => toHtmlString();
299+
}
300+
287301
#endregion HtmlEncode
288302

289303
#region JavaScriptStringEncode

0 commit comments

Comments
 (0)