Skip to content

Commit 4a032ed

Browse files
authored
Don't try and de-dup tag helper descriptors (#8267)
* Don't try and dedup tag helper descriptors
1 parent 3978d52 commit 4a032ed

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/TagHelperBinder.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ public TagHelperBinding GetBinding(
132132

133133
private void Register(TagHelperDescriptor descriptor)
134134
{
135-
bool descriptorAdded = false;
136-
137135
var count = descriptor.TagMatchingRules.Count;
138136
for (var i = 0; i < count; i++)
139137
{
@@ -150,12 +148,7 @@ private void Register(TagHelperDescriptor descriptor)
150148
_registrations[registrationKey] = descriptorSet;
151149
}
152150

153-
// We don't need to keep trying to add the same Descriptor to the HashSet for each matching rule.
154-
if (!descriptorAdded)
155-
{
156-
_ = descriptorSet.Add(descriptor);
157-
descriptorAdded = true;
158-
}
151+
_ = descriptorSet.Add(descriptor);
159152
}
160153
}
161154
}

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TagHelperBinderTest.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,107 @@ public void GetBinding_ReturnsBindingWithInformation()
4242
Assert.Equal(divTagHelper.TagMatchingRules, bindingResult.Mappings[divTagHelper], TagMatchingRuleDescriptorComparer.Default);
4343
}
4444

45+
[Fact]
46+
public void GetBinding_With_Multiple_TagNameRules_SingleHelper()
47+
{
48+
// Arrange
49+
var multiTagHelper = TagHelperDescriptorBuilder.Create("MultiTagHelper", "SomeAssembly")
50+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
51+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("a"))
52+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("img"))
53+
.Build();
54+
var expectedDescriptors = new[] { multiTagHelper };
55+
var tagHelperBinder = new TagHelperBinder("", expectedDescriptors);
56+
57+
TestTagName("div", multiTagHelper.TagMatchingRules[0]);
58+
TestTagName("a", multiTagHelper.TagMatchingRules[1]);
59+
TestTagName("img", multiTagHelper.TagMatchingRules[2]);
60+
TestTagName("p", null);
61+
TestTagName("*", null);
62+
void TestTagName(string tagName, TagMatchingRuleDescriptor expectedBindingResult)
63+
{
64+
// Act
65+
var bindingResult = tagHelperBinder.GetBinding(
66+
67+
tagName: tagName,
68+
attributes: Array.Empty<KeyValuePair<string, string>>(),
69+
parentTagName: "body",
70+
parentIsTagHelper: false);
71+
72+
// Assert
73+
if (expectedBindingResult == null)
74+
{
75+
Assert.Null(bindingResult);
76+
return;
77+
}
78+
else
79+
{
80+
Assert.NotNull(bindingResult);
81+
Assert.Equal(expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.Default);
82+
83+
Assert.Equal(tagName, bindingResult.TagName);
84+
var mapping = Assert.Single(bindingResult.Mappings[multiTagHelper]);
85+
Assert.Equal(expectedBindingResult, mapping, TagMatchingRuleDescriptorComparer.Default);
86+
}
87+
}
88+
}
89+
90+
[Fact]
91+
public void GetBinding_With_Multiple_TagNameRules_MultipleHelpers()
92+
{
93+
// Arrange
94+
var multiTagHelper1 = TagHelperDescriptorBuilder.Create("MultiTagHelper1", "SomeAssembly")
95+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
96+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("a"))
97+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("img"))
98+
.Build();
99+
100+
var multiTagHelper2 = TagHelperDescriptorBuilder.Create("MultiTagHelper2", "SomeAssembly")
101+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
102+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("p"))
103+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("table"))
104+
.Build();
105+
106+
var tagHelperBinder = new TagHelperBinder("", new[] { multiTagHelper1, multiTagHelper2 });
107+
108+
TestTagName("div", new[] { multiTagHelper1, multiTagHelper2 }, new[] { multiTagHelper1.TagMatchingRules[0], multiTagHelper2.TagMatchingRules[0] });
109+
TestTagName("a", new[] { multiTagHelper1 }, new[] { multiTagHelper1.TagMatchingRules[1] });
110+
TestTagName("img", new[] { multiTagHelper1 }, new[] { multiTagHelper1.TagMatchingRules[2] });
111+
TestTagName("p", new[] { multiTagHelper2 }, new[] { multiTagHelper2.TagMatchingRules[1] });
112+
TestTagName("table", new[] { multiTagHelper2 }, new[] { multiTagHelper2.TagMatchingRules[2] });
113+
TestTagName("*", null, null);
114+
115+
116+
void TestTagName(string tagName, TagHelperDescriptor[] expectedDescriptors, TagMatchingRuleDescriptor[] expectedBindingResults)
117+
{
118+
// Act
119+
var bindingResult = tagHelperBinder.GetBinding(
120+
tagName: tagName,
121+
attributes: Array.Empty<KeyValuePair<string, string>>(),
122+
parentTagName: "body",
123+
parentIsTagHelper: false);
124+
125+
// Assert
126+
if (expectedDescriptors is null)
127+
{
128+
Assert.Null(bindingResult);
129+
}
130+
else
131+
{
132+
Assert.NotNull(bindingResult);
133+
Assert.Equal(expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.Default);
134+
135+
Assert.Equal(tagName, bindingResult.TagName);
136+
137+
for(int i = 0; i < expectedDescriptors.Length; i++)
138+
{
139+
var mapping = Assert.Single(bindingResult.Mappings[expectedDescriptors[i]]);
140+
Assert.Equal(expectedBindingResults[i], mapping, TagMatchingRuleDescriptorComparer.Default);
141+
}
142+
}
143+
}
144+
}
145+
45146
public static TheoryData RequiredParentData
46147
{
47148
get

0 commit comments

Comments
 (0)