Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Uno.Extensions.UriExtensions shouldn't be public #11193

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/PackageDiffIgnore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9134,6 +9134,7 @@
<Member fullName="Microsoft.UI.Xaml.Media.RevealBrushState" reason="Not present in WinAppSDK 1.2" />
<Member fullName="Windows.Storage.Streams.InMemoryBuffer" reason="Windows.Storage.Streams.Buffer should be used." />
<Member fullName="Windows.Foundation.HResult" reason="Type is hidden in .NET and shouldn't be used" />
<Member fullName="Uno.Extensions.UriExtensions" reason="Not needed publicly" />
</Types>
<Events>
<Member fullName="Windows.Foundation.TypedEventHandler`2&lt;Microsoft.UI.Xaml.Controls.IInputValidationControl,Microsoft.UI.Xaml.Controls.HasValidationErrorsChangedEventArgs&gt; Microsoft.UI.Xaml.Controls.AutoSuggestBox::HasValidationErrorsChanged" reason="Not present in WinAppSDK 1.2" />
Expand Down
81 changes: 2 additions & 79 deletions src/Uno.Foundation/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Text;
#if WINDOWS_PHONE || !METRO
using System.IO.IsolatedStorage;
#endif

namespace Uno.Extensions
{
public static class UriExtensions
internal static class UriExtensions
{
private const int EscapeDataStringCharactersMaxLength = 10000;

public static IDictionary<string, string> GetParameters(this Uri uri)
{
return uri
.OriginalString
.Split(new[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Split(new[] { '=' }))
.Where(parts => parts.Length > 1)
.ToDictionary(parts => parts[0], parts => String.Join("=", parts.Skip(1)));
}

internal static Uri TrimEndUriSlash(this Uri uri) => new (uri.OriginalString.TrimEnd("/"));

/// <summary>
/// Get extension of the traget file of the uri.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static string GetExtension(this Uri uri)
{
var url = uri.OriginalString;

try
{
//Path.GetExtension can throw if invalid path characters are present in the Uri (some of these characters are valid in Uri)
return System.IO.Path.GetExtension(url);
}
catch (ArgumentException)
{
//Suppress the exception (Could cause crash in some cases, e.g. SetImageSource from OnApplyTemplate).

var lastIndex = url.LastIndexOf('.');

if (lastIndex != -1)
{
return url.Substring(lastIndex);
}
//try to manually extract extension from string. This solution is less efficient than Path.GetExtension, but
//will do the job for the vast majority of paths. Moreso, it will not cause any exceptions of its own.
}

return String.Empty;
}
internal static Uri TrimEndUriSlash(this Uri uri) => new(uri.OriginalString.TrimEnd("/"));

internal static bool IsAppData(this Uri uri)
{
Expand All @@ -77,30 +25,5 @@ public static bool IsLocalResource(this Uri uri)

return uri.Scheme.Equals("ms-appx", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Converts a string to its escaped representation.
/// This extension bypasses the Uri.EscapeDataString characters limit.
/// </summary>
/// Source: http://stackoverflow.com/questions/6695208/uri-escapedatastring-invalid-uri-the-uri-string-is-too-long
public static string EscapeDataString(string value)
{
var sb = new StringBuilder();
var loops = value.Length / EscapeDataStringCharactersMaxLength;

for (var i = 0; i <= loops; i++)
{
if (i < loops)
{
sb.Append(Uri.EscapeDataString(value.Substring(EscapeDataStringCharactersMaxLength * i, EscapeDataStringCharactersMaxLength)));
}
else
{
sb.Append(Uri.EscapeDataString(value.Substring(EscapeDataStringCharactersMaxLength * i)));
}
}

return sb.ToString();
}
}
}