From 9e97ba9b9728ff874e68c591b15df3eb5293b40b Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 7 Feb 2019 14:02:10 -0800 Subject: [PATCH 1/4] Fix crash with code blocks in tag helper attributes (#190) * Fix crash with code blocks in tag helper attributes * Better fix * Fixed another case --- .../Legacy/TagHelperBlockRewriter.cs | 22 ++++++ .../IntegrationTests/ComponentTypingTest.cs | 2 +- .../Legacy/TagHelperBlockRewriterTest.cs | 12 ++++ .../ComplexTagHelpers.cshtml | 1 + .../ComplexTagHelpers_DesignTime.codegen.cs | 6 ++ ...mplexTagHelpers_DesignTime.diagnostics.txt | 2 + .../ComplexTagHelpers_DesignTime.ir.txt | 19 ++++-- .../ComplexTagHelpers_DesignTime.mappings.txt | 4 +- .../ComplexTagHelpers_Runtime.codegen.cs | 25 ++++++- .../ComplexTagHelpers_Runtime.diagnostics.txt | 2 + .../ComplexTagHelpers_Runtime.ir.txt | 21 ++++-- ...ormedTagHelpersWithAttributes20.cspans.txt | 11 +++ ...lformedTagHelpersWithAttributes20.diag.txt | 12 ++++ ...formedTagHelpersWithAttributes20.stree.txt | 67 +++++++++++++++++++ ...ormedTagHelpersWithAttributes20.tspans.txt | 1 + ...rNonStringTagHelperAttributes12.cspans.txt | 4 ++ ...orNonStringTagHelperAttributes12.stree.txt | 47 +++++++++++++ ...rNonStringTagHelperAttributes12.tspans.txt | 1 + .../IntegrationTests/IntegrationTestBase.cs | 26 ++++++- 19 files changed, 267 insertions(+), 18 deletions(-) create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.diagnostics.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.diagnostics.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.cspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.diag.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.stree.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.tspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.cspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.stree.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.tspans.txt diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperBlockRewriter.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperBlockRewriter.cs index ebf32e9f704..a585b8781b3 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperBlockRewriter.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperBlockRewriter.cs @@ -480,6 +480,28 @@ public override SyntaxNode VisitRazorMetaCode(RazorMetaCodeSyntax node) 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.Language.Test/IntegrationTests/ComponentTypingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/IntegrationTests/ComponentTypingTest.cs index d14afcedaa3..0d5ea53eb92 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/IntegrationTests/ComponentTypingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/IntegrationTests/ComponentTypingTest.cs @@ -77,7 +77,7 @@ public class ModelState } } - [Fact(Skip = "https://github.com/aspnet/AspNetCore/issues/6113")] // Regression test for #1068 + [Fact] // Regression test for #1068 public void Regression_1068() { // Arrange diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperBlockRewriterTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperBlockRewriterTest.cs index 26ab511b04e..09b1418aeab 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperBlockRewriterTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperBlockRewriterTest.cs @@ -346,6 +346,12 @@ public void CreatesErrorForMalformedTagHelpersWithAttributes19() RunParseTreeRewriterTest("

", "strong", "p"); } + [Fact] + public void CreatesErrorForMalformedTagHelpersWithAttributes20() + { + RunParseTreeRewriterTest("

}\">

", "strong", "p"); + } + [Fact] public void CreatesErrorForMalformedTagHelper1() { @@ -482,6 +488,12 @@ public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes11() EvaluateData(CodeTagHelperAttributes_Descriptors, ""); } + [Fact] + public void CreatesMarkupCodeSpansForNonStringTagHelperAttributes12() + { + EvaluateData(CodeTagHelperAttributes_Descriptors, ""); + } + [Fact] public void TagHelperParseTreeRewriter_CreatesErrorForIncompleteTagHelper1() { diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml index bc85b1d3890..546906282c2 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml @@ -33,5 +33,6 @@

@someMethod(@

) +

} \ No newline at end of file diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.codegen.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.codegen.cs index f462ec5fb68..ec6011df97b 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.codegen.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.codegen.cs @@ -206,7 +206,13 @@ public async System.Threading.Tasks.Task ExecuteAsync() #line default #line hidden + __TestNamespace_PTagHelper = CreateTagHelper(); #line 36 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" +__TestNamespace_PTagHelper.Age = ; + +#line default +#line hidden +#line 37 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.diagnostics.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.diagnostics.txt new file mode 100644 index 00000000000..fe2b80c9e0f --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.diagnostics.txt @@ -0,0 +1,2 @@ +TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml(36,17): Error RZ2006: Code blocks (e.g. @{var variable = 23;}) must not appear in non-string tag helper attribute values. + Already in an expression (code) context. If necessary an explicit expression (e.g. @(@readonly)) may be used. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt index 6ef9380b3b7..30f540a77dc 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.ir.txt @@ -292,8 +292,17 @@ Document - IntermediateToken - (1387:34,41 [5] ComplexTagHelpers.cshtml) - Html - hello DefaultTagHelperExecute - IntermediateToken - (1424:34,78 [1] ComplexTagHelpers.cshtml) - CSharp - ) - HtmlContent - (1425:34,79 [12] ComplexTagHelpers.cshtml) - IntermediateToken - (1425:34,79 [6] ComplexTagHelpers.cshtml) - Html - \n - IntermediateToken - (1431:35,4 [6] ComplexTagHelpers.cshtml) - Html - - CSharpCode - (1437:35,10 [3] ComplexTagHelpers.cshtml) - IntermediateToken - (1437:35,10 [3] ComplexTagHelpers.cshtml) - CSharp - \n} + HtmlContent - (1425:34,79 [10] ComplexTagHelpers.cshtml) + IntermediateToken - (1425:34,79 [10] ComplexTagHelpers.cshtml) - Html - \n + TagHelper - (1435:35,8 [22] ComplexTagHelpers.cshtml) - p - TagMode.StartTagAndEndTag + DefaultTagHelperBody - + DefaultTagHelperCreate - - TestNamespace.PTagHelper + DefaultTagHelperProperty - (1443:35,16 [8] ComplexTagHelpers.cshtml) - age - int TestNamespace.PTagHelper.Age - HtmlAttributeValueStyle.DoubleQuotes + CSharpCode - (1445:35,18 [5] ComplexTagHelpers.cshtml) + IntermediateToken - (1445:35,18 [5] ComplexTagHelpers.cshtml) - CSharp - 1 + 2 + DefaultTagHelperExecute - + HtmlContent - (1457:35,30 [12] ComplexTagHelpers.cshtml) + IntermediateToken - (1457:35,30 [6] ComplexTagHelpers.cshtml) - Html - \n + IntermediateToken - (1463:36,4 [6] ComplexTagHelpers.cshtml) - Html - + CSharpCode - (1469:36,10 [3] ComplexTagHelpers.cshtml) + IntermediateToken - (1469:36,10 [3] ComplexTagHelpers.cshtml) - CSharp - \n} diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.mappings.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.mappings.txt index 3aadb7916f7..154b6b5cf1e 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.mappings.txt +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_DesignTime.mappings.txt @@ -284,10 +284,10 @@ Source Location: (1424:34,78 [1] TestFiles/IntegrationTests/CodeGenerationIntegr Generated Location: (9546:204,1 [1] ) |)| -Source Location: (1437:35,10 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml) +Source Location: (1469:36,10 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml) | }| -Generated Location: (9685:209,10 [3] ) +Generated Location: (9941:215,10 [3] ) | }| diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.codegen.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.codegen.cs index 8602f83cc4e..41a997a4e20 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.codegen.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.codegen.cs @@ -1,11 +1,11 @@ -#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0ebc2a451a011b17f08d86ab395d2ef2bdaaf44e" +#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "ab167e80e48e48d9984ecd76158a2d87f90f3460" // #pragma warning disable 1591 [assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ComplexTagHelpers_Runtime), @"default", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml")] namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles { #line hidden - [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"0ebc2a451a011b17f08d86ab395d2ef2bdaaf44e", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml")] + [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"ab167e80e48e48d9984ecd76158a2d87f90f3460", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml")] public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_ComplexTagHelpers_Runtime { private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_0 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("type", "text", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); @@ -480,8 +480,27 @@ public async System.Threading.Tasks.Task ExecuteAsync() #line default #line hidden + WriteLiteral("\r\n "); + __tagHelperExecutionContext = __tagHelperScopeManager.Begin("p", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "test", async() => { + } + ); + __TestNamespace_PTagHelper = CreateTagHelper(); + __tagHelperExecutionContext.Add(__TestNamespace_PTagHelper); +#line 36 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" +__TestNamespace_PTagHelper.Age = ; + +#line default +#line hidden + __tagHelperExecutionContext.AddTagHelperAttribute("age", __TestNamespace_PTagHelper.Age, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); + if (!__tagHelperExecutionContext.Output.IsContentModified) + { + await __tagHelperExecutionContext.SetOutputContentAsync(); + } + Write(__tagHelperExecutionContext.Output); + __tagHelperExecutionContext = __tagHelperScopeManager.End(); WriteLiteral("\r\n \r\n"); -#line 37 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" +#line 38 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml" } #line default diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.diagnostics.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.diagnostics.txt new file mode 100644 index 00000000000..fe2b80c9e0f --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.diagnostics.txt @@ -0,0 +1,2 @@ +TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml(36,17): Error RZ2006: Code blocks (e.g. @{var variable = 23;}) must not appear in non-string tag helper attribute values. + Already in an expression (code) context. If necessary an explicit expression (e.g. @(@readonly)) may be used. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.ir.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.ir.txt index bb590984d9d..79168e02adf 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.ir.txt +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers_Runtime.ir.txt @@ -292,9 +292,18 @@ Document - PreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_7 DefaultTagHelperExecute - IntermediateToken - (1424:34,78 [1] ComplexTagHelpers.cshtml) - CSharp - ) - HtmlContent - (1425:34,79 [14] ComplexTagHelpers.cshtml) - IntermediateToken - (1425:34,79 [6] ComplexTagHelpers.cshtml) - Html - \n - IntermediateToken - (1431:35,4 [6] ComplexTagHelpers.cshtml) - Html - - IntermediateToken - (1437:35,10 [2] ComplexTagHelpers.cshtml) - Html - \n - CSharpCode - (1439:36,0 [1] ComplexTagHelpers.cshtml) - IntermediateToken - (1439:36,0 [1] ComplexTagHelpers.cshtml) - CSharp - } + HtmlContent - (1425:34,79 [10] ComplexTagHelpers.cshtml) + IntermediateToken - (1425:34,79 [10] ComplexTagHelpers.cshtml) - Html - \n + TagHelper - (1435:35,8 [22] ComplexTagHelpers.cshtml) - p - TagMode.StartTagAndEndTag + DefaultTagHelperBody - + DefaultTagHelperCreate - - TestNamespace.PTagHelper + DefaultTagHelperProperty - (1443:35,16 [8] ComplexTagHelpers.cshtml) - age - int TestNamespace.PTagHelper.Age - HtmlAttributeValueStyle.DoubleQuotes + CSharpCode - (1445:35,18 [5] ComplexTagHelpers.cshtml) + IntermediateToken - (1445:35,18 [5] ComplexTagHelpers.cshtml) - CSharp - 1 + 2 + DefaultTagHelperExecute - + HtmlContent - (1457:35,30 [14] ComplexTagHelpers.cshtml) + IntermediateToken - (1457:35,30 [6] ComplexTagHelpers.cshtml) - Html - \n + IntermediateToken - (1463:36,4 [6] ComplexTagHelpers.cshtml) - Html - + IntermediateToken - (1469:36,10 [2] ComplexTagHelpers.cshtml) - Html - \n + CSharpCode - (1471:37,0 [1] ComplexTagHelpers.cshtml) + IntermediateToken - (1471:37,0 [1] ComplexTagHelpers.cshtml) - CSharp - } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.cspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.cspans.txt new file mode 100644 index 00000000000..4934c61fc0d --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.cspans.txt @@ -0,0 +1,11 @@ +Transition span at (9:0,9 [1] ) (Accepts:None) - Parent: Statement block at (9:0,9 [34] ) +Code span at (10:0,10 [10] ) (Accepts:Any) - Parent: Statement block at (9:0,9 [34] ) +Markup span at (20:0,20 [2] ) (Accepts:Any) - Parent: Tag block at (20:0,20 [15] ) +Markup span at (22:0,22 [7] ) (Accepts:Any) - Parent: Markup block at (22:0,22 [12] ) +Transition span at (29:0,29 [1] ) (Accepts:None) - Parent: Expression block at (29:0,29 [4] ) +Code span at (30:0,30 [3] ) (Accepts:NonWhitespace) - Parent: Expression block at (29:0,29 [4] ) +Markup span at (33:0,33 [1] ) (Accepts:Any) - Parent: Markup block at (22:0,22 [12] ) +Markup span at (34:0,34 [1] ) (Accepts:None) - Parent: Tag block at (20:0,20 [15] ) +Markup span at (35:0,35 [4] ) (Accepts:Any) - Parent: Markup block at (20:0,20 [23] ) +Markup span at (39:0,39 [4] ) (Accepts:None) - Parent: Tag block at (39:0,39 [4] ) +Code span at (43:0,43 [0] ) (Accepts:Any) - Parent: Statement block at (9:0,9 [34] ) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.diag.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.diag.txt new file mode 100644 index 00000000000..9dba2e6078f --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.diag.txt @@ -0,0 +1,12 @@ +(1,2): Error RZ1035: Missing close angle for tag helper 'p'. +(1,2): Error RZ1034: Found a malformed 'p' tag helper. Tag helpers must have a start and end tag or be self closing. +(1,21): Error RZ1008: Expected a "{" but found a "<". Block statements must be enclosed in "{" and "}". You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed: + +@if(isLoggedIn) +

Hello, @user

+ +Instead, wrap the contents of the block in "{}": + +@if(isLoggedIn) { +

Hello, @user

+} diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.stree.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.stree.txt new file mode 100644 index 00000000000..c7ec7c7f177 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.stree.txt @@ -0,0 +1,67 @@ +RazorDocument - [0..43)::43 - [

] + MarkupBlock - [0..43)::43 + MarkupTagHelperElement - [0..43)::43 - p[StartTagAndEndTag] - ptaghelper + MarkupTagHelperStartTag - [0..43)::43 - [

] - Gen - SpanEditHandler;Accepts:Any + OpenAngle;[<]; + Text;[p]; + MarkupTagHelperAttribute - [2..43)::41 - attr - DoubleQuotes - Unbound - [ attr="@if (true)

}">

] + MarkupTextLiteral - [2..3)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + MarkupTextLiteral - [3..7)::4 - [attr] - Gen - SpanEditHandler;Accepts:Any + Text;[attr]; + Equals;[=]; + MarkupTextLiteral - [8..9)::1 - ["] - Gen - SpanEditHandler;Accepts:Any + DoubleQuote;["]; + MarkupTagHelperAttributeValue - [9..43)::34 + MarkupDynamicAttributeValue - [9..43)::34 - [@if (true)

}">

] + GenericBlock - [9..43)::34 + CSharpCodeBlock - [9..43)::34 + CSharpTransition - [9..10)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + CSharpStatementLiteral - [10..20)::10 - [if (true) ] - Gen - SpanEditHandler;Accepts:Any + Keyword;[if]; + Whitespace;[ ]; + LeftParenthesis;[(]; + Keyword;[true]; + RightParenthesis;[)]; + Whitespace;[ ]; + MarkupBlock - [20..43)::23 + MarkupElement - [20..43)::23 + MarkupStartTag - [20..35)::15 - [

] - Gen - SpanEditHandler;Accepts:None + OpenAngle;[<]; + Text;[p]; + MarkupAttributeBlock - [22..34)::12 - [ attr='@foo'] + MarkupTextLiteral - [22..23)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + MarkupTextLiteral - [23..27)::4 - [attr] - Gen - SpanEditHandler;Accepts:Any + Text;[attr]; + Equals;[=]; + MarkupTextLiteral - [28..29)::1 - ['] - Gen - SpanEditHandler;Accepts:Any + SingleQuote;[']; + GenericBlock - [29..33)::4 + MarkupDynamicAttributeValue - [29..33)::4 - [@foo] + GenericBlock - [29..33)::4 + CSharpCodeBlock - [29..33)::4 + CSharpImplicitExpression - [29..33)::4 + CSharpTransition - [29..30)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + CSharpImplicitExpressionBody - [30..33)::3 + CSharpCodeBlock - [30..33)::3 + CSharpExpressionLiteral - [30..33)::3 - [foo] - Gen - ImplicitExpressionEditHandler;Accepts:NonWhitespace;ImplicitExpression[RTD];K14 + Identifier;[foo]; + MarkupTextLiteral - [33..34)::1 - ['] - Gen - SpanEditHandler;Accepts:Any + SingleQuote;[']; + CloseAngle;[>]; + MarkupTextLiteral - [35..39)::4 - [ }">] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + Text;[}]; + DoubleQuote;["]; + CloseAngle;[>]; + MarkupEndTag - [39..43)::4 - [

] - Gen - SpanEditHandler;Accepts:None + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[p]; + CloseAngle;[>]; + CSharpStatementLiteral - [43..43)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + CloseAngle;[]; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.tspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.tspans.txt new file mode 100644 index 00000000000..2dbca568cdb --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesErrorForMalformedTagHelpersWithAttributes20.tspans.txt @@ -0,0 +1 @@ +TagHelper span at (0:0,0 [43] ) - ptaghelper diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.cspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.cspans.txt new file mode 100644 index 00000000000..53bec4dfc26 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.cspans.txt @@ -0,0 +1,4 @@ +Transition span at (13:0,13 [1] ) (Accepts:None) - Parent: Statement block at (13:0,13 [22] ) +MetaCode span at (14:0,14 [1] ) (Accepts:None) - Parent: Statement block at (13:0,13 [22] ) +Code span at (15:0,15 [19] ) (Accepts:Any) - Parent: Statement block at (13:0,13 [22] ) +MetaCode span at (34:0,34 [1] ) (Accepts:None) - Parent: Statement block at (13:0,13 [22] ) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.stree.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.stree.txt new file mode 100644 index 00000000000..58fba8ce3f6 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.stree.txt @@ -0,0 +1,47 @@ +RazorDocument - [0..39)::39 - [] + MarkupBlock - [0..39)::39 + MarkupTagHelperElement - [0..39)::39 - person[SelfClosing] - PersonTagHelper + MarkupTagHelperStartTag - [0..39)::39 - [] - Gen - SpanEditHandler;Accepts:Any + OpenAngle;[<]; + Text;[person]; + MarkupTagHelperAttribute - [7..36)::29 - age - DoubleQuotes - Bound - [ age="@{flag == 0 ? 11 : 12}"] + MarkupTextLiteral - [7..8)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + MarkupTextLiteral - [8..11)::3 - [age] - Gen - SpanEditHandler;Accepts:Any + Text;[age]; + Equals;[=]; + MarkupTextLiteral - [12..13)::1 - ["] - Gen - SpanEditHandler;Accepts:Any + DoubleQuote;["]; + MarkupTagHelperAttributeValue - [13..35)::22 + MarkupBlock - [13..35)::22 + CSharpCodeBlock - [13..35)::22 + CSharpStatement - [13..35)::22 + CSharpTransition - [13..14)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + CSharpStatementBody - [14..35)::21 + RazorMetaCode - [14..15)::1 - Gen - SpanEditHandler;Accepts:None + LeftBrace;[{]; + CSharpCodeBlock - [15..34)::19 + CSharpStatementLiteral - [15..34)::19 - [flag == 0 ? 11 : 12] - Gen - AutoCompleteEditHandler;Accepts:Any,AutoComplete:[];AtEOL + Identifier;[flag]; + Whitespace;[ ]; + Equals;[==]; + Whitespace;[ ]; + IntegerLiteral;[0]; + Whitespace;[ ]; + QuestionMark;[?]; + Whitespace;[ ]; + IntegerLiteral;[11]; + Whitespace;[ ]; + Colon;[:]; + Whitespace;[ ]; + IntegerLiteral;[12]; + RazorMetaCode - [34..35)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [35..36)::1 - ["] - Gen - SpanEditHandler;Accepts:Any + DoubleQuote;["]; + MarkupMiscAttributeContent - [36..37)::1 + MarkupTextLiteral - [36..37)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + ForwardSlash;[/]; + CloseAngle;[>]; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.tspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.tspans.txt new file mode 100644 index 00000000000..14c5fecb184 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/TagHelperBlockRewriterTest/CreatesMarkupCodeSpansForNonStringTagHelperAttributes12.tspans.txt @@ -0,0 +1 @@ +TagHelper span at (0:0,0 [39] ) - PersonTagHelper 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 11818a68dc8..61890d8f61c 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 @@ protected void AssertCSharpDocumentMatchesBaseline(RazorCSharpDocument cSharpDoc 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 @@ public override Syntax.SyntaxNode VisitCSharpExpressionLiteral(CSharpExpressionL } 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) From 10dbc434ee4ea10bb5add4be01dba838bc1e7718 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 7 Feb 2019 14:03:23 -0800 Subject: [PATCH 2/4] Fixed null-ref with reserved word inside a code block (#192) --- .../Legacy/CSharpCodeParser.cs | 3 +++ .../Legacy/RazorDirectivesTest.cs | 6 +++++ ...ves_ReservedWordInsideCodeBlock.cspans.txt | 7 ++++++ ...tives_ReservedWordInsideCodeBlock.diag.txt | 1 + ...ives_ReservedWordInsideCodeBlock.stree.txt | 25 +++++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.cspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.diag.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.stree.txt diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs index 2abfa4ff27f..b9dc3bc9d22 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs @@ -2179,6 +2179,9 @@ private void ParseReservedDirective(SyntaxListBuilder builder, 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/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/RazorDirectivesTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/RazorDirectivesTest.cs index 427e5f1e42a..905bfcf2926 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/RazorDirectivesTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/RazorDirectivesTest.cs @@ -902,5 +902,11 @@ public void Directives_CanUseReservedWord_Namespace() "@namespace", new[] { descriptor }); } + + [Fact] + public void Directives_ReservedWordInsideCodeBlock() + { + ParseDocumentTest("@{ class }"); + } } } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.cspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.cspans.txt new file mode 100644 index 00000000000..4da87bdd97d --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.cspans.txt @@ -0,0 +1,7 @@ +Markup span at (0:0,0 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [10] ) +Transition span at (0:0,0 [1] ) (Accepts:None) - Parent: Statement block at (0:0,0 [10] ) +MetaCode span at (1:0,1 [1] ) (Accepts:None) - Parent: Statement block at (0:0,0 [10] ) +MetaCode span at (2:0,2 [6] ) (Accepts:None) - Parent: Directive block at (2:0,2 [6] ) +Code span at (8:0,8 [1] ) (Accepts:Any) - Parent: Statement block at (0:0,0 [10] ) +MetaCode span at (9:0,9 [1] ) (Accepts:None) - Parent: Statement block at (0:0,0 [10] ) +Markup span at (10:0,10 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [10] ) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.diag.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.diag.txt new file mode 100644 index 00000000000..46433e5bdb5 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.diag.txt @@ -0,0 +1 @@ +(1,4): Error RZ1007: "class" is a reserved word and cannot be used in implicit expressions. An explicit expression ("@()") must be used. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.stree.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.stree.txt new file mode 100644 index 00000000000..5ec9332eb66 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/RazorDirectivesTest/Directives_ReservedWordInsideCodeBlock.stree.txt @@ -0,0 +1,25 @@ +RazorDocument - [0..10)::10 - [@{ class }] + MarkupBlock - [0..10)::10 + MarkupTextLiteral - [0..0)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + CSharpCodeBlock - [0..10)::10 + CSharpStatement - [0..10)::10 + CSharpTransition - [0..1)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + CSharpStatementBody - [1..10)::9 + RazorMetaCode - [1..2)::1 - Gen - SpanEditHandler;Accepts:None + LeftBrace;[{]; + CSharpCodeBlock - [2..9)::7 + RazorDirective - [2..8)::6 + CSharpTransition - [2..2)::0 + Transition;[]; + RazorDirectiveBody - [2..8)::6 + RazorMetaCode - [2..8)::6 - Gen - AutoCompleteEditHandler;Accepts:None,AutoComplete:[];AtEOL + Whitespace;[ ]; + Keyword;[class]; + CSharpStatementLiteral - [8..9)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + RazorMetaCode - [9..10)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [10..10)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; From 26e8d86b2164ab1b324df935b8387872bd4d42b1 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 7 Feb 2019 14:04:57 -0800 Subject: [PATCH 3/4] Handle multiple nested sections correctly (#196) --- .../DefaultDirectiveSyntaxTreePass.cs | 16 +++- .../Legacy/CSharpSectionTest.cs | 17 ++++ ...utputErrorOtherNestedDirectives.cspans.txt | 16 ++++ ...OutputErrorOtherNestedDirectives.stree.txt | 43 +++++++++ ...tsErrorOnMultipleNestedSections.cspans.txt | 32 +++++++ ...utsErrorOnMultipleNestedSections.stree.txt | 87 +++++++++++++++++++ 6 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.cspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.stree.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.cspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.stree.txt diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/DefaultDirectiveSyntaxTreePass.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/DefaultDirectiveSyntaxTreePass.cs index 8455c9e1978..7f567b11092 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/DefaultDirectiveSyntaxTreePass.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/DefaultDirectiveSyntaxTreePass.cs @@ -65,15 +65,23 @@ public override SyntaxNode Visit(SyntaxNode node) 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/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpSectionTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpSectionTest.cs index 97068e11487..2e703e46eac 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpSectionTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpSectionTest.cs @@ -67,6 +67,23 @@ public void ParserOutputsErrorOnNestedSections() new[] { SectionDirective.Directive }); } + [Fact] + public void ParserOutputsErrorOnMultipleNestedSections() + { + ParseDocumentTest( + "@section foo { @section bar {

Foo

@section baz { } } }", + new[] { SectionDirective.Directive }); + } + + [Fact] + public void ParserDoesNotOutputErrorOtherNestedDirectives() + { + // This isn't a real scenario but we just want to verify we don't show misleading errors. + ParseDocumentTest( + "@section foo { @inherits Bar }", + new[] { SectionDirective.Directive, InheritsDirective.Directive }); + } + [Fact] public void HandlesEOFAfterOpenBrace() { diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.cspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.cspans.txt new file mode 100644 index 00000000000..8e87c7c6c99 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.cspans.txt @@ -0,0 +1,16 @@ +Markup span at (0:0,0 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [30] ) +Transition span at (0:0,0 [1] ) (Accepts:None) - Parent: Directive block at (0:0,0 [30] ) +MetaCode span at (1:0,1 [7] ) (Accepts:None) - Parent: Directive block at (0:0,0 [30] ) +Code span at (8:0,8 [1] ) (Accepts:Whitespace) - Parent: Directive block at (0:0,0 [30] ) +Code span at (9:0,9 [3] ) (Accepts:NonWhitespace) - Parent: Directive block at (0:0,0 [30] ) +None span at (12:0,12 [1] ) (Accepts:AllWhitespace) - Parent: Directive block at (0:0,0 [30] ) +MetaCode span at (13:0,13 [1] ) (Accepts:None) - Parent: Directive block at (0:0,0 [30] ) +Markup span at (14:0,14 [1] ) (Accepts:Any) - Parent: Markup block at (14:0,14 [15] ) +Transition span at (15:0,15 [1] ) (Accepts:None) - Parent: Directive block at (15:0,15 [14] ) +MetaCode span at (16:0,16 [8] ) (Accepts:None) - Parent: Directive block at (15:0,15 [14] ) +Code span at (24:0,24 [1] ) (Accepts:Whitespace) - Parent: Directive block at (15:0,15 [14] ) +Code span at (25:0,25 [3] ) (Accepts:NonWhitespace) - Parent: Directive block at (15:0,15 [14] ) +None span at (28:0,28 [1] ) (Accepts:Whitespace) - Parent: Directive block at (15:0,15 [14] ) +Markup span at (29:0,29 [0] ) (Accepts:Any) - Parent: Markup block at (14:0,14 [15] ) +MetaCode span at (29:0,29 [1] ) (Accepts:None) - Parent: Directive block at (0:0,0 [30] ) +Markup span at (30:0,30 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [30] ) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.stree.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.stree.txt new file mode 100644 index 00000000000..0a06cbca9af --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserDoesNotOutputErrorOtherNestedDirectives.stree.txt @@ -0,0 +1,43 @@ +RazorDocument - [0..30)::30 - [@section foo { @inherits Bar }] + MarkupBlock - [0..30)::30 + MarkupTextLiteral - [0..0)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + CSharpCodeBlock - [0..30)::30 + RazorDirective - [0..30)::30 - Directive:{section;RazorBlock;Unrestricted} + CSharpTransition - [0..1)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + RazorDirectiveBody - [1..30)::29 + RazorMetaCode - [1..8)::7 - Gen - SpanEditHandler;Accepts:None + Identifier;[section]; + CSharpCodeBlock - [8..30)::22 + CSharpStatementLiteral - [8..9)::1 - [ ] - Gen - SpanEditHandler;Accepts:Whitespace + Whitespace;[ ]; + CSharpStatementLiteral - [9..12)::3 - [foo] - Gen - DirectiveTokenEditHandler;Accepts:NonWhitespace + Identifier;[foo]; + UnclassifiedTextLiteral - [12..13)::1 - [ ] - Gen - SpanEditHandler;Accepts:AllWhitespace + Whitespace;[ ]; + RazorMetaCode - [13..14)::1 - Gen - AutoCompleteEditHandler;Accepts:None,AutoComplete:[];AtEnd + LeftBrace;[{]; + MarkupBlock - [14..29)::15 + MarkupTextLiteral - [14..15)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + CSharpCodeBlock - [15..29)::14 + RazorDirective - [15..29)::14 - Directive:{inherits;SingleLine;FileScopedSinglyOccurring} [RZ2005(16:0,16 [8] ), RZ1017(29:0,29 [1] )] + CSharpTransition - [15..16)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + RazorDirectiveBody - [16..29)::13 + RazorMetaCode - [16..24)::8 - Gen - SpanEditHandler;Accepts:None + Identifier;[inherits]; + CSharpCodeBlock - [24..29)::5 + CSharpStatementLiteral - [24..25)::1 - [ ] - Gen - SpanEditHandler;Accepts:Whitespace + Whitespace;[ ]; + CSharpStatementLiteral - [25..28)::3 - [Bar] - Gen - DirectiveTokenEditHandler;Accepts:NonWhitespace + Identifier;[Bar]; + UnclassifiedTextLiteral - [28..29)::1 - [ ] - Gen - SpanEditHandler;Accepts:Whitespace + Whitespace;[ ]; + MarkupTextLiteral - [29..29)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + RazorMetaCode - [29..30)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [30..30)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.cspans.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.cspans.txt new file mode 100644 index 00000000000..7b1ac91e413 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.cspans.txt @@ -0,0 +1,32 @@ +Markup span at (0:0,0 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [61] ) +Transition span at (0:0,0 [1] ) (Accepts:None) - Parent: Directive block at (0:0,0 [61] ) +MetaCode span at (1:0,1 [7] ) (Accepts:None) - Parent: Directive block at (0:0,0 [61] ) +Code span at (8:0,8 [1] ) (Accepts:Whitespace) - Parent: Directive block at (0:0,0 [61] ) +Code span at (9:0,9 [3] ) (Accepts:NonWhitespace) - Parent: Directive block at (0:0,0 [61] ) +None span at (12:0,12 [1] ) (Accepts:AllWhitespace) - Parent: Directive block at (0:0,0 [61] ) +MetaCode span at (13:0,13 [1] ) (Accepts:None) - Parent: Directive block at (0:0,0 [61] ) +Markup span at (14:0,14 [1] ) (Accepts:Any) - Parent: Markup block at (14:0,14 [46] ) +Transition span at (15:0,15 [1] ) (Accepts:None) - Parent: Directive block at (15:0,15 [44] ) +MetaCode span at (16:0,16 [7] ) (Accepts:None) - Parent: Directive block at (15:0,15 [44] ) +Code span at (23:0,23 [1] ) (Accepts:Whitespace) - Parent: Directive block at (15:0,15 [44] ) +Code span at (24:0,24 [3] ) (Accepts:NonWhitespace) - Parent: Directive block at (15:0,15 [44] ) +None span at (27:0,27 [1] ) (Accepts:AllWhitespace) - Parent: Directive block at (15:0,15 [44] ) +MetaCode span at (28:0,28 [1] ) (Accepts:None) - Parent: Directive block at (15:0,15 [44] ) +Markup span at (29:0,29 [1] ) (Accepts:Any) - Parent: Markup block at (29:0,29 [29] ) +Markup span at (30:0,30 [3] ) (Accepts:Any) - Parent: Tag block at (30:0,30 [3] ) +Markup span at (33:0,33 [3] ) (Accepts:Any) - Parent: Markup block at (29:0,29 [29] ) +Markup span at (36:0,36 [4] ) (Accepts:Any) - Parent: Tag block at (36:0,36 [4] ) +Markup span at (40:0,40 [1] ) (Accepts:Any) - Parent: Markup block at (29:0,29 [29] ) +Transition span at (41:0,41 [1] ) (Accepts:None) - Parent: Directive block at (41:0,41 [16] ) +MetaCode span at (42:0,42 [7] ) (Accepts:None) - Parent: Directive block at (41:0,41 [16] ) +Code span at (49:0,49 [1] ) (Accepts:Whitespace) - Parent: Directive block at (41:0,41 [16] ) +Code span at (50:0,50 [3] ) (Accepts:NonWhitespace) - Parent: Directive block at (41:0,41 [16] ) +None span at (53:0,53 [1] ) (Accepts:AllWhitespace) - Parent: Directive block at (41:0,41 [16] ) +MetaCode span at (54:0,54 [1] ) (Accepts:None) - Parent: Directive block at (41:0,41 [16] ) +Markup span at (55:0,55 [1] ) (Accepts:Any) - Parent: Markup block at (55:0,55 [1] ) +MetaCode span at (56:0,56 [1] ) (Accepts:None) - Parent: Directive block at (41:0,41 [16] ) +Markup span at (57:0,57 [1] ) (Accepts:Any) - Parent: Markup block at (29:0,29 [29] ) +MetaCode span at (58:0,58 [1] ) (Accepts:None) - Parent: Directive block at (15:0,15 [44] ) +Markup span at (59:0,59 [1] ) (Accepts:Any) - Parent: Markup block at (14:0,14 [46] ) +MetaCode span at (60:0,60 [1] ) (Accepts:None) - Parent: Directive block at (0:0,0 [61] ) +Markup span at (61:0,61 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [61] ) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.stree.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.stree.txt new file mode 100644 index 00000000000..d0555095918 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpSectionTest/ParserOutputsErrorOnMultipleNestedSections.stree.txt @@ -0,0 +1,87 @@ +RazorDocument - [0..61)::61 - [@section foo { @section bar {

Foo

@section baz { } } }] + MarkupBlock - [0..61)::61 + MarkupTextLiteral - [0..0)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + CSharpCodeBlock - [0..61)::61 + RazorDirective - [0..61)::61 - Directive:{section;RazorBlock;Unrestricted} + CSharpTransition - [0..1)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + RazorDirectiveBody - [1..61)::60 + RazorMetaCode - [1..8)::7 - Gen - SpanEditHandler;Accepts:None + Identifier;[section]; + CSharpCodeBlock - [8..61)::53 + CSharpStatementLiteral - [8..9)::1 - [ ] - Gen - SpanEditHandler;Accepts:Whitespace + Whitespace;[ ]; + CSharpStatementLiteral - [9..12)::3 - [foo] - Gen - DirectiveTokenEditHandler;Accepts:NonWhitespace + Identifier;[foo]; + UnclassifiedTextLiteral - [12..13)::1 - [ ] - Gen - SpanEditHandler;Accepts:AllWhitespace + Whitespace;[ ]; + RazorMetaCode - [13..14)::1 - Gen - AutoCompleteEditHandler;Accepts:None,AutoComplete:[];AtEnd + LeftBrace;[{]; + MarkupBlock - [14..60)::46 + MarkupTextLiteral - [14..15)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + CSharpCodeBlock - [15..59)::44 + RazorDirective - [15..59)::44 - Directive:{section;RazorBlock;Unrestricted} [RZ2002(15:0,15 [8] )] + CSharpTransition - [15..16)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + RazorDirectiveBody - [16..59)::43 + RazorMetaCode - [16..23)::7 - Gen - SpanEditHandler;Accepts:None + Identifier;[section]; + CSharpCodeBlock - [23..59)::36 + CSharpStatementLiteral - [23..24)::1 - [ ] - Gen - SpanEditHandler;Accepts:Whitespace + Whitespace;[ ]; + CSharpStatementLiteral - [24..27)::3 - [bar] - Gen - DirectiveTokenEditHandler;Accepts:NonWhitespace + Identifier;[bar]; + UnclassifiedTextLiteral - [27..28)::1 - [ ] - Gen - SpanEditHandler;Accepts:AllWhitespace + Whitespace;[ ]; + RazorMetaCode - [28..29)::1 - Gen - AutoCompleteEditHandler;Accepts:None,AutoComplete:[];AtEnd + LeftBrace;[{]; + MarkupBlock - [29..58)::29 + MarkupTextLiteral - [29..30)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + MarkupElement - [30..40)::10 + MarkupStartTag - [30..33)::3 - [

] - Gen - SpanEditHandler;Accepts:Any + OpenAngle;[<]; + Text;[p]; + CloseAngle;[>]; + MarkupTextLiteral - [33..36)::3 - [Foo] - Gen - SpanEditHandler;Accepts:Any + Text;[Foo]; + MarkupEndTag - [36..40)::4 - [

] - Gen - SpanEditHandler;Accepts:Any + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[p]; + CloseAngle;[>]; + MarkupTextLiteral - [40..41)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + CSharpCodeBlock - [41..57)::16 + RazorDirective - [41..57)::16 - Directive:{section;RazorBlock;Unrestricted} [RZ2005(42:0,42 [7] ), RZ2002(41:0,41 [8] )] + CSharpTransition - [41..42)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + RazorDirectiveBody - [42..57)::15 + RazorMetaCode - [42..49)::7 - Gen - SpanEditHandler;Accepts:None + Identifier;[section]; + CSharpCodeBlock - [49..57)::8 + CSharpStatementLiteral - [49..50)::1 - [ ] - Gen - SpanEditHandler;Accepts:Whitespace + Whitespace;[ ]; + CSharpStatementLiteral - [50..53)::3 - [baz] - Gen - DirectiveTokenEditHandler;Accepts:NonWhitespace + Identifier;[baz]; + UnclassifiedTextLiteral - [53..54)::1 - [ ] - Gen - SpanEditHandler;Accepts:AllWhitespace + Whitespace;[ ]; + RazorMetaCode - [54..55)::1 - Gen - AutoCompleteEditHandler;Accepts:None,AutoComplete:[];AtEnd + LeftBrace;[{]; + MarkupBlock - [55..56)::1 + MarkupTextLiteral - [55..56)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + RazorMetaCode - [56..57)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [57..58)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + RazorMetaCode - [58..59)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [59..60)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + RazorMetaCode - [60..61)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [61..61)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; From 56fa17b094b61aaedf9b8c8454c84f2cf5e0a63a Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 7 Feb 2019 15:06:26 -0800 Subject: [PATCH 4/4] Fix invalid cast with end script tag containing code (#193) --- .../Legacy/HtmlMarkupParser.cs | 8 ++-- .../CodeGenerationIntegrationTest.cs | 12 ++++++ .../Legacy/HtmlTagsTest.cs | 6 +++ .../CodeGenerationIntegrationTest/Tags.cshtml | 5 +++ .../Tags_DesignTime.codegen.cs | 38 +++++++++++++++++++ .../Tags_DesignTime.diagnostics.txt | 1 + .../Tags_DesignTime.ir.txt | 30 +++++++++++++++ .../Tags_DesignTime.mappings.txt | 19 ++++++++++ .../Tags_Runtime.codegen.cs | 21 ++++++++++ .../Tags_Runtime.diagnostics.txt | 1 + .../Tags_Runtime.ir.txt | 27 +++++++++++++ .../HtmlTagsTest/ScriptTag_Invalid.cspans.txt | 10 +++++ .../HtmlTagsTest/ScriptTag_Invalid.stree.txt | 38 +++++++++++++++++++ 13 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.codegen.cs create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.diagnostics.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.ir.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.mappings.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_Runtime.codegen.cs create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_Runtime.diagnostics.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_Runtime.ir.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/HtmlTagsTest/ScriptTag_Invalid.cspans.txt create mode 100644 src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/HtmlTagsTest/ScriptTag_Invalid.stree.txt diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs index 02c7eec22d3..e66b9c531cd 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs @@ -1357,8 +1357,6 @@ private void ParseJavascriptAndEndScriptTag(in SyntaxListBuilder 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 @@ private void ParseJavascriptAndEndScriptTag(in SyntaxListBuilder }"); + } + [Fact] public void VoidElementFollowedByContent() { diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml new file mode 100644 index 00000000000..f32934b44a0 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml @@ -0,0 +1,5 @@ +@{ + +} + + +#pragma warning disable 1591 +namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles +{ + #line hidden + public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_DesignTime + { + #pragma warning disable 219 + private void __RazorDirectiveTokenHelpers__() { + } + #pragma warning restore 219 + #pragma warning disable 0414 + private static System.Object __o = null; + #pragma warning restore 0414 + #pragma warning disable 1998 + public async System.Threading.Tasks.Task ExecuteAsync() + { +#line 1 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml" + + + +#line default +#line hidden +#line 2 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml" + + +#line default +#line hidden +#line 2 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml" + + +#line default +#line hidden + } + #pragma warning restore 1998 + } +} +#pragma warning restore 1591 diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.diagnostics.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.diagnostics.txt new file mode 100644 index 00000000000..b67fb8bdc0d --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.diagnostics.txt @@ -0,0 +1 @@ +TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml(5,11): Error RZ1024: End of file or an unexpected character was reached before the "script" tag could be parsed. Elements inside markup blocks must be complete. They must either be self-closing ("
") or have matching end tags ("

Hello

"). If you intended to display a "<" character, use the "<" HTML entity. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.ir.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.ir.txt new file mode 100644 index 00000000000..c06477366a8 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_DesignTime.ir.txt @@ -0,0 +1,30 @@ +Document - + NamespaceDeclaration - - Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles + ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_DesignTime - - + DesignTimeDirective - + CSharpCode - + IntermediateToken - - CSharp - #pragma warning disable 0414 + CSharpCode - + IntermediateToken - - CSharp - private static System.Object __o = null; + CSharpCode - + IntermediateToken - - CSharp - #pragma warning restore 0414 + MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync + CSharpCode - (2:0,2 [6] Tags.cshtml) + IntermediateToken - (2:0,2 [6] Tags.cshtml) - CSharp - \n + HtmlContent - (8:1,4 [31] Tags.cshtml) + IntermediateToken - (8:1,4 [7] Tags.cshtml) - Html - + CSharpCode - (59:1,55 [2] Tags.cshtml) + IntermediateToken - (59:1,55 [2] Tags.cshtml) - CSharp - \n + HtmlContent - (64:3,0 [20] Tags.cshtml) + IntermediateToken - (64:3,0 [2] Tags.cshtml) - Html - \n + IntermediateToken - (66:4,0 [7] Tags.cshtml) - Html - +#pragma warning disable 1591 +[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_Runtime), @"default", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml")] +namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles +{ + #line hidden + [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"42fb758ed87c3709cfb3f3aeff7a9cf8d242b74c", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml")] + public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_Runtime + { + #pragma warning disable 1998 + public async System.Threading.Tasks.Task ExecuteAsync() + { + WriteLiteral(" \r\n"); + WriteLiteral("\r\n") or have matching end tags ("

Hello

"). If you intended to display a "<" character, use the "<" HTML entity. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_Runtime.ir.txt b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_Runtime.ir.txt new file mode 100644 index 00000000000..38757651f04 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags_Runtime.ir.txt @@ -0,0 +1,27 @@ +Document - + RazorCompiledItemAttribute - + NamespaceDeclaration - - Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles + RazorSourceChecksumAttribute - + ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_Runtime - - + MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync + CSharpCode - (2:0,2 [2] Tags.cshtml) + IntermediateToken - (2:0,2 [2] Tags.cshtml) - CSharp - \n + HtmlContent - (4:1,0 [35] Tags.cshtml) + IntermediateToken - (4:1,0 [4] Tags.cshtml) - Html - + IntermediateToken - (8:1,4 [7] Tags.cshtml) - Html - + IntermediateToken - (59:1,55 [2] Tags.cshtml) - Html - \n + CSharpCode - (61:2,0 [0] Tags.cshtml) + IntermediateToken - (61:2,0 [0] Tags.cshtml) - CSharp - + HtmlContent - (64:3,0 [20] Tags.cshtml) + IntermediateToken - (64:3,0 [2] Tags.cshtml) - Html - \n + IntermediateToken - (66:4,0 [7] Tags.cshtml) - Html - }] + MarkupBlock - [0..25)::25 + MarkupTextLiteral - [0..0)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + CSharpCodeBlock - [0..25)::25 + CSharpStatement - [0..25)::25 + CSharpTransition - [0..1)::1 - Gen - SpanEditHandler;Accepts:None + Transition;[@]; + CSharpStatementBody - [1..25)::24 + RazorMetaCode - [1..2)::1 - Gen - SpanEditHandler;Accepts:None + LeftBrace;[{]; + CSharpCodeBlock - [2..24)::22 + MarkupBlock - [2..24)::22 + MarkupTextLiteral - [2..3)::1 - [ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + MarkupElement - [3..23)::20 + MarkupStartTag - [3..11)::8 - [] - Gen - SpanEditHandler;Accepts:None + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[script]; + MarkupMiscAttributeContent - [19..22)::3 + MarkupTextLiteral - [19..22)::3 - [ @ ] - Gen - SpanEditHandler;Accepts:Any + Whitespace;[ ]; + Transition;[@]; + Whitespace;[ ]; + CloseAngle;[>]; + MarkupTextLiteral - [23..24)::1 - [ ] - Gen - SpanEditHandler;Accepts:None + Whitespace;[ ]; + CSharpStatementLiteral - [24..24)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[]; + RazorMetaCode - [24..25)::1 - Gen - SpanEditHandler;Accepts:None + RightBrace;[}]; + MarkupTextLiteral - [25..25)::0 - [] - Gen - SpanEditHandler;Accepts:Any + Marker;[];