Skip to content

Commit fe06745

Browse files
committed
fix(wasm): Keep WebView2 Source after a reparent
1 parent f3ba7d7 commit fe06745

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎src/Uno.UI.Runtime.WebAssembly.Browser/ts/Runtime/WebView.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ namespace Microsoft.UI.Xaml.Controls {
7474
}
7575
}
7676

77+
// The URL of the frame's document or, when the browser hides it, the last URL known for the frame. Reading it doesn't
78+
// change that last URL, so a navigation that hasn't loaded yet keeps the URL it recorded.
79+
static getCurrentUrl(htmlId: string): string | null {
80+
const iframe = document.getElementById(htmlId) as HTMLIFrameElement;
81+
return iframe?.contentWindow ? WebView.readUrl(iframe).url : null;
82+
}
83+
7784
private static readUrl(iframe: HTMLIFrameElement): { url: string | null, isCrossOrigin: boolean } {
7885
try {
7986
return { url: iframe.contentWindow.location.href, isCrossOrigin: false };

‎src/Uno.UI.RuntimeTests/Microsoft/UI/Xaml/Controls/Given_WebView2.uno.cs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,82 @@ await ShowString("<p>Hidden</p>"),
298298
}
299299
}
300300

301+
[TestMethod]
302+
[PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.SkiaWasm)]
303+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/25735")]
304+
public async Task When_Url_Read_Before_Cross_Origin_Content_Arrives_Then_Its_Url_Is_Reported()
305+
{
306+
const string DataUrl = "data:text/html,arriving";
307+
308+
var webView = new WebView2 { Width = 200, Height = 200 };
309+
string loadedSource = null;
310+
void OnNavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) => loadedSource ??= sender.CoreWebView2.Source;
311+
312+
try
313+
{
314+
await UITestHelper.Load(webView);
315+
await webView.EnsureCoreWebView2Async();
316+
var elementId = GetIframeElementId(webView);
317+
webView.NavigationCompleted += OnNavigationCompleted;
318+
319+
// Start a navigation to a cross-origin document and, in the same task, read the frame's URL as Loaded does when the
320+
// WebView2 is added to the tree again before that document arrives.
321+
WasmSemanticDomHelper.InvokeBrowserJs(
322+
$"(function(){{var w=Microsoft.UI.Xaml.Controls.WebView;w.navigate('{elementId}','{DataUrl}');w.getCurrentUrl('{elementId}');return '';}})()");
323+
324+
await TestServices.WindowHelper.WaitFor(
325+
() => loadedSource is not null,
326+
timeoutMS: 10000,
327+
message: "NavigationCompleted should be raised when the cross-origin document loads.");
328+
329+
Assert.AreEqual(DataUrl, loadedSource, "The cross-origin document should be reported with its own URL, not the previous page's.");
330+
}
331+
finally
332+
{
333+
webView.NavigationCompleted -= OnNavigationCompleted;
334+
TestServices.WindowHelper.WindowContent = null;
335+
}
336+
}
337+
338+
[TestMethod]
339+
[PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.SkiaWasm)]
340+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/25735")]
341+
public async Task When_Loaded_Again_Then_Source_Is_Kept()
342+
{
343+
// A cross-origin document, whose location the browser hides.
344+
const string DataUrl = "data:text/html,kept";
345+
346+
var webView = new WebView2 { Width = 200, Height = 200 };
347+
var documentLoaded = false;
348+
void OnNavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
349+
=> documentLoaded |= sender.CoreWebView2.Source == DataUrl;
350+
351+
try
352+
{
353+
await UITestHelper.Load(webView);
354+
await webView.EnsureCoreWebView2Async();
355+
webView.NavigationCompleted += OnNavigationCompleted;
356+
357+
webView.CoreWebView2.Navigate(DataUrl);
358+
await TestServices.WindowHelper.WaitFor(
359+
() => documentLoaded,
360+
timeoutMS: 10000,
361+
message: "NavigationCompleted should be raised for the document, with Source set to its URL.");
362+
363+
// Unloading and loading the WebView2 again keeps its frame and the document it shows.
364+
TestServices.WindowHelper.WindowContent = null;
365+
await TestServices.WindowHelper.WaitForIdle();
366+
await UITestHelper.Load(webView);
367+
368+
Assert.AreEqual(new Uri(DataUrl), webView.Source, "Loading the WebView2 again should keep the Source of the document it shows.");
369+
}
370+
finally
371+
{
372+
webView.NavigationCompleted -= OnNavigationCompleted;
373+
TestServices.WindowHelper.WindowContent = null;
374+
}
375+
}
376+
301377
[MethodImpl(MethodImplOptions.NoInlining)]
302378
private static async Task<(WeakReference, string)> LoadNavigateAndUnloadWebView2()
303379
{

‎src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.Interop.wasm.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ internal static partial class NativeMethods
5656
[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.navigateToString")]
5757
internal static partial void NavigateToString(ElementId htmlId, string html);
5858

59+
[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.getCurrentUrl")]
60+
internal static partial string? GetCurrentUrl(ElementId htmlId);
61+
5962
[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.setupEvents")]
6063
internal static partial void SetupEvents(ElementId htmlId);
6164

‎src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ public void OnLoaded()
190190
{
191191
_elementIdToNativeWebView.TryAdd(_elementId, this);
192192
NativeMethods.SetupEvents(_elementId);
193-
DispatchLoadEvent(_elementId, null);
193+
194+
// The frame may already show a document, as after a reparent, so report its URL rather than the src attribute's.
195+
DispatchLoadEvent(_elementId, NativeMethods.GetCurrentUrl(_elementId));
194196
}
195197

196198
public void OnUnloaded()

0 commit comments

Comments
 (0)