Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 26b06da

Browse files
authored
Fix popping with ".." and QueryString (#13507)
* Fix popping with ".." and QueryString * - add additional Unit Tests
1 parent 2289789 commit 26b06da

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Xamarin.Forms.Core.UnitTests/ShellModalTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ public async Task BasicQueryStringTest()
316316
Assert.AreEqual("1234", testPage.SomeQueryParameter);
317317
}
318318

319+
320+
[TestCase("..")]
321+
[TestCase("../")]
322+
public async Task PoppingWithQueryString(string input)
323+
{
324+
Routing.RegisterRoute("details", typeof(ShellTestPage));
325+
var shell = new TestShell(CreateShellItem());
326+
327+
await shell.GoToAsync("details");
328+
await shell.GoToAsync("ModalTestPage");
329+
330+
await shell.GoToAsync(new ShellNavigationState($"{input}?{nameof(ShellTestPage.SomeQueryParameter)}=1234"));
331+
shell.AssertCurrentStateEquals($"//{shell.CurrentItem.CurrentItem.CurrentItem.Route}/details");
332+
333+
var testPage = shell.CurrentPage as ShellTestPage;
334+
Assert.AreEqual("1234", testPage.SomeQueryParameter);
335+
}
336+
319337
[Test]
320338
public async Task NavigatingAndNavigatedFiresForShellModal()
321339
{

Xamarin.Forms.Core.UnitTests/ShellTestBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ public void RegisterPage(string route, ContentPage contentPage)
341341
Routing.RegisterRoute(route, new ConcretePageFactory(contentPage));
342342
}
343343

344+
public void AssertCurrentStateEquals(string expectedState)
345+
{
346+
Assert.AreEqual(expectedState, CurrentState.Location.ToString());
347+
}
348+
344349
public class ConcretePageFactory : RouteFactory
345350
{
346351
ContentPage _contentPage;

Xamarin.Forms.Core/Shell/ShellUriHandler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ internal static Uri FormatUri(Uri path, Shell shell)
1717
{
1818
if (path.OriginalString.StartsWith("..") && shell?.CurrentState != null)
1919
{
20+
var pathAndQueryString = path.OriginalString.Split(new[] { '?' }, 2);
21+
string pathPart = pathAndQueryString[0];
22+
string queryString = (pathAndQueryString.Length > 1) ? $"?{pathAndQueryString[1]}" : String.Empty;
23+
2024
var pages = ShellNavigationManager.BuildFlattenedNavigationStack(shell);
21-
var currentState = shell.CurrentState.FullLocation.OriginalString;
2225

2326
List<string> restOfPath = new List<string>();
2427
bool dotsAllParsed = false;
25-
foreach (var p in path.OriginalString.Split(_pathSeparators))
28+
foreach (var p in pathPart.Split(_pathSeparators))
2629
{
2730
if (p != ".." || dotsAllParsed)
2831
{
@@ -54,7 +57,7 @@ internal static Uri FormatUri(Uri path, Shell shell)
5457
restOfPath.Insert(0, shell.CurrentItem.CurrentItem.CurrentItem.Route);
5558
restOfPath.Insert(0, shell.CurrentItem.CurrentItem.Route);
5659
restOfPath.Insert(0, shell.CurrentItem.Route);
57-
var result = String.Join(_pathSeparator, restOfPath);
60+
var result = $"{String.Join(_pathSeparator, restOfPath)}{queryString}";
5861
var returnValue = ConvertToStandardFormat("scheme", "host", null, new Uri(result, UriKind.Relative));
5962
return new Uri(FormatUri(returnValue.PathAndQuery), UriKind.Relative);
6063
}

0 commit comments

Comments
 (0)