In RazorCompiler, support string-valued attributes

This commit is contained in:
Steve Sanderson 2018-01-16 12:23:12 +00:00
parent 1c1fd69bf2
commit 80b371c647
4 changed files with 24 additions and 20 deletions

View File

@ -148,6 +148,16 @@ namespace Microsoft.Blazor.Build.Core.RazorCompilation.Engine
.WriteEndMethodInvocation();
}
foreach (var attribute in nextTag.Attributes)
{
context.CodeWriter
.WriteStartMethodInvocation($"{builderVarName}.{nameof(RenderTreeBuilder.AddAttribute)}")
.WriteStringLiteral(attribute.Key)
.WriteParameterSeparator()
.WriteStringLiteral(attribute.Value)
.WriteEndMethodInvocation();
}
if (nextToken.Type == HtmlTokenType.EndTag
|| nextTag.IsSelfClosing
|| htmlVoidElementsLookup.Contains(nextTag.Data))

View File

@ -157,6 +157,19 @@ namespace Microsoft.Blazor.Build.Test
node => AssertNode.Element(node, "img", 1));
}
[Fact]
public void CanRenderAttributes()
{
// Arrange/Act
var component = CompileToComponent("<elem attrib-one=\"Value 1\" a2='v2' />");
// Assert
Assert.Collection(GetRenderTree(component),
node => AssertNode.Element(node, "elem", 2),
node => AssertNode.Attribute(node, "attrib-one", "Value 1"),
node => AssertNode.Attribute(node, "a2", "v2"));
}
private static bool NotWhitespace(RenderTreeNode node)
=> node.NodeType != RenderTreeNodeType.Text
|| !string.IsNullOrWhiteSpace(node.TextContent);

View File

@ -1,20 +0,0 @@
// 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 Microsoft.Blazor.Components;
using Microsoft.Blazor.RenderTree;
namespace BasicTestApp
{
public class RedTextComponent : IComponent
{
public void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement("h1");
builder.AddAttribute("style", "color: red;");
builder.AddAttribute("customattribute", "somevalue");
builder.AddText("Hello, world!");
builder.CloseElement();
}
}
}

View File

@ -0,0 +1 @@
<h1 style="color: red;" customattribute="somevalue">Hello, world!</h1>