From ebb8d8b2d3edaf651f24846d2cc70703bb233bd2 Mon Sep 17 00:00:00 2001 From: Andrew Horth Date: Fri, 29 Sep 2023 14:09:54 +0100 Subject: [PATCH] Add tag helper to render empty content as a hyphen (#842) --- .../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 000000000..a99465d55 --- /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("-"); + } + } +}