[Fixes #2144] Incorrect target element of nested tag helper

This commit is contained in:
Kiran Challa 2018-03-19 15:52:21 -07:00
parent 5f3908cb54
commit 431a8e195a
2 changed files with 75 additions and 1 deletions

View File

@ -174,6 +174,11 @@ namespace Microsoft.VisualStudio.Editor.Razor
foreach (var rule in possibleDescriptor.TagMatchingRules)
{
if (!TagHelperMatchingConventions.SatisfiesParentTag(completionContext.ContainingTagName, rule))
{
continue;
}
if (rule.TagName == TagHelperMatchingConventions.ElementCatchAllName)
{
catchAllDescriptors.Add(possibleDescriptor);
@ -210,7 +215,7 @@ namespace Microsoft.VisualStudio.Editor.Razor
{
foreach (var completionTagName in elementCompletions.Keys)
{
if (elementCompletions[completionTagName].Count > 0 ||
if (elementCompletions[completionTagName].Count > 0 ||
!string.IsNullOrEmpty(prefix) && completionTagName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
// The current completion either has other TagHelper's associated with it or is prefixed with a non-empty

View File

@ -838,6 +838,75 @@ namespace Microsoft.VisualStudio.Editor.Razor
AssertCompletionsAreEquivalent(expectedCompletions, completions);
}
[Fact]
public void GetElementCompletions_NoContainingParentTag_DoesNotGetCompletionForRuleWithParentTag()
{
// Arrange
var documentDescriptors = new[]
{
TagHelperDescriptorBuilder.Create("Tag1", "TestAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("outer-child-tag"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("child-tag").RequireParentTag("parent-tag"))
.Build(),
TagHelperDescriptorBuilder.Create("Tag2", "TestAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("parent-tag"))
.AllowChildTag("child-tag")
.Build(),
};
var expectedCompletions = ElementCompletionResult.Create(new Dictionary<string, HashSet<TagHelperDescriptor>>()
{
["outer-child-tag"] = new HashSet<TagHelperDescriptor> { documentDescriptors[0] },
["parent-tag"] = new HashSet<TagHelperDescriptor> { documentDescriptors[1] },
});
var completionContext = BuildElementCompletionContext(
documentDescriptors,
existingCompletions: Enumerable.Empty<string>(),
containingTagName: null,
containingParentTagName: null);
var service = CreateTagHelperCompletionFactsService();
// Act
var completions = service.GetElementCompletions(completionContext);
// Assert
AssertCompletionsAreEquivalent(expectedCompletions, completions);
}
[Fact]
public void GetElementCompletions_WithContainingParentTag_GetsCompletionForRuleWithParentTag()
{
// Arrange
var documentDescriptors = new[]
{
TagHelperDescriptorBuilder.Create("Tag1", "TestAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("outer-child-tag"))
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("child-tag").RequireParentTag("parent-tag"))
.Build(),
TagHelperDescriptorBuilder.Create("Tag2", "TestAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("parent-tag"))
.AllowChildTag("child-tag")
.Build(),
};
var expectedCompletions = ElementCompletionResult.Create(new Dictionary<string, HashSet<TagHelperDescriptor>>()
{
["child-tag"] = new HashSet<TagHelperDescriptor> { documentDescriptors[0] },
});
var completionContext = BuildElementCompletionContext(
documentDescriptors,
existingCompletions: Enumerable.Empty<string>(),
containingTagName: "parent-tag",
containingParentTagName: null);
var service = CreateTagHelperCompletionFactsService();
// Act
var completions = service.GetElementCompletions(completionContext);
// Assert
AssertCompletionsAreEquivalent(expectedCompletions, completions);
}
[Fact]
public void GetElementCompletions_AllowedChildrenAreIgnoredWhenAtRoot()
{