-
Couldn't load subscription status.
- 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
base: main
Are you sure you want to change the base?
Changes from all commits
41dba73
f24b811
d35f613
e4dc461
c544158
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,58 @@ | ||
| 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 | ||
| }; | ||
| webView.Navigated += WebView_Navigated; | ||
| grid.Add(webView); | ||
| Grid.SetRow(webView, 1); | ||
|
|
||
| // Create "below webview" Label | ||
| var bottomLabel = new Label | ||
| { | ||
| Text = "Below webview", | ||
| HorizontalTextAlignment = TextAlignment.Center | ||
| }; | ||
| grid.Add(bottomLabel); | ||
| Grid.SetRow(bottomLabel, 2); | ||
|
|
||
| // Set the grid as the page content | ||
| Content = grid; | ||
| } | ||
|
|
||
| async void WebView_Navigated(object sender, WebNavigatedEventArgs e) | ||
| { | ||
| await DisplayAlert("Navigation Completed", $"WebView navigated to: {e.Url}", "OK"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 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("TopLabel"); | ||
| VerifyScreenshot(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,7 +57,7 @@ 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) | ||||||||||||
| if (navigate && !IsBlankNavigation(url)) | ||||||||||||
| handler.VirtualView.Navigated(handler.CurrentNavigationEvent, GetValidUrl(url), _navigationResult); | ||||||||||||
|
|
||||||||||||
| handler.SyncPlatformCookiesToVirtualView(url); | ||||||||||||
|
|
@@ -96,6 +96,15 @@ public override bool OnRenderProcessGone(WebView? view, RenderProcessGoneDetail? | |||||||||||
| bool NavigatingCanceled(string? url) => | ||||||||||||
| !_handler.TryGetTarget(out var handler) || handler.NavigatingCanceled(url); | ||||||||||||
|
|
||||||||||||
| static bool IsBlankNavigation(string? url) | ||||||||||||
| { | ||||||||||||
| if (string.IsNullOrWhiteSpace(url)) | ||||||||||||
| return false; | ||||||||||||
|
Comment on lines
+101
to
+102
|
||||||||||||
| 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.