From b15af76917d6d7ace90d94c3f4b2465d734ccbd5 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 7 Feb 2019 14:02:10 -0800 Subject: [PATCH] Fix crash with code blocks in tag helper attributes (dotnet/aspnetcore-tooling#190) * Fix crash with code blocks in tag helper attributes * Better fix * Fixed another case \n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/9e97ba9b9728ff874e68c591b15df3eb5293b40b --- .../src/Legacy/TagHelperBlockRewriter.cs | 22 ++++++++++++++++ .../IntegrationTests/IntegrationTestBase.cs | 26 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) 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)