From 80b371c6479a3a3ee51ed52264addb9cee8d0a63 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 16 Jan 2018 12:23:12 +0000 Subject: [PATCH] In RazorCompiler, support string-valued attributes --- .../Engine/BlazorIntermediateNodeWriter.cs | 10 ++++++++++ .../RazorCompilerTest.cs | 13 ++++++++++++ .../testapps/BasicTestApp/RedTextComponent.cs | 20 ------------------- .../BasicTestApp/RedTextComponent.cshtml | 1 + 4 files changed, 24 insertions(+), 20 deletions(-) delete mode 100644 test/testapps/BasicTestApp/RedTextComponent.cs create mode 100644 test/testapps/BasicTestApp/RedTextComponent.cshtml diff --git a/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs b/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs index 88e8393930..128616922f 100644 --- a/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs +++ b/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs @@ -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)) diff --git a/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs b/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs index ac7f730c4e..02e7091cce 100644 --- a/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs +++ b/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs @@ -157,6 +157,19 @@ namespace Microsoft.Blazor.Build.Test node => AssertNode.Element(node, "img", 1)); } + [Fact] + public void CanRenderAttributes() + { + // Arrange/Act + var component = CompileToComponent(""); + + // 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); diff --git a/test/testapps/BasicTestApp/RedTextComponent.cs b/test/testapps/BasicTestApp/RedTextComponent.cs deleted file mode 100644 index 20141e3671..0000000000 --- a/test/testapps/BasicTestApp/RedTextComponent.cs +++ /dev/null @@ -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(); - } - } -} diff --git a/test/testapps/BasicTestApp/RedTextComponent.cshtml b/test/testapps/BasicTestApp/RedTextComponent.cshtml new file mode 100644 index 0000000000..fa16e4f9b8 --- /dev/null +++ b/test/testapps/BasicTestApp/RedTextComponent.cshtml @@ -0,0 +1 @@ +

Hello, world!