Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Components/Components/src/NavigationManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,4 +762,51 @@ private static bool TryRebuildExistingQueryFromUri(

return true;
}

/// <summary>
/// Returns a URI constructed from <see cref="NavigationManager.Uri"/> with a hash
/// added, updated, or removed.
/// </summary>
/// <param name="navigationManager">The <see cref="NavigationManager"/>.</param>
/// <param name="hash">The hash string. If empty or null, the hash will be removed from the URI.</param>
/// <returns>The URI with the specified hash.</returns>
/// <remarks>
/// <para>
/// If <paramref name="hash"/> does not start with <c>#</c>, then <c>#</c> will be prepended.
/// </para>
/// <para>
/// This method is useful when the document's <c>baseURI</c> differs from its location,
/// such as when a <c>&lt;base&gt;</c> element is used, since relative hash URLs are resolved
/// relative to the <c>baseURI</c>.
/// </para>
/// <example>
/// <code>
/// @inject NavigationManager Nav
/// &lt;a href="@Nav.GetUriWithHash("section1")"&gt;Go to section 1&lt;/a&gt;
/// </code>
/// </example>
/// </remarks>
public static string GetUriWithHash(this NavigationManager navigationManager, string? hash)
{
ArgumentNullException.ThrowIfNull(navigationManager);

var uri = navigationManager.Uri;
var hashStartIndex = uri.IndexOf('#');

// Get URI without the existing hash
var uriWithoutHash = hashStartIndex < 0 ? uri : uri.Substring(0, hashStartIndex);

if (string.IsNullOrEmpty(hash))
{
return uriWithoutHash;
}

// Ensure hash starts with '#'
if (hash[0] != '#')
{
return string.Concat(uriWithoutHash, "#", hash);
}

return string.Concat(uriWithoutHash, hash);
}
Comment thread
ilonatommy marked this conversation as resolved.
}
1 change: 1 addition & 0 deletions src/Components/Components/src/PublicAPI.Unshipped.txt
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!
Comment thread
ilonatommy marked this conversation as resolved.
Outdated
42 changes: 42 additions & 0 deletions src/Components/Components/test/NavigationManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
ilonatommy marked this conversation as resolved.
{
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()
{
Expand Down
Loading