From 66968383a87441df90c2ca894666a98e497eefa8 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 15 Jun 2021 07:29:11 -0700 Subject: [PATCH 1/6] Preserve quote style when rendering markup content --- .../Components/ComponentMarkupBlockPass.cs | 9 ++++-- .../ComponentCodeGenerationTestBase.cs | 3 ++ .../TestComponent.ir.txt | 29 +++++++++++++++++++ .../TestComponent.codegen.cs | 2 +- .../TestComponent.ir.txt | 2 +- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs index a800a43122d8..102254ff6794 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs @@ -327,12 +327,17 @@ public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) return; } - Builder.Append("=\""); + // We examine the node.Prefix (e.g. " onfocus='" or " on focus=\"") + // to preserve the quote type that is used in the original markup. + var quoteType = node.Prefix.Contains("'") ? "'" : "\""; + + Builder.Append('='); + Builder.Append(quoteType); // Visit Children base.VisitDefault(node); - Builder.Append('"'); + Builder.Append(quoteType); } public override void VisitHtmlAttributeValue(HtmlAttributeValueIntermediateNode node) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index 411125b5a8d6..7fcef3d42f69 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -6674,6 +6674,9 @@ public void EventHandlerTagHelper_EscapeQuotes() // Act var generated = CompileToCSharp(@" + + +

"); // Assert diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt index 6a821cdca176..7eb5953add0b 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt @@ -20,3 +20,32 @@ Document - LazyIntermediateToken - (16:0,16 [14] x:\dir\subdir\Test\TestComponent.cshtml) - Html - alert("Test"); HtmlContent - (34:0,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) LazyIntermediateToken - (34:0,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + MarkupElement - (36:1,0 [34] x:\dir\subdir\Test\TestComponent.cshtml) - input + HtmlAttribute - (42:1,6 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus=" - " + HtmlAttributeValue - (52:1,16 [6] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (52:1,16 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Html - alert( + HtmlAttribute - (59:1,23 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Test");" - + HtmlContent - (70:1,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (70:1,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + MarkupElement - (72:2,0 [34] x:\dir\subdir\Test\TestComponent.cshtml) - input + HtmlAttribute - (78:2,6 [25] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus=" - " + HtmlAttributeValue - (88:2,16 [14] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (88:2,16 [14] x:\dir\subdir\Test\TestComponent.cshtml) - Html - alert('Test'); + HtmlContent - (106:2,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (106:2,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n + MarkupElement - (108:3,0 [91] x:\dir\subdir\Test\TestComponent.cshtml) - p + HtmlAttribute - (110:3,2 [84] x:\dir\subdir\Test\TestComponent.cshtml) - data-options=' - ' + HtmlAttributeValue - (125:3,17 [11] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (125:3,17 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Html - {direction: + HtmlAttributeValue - (136:3,28 [11] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (137:3,29 [10] x:\dir\subdir\Test\TestComponent.cshtml) - Html - "fromtop", + HtmlAttributeValue - (147:3,39 [20] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (148:3,40 [19] x:\dir\subdir\Test\TestComponent.cshtml) - Html - animation_duration: + HtmlAttributeValue - (167:3,59 [4] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (168:3,60 [3] x:\dir\subdir\Test\TestComponent.cshtml) - Html - 25, + HtmlAttributeValue - (171:3,63 [11] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (172:3,64 [10] x:\dir\subdir\Test\TestComponent.cshtml) - Html - direction: + HtmlAttributeValue - (182:3,74 [11] x:\dir\subdir\Test\TestComponent.cshtml) - + LazyIntermediateToken - (183:3,75 [10] x:\dir\subdir\Test\TestComponent.cshtml) - Html - "reverse"} + HtmlContent - (199:3,91 [2] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (199:3,91 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.codegen.cs index 469d7009f3a5..534256b97545 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.codegen.cs @@ -13,7 +13,7 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa #pragma warning disable 1998 protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) { - __builder.AddMarkupContent(0, ""); + __builder.AddMarkupContent(0, "\r\n\r\n\r\n

"); } #pragma warning restore 1998 } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt index 8d61935237e9..55a1290d3f2e 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandlerTagHelper_EscapeQuotes/TestComponent.ir.txt @@ -7,4 +7,4 @@ Document - UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - MethodDeclaration - - protected override - void - BuildRenderTree - MarkupBlock - - + MarkupBlock - - \n\n\n

From c621ae61a6e1526c4b0e8cfb080aa82ac0a99ba9 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 15 Jun 2021 07:45:18 -0700 Subject: [PATCH 2/6] Replace Contains check with EndsWith check --- .../src/Components/ComponentMarkupBlockPass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs index 102254ff6794..da4f907694e4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs @@ -329,7 +329,7 @@ public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) // We examine the node.Prefix (e.g. " onfocus='" or " on focus=\"") // to preserve the quote type that is used in the original markup. - var quoteType = node.Prefix.Contains("'") ? "'" : "\""; + var quoteType = node.Prefix.EndsWith("'") ? "'" : "\""; Builder.Append('='); Builder.Append(quoteType); From fa1f8ec1dcfd1924440760e28500d2a5e7e60e9d Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 15 Jun 2021 07:58:35 -0700 Subject: [PATCH 3/6] Provide StringComparison arg to appease the linter --- .../src/Components/ComponentMarkupBlockPass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs index da4f907694e4..52dd998120d0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs @@ -329,7 +329,7 @@ public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) // We examine the node.Prefix (e.g. " onfocus='" or " on focus=\"") // to preserve the quote type that is used in the original markup. - var quoteType = node.Prefix.EndsWith("'") ? "'" : "\""; + var quoteType = node.Prefix.EndsWith("'", StringComparison.Ordinal) ? "'" : "\""; Builder.Append('='); Builder.Append(quoteType); From fec0c690a2c1256e876bc28490eabaac4287fe84 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 15 Jun 2021 08:06:00 -0700 Subject: [PATCH 4/6] Update src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs Co-authored-by: campersau --- .../src/Components/ComponentMarkupBlockPass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs index 52dd998120d0..12cdfdd57424 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs @@ -329,7 +329,7 @@ public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) // We examine the node.Prefix (e.g. " onfocus='" or " on focus=\"") // to preserve the quote type that is used in the original markup. - var quoteType = node.Prefix.EndsWith("'", StringComparison.Ordinal) ? "'" : "\""; + var quoteType = node.Prefix.EndsWith('\'') ? '\'' : '"'; Builder.Append('='); Builder.Append(quoteType); From 73335166adcb5c5c7dfec82dd8d33ce72f04a238 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 15 Jun 2021 15:36:39 +0000 Subject: [PATCH 5/6] Back to strings since char overload is not supported --- .../src/Components/ComponentMarkupBlockPass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs index 12cdfdd57424..52dd998120d0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs @@ -329,7 +329,7 @@ public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) // We examine the node.Prefix (e.g. " onfocus='" or " on focus=\"") // to preserve the quote type that is used in the original markup. - var quoteType = node.Prefix.EndsWith('\'') ? '\'' : '"'; + var quoteType = node.Prefix.EndsWith("'", StringComparison.Ordinal) ? "'" : "\""; Builder.Append('='); Builder.Append(quoteType); From ed7a288fd7ba5c38946ba733ac87de0c61e52c3e Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 15 Jun 2021 17:11:39 +0000 Subject: [PATCH 6/6] Update test baselines --- .../test/Components/ComponentMarkupBlockPassTest.cs | 4 ++-- .../ChildComponent_WithChildContent/TestComponent.codegen.cs | 2 +- .../ChildComponent_WithChildContent/TestComponent.ir.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs index 6c00e4f32227..d34c246dc927 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs @@ -347,8 +347,8 @@ public void Execute_CanRewriteHtml_OptionWithNoSelectAncestor() "); var expected = NormalizeContent(@" - - "); + + "); var documentNode = Lower(document); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.codegen.cs index 33e0d55d49b6..4523deddce51 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.codegen.cs @@ -17,7 +17,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin __builder.AddAttribute(1, "MyAttr", "abc"); __builder.AddAttribute(2, "ChildContent", (Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => { __builder2.AddContent(3, "Some text"); - __builder2.AddMarkupContent(4, "Nested text"); + __builder2.AddMarkupContent(4, "Nested text"); } )); __builder.CloseComponent(); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.ir.txt index 591e185625c2..140a14ccea36 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/ChildComponent_WithChildContent/TestComponent.ir.txt @@ -11,7 +11,7 @@ Document - ComponentChildContent - - ChildContent - context HtmlContent - (26:0,26 [9] x:\dir\subdir\Test\TestComponent.cshtml) LazyIntermediateToken - (26:0,26 [9] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Some text - MarkupBlock - - Nested text + MarkupBlock - - Nested text ComponentAttribute - (21:0,21 [3] x:\dir\subdir\Test\TestComponent.cshtml) - MyAttr - MyAttr - AttributeStructure.DoubleQuotes HtmlContent - (21:0,21 [3] x:\dir\subdir\Test\TestComponent.cshtml) LazyIntermediateToken - (21:0,21 [3] x:\dir\subdir\Test\TestComponent.cshtml) - Html - abc