Skip to content

Commit 51d1e45

Browse files
committed
Anchor element has no href when no link provided
1 parent 5241c6f commit 51d1e45

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@if (string.IsNullOrEmpty(Href))
2+
{
3+
<a class="@CssClass">@ChildContent</a>
4+
}
5+
else
6+
{
7+
<a class="@CssClass" href="@Href">@ChildContent</a>
8+
}
9+
@code {
10+
[Parameter]
11+
public string CssClass { get; set; }
12+
13+
[Parameter]
14+
public string Href { get; set; }
15+
16+
[Parameter]
17+
public RenderFragment ChildContent { get; set; }
18+
}

src/LinkDotNet.Blog.Web/Features/Home/Components/BlogPostNavigation.razor

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
<nav aria-label="Page navigation">
44
<ul class="pagination justify-content-center">
55
<li class="page-item @(!PageList.IsFirstPage && PageList.Count > 0 ? string.Empty : "disabled")">
6-
<a class="page-link" href="@PreviousPageLink" tabindex="-1">Previous</a>
6+
<Anchor CssClass="page-link" Href="@PreviousPageLink">Previous</Anchor>
77
</li>
88
<li class="page-item @(!PageList.IsLastPage && PageList.Count > 0 ? string.Empty : "disabled")">
9-
<a class="page-link" href="@NextPageLink">Next</a>
9+
<Anchor CssClass="page-link" Href="@NextPageLink">Next</Anchor>
1010
</li>
1111
</ul>
1212
</nav>
1313

14-
@code {
15-
[Parameter]
16-
public IPagedList<BlogPost> PageList { get; set; }
14+
@code {
15+
[Parameter]
16+
public IPagedList<BlogPost> PageList { get; set; }
1717

18-
private string PreviousPageLink => PageList.IsFirstPage ? string.Empty : $"/{PageList.PageNumber - 1}";
19-
private string NextPageLink => PageList.IsLastPage ? string.Empty : $"/{PageList.PageNumber + 1}";
20-
}
18+
private string PreviousPageLink => PageList.IsFirstPage ? string.Empty : $"/{PageList.PageNumber - 1}";
19+
private string NextPageLink => PageList.IsLastPage ? string.Empty : $"/{PageList.PageNumber + 1}";
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Linq;
2+
using AngleSharp.Html.Dom;
3+
using AngleSharpWrappers;
4+
using Bunit;
5+
using LinkDotNet.Blog.Web.Features.Home.Components;
6+
7+
namespace LinkDotNet.Blog.UnitTests.Web.Features.Home.Components;
8+
9+
public class AnchorTests : TestContext
10+
{
11+
[Fact]
12+
public void ShouldShowHrefWhenNotEmpty()
13+
{
14+
var cut = RenderComponent<Anchor>(ps => ps
15+
.Add(p => p.Href, "http://url/")
16+
.Add(p => p.CssClass, "page"));
17+
18+
var anchor = cut.Find("a").Unwrap() as IHtmlAnchorElement;
19+
anchor.Should().NotBeNull();
20+
anchor.Href.Should().Be("http://url/");
21+
anchor.GetAttribute("class").Should().Be("page");
22+
}
23+
24+
[Fact]
25+
public void ShouldNotShowHrefWhenEmpty()
26+
{
27+
var cut = RenderComponent<Anchor>(ps => ps
28+
.Add(p => p.Href, string.Empty)
29+
.Add(p => p.CssClass, "page"));
30+
31+
var anchor = cut.Find("a").Unwrap() as IHtmlAnchorElement;
32+
anchor.Should().NotBeNull();
33+
anchor.Attributes.Count(a => a.Name == "href").Should().Be(0);
34+
}
35+
}

0 commit comments

Comments
 (0)