TagHelpers attribute targeting - part 1
This commit is contained in:
parent
2b246e7acc
commit
bfdeda797d
|
|
@ -15,6 +15,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
/// <summary>
|
||||
/// <see cref="ITagHelper"/> implementation targeting <input> elements with an <c>asp-for</c> attribute.
|
||||
/// </summary>
|
||||
[TargetElement("input", Attributes = ForAttributeName)]
|
||||
public class InputTagHelper : TagHelper
|
||||
{
|
||||
private const string ForAttributeName = "asp-for";
|
||||
|
|
@ -121,93 +122,79 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
output.CopyHtmlAttribute(nameof(Value), context);
|
||||
}
|
||||
|
||||
if (For == null)
|
||||
// Note null or empty For.Name is allowed because TemplateInfo.HtmlFieldPrefix may be sufficient.
|
||||
// IHtmlGenerator will enforce name requirements.
|
||||
var metadata = For.Metadata;
|
||||
var modelExplorer = For.ModelExplorer;
|
||||
if (metadata == null)
|
||||
{
|
||||
// Regular HTML <input/> element. Just make sure Format wasn't specified.
|
||||
if (Format != null)
|
||||
{
|
||||
throw new InvalidOperationException(Resources.FormatInputTagHelper_UnableToFormat(
|
||||
"<input>",
|
||||
ForAttributeName,
|
||||
FormatAttributeName));
|
||||
}
|
||||
throw new InvalidOperationException(Resources.FormatTagHelpers_NoProvidedMetadata(
|
||||
"<input>",
|
||||
ForAttributeName,
|
||||
nameof(IModelMetadataProvider),
|
||||
For.Name));
|
||||
}
|
||||
|
||||
string inputType;
|
||||
string inputTypeHint;
|
||||
if (string.IsNullOrEmpty(InputTypeName))
|
||||
{
|
||||
// Note GetInputType never returns null.
|
||||
inputType = GetInputType(modelExplorer, out inputTypeHint);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Note null or empty For.Name is allowed because TemplateInfo.HtmlFieldPrefix may be sufficient.
|
||||
// IHtmlGenerator will enforce name requirements.
|
||||
var metadata = For.Metadata;
|
||||
var modelExplorer = For.ModelExplorer;
|
||||
if (metadata == null)
|
||||
{
|
||||
throw new InvalidOperationException(Resources.FormatTagHelpers_NoProvidedMetadata(
|
||||
"<input>",
|
||||
ForAttributeName,
|
||||
nameof(IModelMetadataProvider),
|
||||
For.Name));
|
||||
}
|
||||
inputType = InputTypeName.ToLowerInvariant();
|
||||
inputTypeHint = null;
|
||||
}
|
||||
|
||||
string inputType;
|
||||
string inputTypeHint;
|
||||
if (string.IsNullOrEmpty(InputTypeName))
|
||||
{
|
||||
// Note GetInputType never returns null.
|
||||
inputType = GetInputType(modelExplorer, out inputTypeHint);
|
||||
}
|
||||
else
|
||||
{
|
||||
inputType = InputTypeName.ToLowerInvariant();
|
||||
inputTypeHint = null;
|
||||
}
|
||||
// inputType may be more specific than default the generator chooses below.
|
||||
if (!output.Attributes.ContainsKey("type"))
|
||||
{
|
||||
output.Attributes["type"] = inputType;
|
||||
}
|
||||
|
||||
// inputType may be more specific than default the generator chooses below.
|
||||
if (!output.Attributes.ContainsKey("type"))
|
||||
{
|
||||
output.Attributes["type"] = inputType;
|
||||
}
|
||||
TagBuilder tagBuilder;
|
||||
switch (inputType)
|
||||
{
|
||||
case "checkbox":
|
||||
GenerateCheckBox(modelExplorer, output);
|
||||
return;
|
||||
|
||||
TagBuilder tagBuilder;
|
||||
switch (inputType)
|
||||
{
|
||||
case "checkbox":
|
||||
GenerateCheckBox(modelExplorer, output);
|
||||
return;
|
||||
case "hidden":
|
||||
tagBuilder = Generator.GenerateHidden(
|
||||
ViewContext,
|
||||
modelExplorer,
|
||||
For.Name,
|
||||
value: For.Model,
|
||||
useViewData: false,
|
||||
htmlAttributes: null);
|
||||
break;
|
||||
|
||||
case "hidden":
|
||||
tagBuilder = Generator.GenerateHidden(
|
||||
ViewContext,
|
||||
modelExplorer,
|
||||
For.Name,
|
||||
value: For.Model,
|
||||
useViewData: false,
|
||||
htmlAttributes: null);
|
||||
break;
|
||||
case "password":
|
||||
tagBuilder = Generator.GeneratePassword(
|
||||
ViewContext,
|
||||
modelExplorer,
|
||||
For.Name,
|
||||
value: null,
|
||||
htmlAttributes: null);
|
||||
break;
|
||||
|
||||
case "password":
|
||||
tagBuilder = Generator.GeneratePassword(
|
||||
ViewContext,
|
||||
modelExplorer,
|
||||
For.Name,
|
||||
value: null,
|
||||
htmlAttributes: null);
|
||||
break;
|
||||
case "radio":
|
||||
tagBuilder = GenerateRadio(modelExplorer);
|
||||
break;
|
||||
|
||||
case "radio":
|
||||
tagBuilder = GenerateRadio(modelExplorer);
|
||||
break;
|
||||
default:
|
||||
tagBuilder = GenerateTextBox(modelExplorer, inputTypeHint, inputType);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
tagBuilder = GenerateTextBox(modelExplorer, inputTypeHint, inputType);
|
||||
break;
|
||||
}
|
||||
|
||||
if (tagBuilder != null)
|
||||
{
|
||||
// This TagBuilder contains the one <input/> element of interest. Since this is not the "checkbox"
|
||||
// special-case, output is a self-closing element no longer guarunteed.
|
||||
output.MergeAttributes(tagBuilder);
|
||||
output.Content.Append(tagBuilder.InnerHtml);
|
||||
}
|
||||
if (tagBuilder != null)
|
||||
{
|
||||
// This TagBuilder contains the one <input/> element of interest. Since this is not the "checkbox"
|
||||
// special-case, output is a self-closing element no longer guarunteed.
|
||||
output.MergeAttributes(tagBuilder);
|
||||
output.Content.Append(tagBuilder.InnerHtml);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
/// <summary>
|
||||
/// <see cref="ITagHelper"/> implementation targeting <label> elements with an <c>asp-for</c> attribute.
|
||||
/// </summary>
|
||||
[TargetElement("label", Attributes = ForAttributeName)]
|
||||
public class LabelTagHelper : TagHelper
|
||||
{
|
||||
private const string ForAttributeName = "asp-for";
|
||||
|
|
@ -32,34 +33,32 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
/// <remarks>Does nothing if <see cref="For"/> is <c>null</c>.</remarks>
|
||||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
if (For != null)
|
||||
var tagBuilder = Generator.GenerateLabel(
|
||||
ViewContext,
|
||||
For.ModelExplorer,
|
||||
For.Name,
|
||||
labelText: null,
|
||||
htmlAttributes: null);
|
||||
|
||||
if (tagBuilder != null)
|
||||
{
|
||||
var tagBuilder = Generator.GenerateLabel(ViewContext,
|
||||
For.ModelExplorer,
|
||||
For.Name,
|
||||
labelText: null,
|
||||
htmlAttributes: null);
|
||||
output.MergeAttributes(tagBuilder);
|
||||
|
||||
if (tagBuilder != null)
|
||||
// We check for whitespace to detect scenarios such as:
|
||||
// <label for="Name">
|
||||
// </label>
|
||||
if (!output.IsContentModified)
|
||||
{
|
||||
output.MergeAttributes(tagBuilder);
|
||||
var childContent = await context.GetChildContentAsync();
|
||||
|
||||
// We check for whitespace to detect scenarios such as:
|
||||
// <label for="Name">
|
||||
// </label>
|
||||
if (!output.IsContentModified)
|
||||
if (childContent.IsWhiteSpace)
|
||||
{
|
||||
var childContent = await context.GetChildContentAsync();
|
||||
|
||||
if (childContent.IsWhiteSpace)
|
||||
{
|
||||
// Provide default label text since there was nothing useful in the Razor source.
|
||||
output.Content.SetContent(tagBuilder.InnerHtml);
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Content.SetContent(childContent);
|
||||
}
|
||||
// Provide default label text since there was nothing useful in the Razor source.
|
||||
output.Content.SetContent(tagBuilder.InnerHtml);
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Content.SetContent(childContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,22 +74,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
return string.Format(CultureInfo.CurrentCulture, GetString("InputTagHelper_InvalidExpressionResult"), p0, p1, p2, p3, p4, p5);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unable to format without a '{1}' expression for {0}. '{2}' must be null if '{1}' is null.
|
||||
/// </summary>
|
||||
internal static string InputTagHelper_UnableToFormat
|
||||
{
|
||||
get { return GetString("InputTagHelper_UnableToFormat"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unable to format without a '{1}' expression for {0}. '{2}' must be null if '{1}' is null.
|
||||
/// </summary>
|
||||
internal static string FormatInputTagHelper_UnableToFormat(object p0, object p1, object p2)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("InputTagHelper_UnableToFormat"), p0, p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// '{1}' must not be null for {0} if '{2}' is '{3}'.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -129,9 +129,6 @@
|
|||
<data name="InputTagHelper_InvalidExpressionResult" xml:space="preserve">
|
||||
<value>Unexpected '{1}' expression result type '{2}' for {0}. '{1}' must be of type '{3}' if '{4}' is '{5}'.</value>
|
||||
</data>
|
||||
<data name="InputTagHelper_UnableToFormat" xml:space="preserve">
|
||||
<value>Unable to format without a '{1}' expression for {0}. '{2}' must be null if '{1}' is null.</value>
|
||||
</data>
|
||||
<data name="InputTagHelper_ValueRequired" xml:space="preserve">
|
||||
<value>'{1}' must not be null for {0} if '{2}' is '{3}'.</value>
|
||||
</data>
|
||||
|
|
|
|||
|
|
@ -8,10 +8,10 @@ using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
|||
namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ITagHelper"/> implementation targeting <span> elements with an <c>asp-validation-for</c>
|
||||
/// <see cref="ITagHelper"/> implementation targeting any HTML element with an <c>asp-validation-for</c>
|
||||
/// attribute.
|
||||
/// </summary>
|
||||
[TargetElement("span")]
|
||||
[TargetElement("span", Attributes = ValidationForAttributeName)]
|
||||
public class ValidationMessageTagHelper : TagHelper
|
||||
{
|
||||
private const string ValidationForAttributeName = "asp-validation-for";
|
||||
|
|
|
|||
|
|
@ -8,10 +8,10 @@ using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
|||
namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ITagHelper"/> implementation targeting <div> elements with an <c>asp-validation-summary</c>
|
||||
/// <see cref="ITagHelper"/> implementation targeting any HTML element with an <c>asp-validation-summary</c>
|
||||
/// attribute.
|
||||
/// </summary>
|
||||
[TargetElement("div")]
|
||||
[TargetElement("div", Attributes = ValidationSummaryAttributeName)]
|
||||
public class ValidationSummaryTagHelper : TagHelper
|
||||
{
|
||||
private const string ValidationSummaryAttributeName = "asp-validation-summary";
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@
|
|||
|
||||
<div class="form-group">
|
||||
<div class="col-md-offset-2 col-md-10">
|
||||
<input class="btn btn-default" type="submit" value="Create" />
|
||||
<input type="submit" value="Create" class="btn btn-default" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@
|
|||
|
||||
<div class="form-group">
|
||||
<div class="col-md-offset-2 col-md-10">
|
||||
<input class="btn btn-default" type="submit" value="Create" />
|
||||
<input type="submit" value="Create" class="btn btn-default" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -617,62 +617,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
Assert.Equal(expectedTagName, output.TagName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TagHelper_RestoresTypeAndValue_IfForNotBound()
|
||||
{
|
||||
// Arrange
|
||||
var expectedAttributes = new Dictionary<string, string>
|
||||
{
|
||||
{ "class", "form-control" },
|
||||
{ "type", "datetime" },
|
||||
{ "value", "2014-10-15T23:24:19.000-7:00" },
|
||||
};
|
||||
var expectedPreContent = "original pre-content";
|
||||
var expectedContent = "original content";
|
||||
var expectedPostContent = "original post-content";
|
||||
var expectedTagName = "input";
|
||||
|
||||
var tagHelperContext = new TagHelperContext(
|
||||
allAttributes: new Dictionary<string, object>(),
|
||||
items: new Dictionary<object, object>(),
|
||||
uniqueId: "test",
|
||||
getChildContentAsync: () =>
|
||||
{
|
||||
var tagHelperContent = new DefaultTagHelperContent();
|
||||
tagHelperContent.SetContent("Something");
|
||||
return Task.FromResult<TagHelperContent>(tagHelperContent);
|
||||
});
|
||||
|
||||
var output = new TagHelperOutput(expectedTagName, expectedAttributes)
|
||||
{
|
||||
SelfClosing = false,
|
||||
};
|
||||
output.PreContent.SetContent(expectedPreContent);
|
||||
output.Content.SetContent(expectedContent);
|
||||
output.PostContent.SetContent(expectedPostContent);
|
||||
|
||||
var htmlGenerator = new TestableHtmlGenerator(new EmptyModelMetadataProvider())
|
||||
{
|
||||
ValidationAttributes =
|
||||
{
|
||||
{ "valid", "from validation attributes" },
|
||||
}
|
||||
};
|
||||
var tagHelper = GetTagHelper(htmlGenerator, model: null, propertyName: nameof(Model.Text));
|
||||
tagHelper.For = null;
|
||||
|
||||
// Act
|
||||
await tagHelper.ProcessAsync(tagHelperContext, output);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedAttributes, output.Attributes);
|
||||
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
|
||||
Assert.Equal(expectedContent, output.Content.GetContent());
|
||||
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
|
||||
Assert.False(output.SelfClosing);
|
||||
Assert.Equal(expectedTagName, output.TagName);
|
||||
}
|
||||
|
||||
private static InputTagHelper GetTagHelper(
|
||||
IHtmlGenerator htmlGenerator,
|
||||
object model,
|
||||
|
|
@ -689,38 +633,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
metadataProvider: metadataProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProcessAsync_Throws_IfForNotBoundButFormatIs()
|
||||
{
|
||||
// Arrange
|
||||
var contextAttributes = new Dictionary<string, object>();
|
||||
var originalAttributes = new Dictionary<string, string>();
|
||||
var expectedTagName = "select";
|
||||
var expectedMessage = "Unable to format without a 'asp-for' expression for <input>. 'asp-format' must " +
|
||||
"be null if 'asp-for' is null.";
|
||||
|
||||
var tagHelperContext = new TagHelperContext(
|
||||
allAttributes: new Dictionary<string, object>(),
|
||||
items: new Dictionary<object, object>(),
|
||||
uniqueId: "test",
|
||||
getChildContentAsync: () =>
|
||||
{
|
||||
var tagHelperContent = new DefaultTagHelperContent();
|
||||
tagHelperContent.SetContent("Something");
|
||||
return Task.FromResult<TagHelperContent>(tagHelperContent);
|
||||
});
|
||||
var output = new TagHelperOutput(expectedTagName, originalAttributes);
|
||||
var tagHelper = new InputTagHelper
|
||||
{
|
||||
Format = "{0}",
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => tagHelper.ProcessAsync(tagHelperContext, output));
|
||||
Assert.Equal(expectedMessage, exception.Message);
|
||||
}
|
||||
|
||||
private static InputTagHelper GetTagHelper(
|
||||
IHtmlGenerator htmlGenerator,
|
||||
object container,
|
||||
|
|
|
|||
|
|
@ -228,59 +228,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
|||
Assert.Equal(expectedTagName, output.TagName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TagHelper_LeavesOutputUnchanged_IfForNotBound2()
|
||||
{
|
||||
// Arrange
|
||||
var expectedAttributes = new Dictionary<string, string>
|
||||
{
|
||||
{ "class", "form-control" },
|
||||
};
|
||||
var expectedPreContent = "original pre-content";
|
||||
var expectedContent = "original content";
|
||||
var expectedPostContent = "original post-content";
|
||||
var expectedTagName = "label";
|
||||
|
||||
var metadataProvider = new TestModelMetadataProvider();
|
||||
var modelExplorer = metadataProvider
|
||||
.GetModelExplorerForType(typeof(Model), model: null)
|
||||
.GetExplorerForProperty(nameof(Model.Text));
|
||||
var modelExpression = new ModelExpression(nameof(Model.Text), modelExplorer);
|
||||
|
||||
var tagHelper = new LabelTagHelper();
|
||||
|
||||
var tagHelperContext = new TagHelperContext(
|
||||
allAttributes: new Dictionary<string, object>(),
|
||||
items: new Dictionary<object, object>(),
|
||||
uniqueId: "test",
|
||||
getChildContentAsync: () =>
|
||||
{
|
||||
var tagHelperContent = new DefaultTagHelperContent();
|
||||
tagHelperContent.SetContent("Something");
|
||||
return Task.FromResult<TagHelperContent>(tagHelperContent);
|
||||
});
|
||||
var output = new TagHelperOutput(expectedTagName, expectedAttributes);
|
||||
output.PreContent.SetContent(expectedPreContent);
|
||||
output.Content.SetContent(expectedContent);
|
||||
output.PostContent.SetContent(expectedPostContent);
|
||||
|
||||
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
|
||||
Model model = null;
|
||||
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
|
||||
tagHelper.ViewContext = viewContext;
|
||||
tagHelper.Generator = htmlGenerator;
|
||||
|
||||
// Act
|
||||
await tagHelper.ProcessAsync(tagHelperContext, output);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedAttributes, output.Attributes);
|
||||
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
|
||||
Assert.Equal(expectedContent, output.Content.GetContent());
|
||||
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
|
||||
Assert.Equal(expectedTagName, output.TagName);
|
||||
}
|
||||
|
||||
public class TagHelperOutputContent
|
||||
{
|
||||
public TagHelperOutputContent(string originalChildContent,
|
||||
|
|
|
|||
Loading…
Reference in New Issue