Skip to content

Commit

Permalink
Adding url encoding wrapper function to handle xaml textbox conversio…
Browse files Browse the repository at this point in the history
…n of new line to only carriage return
  • Loading branch information
jlevier committed Apr 5, 2023
1 parent cd45dd4 commit b311afb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dev/impl/DevToys/DevToys.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="Helpers\NumberBaseHelper.cs" />
<Compile Include="Helpers\StringManipulationHelper.cs" />
<Compile Include="Helpers\TimestampToolHelper.cs" />
<Compile Include="Helpers\UrlHelper.cs" />
<Compile Include="Helpers\XmlHelper.cs" />
<Compile Include="Helpers\SqlFormatter\Core\Formatter.cs" />
<Compile Include="Helpers\SqlFormatter\Core\Indentation.cs" />
Expand Down
27 changes: 27 additions & 0 deletions src/dev/impl/DevToys/Helpers/UrlHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.Utilities.Encoders;

namespace DevToys.Helpers
{
internal static class UrlHelper
{
/// <summary>
/// Url encodes a string.
/// This is a wrapper function for the built in System.Uri api to accommodate this issue:
/// https://github.com/microsoft/microsoft-ui-xaml/issues/1826
/// </summary>
/// <param name="data">Input to url encode</param>
/// <returns>Url encoded result</returns>
internal static string UrlEncode(string data)
{
string newLineAdjusted = data.Replace("\r\n", "\n").Replace("\r", "\n");
string encoded = Uri.EscapeDataString(newLineAdjusted);

return encoded;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using DevToys.Api.Tools;
using DevToys.Core;
using DevToys.Core.Threading;
using DevToys.Helpers;
using DevToys.Shared.Core.Threading;
using DevToys.Views.Tools.UrlEncoderDecoder;
using Microsoft.Toolkit.Mvvm.ComponentModel;
Expand Down Expand Up @@ -151,7 +152,7 @@ private async Task<string> EncodeUrlDataAsync(string? data)
string? encoded;
try
{
encoded = Uri.EscapeDataString(data);
encoded = UrlHelper.UrlEncode(data!);
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/tests/DevToys.Tests/DevToys.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="Helpers\JwtHelperTests.cs" />
<Compile Include="Helpers\Base64HelperTests.cs" />
<Compile Include="Helpers\UrlHelperTests.cs" />
<Compile Include="Helpers\XmlHelperTests.cs" />
<Compile Include="Providers\Tools\BaseNumberFormatterTests.cs" />
<Compile Include="Core\Threading\AsyncLazyTests.cs" />
Expand Down
18 changes: 18 additions & 0 deletions src/tests/DevToys.Tests/Helpers/UrlHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using DevToys.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DevToys.Tests.Helpers
{
[TestClass]
public class UrlHelperTests
{
[DataTestMethod]
[DataRow("123\r456", "123%0A456")]
[DataRow("test\r", "test%0A")]
[DataRow("hello\r\nworld", "hello%0Aworld")]
public void EncodeNewLines(string input, string expectedResult)
{
Assert.AreEqual(expectedResult, UrlHelper.UrlEncode(input));
}
}
}

0 comments on commit b311afb

Please sign in to comment.