Skip to content

Commit b866b76

Browse files
authored
Fix VS crash with malformed tag helpers (#26779)
1 parent ab973d2 commit b866b76

6 files changed

+68
-10
lines changed

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperParseTreeRewriter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -163,7 +163,7 @@ public override SyntaxNode VisitMarkupElement(MarkupElementSyntax node)
163163
if (endTag != null)
164164
{
165165
var tagName = endTag.GetTagNameWithOptionalBang();
166-
if (TryRewriteTagHelperEnd(endTag, out tagHelperEnd))
166+
if (TryRewriteTagHelperEnd(startTag, endTag, out tagHelperEnd))
167167
{
168168
// This is a tag helper
169169
if (startTag == null)
@@ -289,10 +289,10 @@ private bool TryRewriteTagHelperStart(
289289
return true;
290290
}
291291

292-
private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHelperEndTagSyntax rewritten)
292+
private bool TryRewriteTagHelperEnd(MarkupStartTagSyntax startTag, MarkupEndTagSyntax endTag, out MarkupTagHelperEndTagSyntax rewritten)
293293
{
294294
rewritten = null;
295-
var tagName = tagBlock.GetTagNameWithOptionalBang();
295+
var tagName = endTag.GetTagNameWithOptionalBang();
296296
// Could not determine tag name, it can't be a TagHelper, continue on and track the element.
297297
if (string.IsNullOrEmpty(tagName) || tagName.StartsWith("!"))
298298
{
@@ -301,13 +301,13 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe
301301

302302
var tracker = CurrentTagHelperTracker;
303303
var tagNameScope = tracker?.TagName ?? string.Empty;
304-
if (!IsPotentialTagHelperEnd(tagName, tagBlock))
304+
if (!IsPotentialTagHelperEnd(tagName, endTag))
305305
{
306306
return false;
307307
}
308308

309309
// Validate that our end tag matches the currently scoped tag, if not we may need to error.
310-
if (tagNameScope.Equals(tagName, StringComparison.OrdinalIgnoreCase))
310+
if (startTag != null && tagNameScope.Equals(tagName, StringComparison.OrdinalIgnoreCase))
311311
{
312312
// If there are additional end tags required before we can build our block it means we're in a
313313
// situation like this: <myth req="..."><myth></myth></myth> where we're at the inside </myth>.
@@ -318,7 +318,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe
318318
return false;
319319
}
320320

321-
ValidateEndTagSyntax(tagName, tagBlock);
321+
ValidateEndTagSyntax(tagName, endTag);
322322

323323
_trackerStack.Pop();
324324
}
@@ -347,7 +347,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe
347347
// End tag TagHelper that states it shouldn't have an end tag.
348348
_errorSink.OnError(
349349
RazorDiagnosticFactory.CreateParsing_TagHelperMustNotHaveAnEndTag(
350-
new SourceSpan(SourceLocationTracker.Advance(tagBlock.GetSourceLocation(_source), "</"), tagName.Length),
350+
new SourceSpan(SourceLocationTracker.Advance(endTag.GetSourceLocation(_source), "</"), tagName.Length),
351351
tagName,
352352
descriptor.DisplayName,
353353
invalidRule.TagStructure));
@@ -358,7 +358,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe
358358
}
359359

360360
rewritten = SyntaxFactory.MarkupTagHelperEndTag(
361-
tagBlock.OpenAngle, tagBlock.ForwardSlash, tagBlock.Bang, tagBlock.Name, tagBlock.MiscAttributeContent, tagBlock.CloseAngle);
361+
endTag.OpenAngle, endTag.ForwardSlash, endTag.Bang, endTag.Name, endTag.MiscAttributeContent, endTag.CloseAngle);
362362

363363
return true;
364364
}

src/Razor/Microsoft.AspNetCore.Razor.Language/test/Legacy/TagHelperParseTreeRewriterTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,13 @@ public void RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly1
12711271
EvaluateData(MalformedRequiredAttribute_Descriptors, "<p notRequired=\"hi\" class=\"btn\" </p");
12721272
}
12731273

1274+
[Fact]
1275+
public void RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11()
1276+
{
1277+
var document = "<p class='foo'>@if(true){</p>}</p>";
1278+
EvaluateData(MalformedRequiredAttribute_Descriptors, document);
1279+
}
1280+
12741281
public static TagHelperDescriptor[] PrefixedTagHelperColon_Descriptors = new TagHelperDescriptor[]
12751282
{
12761283
TagHelperDescriptorBuilder.Create("mythTagHelper", "SomeAssembly")
@@ -1933,4 +1940,4 @@ public void HandlesCaseSensitiveTagHelpersCorrectly5()
19331940
EvaluateData(CaseSensitive_Descriptors, "<p Class='foo'></p>");
19341941
}
19351942
}
1936-
}
1943+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Markup span at (10:0,10 [3] ) (Accepts:Any) - Parent: Tag block at (0:0,0 [34] )
2+
Transition span at (15:0,15 [1] ) (Accepts:None) - Parent: Statement block at (15:0,15 [15] )
3+
Code span at (16:0,16 [9] ) (Accepts:Any) - Parent: Statement block at (15:0,15 [15] )
4+
Markup span at (25:0,25 [4] ) (Accepts:None) - Parent: Tag block at (25:0,25 [4] )
5+
Code span at (29:0,29 [1] ) (Accepts:Any) - Parent: Statement block at (15:0,15 [15] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(1,28): Error RZ1026: Encountered end tag "p" with no matching start tag. Are your start/end tags properly balanced?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
RazorDocument - [0..34)::34 - [<p class='foo'>@if(true){</p>}</p>]
2+
MarkupBlock - [0..34)::34
3+
MarkupTagHelperElement - [0..34)::34 - p[StartTagAndEndTag] - pTagHelper
4+
MarkupTagHelperStartTag - [0..15)::15 - [<p class='foo'>] - Gen<Markup> - SpanEditHandler;Accepts:Any
5+
OpenAngle;[<];
6+
Text;[p];
7+
MarkupTagHelperAttribute - [2..14)::12 - class - SingleQuotes - Unbound - [ class='foo']
8+
MarkupTextLiteral - [2..3)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
9+
Whitespace;[ ];
10+
MarkupTextLiteral - [3..8)::5 - [class] - Gen<Markup> - SpanEditHandler;Accepts:Any
11+
Text;[class];
12+
Equals;[=];
13+
MarkupTextLiteral - [9..10)::1 - ['] - Gen<Markup> - SpanEditHandler;Accepts:Any
14+
SingleQuote;['];
15+
MarkupTagHelperAttributeValue - [10..13)::3
16+
MarkupLiteralAttributeValue - [10..13)::3 - [foo]
17+
MarkupTextLiteral - [10..13)::3 - [foo] - Gen<Markup> - SpanEditHandler;Accepts:Any
18+
Text;[foo];
19+
MarkupTextLiteral - [13..14)::1 - ['] - Gen<Markup> - SpanEditHandler;Accepts:Any
20+
SingleQuote;['];
21+
CloseAngle;[>];
22+
CSharpCodeBlock - [15..30)::15
23+
CSharpTransition - [15..16)::1 - Gen<None> - SpanEditHandler;Accepts:None
24+
Transition;[@];
25+
CSharpStatementLiteral - [16..25)::9 - [if(true){] - Gen<Stmt> - SpanEditHandler;Accepts:Any
26+
Keyword;[if];
27+
LeftParenthesis;[(];
28+
Keyword;[true];
29+
RightParenthesis;[)];
30+
LeftBrace;[{];
31+
MarkupBlock - [25..29)::4
32+
MarkupElement - [25..29)::4
33+
MarkupEndTag - [25..29)::4 - [</p>] - Gen<Markup> - SpanEditHandler;Accepts:None
34+
OpenAngle;[<];
35+
ForwardSlash;[/];
36+
Text;[p];
37+
CloseAngle;[>];
38+
CSharpStatementLiteral - [29..30)::1 - [}] - Gen<Stmt> - SpanEditHandler;Accepts:Any
39+
RightBrace;[}];
40+
MarkupTagHelperEndTag - [30..34)::4 - [</p>]
41+
OpenAngle;[<];
42+
ForwardSlash;[/];
43+
Text;[p];
44+
CloseAngle;[>];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TagHelper span at (0:0,0 [34] ) - pTagHelper

0 commit comments

Comments
 (0)