This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
Allow tag helpers to target elements by attribute #311
Copy link
Copy link
Closed
Description
Currently, tag helpers target elements by element name. They should also be able to target elements via attribute names. This would allow tag helpers to easily target existing elements in a more aspect oriented fashion. It also has the benefit of potentially reducing overhead for said tag helpers, because they're only attached to elements that explicitly opt-in via the attribute.
Some of the MVC tag helpers we've already written would utilize this feature, e.g. validation summary.
A tag helper should be able to target elements by element name(s), attribute name(s), or both. They should be able to specify their targets in groups to allow for both AND and OR combinations.
Attribute names should support partial wild-cards, e.g. asp-param-*
.
Examples
// This tag helper will run on <loc> elements and any other element where the 'asp-loc' attribute is present
[TargetElement("loc")]
[TargetElement(attributes = "asp-loc")]
public class LocTagHelper : TagHelper
{
...
}
// This tag helper will run on <cache> elements and any other element where the 'asp-cache' attribute is present
[TargetElement("cache")]
[TargetElement(attributes = "asp-cache")]
public class CacheTagHelper : TagHelper
{
...
}
// This tag helper will run on <environment> elements where the 'names' attribute is present, and any other element where the 'asp-environment-names' attribute is present
[TargetElement("environment", attributes = "names")]
[TargetElement(attributes = "asp-environment-names")]
public class EnivronmentTagHelper : TagHelper
{
...
}
// This tag helper will run on <input> elements where the 'asp-for' attribute is present
[TargetElement("input", attributes = "asp-for")]
public class InputTagHelper : TagHelper
{
...
}
// This tag helper will run on <div>, <p>, <ul>, and <ol> elements where the 'asp-validation-summary' attribute is present, and on <validation-summary> elements
[TargetElement("div,p,ul,ol", attributes = "asp-validation-summary")]
[TargetElement("validation-summary")]
public class ValidationSummaryTagHelper : TagHelper
{
...
}
// This tag helper will run on any element where both the 'asp-one' and 'asp-two' attributes are present
[TargetElement(attributes = "asp-one,asp-two")]
public class SampleTagHelper : TagHelper
{
...
}
// This tag helper will run on any element where at least one attribute beginning with 'asp-param-' is present
[TargetElement(attributes = "asp-param-*")]
public class SameParamTagHelper : TagHelper
{
...
}
michaelvolz