Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Allow tag helpers to target elements by attribute #311

Closed
DamianEdwards opened this issue Feb 20, 2015 · 1 comment
Closed

Allow tag helpers to target elements by attribute #311

DamianEdwards opened this issue Feb 20, 2015 · 1 comment

Comments

@DamianEdwards
Copy link
Member

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
{
    ...
}
@danroth27 danroth27 added this to the 4.0.0-rc1 milestone Feb 23, 2015
NTaylorMullen added a commit that referenced this issue Mar 2, 2015
- Transitioned HtmlElementNameAttribute into a more generic TargetElementAttribute. Targeting an HTML element can be done by attribute, tag or both.
- Updated TagHelperDescriptor to track required attributes.
- Updated TagHelperProvider to ask for provided attributes when resolving TagHelperDescriptors, this is used to apply RequiredAttributes.
- Updated TagHelperParseTreeRewriter to properly track HTML elements that coincide with a TagHelper scope based on the presence of RequiredAttributes.

#311
NTaylorMullen added a commit that referenced this issue Mar 2, 2015
- Added tests to validate TargetElementAttribute, TagHelperDescriptorFactory, CSharpTagHelperCodeRenderer and TagHelperParseTreeRewriterTests.
- Renamed HtmlElementNameAttributeTest to TargetElementAttributeTest.

#311
NTaylorMullen added a commit that referenced this issue Mar 3, 2015
- Transitioned HtmlElementNameAttribute into a more generic TargetElementAttribute. Targeting an HTML element can be done by attribute, tag or both.
- Updated TagHelperDescriptor to track required attributes.
- Updated TagHelperProvider to ask for provided attributes when resolving TagHelperDescriptors, this is used to apply RequiredAttributes.
- Updated TagHelperParseTreeRewriter to properly track HTML elements that coincide with a TagHelper scope based on the presence of RequiredAttributes.

#311
NTaylorMullen added a commit that referenced this issue Mar 3, 2015
- Added tests to validate TargetElementAttribute, TagHelperDescriptorFactory, CSharpTagHelperCodeRenderer and TagHelperParseTreeRewriterTests.
- Renamed HtmlElementNameAttributeTest to TargetElementAttributeTest.

#311
NTaylorMullen added a commit that referenced this issue Mar 5, 2015
- Transitioned HtmlElementNameAttribute into a more generic TargetElementAttribute. Targeting an HTML element can be done by attribute, tag or both.
- Updated TagHelperDescriptor to track required attributes.
- Updated TagHelperProvider to ask for provided attributes when resolving TagHelperDescriptors, this is used to apply RequiredAttributes.
- Updated TagHelperParseTreeRewriter to properly track HTML elements that coincide with a TagHelper scope based on the presence of RequiredAttributes.

#311
NTaylorMullen added a commit that referenced this issue Mar 5, 2015
- Added tests to validate TargetElementAttribute, TagHelperDescriptorFactory, CSharpTagHelperCodeRenderer and TagHelperParseTreeRewriterTests.
- Renamed HtmlElementNameAttributeTest to TargetElementAttributeTest.

#311
NTaylorMullen added a commit that referenced this issue Mar 11, 2015
- Transitioned HtmlElementNameAttribute into a more generic TargetElementAttribute. Targeting an HTML element can be done by attribute, tag or both.
- Updated TagHelperDescriptor to track required attributes.
- Updated TagHelperProvider to ask for provided attributes when resolving TagHelperDescriptors, this is used to apply RequiredAttributes.
- Updated TagHelperParseTreeRewriter to properly track HTML elements that coincide with a TagHelper scope based on the presence of RequiredAttributes.

#311
NTaylorMullen added a commit that referenced this issue Mar 11, 2015
- Added tests to validate TargetElementAttribute, TagHelperDescriptorFactory, CSharpTagHelperCodeRenderer and TagHelperParseTreeRewriterTests.
- Renamed HtmlElementNameAttributeTest to TargetElementAttributeTest.

#311
NTaylorMullen added a commit that referenced this issue Mar 14, 2015
- Transitioned HtmlElementNameAttribute into a more generic TargetElementAttribute. Targeting an HTML element can be done by attribute, tag or both.
- Updated TagHelperDescriptor to track required attributes.
- Updated TagHelperProvider to ask for provided attributes when resolving TagHelperDescriptors, this is used to apply RequiredAttributes.
- Updated TagHelperParseTreeRewriter to properly track HTML elements that coincide with a TagHelper scope based on the presence of RequiredAttributes.

#311
NTaylorMullen added a commit that referenced this issue Mar 14, 2015
- Added tests to validate TargetElementAttribute, TagHelperDescriptorFactory, CSharpTagHelperCodeRenderer and TagHelperParseTreeRewriterTests.
- Renamed HtmlElementNameAttributeTest to TargetElementAttributeTest.

#311
NTaylorMullen added a commit that referenced this issue Mar 18, 2015
- Transitioned HtmlElementNameAttribute into a more generic TargetElementAttribute. Targeting an HTML element can be done by attribute, tag or both.
- Updated TagHelperDescriptor to track required attributes.
- Updated TagHelperProvider to ask for provided attributes when resolving TagHelperDescriptors, this is used to apply RequiredAttributes.
- Updated TagHelperParseTreeRewriter to properly track HTML elements that coincide with a TagHelper scope based on the presence of RequiredAttributes.

#311
NTaylorMullen added a commit that referenced this issue Mar 18, 2015
- Added tests to validate TargetElementAttribute, TagHelperDescriptorFactory, CSharpTagHelperCodeRenderer and TagHelperParseTreeRewriterTests.
- Renamed HtmlElementNameAttributeTest to TargetElementAttributeTest.

#311
@NTaylorMullen
Copy link
Member

d22246f
4345b06

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants