From a9bc38dd230e6534ffc075cac16517f7834d7dc6 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Mon, 21 Nov 2016 14:57:31 -0800 Subject: [PATCH 1/4] Port WebUtility missing members --- .../ref/System.Runtime.Extensions.cs | 2 ++ .../src/System/Net/WebUtility.cs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs b/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs index 6d4e39e614b0..d5d8c1d14ec4 100644 --- a/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs +++ b/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs @@ -1532,7 +1532,9 @@ namespace System.Net public static partial class WebUtility { public static string HtmlDecode(string value) { throw null; } + public static void HtmlDecode(string value, System.IO.TextWriter output) { } public static string HtmlEncode(string value) { throw null; } + public static void HtmlEncode(string value, System.IO.TextWriter output) { } public static string UrlDecode(string encodedValue) { throw null; } public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count) { throw null; } public static string UrlEncode(string value) { throw null; } diff --git a/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs b/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs index 3f69ab0aca0f..b329a4ce0caf 100644 --- a/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs +++ b/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs @@ -50,6 +50,12 @@ public static string HtmlEncode(string value) return StringBuilderCache.GetStringAndRelease(sb); } + public static void HtmlEncode(string value, TextWriter output) + { + // Since we already have a private method that decodes the value using a StringBuilder we don't want to have duplicate code in order to have a better mantainance. + output.Write(HtmlEncode(value)); + } + private static unsafe void HtmlEncode(string value, int index, StringBuilder output) { Debug.Assert(value != null); @@ -138,7 +144,7 @@ private static unsafe void HtmlEncode(string value, int index, StringBuilder out public static string HtmlDecode(string value) { - if (String.IsNullOrEmpty(value)) + if (string.IsNullOrEmpty(value)) { return value; } @@ -154,6 +160,12 @@ public static string HtmlDecode(string value) return StringBuilderCache.GetStringAndRelease(sb); } + public static void HtmlDecode(string value, TextWriter output) + { + // Since we already have a private method that decodes the value using a StringBuilder we don't want to have duplicate code in order to have a better mantainance. + output.Write(HtmlDecode(value)); + } + [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.UInt16.TryParse(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.UInt16@)", Justification = "UInt16.TryParse guarantees that result is zero if the parse fails.")] private static void HtmlDecode(string value, StringBuilder output) { From 2edb12947a40986fc8f0a1d194587b85ee5fb200 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Mon, 21 Nov 2016 16:53:57 -0800 Subject: [PATCH 2/4] Port WebUtility missing html encoding and decoding members --- .../src/System/Net/WebUtility.cs | 2 +- .../System.Runtime.Extensions.Tests.csproj | 1 + .../tests/System/Net/WebUtility.cs | 4 ++- .../System/Net/WebUtility.netstandard1.7.cs | 35 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs diff --git a/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs b/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs index b329a4ce0caf..25000cf5711a 100644 --- a/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs +++ b/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs @@ -33,7 +33,7 @@ public static class WebUtility public static string HtmlEncode(string value) { - if (String.IsNullOrEmpty(value)) + if (string.IsNullOrEmpty(value)) { return value; } diff --git a/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj b/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj index 598d3eb832f4..048b3eb09aab 100644 --- a/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj +++ b/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj @@ -43,6 +43,7 @@ + Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs diff --git a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.cs b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.cs index da8df3199d9a..6948b14aa844 100644 --- a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.cs +++ b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.cs @@ -4,12 +4,14 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; +using System.IO; using System.Text; using Xunit; namespace System.Net.Tests { - public class WebUtilityTests + public partial class WebUtilityTests { // HtmlEncode + HtmlDecode public static IEnumerable HtmlDecode_TestData() diff --git a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs new file mode 100644 index 000000000000..a330ce864011 --- /dev/null +++ b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.IO; +using Xunit; + +namespace System.Net.Tests +{ + public partial class WebUtilityTests + { + [Theory] + [MemberData(nameof(HtmlDecode_TestData))] + public static void HtmlDecode_TextWriterOutput(string value, string expected) + { + if(value == null) + expected = string.Empty; + StringWriter output = new StringWriter(CultureInfo.InvariantCulture); + WebUtility.HtmlDecode(value, output); + Assert.Equal(expected, output.ToString()); + } + + [Theory] + [MemberData(nameof(HtmlEncode_TestData))] + public static void HtmlEncode_TextWriterOutput(string value, string expected) + { + if(value == null) + expected = string.Empty; + StringWriter output = new StringWriter(CultureInfo.InvariantCulture); + WebUtility.HtmlEncode(value, output); + Assert.Equal(expected, output.ToString()); + } + } +} \ No newline at end of file From 411b2a18139371e6f8356cf3d71436945e3944c3 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Mon, 21 Nov 2016 16:57:20 -0800 Subject: [PATCH 3/4] Nit fix --- .../tests/System/Net/WebUtility.netstandard1.7.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs index a330ce864011..16657edacd9a 100644 --- a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs +++ b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs @@ -32,4 +32,4 @@ public static void HtmlEncode_TextWriterOutput(string value, string expected) Assert.Equal(expected, output.ToString()); } } -} \ No newline at end of file +} From 0752110130d9b3b2ce9de1720afa7f61b00f28a2 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Tue, 22 Nov 2016 09:51:32 -0800 Subject: [PATCH 4/4] PR feedback --- .../src/System/Net/WebUtility.cs | 2 -- .../tests/System/Net/WebUtility.netstandard1.7.cs | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs b/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs index 25000cf5711a..96ec4c6e92cb 100644 --- a/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs +++ b/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs @@ -52,7 +52,6 @@ public static string HtmlEncode(string value) public static void HtmlEncode(string value, TextWriter output) { - // Since we already have a private method that decodes the value using a StringBuilder we don't want to have duplicate code in order to have a better mantainance. output.Write(HtmlEncode(value)); } @@ -162,7 +161,6 @@ public static string HtmlDecode(string value) public static void HtmlDecode(string value, TextWriter output) { - // Since we already have a private method that decodes the value using a StringBuilder we don't want to have duplicate code in order to have a better mantainance. output.Write(HtmlDecode(value)); } diff --git a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs index 16657edacd9a..b5dc8d3972ce 100644 --- a/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs +++ b/src/System.Runtime.Extensions/tests/System/Net/WebUtility.netstandard1.7.cs @@ -10,12 +10,12 @@ namespace System.Net.Tests { public partial class WebUtilityTests { - [Theory] + [Theory] [MemberData(nameof(HtmlDecode_TestData))] public static void HtmlDecode_TextWriterOutput(string value, string expected) { if(value == null) - expected = string.Empty; + expected = string.Empty; StringWriter output = new StringWriter(CultureInfo.InvariantCulture); WebUtility.HtmlDecode(value, output); Assert.Equal(expected, output.ToString()); @@ -25,8 +25,8 @@ public static void HtmlDecode_TextWriterOutput(string value, string expected) [MemberData(nameof(HtmlEncode_TestData))] public static void HtmlEncode_TextWriterOutput(string value, string expected) { - if(value == null) - expected = string.Empty; + if(value == null) + expected = string.Empty; StringWriter output = new StringWriter(CultureInfo.InvariantCulture); WebUtility.HtmlEncode(value, output); Assert.Equal(expected, output.ToString());