-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fix WebView in a grid expands beyond it's cell #32145
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
Changes from all commits
88c28e7
2d119b2
8beb95c
7053952
a3804a8
654b21a
dedfb99
7fbbf73
87777d6
c1b297d
8dc383b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 32030, "Android - WebView in a grid expands beyond it's cell", PlatformAffected.Android)] | ||
|
|
||
| public class Issue32030 : ContentPage | ||
| { | ||
| WebView webView; | ||
|
|
||
| public Issue32030() | ||
| { | ||
| // Create Grid | ||
| var grid = new Grid | ||
| { | ||
| RowDefinitions = new RowDefinitionCollection | ||
| { | ||
| new RowDefinition { Height = new GridLength(50) }, | ||
| new RowDefinition { Height = GridLength.Star }, | ||
| new RowDefinition { Height = new GridLength(50) } | ||
| } | ||
| }; | ||
|
|
||
| // Create "above webview" Label | ||
| var topLabel = new Label | ||
| { | ||
| Text = "Above webview", | ||
| AutomationId = "TopLabel", | ||
| HorizontalTextAlignment = TextAlignment.Center | ||
| }; | ||
| grid.Add(topLabel); | ||
| Grid.SetRow(topLabel, 0); | ||
|
|
||
| // Create WebView | ||
| webView = new WebView | ||
| { | ||
| BackgroundColor = Colors.BurlyWood, | ||
| Source = new HtmlWebViewSource | ||
| { | ||
| Html = @"<html><body><h1>Initial Content</h1></body></html>" | ||
| } | ||
| }; | ||
| grid.Add(webView); | ||
| Grid.SetRow(webView, 1); | ||
|
|
||
| var button = new Button | ||
| { | ||
| Text = "Click Me", | ||
| AutomationId = "BottomButton", | ||
| }; | ||
|
|
||
| button.Clicked += (s, e) => | ||
| { | ||
| webView.Source = null; | ||
| }; | ||
| grid.Add(button); | ||
| Grid.SetRow(button, 2); | ||
|
|
||
| // Set the grid as the page content | ||
| Content = grid; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #if ANDROID //This is Android specific issue | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
| public class Issue32030 : _IssuesUITest | ||
| { | ||
| public Issue32030(TestDevice device) : base(device) { } | ||
|
|
||
| public override string Issue => "Android - WebView in a grid expands beyond it's cell"; | ||
| [Test] | ||
| [Category(UITestCategories.WebView)] | ||
| public void VerifyWebViewStaysWithinGridCell() | ||
| { | ||
| App.WaitForElement("BottomButton"); | ||
| App.Tap("BottomButton"); | ||
| App.WaitForElement("BottomButton"); | ||
| VerifyScreenshot(); | ||
| } | ||
| } | ||
| #endif |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,7 +57,8 @@ public override void OnPageFinished(WebView? view, string? url) | |||||||||||
| bool navigate = _navigationResult != WebNavigationResult.Failure || !GetValidUrl(url).Equals(_lastUrlNavigatedCancel, StringComparison.OrdinalIgnoreCase); | ||||||||||||
| _lastUrlNavigatedCancel = _navigationResult == WebNavigationResult.Cancel ? url : null; | ||||||||||||
|
|
||||||||||||
| if (navigate) | ||||||||||||
| // Skip Navigated event for about:blank to prevent unwanted events when Source is null | ||||||||||||
| if (navigate && !IsBlankNavigation(url)) | ||||||||||||
| handler.VirtualView.Navigated(handler.CurrentNavigationEvent, GetValidUrl(url), _navigationResult); | ||||||||||||
|
|
||||||||||||
| handler.SyncPlatformCookiesToVirtualView(url); | ||||||||||||
|
|
@@ -96,6 +97,17 @@ public override bool OnRenderProcessGone(WebView? view, RenderProcessGoneDetail? | |||||||||||
| bool NavigatingCanceled(string? url) => | ||||||||||||
| !_handler.TryGetTarget(out var handler) || handler.NavigatingCanceled(url); | ||||||||||||
|
|
||||||||||||
| static bool IsBlankNavigation(string? url) | ||||||||||||
| { | ||||||||||||
| // Null/empty URLs are handled by the early return in OnPageFinished, | ||||||||||||
| // so we only need to check for the explicit "about:blank" URL | ||||||||||||
| if (string.IsNullOrWhiteSpace(url)) | ||||||||||||
| return false; | ||||||||||||
|
Comment on lines
+104
to
+105
|
||||||||||||
| if (string.IsNullOrWhiteSpace(url)) | |
| return false; | |
| // Return true for null, empty, or whitespace URLs | |
| if (string.IsNullOrWhiteSpace(url)) | |
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could expand to more variants?
Examples:
- "about:blank#"
- "about:blank/"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be invoked on every Navigation. The impact on performance now is minimal for the current simple string comparison, but take this into account (for example, if think in use URI parsing or more complex stuff).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsuarezruiz , When the source is null, we set the default WebView source to about:blank. As a result, the OnPageFinished method always receives about:blank as the parameter. Therefore, handling additional variants such as about:blank# or about:blank/ is not necessary in this case.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (navigate && !IsBlankNavigation(url))Based on this:
This could break the fix from #29234 which wanted to prevent events when Source is null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsuarezruiz ,
url = null → IsBlankNavigation returns false → Navigated firesurl should never be null. We’ve modified the source so that if the source is not null, it uses that source; otherwise, it uses "about:blank".
In the OnPageFinished method, url should never be null.
url = "about:blank" → IsBlankNavigation returns true → Navigated doesn’t fireYes, this is the expected behavior. The PR#29234 resolves the issue where Android triggered the Navigating event with a null source.
With my fix, if the source is null, we set it to "about:blank". When the URL is "about:blank", the Navigating event will not fire.
So with the current fix, it resolves both issues #32030 and #29234.