Skip to content

Commit

Permalink
Fix default aria-label on pagination items (fixes #256)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Mar 31, 2023
1 parent 5e3c75d commit f3eeeaa
Show file tree
Hide file tree
Showing 91 changed files with 230 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public TagBuilder GeneratePagination(
li.MergeOptionalAttributes(paginationItem.Attributes);
itemLink.AddCssClass("govuk-link");
itemLink.AddCssClass("govuk-pagination__link");
itemLink.Attributes.Add("aria-label", paginationItem.VisuallyHiddenText ?? $"Page {paginationItem.Number}");
itemLink.Attributes.Add("aria-label", paginationItem.VisuallyHiddenText ?? $"Page {paginationItem.Number.ToHtmlString()}");

if (paginationItem.Href is not null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;

namespace GovUk.Frontend.AspNetCore.HtmlGeneration
{
/// <summary>
/// Utility extensions for <see cref="IHtmlContent"/>.
/// </summary>
public static class HtmlContentExtensions
{
/// <summary>
/// Returns a <see cref="string"/> of HTML with the contents of the <paramref name="content"/>.
/// </summary>
public static string ToHtmlString(this IHtmlContent content)
{
using var writer = new StringWriter();
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GovUk.Frontend.AspNetCore\GovUk.Frontend.AspNetCore.csproj" />
</ItemGroup>

</Project>
14 changes: 3 additions & 11 deletions test/GovUk.FrontEnd.AspNetCore.TestCommon/HtmlContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.IO;
using System.Text.Encodings.Web;
using AngleSharp.Dom;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using Microsoft.AspNetCore.Html;

namespace GovUk.Frontend.AspNetCore.TestCommon
Expand All @@ -9,21 +8,14 @@ public static class HtmlContentExtensions
{
public static IElement RenderToElement(this IHtmlContent content)
{
var html = content.RenderToString();
var html = content.ToHtmlString();
return HtmlHelper.ParseHtmlElement(html);
}

public static IElement[] RenderToElements(this IHtmlContent content)
{
var html = content.RenderToString();
var html = content.ToHtmlString();
return HtmlHelper.ParseHtmlElements(html);
}

public static string RenderToString(this IHtmlContent content)
{
using var writer = new StringWriter();
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Accordion(ComponentTestCaseData<OptionsJson.Accordion> data) =>
.OrEmpty();

return generator.GenerateAccordion(options.Id, headingLevel, attributes, items)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void BackLink(ComponentTestCaseData<OptionsJson.BackLink> data) =>
new HtmlString(ComponentGenerator.BackLinkDefaultContent);

return generator.GenerateBackLink(content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Breadcrumbs(ComponentTestCaseData<OptionsJson.Breadcrumbs> data) =>
});

return generator.GenerateBreadcrumbs(collapseOnMobile, attributes, items)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void Button(ComponentTestCaseData<OptionsJson.Button> data) =>
isButtonLink ?
generator.GenerateButtonLink(isStartButton, disabled, content, attributes) :
generator.GenerateButton(isStartButton, disabled, preventDoubleClick, content, attributes)
).RenderToString();
).ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void CharacterCount(ComponentTestCaseData<OptionsJson.CharacterCount> dat
}));

return generator.GenerateCharacterCount(options.Id, options.MaxLength, options.MaxWords, options.Threshold, content, countMessageAttributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Details(ComponentTestCaseData<OptionsJson.Details> data) =>
textContent,
textAttributes: null,
attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class ComponentTests
public void ErrorMessage(ComponentTestCaseData<OptionsJson.ErrorMessage> data) =>
CheckComponentHtmlMatchesExpectedHtml(
data,
(generator, options) => BuildErrorMessage(generator, options).RenderToString());
(generator, options) => BuildErrorMessage(generator, options).ToHtmlString());

private static IHtmlContent BuildErrorMessage(ComponentGenerator generator, OptionsJson.ErrorMessage options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void ErrorSummary(ComponentTestCaseData<OptionsJson.ErrorSummary> data) =
descriptionAttributes: null,
attributes,
items)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Fieldset(ComponentTestCaseData<OptionsJson.Fieldset> data) =>
legendAttributes,
content,
attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class ComponentTests
public void Hint(ComponentTestCaseData<OptionsJson.Hint> data) =>
CheckComponentHtmlMatchesExpectedHtml(
data,
(generator, options) => BuildHint(generator, options).RenderToString());
(generator, options) => BuildHint(generator, options).ToHtmlString());

private static IHtmlContent BuildHint(ComponentGenerator generator, OptionsJson.Hint options)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GovUk.Frontend.AspNetCore.TestCommon;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using Xunit;

namespace GovUk.Frontend.AspNetCore.ConformanceTests
Expand All @@ -19,7 +19,7 @@ public void InsetText(ComponentTestCaseData<OptionsJson.InsetText> data) =>
.MergeAttribute("class", options.Classes);

return generator.GenerateInsetText(id, content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Label(ComponentTestCaseData<OptionsJson.Label> data) =>
{
var labelOptions = options with { For = options.For };

return BuildLabel(generator, labelOptions).RenderToString();
return BuildLabel(generator, labelOptions).ToHtmlString();
});

private static IHtmlContent BuildLabel(ComponentGenerator generator, OptionsJson.Label options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void NotificationBanner(ComponentTestCaseData<OptionsJson.NotificationBan
titleContent,
content,
attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Pagination(ComponentTestCaseData<OptionsJson.Pagination> data) =>
previous,
next,
options.LandmarkLabel,
attributes).RenderToString();
attributes).ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Panel(ComponentTestCaseData<OptionsJson.Panel> data) =>
.MergeAttribute("class", options.Classes);

return generator.GeneratePanel(headingLevel, titleContent, content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GovUk.Frontend.AspNetCore.TestCommon;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using Xunit;

namespace GovUk.Frontend.AspNetCore.ConformanceTests
Expand All @@ -24,7 +24,7 @@ public void PhaseBanner(ComponentTestCaseData<OptionsJson.PhaseBanner> data) =>
.MergeAttribute("class", options.Classes);

return generator.GeneratePhaseBanner(tagContent, tagAttributes, content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void SkipLink(ComponentTestCaseData<OptionsJson.SkipLink> data) =>
var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html) ?? new HtmlString("");

return generator.GenerateSkipLink(href, content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void SummaryList(ComponentTestCaseData<OptionsJson.SummaryList> data) =>
.OrEmpty();

return generator.GenerateSummaryList(attributes, rows)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GovUk.Frontend.AspNetCore.TestCommon;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using Xunit;

namespace GovUk.Frontend.AspNetCore.ConformanceTests
Expand All @@ -18,7 +18,7 @@ public void Tag(ComponentTestCaseData<OptionsJson.Tag> data) =>
.MergeAttribute("class", options.Classes);

return generator.GenerateTag(content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GovUk.Frontend.AspNetCore.TestCommon;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using Xunit;

namespace GovUk.Frontend.AspNetCore.ConformanceTests
Expand All @@ -19,7 +19,7 @@ public void WarningText(ComponentTestCaseData<OptionsJson.WarningText> data) =>
.MergeAttribute("class", options.Classes);

return generator.GenerateWarningText(iconFallbackText, content, attributes)
.RenderToString();
.ToHtmlString();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private string GenerateFormGroup(
haveError,
content,
attributes)
.RenderToString();
.ToHtmlString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using GovUk.Frontend.AspNetCore.TagHelpers;
using GovUk.Frontend.AspNetCore.TestCommon;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
Expand Down Expand Up @@ -47,7 +46,7 @@ public async Task ProcessAsync_AddsHeadingToContext()

// Assert
Assert.NotNull(itemContext.Heading);
Assert.Equal("Summary content", itemContext.Heading?.Content.RenderToString());
Assert.Equal("Summary content", itemContext.Heading?.Content.ToHtmlString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using GovUk.Frontend.AspNetCore.TagHelpers;
using GovUk.Frontend.AspNetCore.TestCommon;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
Expand Down Expand Up @@ -46,7 +46,7 @@ public async Task ProcessAsync_AddsSummaryToContext()

// Assert
Assert.NotNull(itemContext.Summary);
Assert.Equal("Summary content", itemContext.Summary?.Content.RenderToString());
Assert.Equal("Summary content", itemContext.Summary?.Content.ToHtmlString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using GovUk.Frontend.AspNetCore.TagHelpers;
using GovUk.Frontend.AspNetCore.TestCommon;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
Expand Down Expand Up @@ -51,9 +51,9 @@ public async Task ProcessAsync_AddsItemToContext()
Assert.Equal(1, accordionContext.Items.Count);

var firstItem = accordionContext.Items.First();
Assert.Equal("Heading", firstItem.HeadingContent?.RenderToString());
Assert.Equal("Summary", firstItem.SummaryContent?.RenderToString());
Assert.Equal("Content", firstItem.Content?.RenderToString());
Assert.Equal("Heading", firstItem.HeadingContent?.ToHtmlString());
Assert.Equal("Summary", firstItem.SummaryContent?.ToHtmlString());
Assert.Equal("Content", firstItem.Content?.ToHtmlString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ First content
</div>
</div>";

AssertEx.HtmlEqual(@expectedHtml, output.RenderToString());
AssertEx.HtmlEqual(@expectedHtml, output.ToHtmlString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GovUk.Frontend.AspNetCore.HtmlGeneration;
using GovUk.Frontend.AspNetCore.TagHelpers;
using GovUk.Frontend.AspNetCore.TestCommon;
using Microsoft.AspNetCore.Razor.TagHelpers;
Expand Down Expand Up @@ -42,7 +43,7 @@ public async Task ProcessAsync_WithContent_GeneratesExpectedOutput()
var expectedHtml = @"
<a class=""govuk-back-link"" href=""http://foo.com"">My custom link content</a>";

AssertEx.HtmlEqual(@expectedHtml, output.RenderToString());
AssertEx.HtmlEqual(@expectedHtml, output.ToHtmlString());
}

[Fact]
Expand Down Expand Up @@ -77,7 +78,7 @@ public async Task ProcessAsync_WithNoContent_GeneratesExpectedOutput()
var expectedHtml = @"
<a class=""govuk-back-link"" href=""http://foo.com"">Back</a>";

AssertEx.HtmlEqual(@expectedHtml, output.RenderToString());
AssertEx.HtmlEqual(@expectedHtml, output.ToHtmlString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task ProcessAsync_NoLink_AddsItemToContext()
// Assert
var lastItem = breadcrumbsContext.Items.Last();
Assert.Null(lastItem.Href);
Assert.Equal("The item", lastItem.Content?.RenderToString());
Assert.Equal("The item", lastItem.Content?.ToHtmlString());
}

[Fact]
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task ProcessAsync_WithLink_AddsItemToContext()
// Assert
var lastItem = breadcrumbsContext.Items.Last();
Assert.Equal("place.com", lastItem.Href);
Assert.Equal("The item", lastItem.Content?.RenderToString());
Assert.Equal("The item", lastItem.Content?.ToHtmlString());
}
}
}
Loading

0 comments on commit f3eeeaa

Please sign in to comment.