diff --git a/zero/TagHelpers/ActiveTagHelper.cs b/zero/TagHelpers/ActiveTagHelper.cs index 458e70e1..ecc75e60 100644 --- a/zero/TagHelpers/ActiveTagHelper.cs +++ b/zero/TagHelpers/ActiveTagHelper.cs @@ -1,79 +1,53 @@ -using Microsoft.AspNetCore.Http; +using System.Text.Encodings.Web; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers; namespace zero.TagHelpers; [HtmlTargetElement(Attributes = "app-active")] -public class ActiveTagHelper : TagHelper +public class ActiveTagHelper(IHttpContextAccessor contextAccessor) : TagHelper { - private readonly IHttpContextAccessor _httpContextAccessor; + [HtmlAttributeName("href")] + public string Href { get; set; } - [HtmlAttributeName("class")] - public string Classes { get; set; } - - public override int Order => 100; - - - public ActiveTagHelper(IHttpContextAccessor contextAccessor) - { - _httpContextAccessor = contextAccessor; - } + public override int Order { get; } = 100; public override void Process(TagHelperContext context, TagHelperOutput output) { output.Attributes.RemoveAll("app-active"); - output.Attributes.TryGetAttribute("Href", out TagHelperAttribute _href); - string href = _href?.Value?.ToString() ?? string.Empty; - - HashSet classes = Classes?.Split(" ").ToHashSet() ?? new HashSet(); - - if (_httpContextAccessor.HttpContext.IsPartOfUrl(href)) + if (contextAccessor.HttpContext.IsPartOfUrl(Href)) { - classes.Add("is-active"); + output.AddClass("is-active", HtmlEncoder.Default); } - if (_httpContextAccessor.HttpContext.IsUrl(href)) + if (contextAccessor.HttpContext.IsUrl(Href)) { - classes.Add("is-active-exact"); + output.AddClass("is-active-exact", HtmlEncoder.Default); } - output.Attributes.SetAttribute("class", string.Join(" ", classes)); + output.Attributes.SetAttribute("href", Href); } } [HtmlTargetElement(Attributes = "app-active-exact")] -public class ActiveExactTagHelper : TagHelper +public class ActiveExactTagHelper(IHttpContextAccessor contextAccessor) : TagHelper { - private readonly IHttpContextAccessor _httpContextAccessor; - - [HtmlAttributeName("class")] - public string Classes { get; set; } - - public override int Order => 100; - - - public ActiveExactTagHelper(IHttpContextAccessor contextAccessor) - { - _httpContextAccessor = contextAccessor; - } + [HtmlAttributeName("href")] + public string Href { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.Attributes.RemoveAll("app-active-exact"); - output.Attributes.TryGetAttribute("Href", out TagHelperAttribute _href); - string href = _href?.Value?.ToString() ?? string.Empty; - - HashSet classes = Classes?.Split(" ").ToHashSet() ?? new HashSet(); - - if (_httpContextAccessor.HttpContext.IsUrl(href)) + if (contextAccessor.HttpContext.IsUrl(Href)) { - classes.Add("is-active-exact"); + output.AddClass("is-active-exact", HtmlEncoder.Default); } - output.Attributes.SetAttribute("class", string.Join(" ", classes)); + output.Attributes.SetAttribute("href", Href); } } \ No newline at end of file diff --git a/zero/TagHelpers/BaseSelectTagHelper.cs b/zero/TagHelpers/BaseSelectTagHelper.cs deleted file mode 100644 index 35a8bc5b..00000000 --- a/zero/TagHelpers/BaseSelectTagHelper.cs +++ /dev/null @@ -1,134 +0,0 @@ -using Microsoft.AspNetCore.Mvc.Rendering; -using Microsoft.AspNetCore.Mvc.TagHelpers; -using Microsoft.AspNetCore.Mvc.ViewFeatures; -using Microsoft.AspNetCore.Razor.TagHelpers; -using System.Collections; - -namespace zero.TagHelpers; - -public abstract class BaseSelectTagHelper : TagHelper -{ - protected const string FOR_ATTRIBUTE_NAME = "asp-for"; - protected bool AllowMultiple; - protected ICollection CurrentValues; - - protected BaseSelectTagHelper(IHtmlGenerator generator) - { - Generator = generator; - } - - - /// - /// Gets the used to generate the 's output. - /// - protected IHtmlGenerator Generator { get; } - - /// - /// Gets the of the executing view. - /// - [HtmlAttributeNotBound] - [ViewContext] - public ViewContext ViewContext { get; set; } - - /// - /// An expression to be evaluated against the current model. - /// - [HtmlAttributeName(FOR_ATTRIBUTE_NAME)] - public ModelExpression For { get; set; } - - /// - /// A collection of objects used to populate the <select> element with - /// <optgroup> and <option> elements. - /// - public List Items { get; set; } = new List(); - - /// - /// The name of the <input> element. - /// - /// - /// Passed through to the generated HTML in all cases. Also used to determine whether is - /// valid with an empty . - /// - public string Name { get; set; } - - - /// - public override void Init(TagHelperContext context) - { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - if (For == null) - { - // Informs contained elements that they're running within a targeted