diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs index 8455c9e197..7f567b1109 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs @@ -65,15 +65,23 @@ namespace Microsoft.AspNetCore.Razor.Language public override SyntaxNode VisitRazorDirective(RazorDirectiveSyntax node) { - if (_nestedLevel > 0) + if (node.DirectiveDescriptor?.Directive != SectionDirective.Directive.Directive) + { + // We only want to track the nesting of section directives. + return base.VisitRazorDirective(node); + } + + _nestedLevel++; + var result = (RazorDirectiveSyntax)base.VisitRazorDirective(node); + + if (_nestedLevel > 1) { var directiveStart = node.Transition.GetSourceLocation(_syntaxTree.Source); var errorLength = /* @ */ 1 + SectionDirective.Directive.Directive.Length; var error = RazorDiagnosticFactory.CreateParsing_SectionsCannotBeNested(new SourceSpan(directiveStart, errorLength)); - node = node.AppendDiagnostic(error); + result = result.AppendDiagnostic(error); } - _nestedLevel++; - var result = base.VisitRazorDirective(node); + _nestedLevel--; return result; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs index 2abfa4ff27..b9dc3bc9d2 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs @@ -2179,6 +2179,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy CompleteBlock(); var keyword = OutputAsMetaCode(Output()); var directiveBody = SyntaxFactory.RazorDirectiveBody(keyword, cSharpCode: null); + + // transition could be null if we're already inside a code block. + transition = transition ?? SyntaxFactory.CSharpTransition(SyntaxFactory.MissingToken(SyntaxKind.Transition)); var directive = SyntaxFactory.RazorDirective(transition, directiveBody); builder.Add(directive); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs index 02c7eec22d..e66b9c531c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs @@ -1357,8 +1357,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy var tagStart = CurrentStart; builder.Add(OutputAsMarkupLiteral()); - SpanContext.EditHandler.AcceptedCharacters = endTagAcceptedCharacters; - var openAngleToken = EatCurrentToken(); // '<' var forwardSlashToken = EatCurrentToken(); // '/' var tagNameToken = EatCurrentToken(); // 'script' @@ -1369,8 +1367,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy { var miscAttributeBuilder = pooledResult.Builder; - ParseMarkupNodes(miscAttributeBuilder, ParseMode.Text, token => token.Kind == SyntaxKind.CloseAngle); + // We want to accept malformed end tags as content. + AcceptUntil(SyntaxKind.CloseAngle, SyntaxKind.OpenAngle); miscAttributeBuilder.Add(OutputAsMarkupLiteral()); + if (miscAttributeBuilder.Count > 0) { miscContent = SyntaxFactory.MarkupMiscAttributeContent(miscAttributeBuilder.ToList()); @@ -1390,6 +1390,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } } + SpanContext.EditHandler.AcceptedCharacters = endTagAcceptedCharacters; + endTag = SyntaxFactory.MarkupEndTag( openAngleToken, forwardSlashToken, 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)