Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix runtime preventDefault / stopPropagation runtime InvalidOperationException. #1269

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public override void WriteMarkupElement(CodeRenderingContext context, MarkupElem
{
context.RenderNode(attribute);
}
else if (child is ComponentAttributeIntermediateNode componentAttribute)
{
context.RenderNode(componentAttribute);
}
else if (child is SplatIntermediateNode splat)
{
context.RenderNode(splat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class MarkupElementIntermediateNode : IntermediateNode
public IEnumerable<IntermediateNode> Body => Children.Where(c =>
{
return
c as ComponentAttributeIntermediateNode == null &&
c as HtmlAttributeIntermediateNode == null &&
c as SplatIntermediateNode == null &&
c as SetKeyIntermediateNode == null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2905,7 +2905,7 @@ public void EventHandler_PreventDefault_StopPropagation_Minimized()
// Act
var generated = CompileToCSharp(@"
@using Microsoft.AspNetCore.Components.Web
<input @onclick:preventDefault @onclick:stopPropagation />");
<button @onclick:preventDefault @onclick:stopPropagation>Click Me</button>");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the feature not supposed to work for input type=button ?

Copy link
Contributor Author

@NTaylorMullen NTaylorMullen Oct 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for input but inputs don't typically have child content ("Click Me") so I changed it to a button


// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
Expand All @@ -2921,7 +2921,7 @@ public void EventHandler_PreventDefault_StopPropagation()
// Act
var generated = CompileToCSharp(@"
@using Microsoft.AspNetCore.Components.Web
<input @onfocus:preventDefault=""true"" @onclick:stopPropagation=""Foo"" @onfocus:stopPropagation=""false"" />
<button @onclick=""() => Foo = false"" @onfocus:preventDefault=""true"" @onclick:stopPropagation=""Foo"" @onfocus:stopPropagation=""false"">Click Me</button>
@code {
bool Foo { get; set; }
}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o = Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
() => Foo = false

#line default
#line hidden
#nullable disable
);
__o = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<System.Boolean>(
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
true
true

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

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

#line default
#line hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@ Document -
MethodDeclaration - - protected override - void - BuildRenderTree
HtmlContent - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
MarkupElement - (44:1,0 [104] x:\dir\subdir\Test\TestComponent.cshtml) - input
ComponentAttribute - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
MarkupElement - (44:1,0 [149] x:\dir\subdir\Test\TestComponent.cshtml) - button
HtmlContent - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
HtmlAttribute - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onclick=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
IntermediateToken - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - () => Foo = false
IntermediateToken - - CSharp - )
ComponentAttribute - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
ComponentAttribute - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
IntermediateToken - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
ComponentAttribute - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
ComponentAttribute - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
IntermediateToken - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
ComponentAttribute - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
HtmlContent - (148:1,104 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (148:1,104 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n
IntermediateToken - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
HtmlContent - (193:1,149 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (193:1,149 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ Source Location: (1:0,1 [41] x:\dir\subdir\Test\TestComponent.cshtml)
Generated Location: (320:12,0 [41] )
|using Microsoft.AspNetCore.Components.Web|

Source Location: (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml)
Source Location: (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml)
|() => Foo = false|
Generated Location: (1173:32,18 [17] )
|() => Foo = false|

Source Location: (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml)
|true|
Generated Location: (1158:32,32 [4] )
Generated Location: (1500:41,62 [4] )
|true|

Source Location: (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml)
Source Location: (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml)
|Foo|
Generated Location: (1474:41,64 [3] )
Generated Location: (1846:50,94 [3] )
|Foo|

Source Location: (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml)
Source Location: (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|false|
Generated Location: (1820:50,95 [5] )
Generated Location: (2222:59,125 [5] )
|false|

Source Location: (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
Source Location: (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
|
bool Foo { get; set; }
|
Generated Location: (2019:60,7 [30] )
Generated Location: (2421:69,7 [30] )
|
bool Foo { get; set; }
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Document -
MethodDeclaration - - protected override - void - BuildRenderTree
HtmlContent - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (42:0,42 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
MarkupElement - (44:1,0 [58] x:\dir\subdir\Test\TestComponent.cshtml) - input
MarkupElement - (44:1,0 [74] x:\dir\subdir\Test\TestComponent.cshtml) - button
HtmlContent - (101:1,57 [8] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (101:1,57 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
ComponentAttribute - - onclick - PreventDefault - AttributeStructure.Minimized
ComponentAttribute - - onclick - StopPropagation - AttributeStructure.Minimized
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenElement(0, "input");
__builder.AddAttribute(1, "@onclick:preventDefault", true);
__builder.AddEventPreventDefaultAttribute(2, "onclick",
__builder.AddEventPreventDefaultAttribute(1, "onclick",
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
true
Expand All @@ -31,6 +30,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin
#line hidden
#nullable disable
);
__builder.AddAttribute(2, "@onclick:preventDefault", true);
__builder.CloseElement();
}
#pragma warning restore 1998
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,44 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenElement(0, "input");
__builder.AddEventPreventDefaultAttribute(1, "onfocus",
__builder.OpenElement(0, "button");
__builder.AddAttribute(1, "onclick", Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
true
() => Foo = false

#line default
#line hidden
#nullable disable
));
__builder.AddEventPreventDefaultAttribute(2, "onfocus",
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
true

#line default
#line hidden
#nullable disable
);
__builder.AddEventStopPropagationAttribute(2, "onclick",
__builder.AddEventStopPropagationAttribute(3, "onclick",
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
Foo
Foo

#line default
#line hidden
#nullable disable
);
__builder.AddEventStopPropagationAttribute(3, "onfocus",
__builder.AddEventStopPropagationAttribute(4, "onfocus",
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
false
false

#line default
#line hidden
#nullable disable
);
__builder.AddContent(5, "Click Me");
__builder.CloseElement();
}
#pragma warning restore 1998
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ Document -
UsingDirective - (1:0,1 [43] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Web
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (44:1,0 [104] x:\dir\subdir\Test\TestComponent.cshtml) - input
ComponentAttribute - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
MarkupElement - (44:1,0 [149] x:\dir\subdir\Test\TestComponent.cshtml) - button
HtmlContent - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (176:1,132 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
HtmlAttribute - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onclick=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
IntermediateToken - (62:1,18 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - () => Foo = false
IntermediateToken - - CSharp - )
ComponentAttribute - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - PreventDefault - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - (76:1,32 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
ComponentAttribute - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
IntermediateToken - (106:1,62 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - true
ComponentAttribute - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - onclick - StopPropagation - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - (108:1,64 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
ComponentAttribute - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
IntermediateToken - (138:1,94 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - Foo
ComponentAttribute - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus - StopPropagation - AttributeStructure.DoubleQuotes
CSharpExpression -
IntermediateToken - (139:1,95 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
CSharpCode - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n
IntermediateToken - (169:1,125 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - false
CSharpCode - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n bool Foo { get; set; }\n
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Source Location: (157:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
Source Location: (202:2,7 [30] x:\dir\subdir\Test\TestComponent.cshtml)
|
bool Foo { get; set; }
|
Generated Location: (1720:55,7 [30] )
Generated Location: (2205:65,7 [30] )
|
bool Foo { get; set; }
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenElement(0, "input");
__builder.OpenElement(0, "button");
__builder.AddEventPreventDefaultAttribute(1, "onclick", true);
__builder.AddEventStopPropagationAttribute(2, "onclick", true);
__builder.AddContent(3, "Click Me");
__builder.CloseElement();
}
#pragma warning restore 1998
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Document -
UsingDirective - (1:0,1 [43] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Web
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (44:1,0 [58] x:\dir\subdir\Test\TestComponent.cshtml) - input
MarkupElement - (44:1,0 [74] x:\dir\subdir\Test\TestComponent.cshtml) - button
HtmlContent - (101:1,57 [8] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (101:1,57 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Click Me
ComponentAttribute - - onclick - PreventDefault - AttributeStructure.Minimized
ComponentAttribute - - onclick - StopPropagation - AttributeStructure.Minimized