[Fixes #2491] RemoveRange handles attributes parameter safely

This commit is contained in:
Ajay Bhargav Baaskaran 2015-05-05 10:42:27 -07:00
parent c0ca6214c2
commit 0a7b2ac821
2 changed files with 27 additions and 2 deletions

View File

@ -117,7 +117,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
[NotNull] this TagHelperOutput tagHelperOutput, [NotNull] this TagHelperOutput tagHelperOutput,
[NotNull] IEnumerable<TagHelperAttribute> attributes) [NotNull] IEnumerable<TagHelperAttribute> attributes)
{ {
foreach (var attribute in attributes) foreach (var attribute in attributes.ToArray())
{ {
tagHelperOutput.Attributes.Remove(attribute); tagHelperOutput.Attributes.Remove(attribute);
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.AspNet.Razor.Runtime.TagHelpers;
@ -209,7 +210,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
} }
[Fact] [Fact]
public void RemoveRange_RemovesProvidedAttributes() public void RemoveRange_RemovesProvidedAttributes_WithArrayInput()
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
@ -231,6 +232,30 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
Assert.Equal(expectedAttribute, attribute); Assert.Equal(expectedAttribute, attribute);
} }
[Fact]
public void RemoveRange_RemovesProvidedAttributes_WithCollectionInput()
{
// Arrange
var tagHelperOutput = new TagHelperOutput(
"p",
attributes: new TagHelperAttributeList()
{
{ "route-Hello", "World" },
{ "Route-I", "Am" }
});
var expectedAttribute = new TagHelperAttribute("type", "btn");
tagHelperOutput.Attributes.Add(expectedAttribute);
var attributes = tagHelperOutput.Attributes
.Where(item => item.Name.StartsWith("route-", StringComparison.OrdinalIgnoreCase));
// Act
tagHelperOutput.RemoveRange(attributes);
// Assert
var attribute = Assert.Single(tagHelperOutput.Attributes);
Assert.Equal(expectedAttribute, attribute);
}
[Fact] [Fact]
public void FindPrefixedAttributes_ReturnsEmpty_AttributeListIfNoAttributesPrefixed() public void FindPrefixedAttributes_ReturnsEmpty_AttributeListIfNoAttributesPrefixed()
{ {