fix class generation in tag helpers; remove select tag helpers
This commit is contained in:
@@ -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<string> classes = Classes?.Split(" ").ToHashSet() ?? new HashSet<string>();
|
||||
|
||||
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<string> classes = Classes?.Split(" ").ToHashSet() ?? new HashSet<string>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<string> CurrentValues;
|
||||
|
||||
protected BaseSelectTagHelper(IHtmlGenerator generator)
|
||||
{
|
||||
Generator = generator;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IHtmlGenerator"/> used to generate the <see cref="SelectTagHelper"/>'s output.
|
||||
/// </summary>
|
||||
protected IHtmlGenerator Generator { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Rendering.ViewContext"/> of the executing view.
|
||||
/// </summary>
|
||||
[HtmlAttributeNotBound]
|
||||
[ViewContext]
|
||||
public ViewContext ViewContext { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// An expression to be evaluated against the current model.
|
||||
/// </summary>
|
||||
[HtmlAttributeName(FOR_ATTRIBUTE_NAME)]
|
||||
public ModelExpression For { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
||||
/// <optgroup> and <option> elements.
|
||||
/// </summary>
|
||||
public List<SelectListItem> Items { get; set; } = new List<SelectListItem>();
|
||||
|
||||
/// <summary>
|
||||
/// The name of the <input> element.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Passed through to the generated HTML in all cases. Also used to determine whether <see cref="For"/> is
|
||||
/// valid with an empty <see cref="ModelExpression.Name"/>.
|
||||
/// </remarks>
|
||||
public string Name { get; set; }
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
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 <select/> element.
|
||||
context.Items[typeof(SelectTagHelper)] = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Base allowMultiple on the instance or declared type of the expression to avoid a
|
||||
// "SelectExpressionNotEnumerable" InvalidOperationException during generation.
|
||||
// Metadata.IsEnumerableType is similar but does not take runtime type into account.
|
||||
var realModelType = For.ModelExplorer.ModelType;
|
||||
AllowMultiple = typeof(string) != realModelType && typeof(IEnumerable).IsAssignableFrom(realModelType);
|
||||
CurrentValues = Generator.GetCurrentValues(ViewContext, For.ModelExplorer, For.Name, AllowMultiple);
|
||||
|
||||
// Whether or not (not being highly unlikely) we generate anything, could update contained <option/>
|
||||
// elements. Provide selected values for <option/> tag helpers.
|
||||
//var currentValues = _currentValues == null ? null : new CurrentValues(_currentValues);
|
||||
//context.Items[typeof(SelectTagHelper)] = currentValues;
|
||||
}
|
||||
|
||||
|
||||
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
output.TagName = "select";
|
||||
|
||||
// Pass through attribute that is also a well-known HTML attribute. Must be done prior to any copying
|
||||
// from a TagBuilder.
|
||||
if (Name != null)
|
||||
{
|
||||
output.CopyHtmlAttribute(nameof(Name), context);
|
||||
}
|
||||
|
||||
// Ensure GenerateSelect() _never_ looks anything up in ViewData.
|
||||
var items = Items ?? Enumerable.Empty<SelectListItem>();
|
||||
|
||||
// Ensure Generator does not throw due to empty "fullName" if user provided a name attribute.
|
||||
IDictionary<string, object> htmlAttributes = null;
|
||||
if (string.IsNullOrEmpty(For.Name) && string.IsNullOrEmpty(ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix) && !string.IsNullOrEmpty(Name))
|
||||
{
|
||||
htmlAttributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{ "name", Name },
|
||||
};
|
||||
}
|
||||
|
||||
var tagBuilder = Generator.GenerateSelect(
|
||||
ViewContext,
|
||||
For.ModelExplorer,
|
||||
optionLabel: null,
|
||||
expression: For.Name,
|
||||
selectList: items,
|
||||
currentValues: CurrentValues,
|
||||
allowMultiple: AllowMultiple,
|
||||
htmlAttributes: htmlAttributes);
|
||||
|
||||
if (tagBuilder != null)
|
||||
{
|
||||
output.MergeAttributes(tagBuilder);
|
||||
if (tagBuilder.HasInnerHtml)
|
||||
{
|
||||
output.PostContent.AppendHtml(tagBuilder.InnerHtml);
|
||||
}
|
||||
}
|
||||
|
||||
output.PreElement.AppendHtml("<div class=\"form-select\">");
|
||||
// TODO use iconoptions + taghelper
|
||||
output.PostElement.AppendHtml("<span class=\"-icon\"><svg class=\"app-icon\" width=\"18\" height=\"18\" xmlns=\"http://www.w3.org/2000/svg\" stroke-width=\"2\" data-symbol=\"chevron-down\"><use xlink:href=\"/assets/icons/feather.svg#chevron-down\"></use></svg></span>");
|
||||
output.PostElement.AppendHtml("</div>");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Mvc.TagHelpers;
|
||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||
|
||||
namespace zero.TagHelpers;
|
||||
|
||||
[HtmlTargetElement(Attributes = PREFIX + "*")]
|
||||
[HtmlTargetElement(Attributes = Prefix + "*")]
|
||||
public class ClassTagHelper : TagHelper
|
||||
{
|
||||
private const string PREFIX = "app-class:";
|
||||
|
||||
[HtmlAttributeName("class")]
|
||||
public string Classes { get; set; }
|
||||
{
|
||||
private const string Prefix = "app-class:";
|
||||
|
||||
private IDictionary<string, bool> _classValues;
|
||||
|
||||
[HtmlAttributeName("", DictionaryAttributePrefix = PREFIX)]
|
||||
[HtmlAttributeName("", DictionaryAttributePrefix = Prefix)]
|
||||
public IDictionary<string, bool> ClassValues
|
||||
{
|
||||
get => _classValues ??= new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
|
||||
@@ -22,17 +21,9 @@ public class ClassTagHelper : TagHelper
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var items = _classValues.Where(e => e.Value).Select(e => e.Key).ToList();
|
||||
|
||||
if (!String.IsNullOrEmpty(Classes))
|
||||
foreach (KeyValuePair<string, bool> item in _classValues.Where(e => e.Value))
|
||||
{
|
||||
items.Insert(0, Classes);
|
||||
}
|
||||
|
||||
if (items.Any())
|
||||
{
|
||||
var classes = String.Join(" ", items.ToArray());
|
||||
output.Attributes.Add("class", classes);
|
||||
output.AddClass(item.Key, HtmlEncoder.Default);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||
|
||||
namespace zero.TagHelpers;
|
||||
|
||||
[HtmlTargetElement("app-gender-select", Attributes = FOR_ATTRIBUTE_NAME)]
|
||||
public class SelectGenderTagHelper : BaseSelectTagHelper
|
||||
{
|
||||
ILocalizer _localizer;
|
||||
|
||||
public SelectGenderTagHelper(IHtmlGenerator generator, ILocalizer localizer) : base(generator)
|
||||
{
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
|
||||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
Items.Add(new SelectListItem(string.Empty, null));
|
||||
|
||||
foreach (Gender gender in Enum.GetValues<Gender>())
|
||||
{
|
||||
string key = "forms.gender." + gender.ToString().ToLowerInvariant();
|
||||
Items.Add(new SelectListItem(_localizer.Text(key), gender.ToString()));
|
||||
}
|
||||
|
||||
await base.ProcessAsync(context, output);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Razor.TagHelpers;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using static System.Text.RegularExpressions.Regex;
|
||||
|
||||
namespace zero.TagHelpers;
|
||||
|
||||
@@ -18,13 +18,13 @@ public class StripHtmlTagHelper : TagHelper
|
||||
output.TagName = string.Empty;
|
||||
string text = string.Empty;
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(Text))
|
||||
if (!string.IsNullOrWhiteSpace(Text))
|
||||
{
|
||||
text = WebUtility.HtmlDecode(Regex.Replace(Text, "<[^>]*(>|$)", string.Empty).Trim());
|
||||
text = WebUtility.HtmlDecode(Replace(Text, "<[^>]*(>|$)", string.Empty).Trim());
|
||||
|
||||
if (MaxLength > 0 && text.Length > MaxLength)
|
||||
{
|
||||
text = text.Substring(0, MaxLength) + "...";
|
||||
text = string.Concat(text.AsSpan(0, MaxLength), "...");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user