Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public TagHelperBinding GetBinding(

private void Register(TagHelperDescriptor descriptor)
{
bool descriptorAdded = false;

var count = descriptor.TagMatchingRules.Count;
for (var i = 0; i < count; i++)
{
Expand All @@ -150,12 +148,7 @@ private void Register(TagHelperDescriptor descriptor)
_registrations[registrationKey] = descriptorSet;
}

// We don't need to keep trying to add the same Descriptor to the HashSet for each matching rule.
if (!descriptorAdded)
{
_ = descriptorSet.Add(descriptor);
descriptorAdded = true;
}
_ = descriptorSet.Add(descriptor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,107 @@ public void GetBinding_ReturnsBindingWithInformation()
Assert.Equal(divTagHelper.TagMatchingRules, bindingResult.Mappings[divTagHelper], TagMatchingRuleDescriptorComparer.Default);
}

[Fact]
public void GetBinding_With_Multiple_TagNameRules_SingleHelper()
{
// Arrange
var multiTagHelper = TagHelperDescriptorBuilder.Create("MultiTagHelper", "SomeAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("a"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("img"))
.Build();
var expectedDescriptors = new[] { multiTagHelper };
var tagHelperBinder = new TagHelperBinder("", expectedDescriptors);

TestTagName("div", multiTagHelper.TagMatchingRules[0]);
TestTagName("a", multiTagHelper.TagMatchingRules[1]);
TestTagName("img", multiTagHelper.TagMatchingRules[2]);
TestTagName("p", null);
TestTagName("*", null);
void TestTagName(string tagName, TagMatchingRuleDescriptor expectedBindingResult)
{
// Act
var bindingResult = tagHelperBinder.GetBinding(

tagName: tagName,
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "body",
parentIsTagHelper: false);

// Assert
if (expectedBindingResult == null)
{
Assert.Null(bindingResult);
return;
}
else
{
Assert.NotNull(bindingResult);
Assert.Equal(expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.Default);

Assert.Equal(tagName, bindingResult.TagName);
var mapping = Assert.Single(bindingResult.Mappings[multiTagHelper]);
Assert.Equal(expectedBindingResult, mapping, TagMatchingRuleDescriptorComparer.Default);
}
}
}

[Fact]
public void GetBinding_With_Multiple_TagNameRules_MultipleHelpers()
{
// Arrange
var multiTagHelper1 = TagHelperDescriptorBuilder.Create("MultiTagHelper1", "SomeAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("a"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("img"))
.Build();

var multiTagHelper2 = TagHelperDescriptorBuilder.Create("MultiTagHelper2", "SomeAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("p"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("table"))
.Build();

var tagHelperBinder = new TagHelperBinder("", new[] { multiTagHelper1, multiTagHelper2 });

TestTagName("div", new[] { multiTagHelper1, multiTagHelper2 }, new[] { multiTagHelper1.TagMatchingRules[0], multiTagHelper2.TagMatchingRules[0] });
TestTagName("a", new[] { multiTagHelper1 }, new[] { multiTagHelper1.TagMatchingRules[1] });
TestTagName("img", new[] { multiTagHelper1 }, new[] { multiTagHelper1.TagMatchingRules[2] });
TestTagName("p", new[] { multiTagHelper2 }, new[] { multiTagHelper2.TagMatchingRules[1] });
TestTagName("table", new[] { multiTagHelper2 }, new[] { multiTagHelper2.TagMatchingRules[2] });
TestTagName("*", null, null);


void TestTagName(string tagName, TagHelperDescriptor[] expectedDescriptors, TagMatchingRuleDescriptor[] expectedBindingResults)
{
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName: tagName,
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "body",
parentIsTagHelper: false);

// Assert
if (expectedDescriptors is null)
{
Assert.Null(bindingResult);
}
else
{
Assert.NotNull(bindingResult);
Assert.Equal(expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.Default);

Assert.Equal(tagName, bindingResult.TagName);

for(int i = 0; i < expectedDescriptors.Length; i++)
{
var mapping = Assert.Single(bindingResult.Mappings[expectedDescriptors[i]]);
Assert.Equal(expectedBindingResults[i], mapping, TagMatchingRuleDescriptorComparer.Default);
}
}
}
}

public static TheoryData RequiredParentData
{
get
Expand Down