From 787a5e29b650029b653ff1caa07a9bcab6883d62 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Tue, 14 Mar 2023 09:48:01 +0000 Subject: [PATCH] Don't render empty aria-describedby attributes (#247) Fixes #246 --- .../HtmlGeneration/ComponentGenerator.Checkboxes.cs | 2 +- .../HtmlGeneration/ComponentGenerator.Fieldset.cs | 4 ++-- .../HtmlGeneration/ComponentGenerator.FileUpload.cs | 2 +- .../HtmlGeneration/ComponentGenerator.InsetText.cs | 2 +- .../HtmlGeneration/ComponentGenerator.Select.cs | 2 +- .../HtmlGeneration/ComponentGenerator.TextArea.cs | 4 ++-- .../HtmlGeneration/ComponentGenerator.TextInput.cs | 6 +++--- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Checkboxes.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Checkboxes.cs index 4600b494..0bca83b3 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Checkboxes.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Checkboxes.cs @@ -133,7 +133,7 @@ void AddItem(CheckboxesItem item) AppendToDescribedBy(ref itemDescribedBy, hintId); } - if (itemDescribedBy != null) + if (!string.IsNullOrEmpty(itemDescribedBy)) { input.Attributes.Add("aria-describedby", itemDescribedBy); } diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Fieldset.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Fieldset.cs index 42783022..2a160c84 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Fieldset.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Fieldset.cs @@ -25,12 +25,12 @@ public TagBuilder GenerateFieldset( tagBuilder.MergeOptionalAttributes(attributes); tagBuilder.MergeCssClass("govuk-fieldset"); - if (role != null) + if (!string.IsNullOrEmpty(role)) { tagBuilder.Attributes.Add("role", role); } - if (describedBy != null) + if (!string.IsNullOrEmpty(describedBy)) { tagBuilder.Attributes.Add("aria-describedby", describedBy); } diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.FileUpload.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.FileUpload.cs index a0f11552..e05bbfd0 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.FileUpload.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.FileUpload.cs @@ -30,7 +30,7 @@ public TagBuilder GenerateFileUpload( tagBuilder.Attributes.Add("name", name); tagBuilder.Attributes.Add("type", "file"); - if (describedBy != null) + if (!string.IsNullOrEmpty(describedBy)) { tagBuilder.Attributes.Add("aria-describedby", describedBy); } diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.InsetText.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.InsetText.cs index 36f2319c..f5b78bd4 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.InsetText.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.InsetText.cs @@ -19,7 +19,7 @@ public TagBuilder GenerateInsetText( tagBuilder.MergeOptionalAttributes(attributes); tagBuilder.MergeCssClass("govuk-inset-text"); - if (id != null) + if (!string.IsNullOrEmpty(id)) { tagBuilder.Attributes.Add("id", id); } diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Select.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Select.cs index 696cef25..487c11ff 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Select.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.Select.cs @@ -36,7 +36,7 @@ public TagBuilder GenerateSelect( tagBuilder.Attributes.Add("id", id); tagBuilder.Attributes.Add("name", name); - if (describedBy != null) + if (!string.IsNullOrEmpty(describedBy)) { tagBuilder.Attributes.Add("aria-describedby", describedBy); } diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextArea.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextArea.cs index dfdb5838..d790db4d 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextArea.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextArea.cs @@ -39,12 +39,12 @@ public virtual TagBuilder GenerateTextArea( tagBuilder.Attributes.Add("name", name); tagBuilder.Attributes.Add("rows", rows.ToString()); - if (describedBy != null) + if (!string.IsNullOrEmpty(describedBy)) { tagBuilder.Attributes.Add("aria-describedby", describedBy); } - if (autocomplete != null) + if (!string.IsNullOrEmpty(autocomplete)) { tagBuilder.Attributes.Add("autocomplete", autocomplete); } diff --git a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextInput.cs b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextInput.cs index 02b49cbc..a93cab7a 100644 --- a/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextInput.cs +++ b/src/GovUk.Frontend.AspNetCore/HtmlGeneration/ComponentGenerator.TextInput.cs @@ -52,12 +52,12 @@ public TagBuilder GenerateTextInput( tagBuilder.Attributes.Add("value", value); } - if (describedBy != null) + if (!string.IsNullOrEmpty(describedBy)) { tagBuilder.Attributes.Add("aria-describedby", describedBy); } - if (autocomplete != null) + if (!string.IsNullOrEmpty(autocomplete)) { tagBuilder.Attributes.Add("autocomplete", autocomplete); } @@ -67,7 +67,7 @@ public TagBuilder GenerateTextInput( tagBuilder.Attributes.Add("pattern", pattern); } - if (inputMode != null) + if (!string.IsNullOrEmpty(inputMode)) { tagBuilder.Attributes.Add("inputmode", inputMode); }