diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperBlockRewriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperBlockRewriter.cs
index ebf32e9f70..a585b8781b 100644
--- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperBlockRewriter.cs
+++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TagHelperBlockRewriter.cs
@@ -480,6 +480,28 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
return base.VisitRazorMetaCode(node);
}
+ public override SyntaxNode VisitCSharpStatement(CSharpStatementSyntax node)
+ {
+ // We don't support code blocks inside tag helper attributes. Don't rewrite anything inside a code block.
+ // E.g,
is not supported.
+ return node;
+ }
+
+ public override SyntaxNode VisitRazorDirective(RazorDirectiveSyntax node)
+ {
+ // We don't support directives inside tag helper attributes. Don't rewrite anything inside a directive.
+ // E.g,
is not supported.
+ return node;
+ }
+
+ public override SyntaxNode VisitMarkupElement(MarkupElementSyntax node)
+ {
+ // We're visiting an attribute value. If we encounter a MarkupElement this means the attribute value is invalid.
+ // We don't want to rewrite anything here.
+ // E.g,
+ return node;
+ }
+
public override SyntaxNode VisitCSharpExpressionLiteral(CSharpExpressionLiteralSyntax node)
{
if (!_tryParseResult.IsBoundNonStringAttribute)
diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs
index 11818a68dc..61890d8f61 100644
--- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs
+++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs
@@ -409,7 +409,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
baselineDiagnostics = diagnosticsFile.ReadAllText();
}
- var actualDiagnostics = string.Concat(cSharpDocument.Diagnostics.Select(d => RazorDiagnosticSerializer.Serialize(d) + "\r\n"));
+ var actualDiagnostics = string.Concat(cSharpDocument.Diagnostics.Select(d => NormalizeNewLines(RazorDiagnosticSerializer.Serialize(d)) + "\r\n"));
Assert.Equal(baselineDiagnostics, actualDiagnostics);
}
@@ -541,6 +541,30 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
}
return base.VisitCSharpExpressionLiteral(node);
}
+
+ public override Syntax.SyntaxNode VisitCSharpStatement(CSharpStatementSyntax node)
+ {
+ if (node.FirstAncestorOrSelf() != null)
+ {
+ // We don't support code blocks inside tag helper attribute values.
+ // If it exists, we don't want to track its code spans for source mappings.
+ return node;
+ }
+
+ return base.VisitCSharpStatement(node);
+ }
+
+ public override Syntax.SyntaxNode VisitRazorDirective(RazorDirectiveSyntax node)
+ {
+ if (node.FirstAncestorOrSelf() != null)
+ {
+ // We don't support Razor directives inside tag helper attribute values.
+ // If it exists, we don't want to track its code spans for source mappings.
+ return node;
+ }
+
+ return base.VisitRazorDirective(node);
+ }
}
private static string EscapeWhitespace(string content)