diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs index 014587d2c1..b46fb85612 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/PageDirective.cs @@ -67,15 +67,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions private static string TrimQuotes(string content) { - if (content.Length >= 2 && - content.StartsWith("\"", StringComparison.Ordinal) && - content.EndsWith("\"", StringComparison.Ordinal)) - { - return content.Substring(1, content.Length - 2); - } + Debug.Assert(content.Length >= 2); + Debug.Assert(content.StartsWith("\"", StringComparison.Ordinal)); + Debug.Assert(content.EndsWith("\"", StringComparison.Ordinal)); - // The extensible directive system handed us invalid content. There's already an error logged. - return null; + return content.Substring(1, content.Length - 2); } private class Visitor : RazorIRNodeWalker diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs index ca206343a5..67c72f9712 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs @@ -1614,18 +1614,17 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy break; case DirectiveTokenKind.String: - if (At(CSharpSymbolType.StringLiteral)) + if (At(CSharpSymbolType.StringLiteral) && CurrentSymbol.Errors.Count == 0) { AcceptAndMoveNext(); } else { - var startLocation = CurrentStart; - AcceptUntil(CSharpSymbolType.WhiteSpace, CSharpSymbolType.NewLine); Context.ErrorSink.OnError( - startLocation, + CurrentStart, LegacyResources.FormatDirectiveExpectsQuotedStringLiteral(descriptor.Name), - Span.End.AbsoluteIndex - Span.Start.AbsoluteIndex); + CurrentSymbol.Content.Length); + return; } break; } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.codegen.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.codegen.cs index 26ffa461cd..e7dac6c06f 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.codegen.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.codegen.cs @@ -13,10 +13,6 @@ namespace AspNetCore { #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { - ((System.Action)(() => { -global::System.Object __typeHelper = "foo; - } - ))(); } #pragma warning restore 219 private static System.Object __o = null; diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.ir.txt b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.ir.txt index a4788b3a3c..56f9d162c4 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.ir.txt +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_DesignTime.ir.txt @@ -24,12 +24,11 @@ Document - DirectiveToken - (586:11,14 [96] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor DirectiveToken - (698:12,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor DirectiveToken - (801:13,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor - DirectiveToken - (6:0,6 [4] MalformedPageDirective.cshtml) - "foo CSharpStatement - RazorIRToken - - CSharp - private static System.Object __o = null; RazorMethodDeclaration - - public - async, override - global::System.Threading.Tasks.Task - ExecuteAsync - HtmlContent - (12:1,0 [43] MalformedPageDirective.cshtml) - RazorIRToken - (12:1,0 [2] MalformedPageDirective.cshtml) - Html - \n + HtmlContent - (6:0,6 [49] MalformedPageDirective.cshtml) + RazorIRToken - (6:0,6 [8] MalformedPageDirective.cshtml) - Html - "foo\n\n RazorIRToken - (14:2,0 [4] MalformedPageDirective.cshtml) - Html -
We are awesome.
"); + BeginContext(6, 49, true); + WriteLiteral("\"foo\r\n\r\nWe are awesome.
"); EndContext(); } #pragma warning restore 1998 diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_Runtime.ir.txt b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_Runtime.ir.txt index 4553832fcd..5718255912 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_Runtime.ir.txt +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/MalformedPageDirective_Runtime.ir.txt @@ -11,9 +11,9 @@ Document - ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective_cshtml - global::Microsoft.AspNetCore.Mvc.RazorPages.Page - RazorMethodDeclaration - - public - async, override - global::System.Threading.Tasks.Task - ExecuteAsync CSharpStatement - - RazorIRToken - - CSharp - BeginContext(12, 43, true); - HtmlContent - (12:1,0 [43] MalformedPageDirective.cshtml) - RazorIRToken - (12:1,0 [2] MalformedPageDirective.cshtml) - Html - \n + RazorIRToken - - CSharp - BeginContext(6, 49, true); + HtmlContent - (6:0,6 [49] MalformedPageDirective.cshtml) + RazorIRToken - (6:0,6 [8] MalformedPageDirective.cshtml) - Html - "foo\n\n RazorIRToken - (14:2,0 [4] MalformedPageDirective.cshtml) - Html -