diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperParseTreeRewriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperParseTreeRewriter.cs index 25e191a36987..e04178acf9a1 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperParseTreeRewriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperParseTreeRewriter.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -163,7 +163,7 @@ public override SyntaxNode VisitMarkupElement(MarkupElementSyntax node) if (endTag != null) { var tagName = endTag.GetTagNameWithOptionalBang(); - if (TryRewriteTagHelperEnd(endTag, out tagHelperEnd)) + if (TryRewriteTagHelperEnd(startTag, endTag, out tagHelperEnd)) { // This is a tag helper if (startTag == null) @@ -289,10 +289,10 @@ private bool TryRewriteTagHelperStart( return true; } - private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHelperEndTagSyntax rewritten) + private bool TryRewriteTagHelperEnd(MarkupStartTagSyntax startTag, MarkupEndTagSyntax endTag, out MarkupTagHelperEndTagSyntax rewritten) { rewritten = null; - var tagName = tagBlock.GetTagNameWithOptionalBang(); + var tagName = endTag.GetTagNameWithOptionalBang(); // Could not determine tag name, it can't be a TagHelper, continue on and track the element. if (string.IsNullOrEmpty(tagName) || tagName.StartsWith("!")) { @@ -301,13 +301,13 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe var tracker = CurrentTagHelperTracker; var tagNameScope = tracker?.TagName ?? string.Empty; - if (!IsPotentialTagHelperEnd(tagName, tagBlock)) + if (!IsPotentialTagHelperEnd(tagName, endTag)) { return false; } // Validate that our end tag matches the currently scoped tag, if not we may need to error. - if (tagNameScope.Equals(tagName, StringComparison.OrdinalIgnoreCase)) + if (startTag != null && tagNameScope.Equals(tagName, StringComparison.OrdinalIgnoreCase)) { // If there are additional end tags required before we can build our block it means we're in a // situation like this: where we're at the inside . @@ -318,7 +318,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe return false; } - ValidateEndTagSyntax(tagName, tagBlock); + ValidateEndTagSyntax(tagName, endTag); _trackerStack.Pop(); } @@ -347,7 +347,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe // End tag TagHelper that states it shouldn't have an end tag. _errorSink.OnError( RazorDiagnosticFactory.CreateParsing_TagHelperMustNotHaveAnEndTag( - new SourceSpan(SourceLocationTracker.Advance(tagBlock.GetSourceLocation(_source), "@if(true){

}

"; + EvaluateData(MalformedRequiredAttribute_Descriptors, document); + } + public static TagHelperDescriptor[] PrefixedTagHelperColon_Descriptors = new TagHelperDescriptor[] { TagHelperDescriptorBuilder.Create("mythTagHelper", "SomeAssembly") @@ -1933,4 +1940,4 @@ public void HandlesCaseSensitiveTagHelpersCorrectly5() EvaluateData(CaseSensitive_Descriptors, "

"); } } -} \ No newline at end of file +} diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.cspans.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.cspans.txt new file mode 100644 index 000000000000..29890f3bd26a --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.cspans.txt @@ -0,0 +1,5 @@ +Markup span at (10:0,10 [3] ) (Accepts:Any) - Parent: Tag block at (0:0,0 [34] ) +Transition span at (15:0,15 [1] ) (Accepts:None) - Parent: Statement block at (15:0,15 [15] ) +Code span at (16:0,16 [9] ) (Accepts:Any) - Parent: Statement block at (15:0,15 [15] ) +Markup span at (25:0,25 [4] ) (Accepts:None) - Parent: Tag block at (25:0,25 [4] ) +Code span at (29:0,29 [1] ) (Accepts:Any) - Parent: Statement block at (15:0,15 [15] ) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.diag.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.diag.txt new file mode 100644 index 000000000000..8049d366d19c --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.diag.txt @@ -0,0 +1 @@ +(1,28): Error RZ1026: Encountered end tag "p" with no matching start tag. Are your start/end tags properly balanced? diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.stree.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.stree.txt new file mode 100644 index 000000000000..c2d296ae7cd4 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.stree.txt @@ -0,0 +1,44 @@ +RazorDocument - [0..34)::34 - [

@if(true){

}

] + MarkupBlock - [0..34)::34 + MarkupTagHelperElement - [0..34)::34 - p[StartTagAndEndTag] - pTagHelper + MarkupTagHelperStartTag - [0..15)::15 - [

] - Gen - SpanEditHandler;Accepts:Any + OpenAngle;[<]; + Text;[p]; + MarkupTagHelperAttribute - [2..14)::12 - class - SingleQuotes - Unbound - [ class='foo'] + MarkupTextLiteral - [2..3)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + MarkupTextLiteral - [3..8)::5 - [class] - Gen - SpanEditHandler;Accepts:Any + Text;[class]; + Equals;[=]; + MarkupTextLiteral - [9..10)::1 - ['] - Gen - SpanEditHandler;Accepts:Any + SingleQuote;[']; + MarkupTagHelperAttributeValue - [10..13)::3 + MarkupLiteralAttributeValue - [10..13)::3 - [foo] + MarkupTextLiteral - [10..13)::3 - [foo] - Gen - SpanEditHandler;Accepts:Any + Text;[foo]; + MarkupTextLiteral - [13..14)::1 - ['] - Gen - SpanEditHandler;Accepts:Any + SingleQuote;[']; + CloseAngle;[>]; + CSharpCodeBlock - [15..30)::15 + CSharpTransition - [15..16)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + CSharpStatementLiteral - [16..25)::9 - [if(true){] - Gen - SpanEditHandler;Accepts:Any + Keyword;[if]; + LeftParenthesis;[(]; + Keyword;[true]; + RightParenthesis;[)]; + LeftBrace;[{]; + MarkupBlock - [25..29)::4 + MarkupElement - [25..29)::4 + MarkupEndTag - [25..29)::4 - [

] - Gen - SpanEditHandler;Accepts:None + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[p]; + CloseAngle;[>]; + CSharpStatementLiteral - [29..30)::1 - [}] - Gen - SpanEditHandler;Accepts:Any + RightBrace;[}]; + MarkupTagHelperEndTag - [30..34)::4 - [

] + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[p]; + CloseAngle;[>]; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.tspans.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.tspans.txt new file mode 100644 index 000000000000..cdee45b5c53c --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/TagHelperParseTreeRewriterTest/RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11.tspans.txt @@ -0,0 +1 @@ +TagHelper span at (0:0,0 [34] ) - pTagHelper