Skip to content
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

Replace standard Hyphens with non breaking hyphens #958

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions src/Dfe.PlanTech.Application/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Net;

namespace Dfe.PlanTech;

public static class StringExtensions
{
public const string NonBreakingHyphen = "‑";
public static string FirstCharToUpper(this string input)
{
if (string.IsNullOrEmpty(input))
Expand All @@ -12,4 +15,13 @@ public static string FirstCharToUpper(this string input)
input.AsSpan(0, 1).ToUpperInvariant(destination);
return $"{destination}{input.AsSpan(1)}";
}

public static string? UseNonBreakingHyphenAndHtmlDecode(this string? text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
return WebUtility.HtmlDecode(text.Replace("-", NonBreakingHyphen));
}
}
3 changes: 2 additions & 1 deletion src/Dfe.PlanTech.Web/Models/PageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class PageViewModel

public PageViewModel(Page page, Controller controller, IUser user, ILogger logger, bool displayBlueBanner = true)
{
controller.ViewData["Title"] = System.Net.WebUtility.HtmlDecode(page.Title?.Text) ??

controller.ViewData["Title"] = StringExtensions.UseNonBreakingHyphenAndHtmlDecode(page.Title?.Text) ??
"Plan Technology For Your School";
Page = page;
DisplayBlueBanner = displayBlueBanner;
Expand Down
3 changes: 2 additions & 1 deletion src/Dfe.PlanTech.Web/Views/Shared/Components/Title.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@using Dfe.PlanTech
@model Dfe.PlanTech.Domain.Content.Models.Title;

<h1 class="govuk-heading-xl">@System.Net.WebUtility.HtmlDecode(Model.Text)</h1>
<h1 class="govuk-heading-xl">@StringExtensions.UseNonBreakingHyphenAndHtmlDecode(Model.Text)</h1>
22 changes: 22 additions & 0 deletions tests/Dfe.PlanTech.Web.UnitTests/Helpers/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Xunit;

namespace Dfe.PlanTech.Web.UnitTests.Helpers
{
public class StringExtensionsTests
{
// Just to be clear, the expectedText Hyphens are Non breaking Hyphens and inputText Hyphens are standard
[Theory]
[InlineData("Single test-", "Single test‑")]
[InlineData("Single-test", "Single‑test")]
[InlineData("This is -a-test-", "This is ‑a‑test‑")]
[InlineData("This is-a-test", "This is‑a‑test")]
[InlineData(null, null)]
[InlineData("", "")]
public void CheckHyphensConvertedCorrectly(string? inputText, string? expectedText)
{
var result = StringExtensions.UseNonBreakingHyphenAndHtmlDecode(inputText);

Assert.Equal(expectedText, result);
}
}
}
Loading