Enable `TagHelper`s with `[RestrictChildren]` to log errors for no tag name tags when targeted by catch all.

#534
This commit is contained in:
N. Taylor Mullen 2015-09-18 15:33:19 -07:00
parent 635514453b
commit c494cb344d
2 changed files with 87 additions and 3 deletions

View File

@ -325,7 +325,7 @@ namespace Microsoft.AspNet.Razor.Parser.TagHelpers.Internal
private void ValidateParentTagHelperAllowsTagHelper(string tagName, Block tagBlock, ErrorSink errorSink)
{
var currentlyAllowedChildren = _currentTagHelperTracker?.AllowedChildren;
var currentlyAllowedChildren = _currentTagHelperTracker?.PrefixedAllowedChildren;
if (currentlyAllowedChildren != null &&
!currentlyAllowedChildren.Contains(tagName, StringComparer.OrdinalIgnoreCase))
@ -583,9 +583,11 @@ namespace Microsoft.AspNet.Razor.Parser.TagHelpers.Internal
var tagHelperPrefix = Builder.Descriptors.First().Prefix;
AllowedChildren = Builder.Descriptors
.Where(descriptor => descriptor.AllowedChildren != null)
.SelectMany(descriptor => descriptor.AllowedChildren)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Select(allowedChild => tagHelperPrefix + allowedChild);
.Distinct(StringComparer.OrdinalIgnoreCase);
PrefixedAllowedChildren = AllowedChildren.Select(allowedChild => tagHelperPrefix + allowedChild);
}
}
@ -594,6 +596,8 @@ namespace Microsoft.AspNet.Razor.Parser.TagHelpers.Internal
public uint OpenMatchingTags { get; set; }
public IEnumerable<string> AllowedChildren { get; }
public IEnumerable<string> PrefixedAllowedChildren { get; }
}
}
}

View File

@ -747,6 +747,86 @@ namespace Microsoft.AspNet.Razor.Test.TagHelpers
EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors);
}
[Fact]
public void Rewrite_UnderstandsNullTagNameWithAllowedChildrenForCatchAll()
{
// Arrange
var documentContent = "<p></</p>";
var descriptors = new TagHelperDescriptor[]
{
new TagHelperDescriptor
{
TagName = "p",
TypeName = "PTagHelper",
AssemblyName = "SomeAssembly",
AllowedChildren = new[] { "custom" },
},
new TagHelperDescriptor
{
TagName = "*",
TypeName = "CatchAllTagHelper",
AssemblyName = "SomeAssembly",
}
};
var expectedOutput = new MarkupBlock(
new MarkupTagHelperBlock("p",
BlockFactory.MarkupTagBlock("</")));
var descriptorProvider = new TagHelperDescriptorProvider(descriptors);
var expectedErrors = new[]
{
new RazorError(
RazorResources.FormatTagHelperParseTreeRewriter_CannotHaveNonTagContent("p", "custom"),
absoluteIndex: 3,
lineIndex: 0,
columnIndex: 3,
length: 2)
};
// Act & Assert
EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors);
}
[Fact]
public void Rewrite_UnderstandsNullTagNameWithAllowedChildrenForCatchAllWithPrefix()
{
// Arrange
var documentContent = "<th:p></</th:p>";
var descriptors = new TagHelperDescriptor[]
{
new TagHelperDescriptor
{
TagName = "p",
TypeName = "PTagHelper",
AssemblyName = "SomeAssembly",
AllowedChildren = new[] { "custom" },
Prefix = "th:",
},
new TagHelperDescriptor
{
TagName = "*",
TypeName = "CatchAllTagHelper",
AssemblyName = "SomeAssembly",
Prefix = "th:",
}
};
var expectedOutput = new MarkupBlock(
new MarkupTagHelperBlock("th:p",
BlockFactory.MarkupTagBlock("</")));
var descriptorProvider = new TagHelperDescriptorProvider(descriptors);
var expectedErrors = new[]
{
new RazorError(
RazorResources.FormatTagHelperParseTreeRewriter_CannotHaveNonTagContent("th:p", "custom"),
absoluteIndex: 6,
lineIndex: 0,
columnIndex: 6,
length: 2)
};
// Act & Assert
EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors);
}
[Fact]
public void Rewrite_CanHandleStartTagOnlyTagTagMode()
{