-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add NavigationManager.GetUriWithHash() extension method #64615
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 2 commits
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #nullable enable | ||
| static Microsoft.AspNetCore.Components.NavigationManagerExtensions.GetUriWithHash(this Microsoft.AspNetCore.Components.NavigationManager! navigationManager, string? hash) -> string! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a point of
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,6 +213,48 @@ public void GetUriWithQueryParameters_ThrowsWhenAnyParameterNameIsEmpty(string b | |
| Assert.StartsWith("Cannot have empty query parameter names.", exception.Message); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("scheme://host/", "section1", "scheme://host/#section1")] | ||
| [InlineData("scheme://host/", "#section1", "scheme://host/#section1")] | ||
| [InlineData("scheme://host/path", "section1", "scheme://host/path#section1")] | ||
| [InlineData("scheme://host/path/", "section1", "scheme://host/path/#section1")] | ||
| [InlineData("scheme://host/path?query=value", "section1", "scheme://host/path?query=value#section1")] | ||
| [InlineData("scheme://host/path?query=value#oldHash", "section1", "scheme://host/path?query=value#section1")] | ||
| [InlineData("scheme://host/path#oldHash", "newHash", "scheme://host/path#newHash")] | ||
| [InlineData("scheme://host/path#old", "#new", "scheme://host/path#new")] | ||
| public void GetUriWithHash_AddsOrReplacesHash(string baseUri, string hash, string expectedUri) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of these tests really checks base uri different than root. In the issue we had an example of a case with |
||
| { | ||
| var navigationManager = new TestNavigationManager(baseUri); | ||
| var actualUri = navigationManager.GetUriWithHash(hash); | ||
|
|
||
| Assert.Equal(expectedUri, actualUri); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("scheme://host/", "scheme://host/")] | ||
| [InlineData("scheme://host/path", "scheme://host/path")] | ||
| [InlineData("scheme://host/path#hash", "scheme://host/path")] | ||
| [InlineData("scheme://host/path?query=value#hash", "scheme://host/path?query=value")] | ||
| public void GetUriWithHash_RemovesHashWhenHashIsNullOrEmpty(string baseUri, string expectedUri) | ||
| { | ||
| var navigationManager = new TestNavigationManager(baseUri); | ||
|
|
||
| var actualUriWithNull = navigationManager.GetUriWithHash(null); | ||
| Assert.Equal(expectedUri, actualUriWithNull); | ||
|
|
||
| var actualUriWithEmpty = navigationManager.GetUriWithHash(string.Empty); | ||
| Assert.Equal(expectedUri, actualUriWithEmpty); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetUriWithHash_ThrowsWhenNavigationManagerIsNull() | ||
| { | ||
| NavigationManager navigationManager = null; | ||
|
|
||
| var exception = Assert.Throws<ArgumentNullException>(() => navigationManager.GetUriWithHash("hash")); | ||
| Assert.Equal("navigationManager", exception.ParamName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LocationChangingHandlers_CanContinueTheNavigationSynchronously_WhenOneHandlerIsRegistered() | ||
| { | ||
|
|
||
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.
@copilot can you search on the docs and in other helpers on the repo how to do this with minimal allocations? (Using the Span APIs)
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.
Refactored to use
string.Createfor minimal allocations (e49d709). The implementation now:string.Createwith the existing strings and length values