Skip to content

Commit

Permalink
Add back IHtmlString to System.Web.HttpUtility (#85673)
Browse files Browse the repository at this point in the history
* Add back IHtmlString to System.Web.HttpUtility

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

Co-authored-by: Miha Zupan <[email protected]>

---------

Co-authored-by: Miha Zupan <[email protected]>
  • Loading branch information
stephentoub and MihaZupan authored May 3, 2023
1 parent 27e725d commit 29d8a16
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ public static void HtmlEncode(string? s, System.IO.TextWriter output) { }
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("str")]
public static string? UrlPathEncode(string? str) { throw null; }
}
public partial interface IHtmlString
{
string ToHtmlString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Web\IHtmlString.cs" />
<Compile Include="System\Web\HttpUtility.cs" />
<Compile Include="System\Web\Util\HttpEncoder.cs" />
<Compile Include="System\Web\Util\HttpEncoderUtility.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ public static NameValueCollection ParseQueryString(string query, Encoding encodi

[return: NotNullIfNotNull(nameof(value))]
public static string? HtmlEncode(object? value) =>
value == null ? null : HtmlEncode(Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty);
value switch
{
null => null,
IHtmlString ihs => ihs.ToHtmlString() ?? string.Empty,
_ => HtmlEncode(Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty),
};

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

Expand Down
13 changes: 13 additions & 0 deletions src/libraries/System.Web.HttpUtility/src/System/Web/IHtmlString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Web
{
/// <summary>Represents an HTML-encoded string that should not be encoded again.</summary>
public interface IHtmlString
{
/// <summary>Returns an HTML-encoded string.</summary>
/// <returns>An HTML-encoded string.</returns>
string ToHtmlString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ public void HtmlEncode_TextWriter_null()
});
}

[Fact]
public void HtmlEncode_IHtmlString_UseToHtmlString()
{
Assert.Equal(string.Empty, HttpUtility.HtmlEncode(new ActionHtmlString(() => null)));
Assert.Equal(string.Empty, HttpUtility.HtmlEncode(new ActionHtmlString(() => string.Empty)));
Assert.Equal("<", HttpUtility.HtmlEncode(new ActionHtmlString(() => "<")));
Assert.Throws<FormatException>(() => HttpUtility.HtmlEncode(new ActionHtmlString(() => throw new FormatException())));
}

private sealed class ActionHtmlString(Func<string> toHtmlString) : IHtmlString
{
public string ToHtmlString() => toHtmlString();
}

#endregion HtmlEncode

#region JavaScriptStringEncode
Expand Down

0 comments on commit 29d8a16

Please sign in to comment.