Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
Fix #597 Allow multiple tokens in attributes
Browse files Browse the repository at this point in the history
The issue here is that we were missing tests for the design time code
path. We have tests that the bind-... cases work at runtime but were
missing coverage for the editor.

I took the most relevant set of the tests for running bind-... code and
added them to the tests for codegen.
  • Loading branch information
rynowak committed Apr 19, 2018
1 parent 2469f0d commit dda0051
Show file tree
Hide file tree
Showing 123 changed files with 4,074 additions and 892 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<RepositoryUrl>$(RepositoryUrl)</RepositoryUrl>
<Category>$(PackageArtifactCategory)</Category>
<Certificate>$(PackageSigningCertName)</Certificate>
<ShouldBeSigned Condition="'$(PackageSigningCertName)' != '' OR @(SignedPackageFile->Count()) != 0 ">true</ShouldBeSigned>
<ShouldBeSigned Condition="'$(PackageSigningCertName)' != '' OR @(SignedPackageFile-&gt;Count()) != 0 ">true</ShouldBeSigned>
<ShouldBeSigned Condition=" '$(DisableCodeSigning)' == 'true' ">false</ShouldBeSigned>
<IsContainer>true</IsContainer>
</ArtifactInfo>
Expand Down Expand Up @@ -73,5 +73,23 @@

<ItemGroup>
<Reference Include="..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll" />
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
Expand Down Expand Up @@ -411,52 +412,54 @@ public override void WriteComponentAttribute(CodeRenderingContext context, Compo
// to handle here, since there are a few different cases for how an attribute might be structured.
//
// This roughly follows the design of the runtime writer for simplicity.
HtmlContentIntermediateNode htmlNode;
CSharpExpressionIntermediateNode cSharpNode;
if (node.AttributeStructure == AttributeStructure.Minimized)
{
// Do nothing
}
else if (
node.Children.Count != 1 ||
node.Children[0] is HtmlContentIntermediateNode htmlNode && htmlNode.Children.Count != 1 ||
node.Children[0] is CSharpExpressionIntermediateNode cSharpNode && cSharpNode.Children.Count != 1)
else if (node.Children.Count != 1)
{
// We don't expect this to happen, we just want to know if it can.
throw new InvalidOperationException("Attribute nodes should either be minimized or a single content node.");
throw new InvalidOperationException("Attribute nodes should either be minimized or a single type of content." + node.Children[0].ToString());
}
else if (node.BoundAttribute?.IsDelegateProperty() ?? false)
{
// We always surround the expression with the delegate constructor. This makes type
// inference inside lambdas, and method group conversion do the right thing.
IntermediateToken token = null;
var tokens = node.Children;
if ((cSharpNode = node.Children[0] as CSharpExpressionIntermediateNode) != null)
{
token = cSharpNode.Children[0] as IntermediateToken;
}
else
{
token = node.Children[0] as IntermediateToken;
tokens = node.Children[0].Children;
}

if (token != null)
{
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
context.CodeWriter.Write("new ");
context.CodeWriter.Write(node.BoundAttribute.TypeName);
context.CodeWriter.Write("(");
context.CodeWriter.WriteLine();
WriteCSharpToken(context, token);
context.CodeWriter.Write(");");
context.CodeWriter.WriteLine();
}
// We always surround the expression with the delegate constructor. This makes type
// inference inside lambdas, and method group conversion do the right thing.
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
context.CodeWriter.Write("new ");
context.CodeWriter.Write(node.BoundAttribute.TypeName);
context.CodeWriter.Write("(");
context.CodeWriter.WriteLine();

for (var i = 0; i < tokens.Count; i++)
{
WriteCSharpToken(context, (IntermediateToken)tokens[i]);
}

context.CodeWriter.Write(");");
context.CodeWriter.WriteLine();
}
else if ((cSharpNode = node.Children[0] as CSharpExpressionIntermediateNode) != null)
{
// This is the case when an attribute has an explicit C# transition like:
// <MyComponent Foo="@bar" />
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
WriteCSharpToken(context, ((IntermediateToken)cSharpNode.Children[0]));

for (var i = 0; i < cSharpNode.Children.Count; i++)
{
WriteCSharpToken(context, (IntermediateToken)cSharpNode.Children[i]);
}

context.CodeWriter.Write(";");
context.CodeWriter.WriteLine();
}
Expand All @@ -468,7 +471,12 @@ public override void WriteComponentAttribute(CodeRenderingContext context, Compo
{
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
WriteCSharpToken(context, token);

for (var i = 0; i < node.Children.Count; i++)
{
WriteCSharpToken(context, (IntermediateToken)node.Children[i]);
}

context.CodeWriter.Write(";");
context.CodeWriter.WriteLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@

<ItemGroup>
<ProjectReference Include="..\anglesharp\AngleSharpBuilder\AngleSharpBuilder.csproj">
<Name>AngleSharpBuilder</Name>
<Private>False</Private>
<SetTargetFramework>TargetFramework=netcoreapp2.0</SetTargetFramework>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<Reference Include="..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll" />
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp">
<HintPath>..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit dda0051

Please sign in to comment.