Skip to content

Commit

Permalink
Fix VS crash with malformed tag helpers (#26779)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb authored Oct 12, 2020
1 parent ab973d2 commit b866b76
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("!"))
{
Expand All @@ -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: <myth req="..."><myth></myth></myth> where we're at the inside </myth>.
Expand All @@ -318,7 +318,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe
return false;
}

ValidateEndTagSyntax(tagName, tagBlock);
ValidateEndTagSyntax(tagName, endTag);

_trackerStack.Pop();
}
Expand Down Expand Up @@ -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), "</"), tagName.Length),
new SourceSpan(SourceLocationTracker.Advance(endTag.GetSourceLocation(_source), "</"), tagName.Length),
tagName,
descriptor.DisplayName,
invalidRule.TagStructure));
Expand All @@ -358,7 +358,7 @@ private bool TryRewriteTagHelperEnd(MarkupEndTagSyntax tagBlock, out MarkupTagHe
}

rewritten = SyntaxFactory.MarkupTagHelperEndTag(
tagBlock.OpenAngle, tagBlock.ForwardSlash, tagBlock.Bang, tagBlock.Name, tagBlock.MiscAttributeContent, tagBlock.CloseAngle);
endTag.OpenAngle, endTag.ForwardSlash, endTag.Bang, endTag.Name, endTag.MiscAttributeContent, endTag.CloseAngle);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,13 @@ public void RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly1
EvaluateData(MalformedRequiredAttribute_Descriptors, "<p notRequired=\"hi\" class=\"btn\" </p");
}

[Fact]
public void RequiredAttributeDescriptorsCreateMalformedTagHelperBlocksCorrectly11()
{
var document = "<p class='foo'>@if(true){</p>}</p>";
EvaluateData(MalformedRequiredAttribute_Descriptors, document);
}

public static TagHelperDescriptor[] PrefixedTagHelperColon_Descriptors = new TagHelperDescriptor[]
{
TagHelperDescriptorBuilder.Create("mythTagHelper", "SomeAssembly")
Expand Down Expand Up @@ -1933,4 +1940,4 @@ public void HandlesCaseSensitiveTagHelpersCorrectly5()
EvaluateData(CaseSensitive_Descriptors, "<p Class='foo'></p>");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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] )
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(1,28): Error RZ1026: Encountered end tag "p" with no matching start tag. Are your start/end tags properly balanced?
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
RazorDocument - [0..34)::34 - [<p class='foo'>@if(true){</p>}</p>]
MarkupBlock - [0..34)::34
MarkupTagHelperElement - [0..34)::34 - p[StartTagAndEndTag] - pTagHelper
MarkupTagHelperStartTag - [0..15)::15 - [<p class='foo'>] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[p];
MarkupTagHelperAttribute - [2..14)::12 - class - SingleQuotes - Unbound - [ class='foo']
MarkupTextLiteral - [2..3)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
MarkupTextLiteral - [3..8)::5 - [class] - Gen<Markup> - SpanEditHandler;Accepts:Any
Text;[class];
Equals;[=];
MarkupTextLiteral - [9..10)::1 - ['] - Gen<Markup> - SpanEditHandler;Accepts:Any
SingleQuote;['];
MarkupTagHelperAttributeValue - [10..13)::3
MarkupLiteralAttributeValue - [10..13)::3 - [foo]
MarkupTextLiteral - [10..13)::3 - [foo] - Gen<Markup> - SpanEditHandler;Accepts:Any
Text;[foo];
MarkupTextLiteral - [13..14)::1 - ['] - Gen<Markup> - SpanEditHandler;Accepts:Any
SingleQuote;['];
CloseAngle;[>];
CSharpCodeBlock - [15..30)::15
CSharpTransition - [15..16)::1 - Gen<None> - SpanEditHandler;Accepts:None
Transition;[@];
CSharpStatementLiteral - [16..25)::9 - [if(true){] - Gen<Stmt> - SpanEditHandler;Accepts:Any
Keyword;[if];
LeftParenthesis;[(];
Keyword;[true];
RightParenthesis;[)];
LeftBrace;[{];
MarkupBlock - [25..29)::4
MarkupElement - [25..29)::4
MarkupEndTag - [25..29)::4 - [</p>] - Gen<Markup> - SpanEditHandler;Accepts:None
OpenAngle;[<];
ForwardSlash;[/];
Text;[p];
CloseAngle;[>];
CSharpStatementLiteral - [29..30)::1 - [}] - Gen<Stmt> - SpanEditHandler;Accepts:Any
RightBrace;[}];
MarkupTagHelperEndTag - [30..34)::4 - [</p>]
OpenAngle;[<];
ForwardSlash;[/];
Text;[p];
CloseAngle;[>];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TagHelper span at (0:0,0 [34] ) - pTagHelper

0 comments on commit b866b76

Please sign in to comment.