diff --git a/src/Dfe.PlanTech.Application/Extensions/StringExtensions.cs b/src/Dfe.PlanTech.Application/Extensions/StringExtensions.cs index a2920c852..e558debca 100644 --- a/src/Dfe.PlanTech.Application/Extensions/StringExtensions.cs +++ b/src/Dfe.PlanTech.Application/Extensions/StringExtensions.cs @@ -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)) @@ -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)); + } } diff --git a/src/Dfe.PlanTech.Web/Models/PageViewModel.cs b/src/Dfe.PlanTech.Web/Models/PageViewModel.cs index a1e76a7d8..5998d41a8 100644 --- a/src/Dfe.PlanTech.Web/Models/PageViewModel.cs +++ b/src/Dfe.PlanTech.Web/Models/PageViewModel.cs @@ -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; diff --git a/src/Dfe.PlanTech.Web/Views/Shared/Components/Title.cshtml b/src/Dfe.PlanTech.Web/Views/Shared/Components/Title.cshtml index 89f96cc08..12980692f 100644 --- a/src/Dfe.PlanTech.Web/Views/Shared/Components/Title.cshtml +++ b/src/Dfe.PlanTech.Web/Views/Shared/Components/Title.cshtml @@ -1,3 +1,4 @@ +@using Dfe.PlanTech @model Dfe.PlanTech.Domain.Content.Models.Title; -

@System.Net.WebUtility.HtmlDecode(Model.Text)

\ No newline at end of file +

@StringExtensions.UseNonBreakingHyphenAndHtmlDecode(Model.Text)

diff --git a/tests/Dfe.PlanTech.Web.UnitTests/Helpers/StringExtensionsTests.cs b/tests/Dfe.PlanTech.Web.UnitTests/Helpers/StringExtensionsTests.cs new file mode 100644 index 000000000..a474680fe --- /dev/null +++ b/tests/Dfe.PlanTech.Web.UnitTests/Helpers/StringExtensionsTests.cs @@ -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); + } + } +}