Handle ternary expressions inside interpolated attributes. Fixes #446
This commit is contained in:
parent
2a8d06b539
commit
b52912a460
|
|
@ -594,7 +594,21 @@ namespace Microsoft.AspNetCore.Blazor.Razor
|
||||||
writer.Write(" + ");
|
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);
|
WriteAttributeValue(writer, concatenatedValue);
|
||||||
|
|
||||||
|
if (isCSharp)
|
||||||
|
{
|
||||||
|
writer.Write(")");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,20 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
|
||||||
frame => AssertFrame.Attribute(frame, "attr", "Hello, WORLD with number 246!", 1));
|
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]
|
[Fact]
|
||||||
public void SupportsHyphenedAttributesWithCSharpExpressionValues()
|
public void SupportsHyphenedAttributesWithCSharpExpressionValues()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue