Enable `TagHelper`s with `[RestrictChildren]` to log errors for no tag name tags.
- Added test cases for situations when tags have no names. #534
This commit is contained in:
parent
36450c978b
commit
635514453b
|
|
@ -308,7 +308,18 @@ namespace Microsoft.AspNet.Razor.Parser.TagHelpers.Internal
|
||||||
{
|
{
|
||||||
if (_currentTagHelperTracker?.AllowedChildren != null)
|
if (_currentTagHelperTracker?.AllowedChildren != null)
|
||||||
{
|
{
|
||||||
OnAllowedChildrenTagError(_currentTagHelperTracker, tagBlock, errorSink);
|
var tagName = GetTagName(tagBlock);
|
||||||
|
|
||||||
|
// Treat partial tags such as '</' which have no tag names as content based errors.
|
||||||
|
if (string.IsNullOrEmpty(tagName))
|
||||||
|
{
|
||||||
|
Debug.Assert(tagBlock.Children.First() is Span);
|
||||||
|
|
||||||
|
ValidateParentTagHelperAllowsContent(tagBlock.Children.First() as Span, errorSink);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnAllowedChildrenTagError(_currentTagHelperTracker, tagName, tagBlock, errorSink);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -319,16 +330,16 @@ namespace Microsoft.AspNet.Razor.Parser.TagHelpers.Internal
|
||||||
if (currentlyAllowedChildren != null &&
|
if (currentlyAllowedChildren != null &&
|
||||||
!currentlyAllowedChildren.Contains(tagName, StringComparer.OrdinalIgnoreCase))
|
!currentlyAllowedChildren.Contains(tagName, StringComparer.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
OnAllowedChildrenTagError(_currentTagHelperTracker, tagBlock, errorSink);
|
OnAllowedChildrenTagError(_currentTagHelperTracker, tagName, tagBlock, errorSink);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnAllowedChildrenTagError(
|
private static void OnAllowedChildrenTagError(
|
||||||
TagHelperBlockTracker tracker,
|
TagHelperBlockTracker tracker,
|
||||||
|
string tagName,
|
||||||
Block tagBlock,
|
Block tagBlock,
|
||||||
ErrorSink errorSink)
|
ErrorSink errorSink)
|
||||||
{
|
{
|
||||||
var tagName = GetTagName(tagBlock);
|
|
||||||
var allowedChildrenString = string.Join(", ", tracker.AllowedChildren);
|
var allowedChildrenString = string.Join(", ", tracker.AllowedChildren);
|
||||||
var errorMessage = RazorResources.FormatTagHelperParseTreeRewriter_InvalidNestedTag(
|
var errorMessage = RazorResources.FormatTagHelperParseTreeRewriter_InvalidNestedTag(
|
||||||
tagName,
|
tagName,
|
||||||
|
|
|
||||||
|
|
@ -19,11 +19,9 @@ namespace Microsoft.AspNet.Razor.Test.TagHelpers
|
||||||
public class TagHelperParseTreeRewriterTest : TagHelperRewritingTestBase
|
public class TagHelperParseTreeRewriterTest : TagHelperRewritingTestBase
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Rewrite_UnderstandsTagHelperPrefixAndRestrictedChildren()
|
public void Rewrite_UnderstandsTagHelperPrefixAndAllowedChildren()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var factory = CreateDefaultSpanFactory();
|
|
||||||
var blockFactory = new BlockFactory(factory);
|
|
||||||
var documentContent = "<th:p><th:strong></th:strong></th:p>";
|
var documentContent = "<th:p><th:strong></th:strong></th:p>";
|
||||||
var expectedOutput = new MarkupBlock(
|
var expectedOutput = new MarkupBlock(
|
||||||
new MarkupTagHelperBlock("th:p",
|
new MarkupTagHelperBlock("th:p",
|
||||||
|
|
@ -683,6 +681,28 @@ namespace Microsoft.AspNet.Razor.Test.TagHelpers
|
||||||
nestedTagError("br", "p", "custom", 51, 2),
|
nestedTagError("br", "p", "custom", 51, 2),
|
||||||
nestedContentError("p", "custom", 56, 9)
|
nestedContentError("p", "custom", 56, 9)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"<p></</p>",
|
||||||
|
new[] { "custom" },
|
||||||
|
new MarkupBlock(
|
||||||
|
new MarkupTagHelperBlock("p",
|
||||||
|
blockFactory.MarkupTagBlock("</"))),
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
nestedContentError("p", "custom", 3, 2),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"<p><</p>",
|
||||||
|
new[] { "custom" },
|
||||||
|
new MarkupBlock(
|
||||||
|
new MarkupTagHelperBlock("p",
|
||||||
|
blockFactory.MarkupTagBlock("<"))),
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
nestedContentError("p", "custom", 3, 1),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue