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

[Android] Fix for WebView Fails to Load URLs with Certain Encoded Characters #27003

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions src/Controls/tests/Core.UnitTests/WebViewUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using Microsoft.Maui.Graphics;
using Xunit;
using Microsoft.Maui.Platform;
using WindowsOS = Microsoft.Maui.Controls.PlatformConfiguration.Windows;

namespace Microsoft.Maui.Controls.Core.UnitTests
Expand Down Expand Up @@ -161,5 +162,18 @@ public void TestSettingOfCookie()

Assert.NotNull(defaultWebView.Cookies);
}

[Fact]
public void TestUrlWithNonWesternCharacters()
{
// This test validates that URLs with non-Western characters (e.g., "Ğ" and spaces encoded as "%20")
// are correctly identified as absolute URLs and not treated as relative URLs.

string finalUrl = "https://example.com/test-Ağ-Sistem%20Bilgi%20Güvenliği%20Md/Guide.pdf";

bool valid = WebViewHelper.IsRelativeUrl(finalUrl);

Assert.False(valid, "The URL was identified as a relative URL");
}
jfversluis marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Android/MauiWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void IWebViewDelegate.LoadUrl(string? url)
_handler.CurrentNavigationEvent = WebNavigationEvent.NewPage;
}

if (url != null && !url.StartsWith('/') && !Uri.IsWellFormedUriString(url, UriKind.Absolute))
if (url is not null && WebViewHelper.IsRelativeUrl(url))
{
// URLs like "index.html" can't possibly load, so try "file:///android_asset/index.html"
url = AssetBaseUrl + url;
Expand Down
11 changes: 11 additions & 0 deletions src/Core/src/Platform/WebViewHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Microsoft.Maui.Platform;

internal static class WebViewHelper
{
internal static bool IsRelativeUrl(string url)
{
return !url.StartsWith('/') && !Uri.TryCreate(url, UriKind.Absolute, out _);
}
}
Loading