Skip to content

Commit

Permalink
Merge pull request #958 from DFE-Digital/feat/244053/NonBreakingHyphen
Browse files Browse the repository at this point in the history
Replace standard Hyphens with non breaking hyphens
  • Loading branch information
DrewAire authored Jan 31, 2025
2 parents d9f1722 + 5a759fe commit 43508c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
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);
}
}
}

0 comments on commit 43508c7

Please sign in to comment.