diff --git a/samples/StandaloneApp/Home.cshtml b/samples/StandaloneApp/Home.cshtml index 921a01483e..0ad1ea275e 100644 --- a/samples/StandaloneApp/Home.cshtml +++ b/samples/StandaloneApp/Home.cshtml @@ -1 +1 @@ -Hello, world! +

Hello, world!

diff --git a/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs b/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs index 5a81d8ebe9..4b1c904906 100644 --- a/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs +++ b/src/Microsoft.Blazor.Build/Core/RazorCompilation/Engine/BlazorIntermediateNodeWriter.cs @@ -1,6 +1,9 @@ // 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 AngleSharp; +using AngleSharp.Html; +using AngleSharp.Parser.Html; using Microsoft.AspNetCore.Razor.Language.CodeGeneration; using Microsoft.AspNetCore.Razor.Language.Intermediate; using Microsoft.Blazor.RenderTree; @@ -105,10 +108,44 @@ namespace Microsoft.Blazor.Build.Core.RazorCompilation.Engine public override void WriteHtmlContent(CodeRenderingContext context, HtmlContentIntermediateNode node) { - context.CodeWriter - .WriteStartMethodInvocation($"{builderVarName}.{nameof(RenderTreeBuilder.AddText)}") - .WriteStringLiteral(GetContent(node)) - .WriteEndMethodInvocation(); + var originalHtmlContent = GetContent(node); + var tokenizer = new HtmlTokenizer( + new TextSource(originalHtmlContent), + HtmlEntityService.Resolver); + + HtmlToken nextToken; + while ((nextToken = tokenizer.Get()).Type != HtmlTokenType.EndOfFile) + { + switch (nextToken.Type) + { + case HtmlTokenType.Character: + // Text node + context.CodeWriter + .WriteStartMethodInvocation($"{builderVarName}.{nameof(RenderTreeBuilder.AddText)}") + .WriteStringLiteral(nextToken.Data) + .WriteEndMethodInvocation(); + break; + + case HtmlTokenType.StartTag: + var nextTag = nextToken.AsTag(); + context.CodeWriter + .WriteStartMethodInvocation($"{builderVarName}.{nameof(RenderTreeBuilder.OpenElement)}") + .WriteStringLiteral(nextTag.Data) + .WriteEndMethodInvocation(); + break; + + case HtmlTokenType.EndTag: + context.CodeWriter + .WriteStartMethodInvocation($"{builderVarName}.{nameof(RenderTreeBuilder.CloseElement)}") + .WriteEndMethodInvocation(); + break; + + default: + throw new InvalidCastException($"Unsupported token type: {nextToken.Type.ToString()}"); + } + } + + } public override void WriteUsingDirective(CodeRenderingContext context, UsingDirectiveIntermediateNode node) diff --git a/test/Microsoft.Blazor.Build.Test/Microsoft.Blazor.Build.Test.csproj b/test/Microsoft.Blazor.Build.Test/Microsoft.Blazor.Build.Test.csproj index 3f133c9bad..7d91676b6f 100644 --- a/test/Microsoft.Blazor.Build.Test/Microsoft.Blazor.Build.Test.csproj +++ b/test/Microsoft.Blazor.Build.Test/Microsoft.Blazor.Build.Test.csproj @@ -7,7 +7,6 @@ - @@ -22,4 +21,9 @@ + + + + + diff --git a/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs b/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs index 5b91c4235b..8bfba05999 100644 --- a/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs +++ b/test/Microsoft.Blazor.Build.Test/RazorCompilerTest.cs @@ -121,6 +121,19 @@ namespace Microsoft.Blazor.Build.Test node => AssertNode.Text(node, "Third")); } + [Fact] + public void CanRenderElements() + { + // Arrange/Act + var component = CompileToComponent("Hello"); + + // Assert + var nodes = GetRenderTree(component).Where(NotWhitespace); + Assert.Collection(nodes, + node => AssertNode.Element(node, "myelem", 1), + node => AssertNode.Text(node, "Hello")); + } + private static bool NotWhitespace(RenderTreeNode node) => node.NodeType != RenderTreeNodeType.Text || !string.IsNullOrWhiteSpace(node.TextContent); diff --git a/test/Microsoft.Blazor.E2ETest/Tests/StandaloneAppTest.cs b/test/Microsoft.Blazor.E2ETest/Tests/StandaloneAppTest.cs index ded75789c9..b585748ad3 100644 --- a/test/Microsoft.Blazor.E2ETest/Tests/StandaloneAppTest.cs +++ b/test/Microsoft.Blazor.E2ETest/Tests/StandaloneAppTest.cs @@ -27,10 +27,9 @@ namespace Microsoft.Blazor.E2ETest.Tests } [Fact] - public void HasBodyText() + public void HasHeading() { - var bodyText = Browser.FindElement(By.TagName("body")).Text; - Assert.Equal("Hello, world!", bodyText); + Assert.Equal("Hello, world!", Browser.FindElement(By.TagName("h1")).Text); } private void WaitUntilLoaded()