Skip to content

Commit d273a0f

Browse files
authored
Merge pull request #1269 from aspnet/nimullen/16611
Fix runtime preventDefault / stopPropagation runtime InvalidOperationException.
2 parents 13934bc + 1513cd2 commit d273a0f

File tree

13 files changed

+95
-47
lines changed

13 files changed

+95
-47
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ public override void WriteMarkupElement(CodeRenderingContext context, MarkupElem
191191
{
192192
context.RenderNode(attribute);
193193
}
194+
else if (child is ComponentAttributeIntermediateNode componentAttribute)
195+
{
196+
context.RenderNode(componentAttribute);
197+
}
194198
else if (child is SplatIntermediateNode splat)
195199
{
196200
context.RenderNode(splat);

src/Razor/src/Microsoft.AspNetCore.Razor.Language/Intermediate/MarkupElementIntermediateNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public sealed class MarkupElementIntermediateNode : IntermediateNode
1919
public IEnumerable<IntermediateNode> Body => Children.Where(c =>
2020
{
2121
return
22+
c as ComponentAttributeIntermediateNode == null &&
2223
c as HtmlAttributeIntermediateNode == null &&
2324
c as SplatIntermediateNode == null &&
2425
c as SetKeyIntermediateNode == null &&

src/Razor/test/RazorLanguage.Test/IntegrationTests/ComponentCodeGenerationTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,7 @@ public void EventHandler_PreventDefault_StopPropagation_Minimized()
29052905
// Act
29062906
var generated = CompileToCSharp(@"
29072907
@using Microsoft.AspNetCore.Components.Web
2908-
<input @onclick:preventDefault @onclick:stopPropagation />");
2908+
<button @onclick:preventDefault @onclick:stopPropagation>Click Me</button>");
29092909

29102910
// Assert
29112911
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
@@ -2921,7 +2921,7 @@ public void EventHandler_PreventDefault_StopPropagation()
29212921
// Act
29222922
var generated = CompileToCSharp(@"
29232923
@using Microsoft.AspNetCore.Components.Web
2924-
<input @onfocus:preventDefault=""true"" @onclick:stopPropagation=""Foo"" @onfocus:stopPropagation=""false"" />
2924+
<button @onclick=""() => Foo = false"" @onfocus:preventDefault=""true"" @onclick:stopPropagation=""Foo"" @onfocus:stopPropagation=""false"">Click Me</button>
29252925
@code {
29262926
bool Foo { get; set; }
29272927
}");

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandler_PreventDefault_StopPropagation/TestComponent.codegen.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ private void __RazorDirectiveTokenHelpers__() {
2727
#pragma warning disable 1998
2828
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
2929
{
30+
__o = Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
31+
#nullable restore
32+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
33+
() => Foo = false
34+
35+
#line default
36+
#line hidden
37+
#nullable disable
38+
);
3039
__o = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<System.Boolean>(
3140
#nullable restore
3241
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
33-
true
42+
true
3443

3544
#line default
3645
#line hidden
@@ -39,7 +48,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin
3948
__o = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<System.Boolean>(
4049
#nullable restore
4150
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
42-
Foo
51+
Foo
4352

4453
#line default
4554
#line hidden
@@ -48,7 +57,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin
4857
__o = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<System.Boolean>(
4958
#nullable restore
5059
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
51-
false
60+
false
5261

5362
#line default
5463
#line hidden

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandler_PreventDefault_StopPropagation/TestComponent.ir.txt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ Document -
1717
MethodDeclaration - - protected override - void - BuildRenderTree
1818
HtmlContent - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml)
1919
IntermediateToken - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
20-
MarkupElement - (44:1,0 [104] x:\dir\subdir\Test\TestComponent.cshtml) - input
21-
ComponentAttribute - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
20+
MarkupElement - (44:1,0 [149] x:\dir\subdir\Test\TestComponent.cshtml) - button
21+
HtmlContent - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml)
22+
IntermediateToken - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
23+
HtmlAttribute - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onclick=" - "
24+
CSharpExpressionAttributeValue - -
25+
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
26+
IntermediateToken - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - () => Foo = false
27+
IntermediateToken - - CSharp - )
28+
ComponentAttribute - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
2229
CSharpExpression -
23-
IntermediateToken - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
24-
ComponentAttribute - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
30+
IntermediateToken - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
31+
ComponentAttribute - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
2532
CSharpExpression -
26-
IntermediateToken - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
27-
ComponentAttribute - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
33+
IntermediateToken - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
34+
ComponentAttribute - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
2835
CSharpExpression -
29-
IntermediateToken - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
30-
HtmlContent - (148:1,104 [2] x:\dir\subdir\Test\TestComponent.cshtml)
31-
IntermediateToken - (148:1,104 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
32-
CSharpCode - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
33-
IntermediateToken - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n
36+
IntermediateToken - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
37+
HtmlContent - (193:1,149 [2] x:\dir\subdir\Test\TestComponent.cshtml)
38+
IntermediateToken - (193:1,149 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
39+
CSharpCode - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
40+
IntermediateToken - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandler_PreventDefault_StopPropagation/TestComponent.mappings.txt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@ Source Location: (1:0,1 [41] x:\dir\subdir\Test\TestComponent.cshtml)
33
Generated Location: (320:12,0 [41] )
44
|using Microsoft.AspNetCore.Components.Web|
55

6-
Source Location: (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml)
6+
Source Location: (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml)
7+
|() => Foo = false|
8+
Generated Location: (1173:32,18 [17] )
9+
|() => Foo = false|
10+
11+
Source Location: (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml)
712
|true|
8-
Generated Location: (1158:32,32 [4] )
13+
Generated Location: (1500:41,62 [4] )
914
|true|
1015

11-
Source Location: (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml)
16+
Source Location: (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml)
1217
|Foo|
13-
Generated Location: (1474:41,64 [3] )
18+
Generated Location: (1846:50,94 [3] )
1419
|Foo|
1520

16-
Source Location: (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml)
21+
Source Location: (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml)
1722
|false|
18-
Generated Location: (1820:50,95 [5] )
23+
Generated Location: (2222:59,125 [5] )
1924
|false|
2025

21-
Source Location: (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
26+
Source Location: (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
2227
|
2328
bool Foo { get; set; }
2429
|
25-
Generated Location: (2019:60,7 [30] )
30+
Generated Location: (2421:69,7 [30] )
2631
|
2732
bool Foo { get; set; }
2833
|

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/EventHandler_PreventDefault_StopPropagation_Minimized/TestComponent.ir.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Document -
1717
MethodDeclaration - - protected override - void - BuildRenderTree
1818
HtmlContent - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml)
1919
IntermediateToken - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
20-
MarkupElement - (44:1,0 [58] x:\dir\subdir\Test\TestComponent.cshtml) - input
20+
MarkupElement - (44:1,0 [74] x:\dir\subdir\Test\TestComponent.cshtml) - button
21+
HtmlContent - (101:1,57 [8] x:\dir\subdir\Test\TestComponent.cshtml)
22+
IntermediateToken - (101:1,57 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
2123
ComponentAttribute - - onclick - PreventDefault - AttributeStructure.Minimized
2224
ComponentAttribute - - onclick - StopPropagation - AttributeStructure.Minimized

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandler_PreventDefault_Duplicates/TestComponent.codegen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
2121
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
2222
{
2323
__builder.OpenElement(0, "input");
24-
__builder.AddAttribute(1, "@onclick:preventDefault", true);
25-
__builder.AddEventPreventDefaultAttribute(2, "onclick",
24+
__builder.AddEventPreventDefaultAttribute(1, "onclick",
2625
#nullable restore
2726
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
2827
true
@@ -31,6 +30,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin
3130
#line hidden
3231
#nullable disable
3332
);
33+
__builder.AddAttribute(2, "@onclick:preventDefault", true);
3434
__builder.CloseElement();
3535
}
3636
#pragma warning restore 1998

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandler_PreventDefault_StopPropagation/TestComponent.codegen.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,44 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
2020
#pragma warning disable 1998
2121
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
2222
{
23-
__builder.OpenElement(0, "input");
24-
__builder.AddEventPreventDefaultAttribute(1, "onfocus",
23+
__builder.OpenElement(0, "button");
24+
__builder.AddAttribute(1, "onclick", Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
2525
#nullable restore
2626
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
27-
true
27+
() => Foo = false
28+
29+
#line default
30+
#line hidden
31+
#nullable disable
32+
));
33+
__builder.AddEventPreventDefaultAttribute(2, "onfocus",
34+
#nullable restore
35+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
36+
true
2837

2938
#line default
3039
#line hidden
3140
#nullable disable
3241
);
33-
__builder.AddEventStopPropagationAttribute(2, "onclick",
42+
__builder.AddEventStopPropagationAttribute(3, "onclick",
3443
#nullable restore
3544
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
36-
Foo
45+
Foo
3746

3847
#line default
3948
#line hidden
4049
#nullable disable
4150
);
42-
__builder.AddEventStopPropagationAttribute(3, "onfocus",
51+
__builder.AddEventStopPropagationAttribute(4, "onfocus",
4352
#nullable restore
4453
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
45-
false
54+
false
4655

4756
#line default
4857
#line hidden
4958
#nullable disable
5059
);
60+
__builder.AddContent(5, "Click Me");
5161
__builder.CloseElement();
5262
}
5363
#pragma warning restore 1998

src/Razor/test/RazorLanguage.Test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/EventHandler_PreventDefault_StopPropagation/TestComponent.ir.txt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ Document -
88
UsingDirective - (1:0,1 [43] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Web
99
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
1010
MethodDeclaration - - protected override - void - BuildRenderTree
11-
MarkupElement - (44:1,0 [104] x:\dir\subdir\Test\TestComponent.cshtml) - input
12-
ComponentAttribute - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
11+
MarkupElement - (44:1,0 [149] x:\dir\subdir\Test\TestComponent.cshtml) - button
12+
HtmlContent - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml)
13+
IntermediateToken - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
14+
HtmlAttribute - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onclick=" - "
15+
CSharpExpressionAttributeValue - -
16+
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
17+
IntermediateToken - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - () => Foo = false
18+
IntermediateToken - - CSharp - )
19+
ComponentAttribute - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
1320
CSharpExpression -
14-
IntermediateToken - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
15-
ComponentAttribute - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
21+
IntermediateToken - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
22+
ComponentAttribute - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
1623
CSharpExpression -
17-
IntermediateToken - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
18-
ComponentAttribute - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
24+
IntermediateToken - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
25+
ComponentAttribute - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
1926
CSharpExpression -
20-
IntermediateToken - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
21-
CSharpCode - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
22-
IntermediateToken - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n
27+
IntermediateToken - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
28+
CSharpCode - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
29+
IntermediateToken - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n

0 commit comments

Comments
 (0)