Skip to content

Commit ebaaa0f

Browse files
Fix minimized attributes when using 5.0 SDK with 3.x target (#25307)
* Write minimized attribute values explicitly for older language version * Add tests * Add baselines for new tests
1 parent 432f9b8 commit ebaaa0f

File tree

18 files changed

+304
-14
lines changed

18 files changed

+304
-14
lines changed

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentRuntimeNodeWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,20 @@ protected override void WriteReferenceCaptureInnards(CodeRenderingContext contex
852852
private void WriteAttribute(CodeRenderingContext context, string key, IList<IntermediateToken> value)
853853
{
854854
BeginWriteAttribute(context, key);
855+
855856
if (value.Count > 0)
856857
{
857858
context.CodeWriter.WriteParameterSeparator();
858859
WriteAttributeValue(context, value);
859860
}
861+
else if (!context.Options.OmitMinimizedComponentAttributeValues)
862+
{
863+
// In version 5+, there's no need to supply a value for a minimized attribute.
864+
// But for older language versions, minimized attributes were represented as "true".
865+
context.CodeWriter.WriteParameterSeparator();
866+
context.CodeWriter.WriteBooleanLiteral(true);
867+
}
868+
860869
context.CodeWriter.WriteEndMethodInvocation();
861870
}
862871

src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
namespace Microsoft.AspNetCore.Razor.Language
@@ -13,7 +13,8 @@ public DefaultRazorCodeGenerationOptions(
1313
bool suppressChecksum,
1414
bool suppressMetadataAttributes,
1515
bool suppressPrimaryMethodBody,
16-
bool suppressNullabilityEnforcement)
16+
bool suppressNullabilityEnforcement,
17+
bool omitMinimizedComponentAttributeValues)
1718
{
1819
IndentWithTabs = indentWithTabs;
1920
IndentSize = indentSize;
@@ -23,6 +24,7 @@ public DefaultRazorCodeGenerationOptions(
2324
SuppressMetadataAttributes = suppressMetadataAttributes;
2425
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
2526
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
27+
OmitMinimizedComponentAttributeValues = omitMinimizedComponentAttributeValues;
2628
}
2729

2830
public override bool DesignTime { get; }
@@ -36,5 +38,7 @@ public DefaultRazorCodeGenerationOptions(
3638
public override bool SuppressChecksum { get; }
3739

3840
public override bool SuppressNullabilityEnforcement { get; }
41+
42+
public override bool OmitMinimizedComponentAttributeValues { get; }
3943
}
4044
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -39,6 +39,8 @@ public DefaultRazorCodeGenerationOptionsBuilder(bool designTime)
3939

4040
public override bool SuppressNullabilityEnforcement { get; set; }
4141

42+
public override bool OmitMinimizedComponentAttributeValues { get; set; }
43+
4244
public override RazorCodeGenerationOptions Build()
4345
{
4446
return new DefaultRazorCodeGenerationOptions(
@@ -49,7 +51,8 @@ public override RazorCodeGenerationOptions Build()
4951
SuppressChecksum,
5052
SuppressMetadataAttributes,
5153
SuppressPrimaryMethodBody,
52-
SuppressNullabilityEnforcement);
54+
SuppressNullabilityEnforcement,
55+
OmitMinimizedComponentAttributeValues);
5356
}
5457

5558
public override void SetDesignTime(bool designTime)

src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -17,7 +17,8 @@ public static RazorCodeGenerationOptions CreateDefault()
1717
rootNamespace: null,
1818
suppressMetadataAttributes: false,
1919
suppressPrimaryMethodBody: false,
20-
suppressNullabilityEnforcement: false);
20+
suppressNullabilityEnforcement: false,
21+
omitMinimizedComponentAttributeValues: false);
2122
}
2223

2324
public static RazorCodeGenerationOptions CreateDesignTimeDefault()
@@ -30,7 +31,8 @@ public static RazorCodeGenerationOptions CreateDesignTimeDefault()
3031
suppressChecksum: false,
3132
suppressMetadataAttributes: true,
3233
suppressPrimaryMethodBody: false,
33-
suppressNullabilityEnforcement: false);
34+
suppressNullabilityEnforcement: false,
35+
omitMinimizedComponentAttributeValues: false);
3436
}
3537

3638
public static RazorCodeGenerationOptions Create(Action<RazorCodeGenerationOptionsBuilder> configure)
@@ -114,5 +116,10 @@ public static RazorCodeGenerationOptions CreateDesignTime(Action<RazorCodeGenera
114116
/// Gets a value that determines if nullability type enforcement should be suppressed for user code.
115117
/// </summary>
116118
public virtual bool SuppressNullabilityEnforcement { get; }
119+
120+
/// <summary>
121+
/// Gets a value that determines if the components code writer may omit values for minimized attributes.
122+
/// </summary>
123+
public virtual bool OmitMinimizedComponentAttributeValues { get; }
117124
}
118125
}

src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
namespace Microsoft.AspNetCore.Razor.Language
@@ -59,6 +59,11 @@ public abstract class RazorCodeGenerationOptionsBuilder
5959
/// </summary>
6060
public virtual bool SuppressNullabilityEnforcement { get; set; }
6161

62+
/// <summary>
63+
/// Gets or sets a value that determines if the components code writer may omit values for minimized attributes.
64+
/// </summary>
65+
public virtual bool OmitMinimizedComponentAttributeValues { get; set; }
66+
6267
public abstract RazorCodeGenerationOptions Build();
6368

6469
public virtual void SetDesignTime(bool designTime)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
99
{
1010
public abstract class ComponentCodeGenerationTestBase : RazorBaselineIntegrationTestBase
1111
{
12+
private RazorConfiguration _configuration;
13+
1214
internal override string FileKind => FileKinds.Component;
1315

1416
internal override bool UseTwoPhaseCompilation => true;
1517

18+
internal override RazorConfiguration Configuration => _configuration ?? base.Configuration;
19+
1620
protected ComponentCodeGenerationTestBase()
1721
: base(generateBaselines: null)
1822
{
@@ -364,6 +368,38 @@ public void MarkupComment_IsNotIncluded()
364368
CompileToAssembly(generated);
365369
}
366370

371+
[Fact]
372+
public void OmitsMinimizedAttributeValueParameter()
373+
{
374+
// Act
375+
var generated = CompileToCSharp(@"
376+
<elem normal-attr=""@(""val"")"" minimized-attr empty-string-atttr=""""></elem>");
377+
378+
// Assert
379+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
380+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
381+
CompileToAssembly(generated);
382+
}
383+
384+
[Fact]
385+
public void IncludesMinimizedAttributeValueParameterBeforeLanguageVersion5()
386+
{
387+
// Arrange
388+
_configuration = RazorConfiguration.Create(
389+
RazorLanguageVersion.Version_3_0,
390+
base.Configuration.ConfigurationName,
391+
base.Configuration.Extensions);
392+
393+
// Act
394+
var generated = CompileToCSharp(@"
395+
<elem normal-attr=""@(""val"")"" minimized-attr empty-string-atttr=""""></elem>");
396+
397+
// Assert
398+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
399+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
400+
CompileToAssembly(generated);
401+
}
402+
367403
[Fact]
368404
public void Component_WithFullyQualifiedTagNames()
369405
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line hidden
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Components;
11+
public partial class TestComponent : 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 System.Object __o = null;
19+
#pragma warning restore 0414
20+
#pragma warning disable 1998
21+
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
22+
{
23+
__o =
24+
#nullable restore
25+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
26+
"val"
27+
28+
#line default
29+
#line hidden
30+
#nullable disable
31+
;
32+
}
33+
#pragma warning restore 1998
34+
}
35+
}
36+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [12] ) - System
4+
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
5+
UsingDirective - (53:3,1 [17] ) - System.Linq
6+
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
7+
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
9+
DesignTimeDirective -
10+
CSharpCode -
11+
IntermediateToken - - CSharp - #pragma warning disable 0414
12+
CSharpCode -
13+
IntermediateToken - - CSharp - private static System.Object __o = null;
14+
CSharpCode -
15+
IntermediateToken - - CSharp - #pragma warning restore 0414
16+
MethodDeclaration - - protected override - void - BuildRenderTree
17+
MarkupElement - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - elem
18+
HtmlAttribute - (5:0,5 [23] x:\dir\subdir\Test\TestComponent.cshtml) - normal-attr=" - "
19+
CSharpExpressionAttributeValue - (19:0,19 [8] x:\dir\subdir\Test\TestComponent.cshtml) -
20+
LazyIntermediateToken - (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "val"
21+
HtmlAttribute - (28:0,28 [15] x:\dir\subdir\Test\TestComponent.cshtml) - minimized-attr -
22+
HtmlAttribute - (43:0,43 [22] x:\dir\subdir\Test\TestComponent.cshtml) - empty-string-atttr=" - "
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Source Location: (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|"val"|
3+
Generated Location: (893:25,21 [5] )
4+
|"val"|
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line hidden
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Components;
11+
public partial class TestComponent : 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 System.Object __o = null;
19+
#pragma warning restore 0414
20+
#pragma warning disable 1998
21+
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
22+
{
23+
__o =
24+
#nullable restore
25+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
26+
"val"
27+
28+
#line default
29+
#line hidden
30+
#nullable disable
31+
;
32+
}
33+
#pragma warning restore 1998
34+
}
35+
}
36+
#pragma warning restore 1591

0 commit comments

Comments
 (0)