diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs index 6acb3d4bf4d3..49d5f463f7c2 100644 --- a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Linq; @@ -525,6 +525,90 @@ public void RazorPageWithNoLeadingPageDirective_Runtime() var diagnotics = compiled.CodeDocument.GetCSharpDocument().Diagnostics; Assert.Equal("RZ3906", Assert.Single(diagnotics).Id); } + + [Fact] + public void RazorPage_WithCssScope() + { + // Arrange + AddCSharpSyntaxTree($@" +[{typeof(HtmlTargetElementAttribute).FullName}({"\"all\""})] +public class AllTagHelper : {typeof(TagHelper).FullName} +{{ + public string Bar {{ get; set; }} +}} + +[{typeof(HtmlTargetElementAttribute).FullName}({"\"form\""})] +public class FormTagHelper : {typeof(TagHelper).FullName} +{{ +}} +"); + + // Act + // This test case attempts to use all syntaxes that might interact with auto-generated attributes + var generated = CompileToCSharp(@"@page +@addTagHelper *, AppCode +@{ + ViewData[""Title""] = ""Home page""; +} +
+

Welcome

+

Learn about building Web apps with ASP.NET Core.

+
+ +
+ +
+", cssScope: "TestCssScope"); + + // Assert + var intermediate = generated.CodeDocument.GetDocumentIntermediateNode(); + var csharp = generated.CodeDocument.GetCSharpDocument(); + AssertDocumentNodeMatchesBaseline(intermediate); + AssertCSharpDocumentMatchesBaseline(csharp); + CompileToAssembly(generated); + } + + [Fact] + public void RazorView_WithCssScope() + { + // Arrange + AddCSharpSyntaxTree($@" +[{typeof(HtmlTargetElementAttribute).FullName}({"\"all\""})] +public class AllTagHelper : {typeof(TagHelper).FullName} +{{ + public string Bar {{ get; set; }} +}} + +[{typeof(HtmlTargetElementAttribute).FullName}({"\"form\""})] +public class FormTagHelper : {typeof(TagHelper).FullName} +{{ +}} +"); + + // Act + // This test case attempts to use all syntaxes that might interact with auto-generated attributes + var generated = CompileToCSharp(@"@addTagHelper *, AppCode +@{ + ViewData[""Title""] = ""Home page""; +} +
+

Welcome

+

Learn about building Web apps with ASP.NET Core.

+
+ +
+ +
+", cssScope: "TestCssScope"); + + // Assert + var intermediate = generated.CodeDocument.GetDocumentIntermediateNode(); + var csharp = generated.CodeDocument.GetCSharpDocument(); + AssertDocumentNodeMatchesBaseline(intermediate); + AssertCSharpDocumentMatchesBaseline(csharp); + CompileToAssembly(generated); + } + #endregion #region DesignTime diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPage_WithCssScope.codegen.cs b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPage_WithCssScope.codegen.cs new file mode 100644 index 000000000000..47c51e7662e3 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPage_WithCssScope.codegen.cs @@ -0,0 +1,102 @@ +#pragma checksum "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "49d2096aaab8b6d729072d0a3bd7f0417de686ed" +// +#pragma warning disable 1591 +[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test), @"mvc.1.0.razor-page", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/test.cshtml")] +namespace AspNetCore +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.Rendering; + using Microsoft.AspNetCore.Mvc.ViewFeatures; + [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"49d2096aaab8b6d729072d0a3bd7f0417de686ed", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/test.cshtml")] + public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test : global::Microsoft.AspNetCore.Mvc.RazorPages.Page + { + private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_0 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("Bar", "Foo", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_1 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("asp-route", new global::Microsoft.AspNetCore.Html.HtmlString("register"), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_2 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("method", new global::Microsoft.AspNetCore.Html.HtmlString("post"), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + #line hidden + #pragma warning disable 0649 + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext; + #pragma warning restore 0649 + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner(); + #pragma warning disable 0169 + private string __tagHelperStringValueBuffer; + #pragma warning restore 0169 + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __backed__tagHelperScopeManager = null; + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager + { + get + { + if (__backed__tagHelperScopeManager == null) + { + __backed__tagHelperScopeManager = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager(StartTagHelperWritingScope, EndTagHelperWritingScope); + } + return __backed__tagHelperScopeManager; + } + } + private global::AllTagHelper __AllTagHelper; + private global::FormTagHelper __FormTagHelper; + #pragma warning disable 1998 + public async override global::System.Threading.Tasks.Task ExecuteAsync() + { +#nullable restore +#line 3 "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml" + + ViewData["Title"] = "Home page"; + +#line default +#line hidden +#nullable disable + WriteLiteral("
\r\n

Welcome

\r\n

Learn about building Web apps with ASP.NET Core.

\r\n
\r\n"); + __tagHelperExecutionContext = __tagHelperScopeManager.Begin("all", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "test", async() => { + } + ); + __AllTagHelper = CreateTagHelper(); + __tagHelperExecutionContext.Add(__AllTagHelper); + __AllTagHelper.Bar = (string)__tagHelperAttribute_0.Value; + __tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_0); + await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); + if (!__tagHelperExecutionContext.Output.IsContentModified) + { + await __tagHelperExecutionContext.SetOutputContentAsync(); + } + Write(__tagHelperExecutionContext.Output); + __tagHelperExecutionContext = __tagHelperScopeManager.End(); + WriteLiteral("\r\n"); + __tagHelperExecutionContext = __tagHelperScopeManager.Begin("form", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "test", async() => { + WriteLiteral("\r\n \r\n"); + } + ); + __FormTagHelper = CreateTagHelper(); + __tagHelperExecutionContext.Add(__FormTagHelper); + __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_1); + __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_2); + await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); + if (!__tagHelperExecutionContext.Output.IsContentModified) + { + await __tagHelperExecutionContext.SetOutputContentAsync(); + } + Write(__tagHelperExecutionContext.Output); + __tagHelperExecutionContext = __tagHelperScopeManager.End(); + WriteLiteral("\r\n"); + } + #pragma warning restore 1998 + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData; + public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model; + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPage_WithCssScope.ir.txt b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPage_WithCssScope.ir.txt new file mode 100644 index 000000000000..1422496c1a2a --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPage_WithCssScope.ir.txt @@ -0,0 +1,82 @@ +Document - + RazorCompiledItemAttribute - + NamespaceDeclaration - - AspNetCore + UsingDirective - (1:0,1 [14] ) - System + UsingDirective - (16:1,1 [34] ) - System.Collections.Generic + UsingDirective - (51:2,1 [19] ) - System.Linq + UsingDirective - (71:3,1 [30] ) - System.Threading.Tasks + UsingDirective - (102:4,1 [32] ) - Microsoft.AspNetCore.Mvc + UsingDirective - (135:5,1 [42] ) - Microsoft.AspNetCore.Mvc.Rendering + UsingDirective - (178:6,1 [45] ) - Microsoft.AspNetCore.Mvc.ViewFeatures + RazorSourceChecksumAttribute - + ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test - global::Microsoft.AspNetCore.Mvc.RazorPages.Page - + PreallocatedTagHelperPropertyValue - - __tagHelperAttribute_0 - Bar - Foo - HtmlAttributeValueStyle.DoubleQuotes + PreallocatedTagHelperHtmlAttributeValue - - __tagHelperAttribute_1 - asp-route - register - HtmlAttributeValueStyle.DoubleQuotes + PreallocatedTagHelperHtmlAttributeValue - - __tagHelperAttribute_2 - method - post - HtmlAttributeValueStyle.DoubleQuotes + DefaultTagHelperRuntime - + FieldDeclaration - - private - global::AllTagHelper - __AllTagHelper + FieldDeclaration - - private - global::FormTagHelper - __FormTagHelper + MethodDeclaration - - public async override - global::System.Threading.Tasks.Task - ExecuteAsync + CSharpCode - (35:2,2 [40] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (35:2,2 [40] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - CSharp - \n ViewData["Title"] = "Home page";\n + HtmlContent - (78:5,0 [191] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (78:5,0 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -
+ LazyIntermediateToken - (103:5,25 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (109:6,4 [3] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (131:6,26 [7] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - Welcome + LazyIntermediateToken - (138:6,33 [5] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (143:6,38 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (149:7,4 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (152:7,7 [11] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - Learn about + LazyIntermediateToken - (163:7,18 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - + LazyIntermediateToken - (214:7,69 [36] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - building Web apps with ASP.NET Core + LazyIntermediateToken - (250:7,105 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - + LazyIntermediateToken - (254:7,109 [1] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - . + LazyIntermediateToken - (255:7,110 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (259:7,114 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (261:8,0 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -
+ LazyIntermediateToken - (267:8,6 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + TagHelper - (269:9,0 [21] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - all - TagMode.StartTagAndEndTag + DefaultTagHelperBody - + DefaultTagHelperCreate - - AllTagHelper + PreallocatedTagHelperProperty - (279:9,10 [3] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - __tagHelperAttribute_0 - Bar - Bar + DefaultTagHelperExecute - + HtmlContent - (290:9,21 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (290:9,21 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + TagHelper - (292:10,0 [84] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - form - TagMode.StartTagAndEndTag + DefaultTagHelperBody - + HtmlContent - (333:10,41 [36] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (333:10,41 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (337:11,2 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - + LazyIntermediateToken - (367:11,32 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + DefaultTagHelperCreate - - FormTagHelper + PreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_1 + PreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_2 + DefaultTagHelperExecute - + HtmlContent - (376:12,7 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (376:12,7 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + Inject - + Inject - + Inject - + Inject - + Inject - + CSharpCode - + IntermediateToken - - CSharp - public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData; + CSharpCode - + IntermediateToken - - CSharp - public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model; diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorView_WithCssScope.codegen.cs b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorView_WithCssScope.codegen.cs new file mode 100644 index 000000000000..e51167c1f7e0 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorView_WithCssScope.codegen.cs @@ -0,0 +1,100 @@ +#pragma checksum "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "1df3059703826ccbcf3c807ea436c462bbd7c760" +// +#pragma warning disable 1591 +[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test), @"mvc.1.0.view", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/test.cshtml")] +namespace AspNetCore +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.Rendering; + using Microsoft.AspNetCore.Mvc.ViewFeatures; + [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"1df3059703826ccbcf3c807ea436c462bbd7c760", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/test.cshtml")] + public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage + { + private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_0 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("Bar", "Foo", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_1 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("asp-route", new global::Microsoft.AspNetCore.Html.HtmlString("register"), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_2 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("method", new global::Microsoft.AspNetCore.Html.HtmlString("post"), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); + #line hidden + #pragma warning disable 0649 + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext; + #pragma warning restore 0649 + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner(); + #pragma warning disable 0169 + private string __tagHelperStringValueBuffer; + #pragma warning restore 0169 + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __backed__tagHelperScopeManager = null; + private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager + { + get + { + if (__backed__tagHelperScopeManager == null) + { + __backed__tagHelperScopeManager = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager(StartTagHelperWritingScope, EndTagHelperWritingScope); + } + return __backed__tagHelperScopeManager; + } + } + private global::AllTagHelper __AllTagHelper; + private global::FormTagHelper __FormTagHelper; + #pragma warning disable 1998 + public async override global::System.Threading.Tasks.Task ExecuteAsync() + { +#nullable restore +#line 2 "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml" + + ViewData["Title"] = "Home page"; + +#line default +#line hidden +#nullable disable + WriteLiteral("
\r\n

Welcome

\r\n

Learn about building Web apps with ASP.NET Core.

\r\n
\r\n"); + __tagHelperExecutionContext = __tagHelperScopeManager.Begin("all", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "test", async() => { + } + ); + __AllTagHelper = CreateTagHelper(); + __tagHelperExecutionContext.Add(__AllTagHelper); + __AllTagHelper.Bar = (string)__tagHelperAttribute_0.Value; + __tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_0); + await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); + if (!__tagHelperExecutionContext.Output.IsContentModified) + { + await __tagHelperExecutionContext.SetOutputContentAsync(); + } + Write(__tagHelperExecutionContext.Output); + __tagHelperExecutionContext = __tagHelperScopeManager.End(); + WriteLiteral("\r\n"); + __tagHelperExecutionContext = __tagHelperScopeManager.Begin("form", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "test", async() => { + WriteLiteral("\r\n \r\n"); + } + ); + __FormTagHelper = CreateTagHelper(); + __tagHelperExecutionContext.Add(__FormTagHelper); + __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_1); + __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_2); + await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); + if (!__tagHelperExecutionContext.Output.IsContentModified) + { + await __tagHelperExecutionContext.SetOutputContentAsync(); + } + Write(__tagHelperExecutionContext.Output); + __tagHelperExecutionContext = __tagHelperScopeManager.End(); + WriteLiteral("\r\n"); + } + #pragma warning restore 1998 + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } + [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] + public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } + } +} +#pragma warning restore 1591 diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorView_WithCssScope.ir.txt b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorView_WithCssScope.ir.txt new file mode 100644 index 000000000000..ceb10cb442a6 --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorView_WithCssScope.ir.txt @@ -0,0 +1,78 @@ +Document - + RazorCompiledItemAttribute - + NamespaceDeclaration - - AspNetCore + UsingDirective - (1:0,1 [14] ) - System + UsingDirective - (16:1,1 [34] ) - System.Collections.Generic + UsingDirective - (51:2,1 [19] ) - System.Linq + UsingDirective - (71:3,1 [30] ) - System.Threading.Tasks + UsingDirective - (102:4,1 [32] ) - Microsoft.AspNetCore.Mvc + UsingDirective - (135:5,1 [42] ) - Microsoft.AspNetCore.Mvc.Rendering + UsingDirective - (178:6,1 [45] ) - Microsoft.AspNetCore.Mvc.ViewFeatures + RazorSourceChecksumAttribute - + ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test - global::Microsoft.AspNetCore.Mvc.Razor.RazorPage - + PreallocatedTagHelperPropertyValue - - __tagHelperAttribute_0 - Bar - Foo - HtmlAttributeValueStyle.DoubleQuotes + PreallocatedTagHelperHtmlAttributeValue - - __tagHelperAttribute_1 - asp-route - register - HtmlAttributeValueStyle.DoubleQuotes + PreallocatedTagHelperHtmlAttributeValue - - __tagHelperAttribute_2 - method - post - HtmlAttributeValueStyle.DoubleQuotes + DefaultTagHelperRuntime - + FieldDeclaration - - private - global::AllTagHelper - __AllTagHelper + FieldDeclaration - - private - global::FormTagHelper - __FormTagHelper + MethodDeclaration - - public async override - global::System.Threading.Tasks.Task - ExecuteAsync + CSharpCode - (28:1,2 [40] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (28:1,2 [40] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - CSharp - \n ViewData["Title"] = "Home page";\n + HtmlContent - (71:4,0 [191] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (71:4,0 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -
+ LazyIntermediateToken - (96:4,25 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (102:5,4 [3] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (124:5,26 [7] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - Welcome + LazyIntermediateToken - (131:5,33 [5] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (136:5,38 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (142:6,4 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (145:6,7 [11] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - Learn about + LazyIntermediateToken - (156:6,18 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - + LazyIntermediateToken - (207:6,69 [36] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - building Web apps with ASP.NET Core + LazyIntermediateToken - (243:6,105 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - + LazyIntermediateToken - (247:6,109 [1] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - . + LazyIntermediateToken - (248:6,110 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -

+ LazyIntermediateToken - (252:6,114 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (254:7,0 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html -
+ LazyIntermediateToken - (260:7,6 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + TagHelper - (262:8,0 [21] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - all - TagMode.StartTagAndEndTag + DefaultTagHelperBody - + DefaultTagHelperCreate - - AllTagHelper + PreallocatedTagHelperProperty - (272:8,10 [3] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - __tagHelperAttribute_0 - Bar - Bar + DefaultTagHelperExecute - + HtmlContent - (283:8,21 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (283:8,21 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + TagHelper - (285:9,0 [84] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - form - TagMode.StartTagAndEndTag + DefaultTagHelperBody - + HtmlContent - (326:9,41 [36] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (326:9,41 [4] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + LazyIntermediateToken - (330:10,2 [6] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - + LazyIntermediateToken - (360:10,32 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + DefaultTagHelperCreate - - FormTagHelper + PreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_1 + PreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_2 + DefaultTagHelperExecute - + HtmlContent - (369:11,7 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) + LazyIntermediateToken - (369:11,7 [2] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml) - Html - \n + Inject - + Inject - + Inject - + Inject - + Inject - diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/ViewCssScopePass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/ViewCssScopePass.cs new file mode 100644 index 000000000000..c17844a438bb --- /dev/null +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/ViewCssScopePass.cs @@ -0,0 +1,59 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Razor.Language.Intermediate; + +namespace Microsoft.AspNetCore.Razor.Language.Extensions +{ + internal class ViewCssScopePass : IntermediateNodePassBase, IRazorOptimizationPass + { + // Runs after taghelpers are bound + public override int Order => 110; + + protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) + { + var cssScope = codeDocument.GetCssScope(); + if (string.IsNullOrEmpty(cssScope)) + { + return; + } + + if (!string.Equals(documentNode.DocumentKind, "mvc.1.0.view", StringComparison.Ordinal) && + !string.Equals(documentNode.DocumentKind, "mvc.1.0.razor-page", StringComparison.Ordinal)) + { + return; + } + + var scopeWithSeparator = " " + cssScope; + var nodes = documentNode.FindDescendantNodes(); + for (var i = 0; i < nodes.Count; i++) + { + ProcessElement(nodes[i], scopeWithSeparator); + } + } + + private void ProcessElement(HtmlContentIntermediateNode node, string cssScope) + { + // Add a minimized attribute whose name is simply the CSS scope + for (var i = 0; i < node.Children.Count; i++) + { + var child = node.Children[i]; + if (child is IntermediateToken token && token.IsHtml) + { + var content = token.Content; + if (content.StartsWith("<", StringComparison.Ordinal) && !content.StartsWith("= 0) + { + builder.Features.Add(new ViewCssScopePass()); + } + if (configuration.LanguageVersion.CompareTo(RazorLanguageVersion.Version_3_0) >= 0) { FunctionsDirective.Register(builder); @@ -228,7 +233,7 @@ private static void AddComponentFeatures(RazorProjectEngineBuilder builder, Razo // Directive Classifier builder.Features.Add(new ComponentWhitespacePass()); - + // Optimization builder.Features.Add(new ComponentComplexAttributeContentPass()); builder.Features.Add(new ComponentLoweringPass()); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorProjectEngineTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorProjectEngineTest.cs index d483472fa4ac..6b21818edca8 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorProjectEngineTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorProjectEngineTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Linq; @@ -83,7 +83,8 @@ private static void AssertDefaultFeatures(RazorProjectEngine engine) feature => Assert.IsType(feature), feature => Assert.IsType(feature), feature => Assert.IsType(feature), - feature => Assert.IsType(feature)); + feature => Assert.IsType(feature), + feature => Assert.IsType(feature)); } private static void AssertDefaultDirectives(RazorProjectEngine engine) 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 02024ecb1d49..2b724538a808 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 @@ -133,7 +133,7 @@ protected RazorProjectItem AddProjectItemFromText(string text, string filePath = return projectItem; } - private RazorProjectItem CreateProjectItemFromText(string text, string filePath) + private RazorProjectItem CreateProjectItemFromText(string text, string filePath, string? cssScope = null) { // Consider the file path to be relative to the 'FileName' of the test. var workingDirectory = Path.GetDirectoryName(FileName); @@ -158,7 +158,8 @@ private RazorProjectItem CreateProjectItemFromText(string text, string filePath) basePath: basePath, filePath: filePath, physicalPath: physicalPath, - relativePhysicalPath: relativePhysicalPath) + relativePhysicalPath: relativePhysicalPath, + cssScope: cssScope) { Content = text, }; @@ -212,9 +213,9 @@ protected RazorProjectItem CreateProjectItemFromFile(string? filePath = null, st return projectItem; } - protected CompiledCSharpCode CompileToCSharp(string text, string path = "test.cshtml", bool? designTime = null) + protected CompiledCSharpCode CompileToCSharp(string text, string path = "test.cshtml", bool? designTime = null, string? cssScope = null) { - var projectItem = CreateProjectItemFromText(text, path); + var projectItem = CreateProjectItemFromText(text, path, cssScope); return CompileToCSharp(projectItem, designTime); }