Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ void uno_set_webview_unsupported_scheme_identified_callback(uno_webview_unsuppor
config.preferences.javaScriptCanOpenWindowsAutomatically = true;
config.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeVideo | WKAudiovisualMediaTypeAudio;

// Enable file access from file URLs to support relative paths in local HTML content
[config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];

UNOWebView* webview = [[UNOWebView alloc] initWithFrame:NSMakeRect(0,0,0,0) configuration:config];
#if DEBUG
NSLog(@"uno_webview_create %p", webview);
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI.RuntimeTests/Assets/WebView_FileAccess_Test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#test-div {
background-color: green;
color: white;
padding: 10px;
}
22 changes: 22 additions & 0 deletions src/Uno.UI.RuntimeTests/Assets/WebView_FileAccess_Test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>File Access Test</title>
<link rel="stylesheet" href="WebView_FileAccess_Test.css">
</head>
<body>
<div id="test-div">Test Content</div>
<script>
// Set a flag that can be checked by the test to verify the page loaded
window.fileAccessTestLoaded = true;

// Check if CSS was loaded by reading the computed style
window.addEventListener('load', function() {
var testDiv = document.getElementById('test-div');
var computedStyle = window.getComputedStyle(testDiv);
window.cssLoaded = computedStyle.backgroundColor === 'rgb(0, 128, 0)' || computedStyle.backgroundColor === 'green';
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,49 @@ public async Task When_InvokeScriptAsync_Non_String()
Assert.AreEqual("", result);
}
#endif

#if __IOS__ || __SKIA__
[TestMethod]
public async Task When_FileUri_With_Relative_Paths()
{
var border = new Border();
var webView = new WebView();
webView.Width = 200;
webView.Height = 200;
border.Child = webView;
TestServices.WindowHelper.WindowContent = border;
await TestServices.WindowHelper.WaitForLoaded(border);

bool navigationStarting = false;
bool navigationDone = false;
webView.NavigationStarting += (s, e) => navigationStarting = true;
webView.NavigationCompleted += (s, e) => navigationDone = true;

// Get the path to the test HTML file
var testHtmlPath = System.IO.Path.Combine(
global::Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
"Assets",
"WebView_FileAccess_Test.html");

// Navigate to the file:// URI
var fileUri = new Uri($"file://{testHtmlPath}");
webView.Source = fileUri;

await TestServices.WindowHelper.WaitFor(() => navigationStarting, 3000);
await TestServices.WindowHelper.WaitFor(() => navigationDone, 3000);

// Wait a bit for the page to fully load and execute scripts
await Task.Delay(500);

// Verify the page loaded
var pageLoaded = await webView.InvokeScriptAsync("eval", new[] { "window.fileAccessTestLoaded ? 'true' : 'false'" });
Assert.AreEqual("true", pageLoaded, "HTML page should have loaded");

// Verify the CSS file was loaded and applied
// This tests that relative file:// URIs work correctly
var cssLoaded = await webView.InvokeScriptAsync("eval", new[] { "window.cssLoaded ? 'true' : 'false'" });
Assert.AreEqual("true", cssLoaded, "CSS file should have been loaded via relative path");
}
#endif
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ partial class UnoWKWebView : WKWebView, INativeWebView, IWKScriptMessageHandler

Configuration.UserContentController.AddScriptMessageHandler(this, WebMessageHandlerName);

// Enable file access from file URLs to support relative paths in local HTML content
Configuration.Preferences.SetValueForKey(NSObject.FromObject(true), (NSString)"allowFileAccessFromFileURLs");

// Set strings with fallback to default English
OkString = !string.IsNullOrEmpty(ok) ? ok : "OK";
CancelString = !string.IsNullOrEmpty(cancel) ? cancel : "Cancel";
Expand Down
Loading