In RazorCompiler, support @using statements

This commit is contained in:
Steve Sanderson 2018-01-16 17:17:22 +00:00
parent 7cd5228b7f
commit 946e25462e
4 changed files with 36 additions and 37 deletions

View File

@ -244,7 +244,7 @@ namespace Microsoft.Blazor.Build.Core.RazorCompilation.Engine
public override void WriteUsingDirective(CodeRenderingContext context, UsingDirectiveIntermediateNode node)
{
throw new System.NotImplementedException(nameof(WriteUsingDirective));
context.CodeWriter.WriteUsing(node.Content, endLine: true);
}
private static string GetContent(HtmlContentIntermediateNode node)

View File

@ -286,6 +286,23 @@ namespace Microsoft.Blazor.Build.Test
});
}
[Fact]
public void SupportsUsingStatements()
{
// Arrange
var treeBuilder = new RenderTreeBuilder(new TestRenderer());
// Arrange/Act
var component = CompileToComponent(@"
@using System.Collections.Generic
@(typeof(List<string>).FullName)");
component.BuildRenderTree(treeBuilder);
// Assert
Assert.Collection(treeBuilder.GetNodes().Where(NotWhitespace),
node => AssertNode.Text(node, typeof(List<string>).FullName));
}
private static bool NotWhitespace(RenderTreeNode node)
=> node.NodeType != RenderTreeNodeType.Text
|| !string.IsNullOrWhiteSpace(node.TextContent);

View File

@ -1,36 +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;
using System.Collections.Generic;
namespace BasicTestApp
{
public class KeyPressEventComponent : IComponent
{
private List<string> keysPressed = new List<string>();
public void BuildRenderTree(RenderTreeBuilder builder)
{
builder.AddText("Type here:");
builder.OpenElement("input");
builder.AddAttribute("onkeypress", OnKeyPressed);
builder.CloseElement();
builder.OpenElement("ul");
foreach (var key in keysPressed)
{
builder.OpenElement("li");
builder.AddText(key);
builder.CloseElement();
}
builder.CloseElement();
}
private void OnKeyPressed(UIEventArgs eventInfo)
{
keysPressed.Add(((UIKeyboardEventArgs)eventInfo).Key);
}
}
}

View File

@ -0,0 +1,18 @@
@using System.Collections.Generic
@using Microsoft.Blazor.RenderTree
Type here: <input onkeypress=@OnKeyPressed />
<ul>
@foreach (var key in keysPressed)
{
<li>@key</li>
}
</ul>
@functions {
List<string> keysPressed = new List<string>();
void OnKeyPressed(UIEventArgs eventArgs)
{
keysPressed.Add(((UIKeyboardEventArgs)eventArgs).Key);
}
}