From 9a7b37282a31e86ca51c53e46855e54c23950020 Mon Sep 17 00:00:00 2001 From: Andrew Horth Date: Thu, 28 Sep 2023 17:33:32 +0100 Subject: [PATCH] Add tag helper to render empty content as a hyphen --- .../TagHelpers/UseEmptyFallbackTagHelper.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TagHelpers/UseEmptyFallbackTagHelper.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TagHelpers/UseEmptyFallbackTagHelper.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TagHelpers/UseEmptyFallbackTagHelper.cs new file mode 100644 index 0000000000..a99465d55b --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/TagHelpers/UseEmptyFallbackTagHelper.cs @@ -0,0 +1,23 @@ +using System.Text.Encodings.Web; +using Microsoft.AspNetCore.Mvc.TagHelpers; +using Microsoft.AspNetCore.Razor.TagHelpers; + +namespace TeachingRecordSystem.SupportUi.TagHelpers; + +[HtmlTargetElement("*", Attributes = "use-empty-fallback")] +public class UseEmptyFallbackTagHelper : TagHelper +{ + public override int Order => -1; + + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + { + var content = await output.GetChildContentAsync(); + output.Attributes.RemoveAll("use-empty-fallback"); + + if (content.IsEmptyOrWhiteSpace) + { + output.AddClass("trs-subtle-emphasis", HtmlEncoder.Default); + output.Content.SetContent("-"); + } + } +}