Handle ternary expressions inside interpolated attributes. Fixes #446

This commit is contained in:
Steve Sanderson 2018-03-30 10:40:26 +01:00
parent 2a8d06b539
commit b52912a460
2 changed files with 28 additions and 0 deletions

View File

@ -594,7 +594,21 @@ namespace Microsoft.AspNetCore.Blazor.Razor
writer.Write(" + ");
}
// If it's a C# expression, we have to wrap it in parens, otherwise
// things like ternary expressions don't compose with concatenation
var isCSharp = concatenatedValue is IntermediateToken intermediateToken
&& intermediateToken.Kind == TokenKind.CSharp;
if (isCSharp)
{
writer.Write("(");
}
WriteAttributeValue(writer, concatenatedValue);
if (isCSharp)
{
writer.Write(")");
}
}
break;
}

View File

@ -180,6 +180,20 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
frame => AssertFrame.Attribute(frame, "attr", "Hello, WORLD with number 246!", 1));
}
[Fact]
public void SupportsAttributesWithInterpolatedTernaryExpressionValues()
{
// Arrange/Act
var component = CompileToComponent(
"@{ var myValue = \"world\"; }"
+ "<elem attr=\"Hello, @(true ? myValue : \"nothing\")!\" />");
// Assert
Assert.Collection(GetRenderTree(component),
frame => AssertFrame.Element(frame, "elem", 2, 0),
frame => AssertFrame.Attribute(frame, "attr", "Hello, world!", 1));
}
[Fact]
public void SupportsHyphenedAttributesWithCSharpExpressionValues()
{