Fix #784 - allow complex content for HTML

The logic that binds event handlers was interfering with the code that
prevents component properties from receiving complex content.

This check was a little overzealous.
This commit is contained in:
Ryan Nowak 2018-05-22 11:34:31 -07:00
parent 9aed80aaed
commit c6148b6fe6
10 changed files with 275 additions and 7 deletions

View File

@ -1,6 +1,7 @@
// 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;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@ -31,7 +32,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor
{
if (node.Children[i] is TagHelperPropertyIntermediateNode propertyNode)
{
if (HasComplexChildContent(propertyNode))
if (TrySimplifyContent(propertyNode) && node.TagHelpers.Any(t => t.IsComponentTagHelper()))
{
node.Diagnostics.Add(BlazorDiagnosticFactory.Create_UnsupportedComplexContent(
propertyNode,
@ -42,7 +43,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor
}
else if (node.Children[i] is TagHelperHtmlAttributeIntermediateNode htmlNode)
{
if (HasComplexChildContent(htmlNode))
if (TrySimplifyContent(htmlNode) && node.TagHelpers.Any(t => t.IsComponentTagHelper()))
{
node.Diagnostics.Add(BlazorDiagnosticFactory.Create_UnsupportedComplexContent(
htmlNode,
@ -54,7 +55,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor
}
}
private static bool HasComplexChildContent(IntermediateNode node)
private static bool TrySimplifyContent(IntermediateNode node)
{
if (node.Children.Count == 1 &&
node.Children[0] is HtmlAttributeIntermediateNode htmlNode &&

View File

@ -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;
@ -442,5 +442,34 @@ namespace Test
frame => AssertFrame.Component(frame, "Test.SurveyPrompt", 2, 0),
frame => AssertFrame.Attribute(frame, "Title", "<div>Test!</div>", 1));
}
[Fact]
public void Regression_784()
{
// Arrange
// Act
var component = CompileToComponent(@"
<p onmouseover=""@OnComponentHover"" style=""background: @ParentBgColor;"" />
@functions {
public string ParentBgColor { get; set; } = ""#FFFFFF"";
public void OnComponentHover(UIMouseEventArgs e)
{
}
}
");
// Act
var frames = GetRenderTree(component);
// Assert
Assert.Collection(
frames,
frame => AssertFrame.Element(frame, "p", 3, 0),
frame => AssertFrame.Attribute(frame, "onmouseover", 1),
frame => AssertFrame.Attribute(frame, "style", "background: #FFFFFF;", 2));
}
}
}

View File

@ -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;
@ -441,6 +441,29 @@ Welcome to your new app.
CompileToAssembly(generated);
}
[Fact]
public void Regression_784()
{
// Arrange
// Act
var generated = CompileToCSharp(@"
<p onmouseover=""@OnComponentHover"" style=""background: @ParentBgColor;"" />
@functions {
public string ParentBgColor { get; set; } = ""#FFFFFF"";
public void OnComponentHover(UIMouseEventArgs e)
{
}
}
");
// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}
[Fact]
public void BindToComponent_SpecifiesValue_WithMatchingProperties()
{

View File

@ -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;
@ -693,6 +693,29 @@ Welcome to your new app.
CompileToAssembly(generated);
}
[Fact]
public void Regression_784()
{
// Arrange
// Act
var generated = CompileToCSharp(@"
<p onmouseover=""@OnComponentHover"" style=""background: @ParentBgColor;"" />
@functions {
public string ParentBgColor { get; set; } = ""#FFFFFF"";
public void OnComponentHover(UIMouseEventArgs e)
{
}
}
");
// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}
[Fact]
public void BindToComponent_SpecifiesValue_WithMatchingProperties()
{

View File

@ -0,0 +1,45 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor;
using Microsoft.AspNetCore.Blazor.Components;
public class TestComponent : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
__o = Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>(OnComponentHover);
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = ParentBgColor;
#line default
#line hidden
}
#pragma warning restore 1998
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
public string ParentBgColor { get; set; } = "#FFFFFF";
public void OnComponentHover(UIMouseEventArgs e)
{
}
#line default
#line hidden
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,40 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [33] ) - Microsoft.AspNetCore.Blazor
UsingDirective - (140:6,1 [44] ) - Microsoft.AspNetCore.Blazor.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Blazor.Components.BlazorComponent -
DesignTimeDirective -
DirectiveToken - (14:0,14 [32] ) - "*, Microsoft.AspNetCore.Blazor"
DirectiveToken - (14:0,14 [9] ) - "*, Test"
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
CSharpCode -
IntermediateToken - - CSharp - base.BuildRenderTree(builder);
HtmlElement - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - p
HtmlAttribute - (16:0,16 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onmouseover=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>(
IntermediateToken - (17:0,17 [16] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - OnComponentHover
IntermediateToken - - CSharp - )
HtmlAttribute - - style=" - "
HtmlAttributeValue - (42:0,42 [11] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (42:0,42 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Html - background:
CSharpExpressionAttributeValue - (53:0,53 [15] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (55:0,55 [13] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ParentBgColor
HtmlAttributeValue - (68:0,68 [1] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (68:0,68 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Html - ;
HtmlContent - (73:0,73 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (73:0,73 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
HtmlContent - (220:7,1 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (220:7,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (87:1,12 [132] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (87:1,12 [132] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public string ParentBgColor { get; set; } = "#FFFFFF";\n\n public void OnComponentHover(UIMouseEventArgs e)\n {\n }\n

View File

@ -0,0 +1,27 @@
Source Location: (17:0,17 [16] x:\dir\subdir\Test\TestComponent.cshtml)
|OnComponentHover|
Generated Location: (1005:24,136 [16] )
|OnComponentHover|
Source Location: (55:0,55 [13] x:\dir\subdir\Test\TestComponent.cshtml)
|ParentBgColor|
Generated Location: (1131:26,55 [13] )
|ParentBgColor|
Source Location: (87:1,12 [132] x:\dir\subdir\Test\TestComponent.cshtml)
|
public string ParentBgColor { get; set; } = "#FFFFFF";
public void OnComponentHover(UIMouseEventArgs e)
{
}
|
Generated Location: (1290:33,12 [132] )
|
public string ParentBgColor { get; set; } = "#FFFFFF";
public void OnComponentHover(UIMouseEventArgs e)
{
}
|

View File

@ -0,0 +1,36 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor;
using Microsoft.AspNetCore.Blazor.Components;
public class TestComponent : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
builder.OpenElement(0, "p");
builder.AddAttribute(1, "onmouseover", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>(OnComponentHover));
builder.AddAttribute(2, "style", "background:" + " " + (ParentBgColor) + ";");
builder.CloseElement();
}
#pragma warning restore 1998
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
public string ParentBgColor { get; set; } = "#FFFFFF";
public void OnComponentHover(UIMouseEventArgs e)
{
}
#line default
#line hidden
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,27 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [14] ) - System
UsingDirective - (18:2,1 [34] ) - System.Collections.Generic
UsingDirective - (53:3,1 [19] ) - System.Linq
UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [35] ) - Microsoft.AspNetCore.Blazor
UsingDirective - (140:6,1 [46] ) - Microsoft.AspNetCore.Blazor.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Blazor.Components.BlazorComponent -
MethodDeclaration - - protected override - void - BuildRenderTree
CSharpCode -
IntermediateToken - - CSharp - base.BuildRenderTree(builder);
HtmlElement - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - p
HtmlAttribute - (16:0,16 [17] x:\dir\subdir\Test\TestComponent.cshtml) - onmouseover=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>(
IntermediateToken - (17:0,17 [16] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - OnComponentHover
IntermediateToken - - CSharp - )
HtmlAttribute - - style=" - "
HtmlAttributeValue - (42:0,42 [11] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (42:0,42 [11] x:\dir\subdir\Test\TestComponent.cshtml) - Html - background:
CSharpExpressionAttributeValue - (53:0,53 [15] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (55:0,55 [13] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - ParentBgColor
HtmlAttributeValue - (68:0,68 [1] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (68:0,68 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Html - ;
CSharpCode - (87:1,12 [132] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (87:1,12 [132] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public string ParentBgColor { get; set; } = "#FFFFFF";\n\n public void OnComponentHover(UIMouseEventArgs e)\n {\n }\n

View File

@ -0,0 +1,17 @@
Source Location: (87:1,12 [132] x:\dir\subdir\Test\TestComponent.cshtml)
|
public string ParentBgColor { get; set; } = "#FFFFFF";
public void OnComponentHover(UIMouseEventArgs e)
{
}
|
Generated Location: (1074:24,12 [132] )
|
public string ParentBgColor { get; set; } = "#FFFFFF";
public void OnComponentHover(UIMouseEventArgs e)
{
}
|