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..52dd998120d0 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.EndsWith("'", StringComparison.Ordinal) ? "'" : "\""; + + 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/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/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/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 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