Skip to content

Commit 0e8b3ab

Browse files
authored
Revert <script> codegen for Razor 7 and lower (#9603)
* Add tests * Revert `script` tag codegen for Razor 7 and lower
1 parent 5c69a9f commit 0e8b3ab

File tree

12 files changed

+206
-4
lines changed

12 files changed

+206
-4
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentMarkupBlockPass.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ namespace Microsoft.AspNetCore.Razor.Language.Components;
1818
// or quote style.
1919
internal class ComponentMarkupBlockPass : ComponentIntermediateNodePassBase, IRazorOptimizationPass
2020
{
21+
private readonly RazorLanguageVersion _version;
22+
23+
public ComponentMarkupBlockPass(RazorLanguageVersion version)
24+
{
25+
_version = version;
26+
}
27+
2128
// Runs LATE because we want to destroy structure.
2229
//
2330
// We also need to run after ComponentMarkupDiagnosticPass to avoid destroying diagnostics
@@ -39,7 +46,7 @@ protected override void ExecuteCore(
3946
return;
4047
}
4148

42-
var findVisitor = new FindHtmlTreeVisitor();
49+
var findVisitor = new FindHtmlTreeVisitor(_version);
4350
findVisitor.Visit(documentNode);
4451

4552
var trees = findVisitor.Trees;
@@ -126,6 +133,13 @@ protected override void ExecuteCore(
126133
// the a from the list.
127134
private class FindHtmlTreeVisitor : IntermediateNodeWalker
128135
{
136+
private readonly RazorLanguageVersion _version;
137+
138+
public FindHtmlTreeVisitor(RazorLanguageVersion version)
139+
{
140+
_version = version;
141+
}
142+
129143
private bool _foundNonHtml;
130144

131145
public List<IntermediateNodeReference> Trees { get; } = new List<IntermediateNodeReference>();
@@ -152,7 +166,13 @@ public override void VisitMarkupElement(MarkupElementIntermediateNode node)
152166
_foundNonHtml = true;
153167
}
154168

155-
if (string.Equals("option", node.TagName, StringComparison.OrdinalIgnoreCase))
169+
if (_version.CompareTo(RazorLanguageVersion.Version_7_0) <= 0 &&
170+
string.Equals("script", node.TagName, StringComparison.OrdinalIgnoreCase))
171+
{
172+
// Treat script tags as non-HTML in .NET 7 and earlier.
173+
_foundNonHtml = true;
174+
}
175+
else if (string.Equals("option", node.TagName, StringComparison.OrdinalIgnoreCase))
156176
{
157177
// Also, treat <option>...</option> as non-HTML - we don't want it to be coalesced so that we can support setting "selected" attribute on it.
158178
// We only care about option tags that are nested under a select tag.

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorProjectEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ private static void AddComponentFeatures(RazorProjectEngineBuilder builder, Razo
497497
builder.Features.Add(new ComponentGenericTypePass());
498498
builder.Features.Add(new ComponentChildContentDiagnosticPass());
499499
builder.Features.Add(new ComponentMarkupDiagnosticPass());
500-
builder.Features.Add(new ComponentMarkupBlockPass());
500+
builder.Features.Add(new ComponentMarkupBlockPass(razorLanguageVersion));
501501
builder.Features.Add(new ComponentMarkupEncodingPass());
502502
}
503503

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Components/ComponentMarkupBlockPassTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ComponentMarkupBlockPassTest
1616
{
1717
public ComponentMarkupBlockPassTest()
1818
{
19-
Pass = new ComponentMarkupBlockPass();
19+
Pass = new ComponentMarkupBlockPass(RazorLanguageVersion.Latest);
2020
ProjectEngine = RazorProjectEngine.Create(
2121
RazorConfiguration.Default,
2222
RazorProjectFileSystem.Create(Environment.CurrentDirectory),

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10006,6 +10006,30 @@ public void ElementWithUppercaseTagName_CanHideWarningWithBang()
1000610006
CompileToAssembly(generated);
1000710007
}
1000810008

10009+
[IntegrationTestTheory, CombinatorialData, WorkItem("https://github.com/dotnet/razor/issues/9584")]
10010+
public void ScriptTag_Razor8([CombinatorialValues("8.0", "latest")] string langVersion)
10011+
{
10012+
var generated = CompileToCSharp("""
10013+
<script>alert("Hello");</script>
10014+
""",
10015+
configuration: Configuration.WithVersion(RazorLanguageVersion.Parse(langVersion)));
10016+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
10017+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
10018+
CompileToAssembly(generated);
10019+
}
10020+
10021+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/9584")]
10022+
public void ScriptTag_Razor7()
10023+
{
10024+
var generated = CompileToCSharp("""
10025+
<script>alert("Hello");</script>
10026+
""",
10027+
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
10028+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
10029+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
10030+
CompileToAssembly(generated);
10031+
}
10032+
1000910033
#endregion
1001010034

1001110035
#region LinePragmas
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line hidden
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
12+
{
13+
#pragma warning disable 219
14+
private void __RazorDirectiveTokenHelpers__() {
15+
}
16+
#pragma warning restore 219
17+
#pragma warning disable 0414
18+
private static object __o = null;
19+
#pragma warning restore 0414
20+
#pragma warning disable 1998
21+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
22+
{
23+
}
24+
#pragma warning restore 1998
25+
}
26+
}
27+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [20] ) - global::System
4+
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
5+
UsingDirective - (69:3,1 [25] ) - global::System.Linq
6+
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
7+
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
9+
DesignTimeDirective -
10+
CSharpCode -
11+
IntermediateToken - - CSharp - #pragma warning disable 0414
12+
CSharpCode -
13+
IntermediateToken - - CSharp - private static object __o = null;
14+
CSharpCode -
15+
IntermediateToken - - CSharp - #pragma warning restore 0414
16+
MethodDeclaration - - protected override - void - BuildRenderTree
17+
MarkupElement - (0:0,0 [32] x:\dir\subdir\Test\TestComponent.cshtml) - script
18+
HtmlContent - (8:0,8 [15] x:\dir\subdir\Test\TestComponent.cshtml)
19+
LazyIntermediateToken - (8:0,8 [15] x:\dir\subdir\Test\TestComponent.cshtml) - Html - alert("Hello");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line hidden
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
12+
{
13+
#pragma warning disable 219
14+
private void __RazorDirectiveTokenHelpers__() {
15+
}
16+
#pragma warning restore 219
17+
#pragma warning disable 0414
18+
private static object __o = null;
19+
#pragma warning restore 0414
20+
#pragma warning disable 1998
21+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
22+
{
23+
}
24+
#pragma warning restore 1998
25+
}
26+
}
27+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [20] ) - global::System
4+
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
5+
UsingDirective - (69:3,1 [25] ) - global::System.Linq
6+
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
7+
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
9+
DesignTimeDirective -
10+
CSharpCode -
11+
IntermediateToken - - CSharp - #pragma warning disable 0414
12+
CSharpCode -
13+
IntermediateToken - - CSharp - private static object __o = null;
14+
CSharpCode -
15+
IntermediateToken - - CSharp - #pragma warning restore 0414
16+
MethodDeclaration - - protected override - void - BuildRenderTree
17+
MarkupElement - (0:0,0 [32] x:\dir\subdir\Test\TestComponent.cshtml) - script
18+
HtmlContent - (8:0,8 [15] x:\dir\subdir\Test\TestComponent.cshtml)
19+
LazyIntermediateToken - (8:0,8 [15] x:\dir\subdir\Test\TestComponent.cshtml) - Html - alert("Hello");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line hidden
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
12+
{
13+
#pragma warning disable 1998
14+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
15+
{
16+
__builder.OpenElement(0, "script");
17+
__builder.AddContent(1, "alert(\"Hello\");");
18+
__builder.CloseElement();
19+
}
20+
#pragma warning restore 1998
21+
}
22+
}
23+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [22] ) - global::System
4+
UsingDirective - (26:2,1 [42] ) - global::System.Collections.Generic
5+
UsingDirective - (69:3,1 [27] ) - global::System.Linq
6+
UsingDirective - (97:4,1 [38] ) - global::System.Threading.Tasks
7+
UsingDirective - (136:5,1 [47] ) - global::Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
9+
MethodDeclaration - - protected override - void - BuildRenderTree
10+
MarkupElement - (0:0,0 [32] x:\dir\subdir\Test\TestComponent.cshtml) - script
11+
HtmlContent - (8:0,8 [15] x:\dir\subdir\Test\TestComponent.cshtml)
12+
LazyIntermediateToken - (8:0,8 [15] x:\dir\subdir\Test\TestComponent.cshtml) - Html - alert("Hello");

0 commit comments

Comments
 (0)