mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
[PT Run] Support for application URI (#14391)
* [RUN] Add support for uri with scheme only * Fix typo * Add full support for application URI * Apply suggestions from code review and add tests * [PT Run] Add support for application uri * Update error message * Adapt the icon if the result is web URI * Update icons for application URI * Update icons for application URI (dark mode) * Update icon
This commit is contained in:
@@ -10,22 +10,37 @@ namespace Microsoft.Plugin.Uri.UriHelper
|
||||
{
|
||||
public class ExtendedUriParser : IUriParser
|
||||
{
|
||||
public bool TryParse(string input, out System.Uri result)
|
||||
public bool TryParse(string input, out System.Uri result, out bool isWebUri)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
result = default;
|
||||
isWebUri = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handling URL with only scheme, typically mailto or application uri.
|
||||
// Do nothing, return the result without urlBuilder
|
||||
if (input.EndsWith(":", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.StartsWith("http", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.Contains("/", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.All(char.IsDigit))
|
||||
{
|
||||
result = new System.Uri(input);
|
||||
isWebUri = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Handle common cases UriBuilder does not handle
|
||||
// Using CurrentCulture since this is a user typed string
|
||||
if (input.EndsWith(":", StringComparison.CurrentCulture)
|
||||
|| input.EndsWith(".", StringComparison.CurrentCulture)
|
||||
|| input.EndsWith(":/", StringComparison.CurrentCulture)
|
||||
|| input.EndsWith("://", StringComparison.CurrentCulture)
|
||||
|| input.All(char.IsDigit))
|
||||
{
|
||||
result = default;
|
||||
isWebUri = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -35,27 +50,31 @@ namespace Microsoft.Plugin.Uri.UriHelper
|
||||
var hadDefaultPort = urlBuilder.Uri.IsDefaultPort;
|
||||
urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port;
|
||||
|
||||
if (input.Contains("HTTP://", StringComparison.OrdinalIgnoreCase))
|
||||
if (input.StartsWith("HTTP://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
urlBuilder.Scheme = System.Uri.UriSchemeHttp;
|
||||
isWebUri = true;
|
||||
}
|
||||
else if (input.Contains(":", StringComparison.OrdinalIgnoreCase) &&
|
||||
!input.Contains("http", StringComparison.OrdinalIgnoreCase) &&
|
||||
!input.StartsWith("http", StringComparison.OrdinalIgnoreCase) &&
|
||||
!input.Contains("[", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Do nothing, leave unchanged
|
||||
isWebUri = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
urlBuilder.Scheme = System.Uri.UriSchemeHttps;
|
||||
isWebUri = true;
|
||||
}
|
||||
|
||||
result = urlBuilder.Uri;
|
||||
return true;
|
||||
}
|
||||
catch (System.UriFormatException)
|
||||
catch (UriFormatException)
|
||||
{
|
||||
result = default;
|
||||
isWebUri = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user