React to case insensitive TagHelperOutput.Attributes.

- Cleaned up some existing bad code that worked around case sensitive TagHelperOutput.Attributes.
- Modified MergeAttributes to be case insensitive when it comes to merging class attributes.
- Added a test to verify the new MergeAttribute case insensitive class merging.

aspnet/Razor#186
This commit is contained in:
N. Taylor Mullen 2014-10-20 14:34:39 -07:00 committed by NTaylorMullen
parent 09af7bb77b
commit 7934c432c8
3 changed files with 28 additions and 7 deletions

View File

@ -154,9 +154,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
if (!string.IsNullOrEmpty(inputType))
{
// inputType may be more specific than default the generator chooses below.
// TODO: Use Attributes.ContainsKey once aspnet/Razor#186 is fixed.
if (!output.Attributes.Any(
item => string.Equals("type", item.Key, StringComparison.OrdinalIgnoreCase)))
if (!output.Attributes.ContainsKey("type"))
{
output.Attributes["type"] = inputType;
}

View File

@ -89,13 +89,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{
foreach (var attribute in tagBuilder.Attributes)
{
// TODO: Use Attributes.ContainsKey once aspnet/Razor#186 is fixed.
if (!tagHelperOutput.Attributes.Any(
item => string.Equals(attribute.Key, item.Key, StringComparison.OrdinalIgnoreCase)))
if (!tagHelperOutput.Attributes.ContainsKey(attribute.Key))
{
tagHelperOutput.Attributes.Add(attribute.Key, attribute.Value);
}
else if (attribute.Key.Equals("class", StringComparison.Ordinal))
else if (attribute.Key.Equals("class", StringComparison.OrdinalIgnoreCase))
{
tagHelperOutput.Attributes["class"] += " " + attribute.Value;
}

View File

@ -157,6 +157,31 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
Assert.Equal(expectedAttribute, attribute);
}
[Theory]
[InlineData("class", "CLAss")]
[InlineData("ClaSS", "class")]
[InlineData("ClaSS", "cLaSs")]
public void MergeAttributes_AppendsClass_TagHelperOutputAttributeValues_IgnoresCase(
string originalName, string updateName)
{
// Arrange
var tagHelperOutput = new TagHelperOutput(
"p",
attributes: new Dictionary<string, string>(),
content: string.Empty);
tagHelperOutput.Attributes.Add(originalName, "Hello");
var tagBuilder = new TagBuilder("p");
tagBuilder.Attributes.Add(updateName, "btn");
// Act
tagHelperOutput.MergeAttributes(tagBuilder);
// Assert
var attribute = Assert.Single(tagHelperOutput.Attributes);
Assert.Equal(new KeyValuePair<string, string>(originalName, "Hello btn"), attribute);
}
[Fact]
public void MergeAttributes_DoesNotEncode_TagHelperOutputAttributeValues()
{