From 7934c432c8b4a6c229b1a461bfbc06c816a242fe Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Mon, 20 Oct 2014 14:34:39 -0700 Subject: [PATCH] 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 --- .../InputTagHelper.cs | 4 +-- .../TagHelperOutputExtensions.cs | 6 ++--- .../TagHelperOutputExtensionsTest.cs | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs index d90a25d5c2..0255cf9d1f 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs @@ -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; } diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/TagHelperOutputExtensions.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/TagHelperOutputExtensions.cs index 1dd3e897ed..4c6e2baa85 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/TagHelperOutputExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/TagHelperOutputExtensions.cs @@ -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; } diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs index 19378c4585..8a394afaa8 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs @@ -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(), + 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(originalName, "Hello btn"), attribute); + } + [Fact] public void MergeAttributes_DoesNotEncode_TagHelperOutputAttributeValues() {