Add back IHtmlString to System.Web.HttpUtility (#85673)
authorStephen Toub <stoub@microsoft.com>
Wed, 3 May 2023 20:51:09 +0000 (13:51 -0700)
committerGitHub <noreply@github.com>
Wed, 3 May 2023 20:51:09 +0000 (13:51 -0700)
* 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>
src/libraries/System.Web.HttpUtility/ref/System.Web.HttpUtility.cs
src/libraries/System.Web.HttpUtility/src/System.Web.HttpUtility.csproj
src/libraries/System.Web.HttpUtility/src/System/Web/HttpUtility.cs
src/libraries/System.Web.HttpUtility/src/System/Web/IHtmlString.cs [new file with mode: 0644]
src/libraries/System.Web.HttpUtility/tests/HttpUtility/HttpUtilityTest.cs

index 2086170..f319c25 100644 (file)
@@ -65,4 +65,8 @@ namespace System.Web
         [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("str")]
         public static string? UrlPathEncode(string? str) { throw null; }
     }
+    public partial interface IHtmlString
+    {
+        string ToHtmlString();
+    }
 }
index 0af2959..df97d2a 100644 (file)
@@ -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" />
index 57964af..5d0d503 100644 (file)
@@ -143,7 +143,12 @@ namespace System.Web
 
         [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);
 
diff --git a/src/libraries/System.Web.HttpUtility/src/System/Web/IHtmlString.cs b/src/libraries/System.Web.HttpUtility/src/System/Web/IHtmlString.cs
new file mode 100644 (file)
index 0000000..da6f3ce
--- /dev/null
@@ -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();
+    }
+}
index 416d321..82268b3 100644 (file)
@@ -284,6 +284,20 @@ namespace System.Web.Tests
             });
         }
 
+        [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