In RazorCompiler, support HTML elements
This commit is contained in:
parent
75b083911c
commit
a64ece0319
|
|
@ -1 +1 @@
|
|||
Hello, world!
|
||||
<h1>Hello, world!</h1>
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AngleSharp" Version="0.9.9.1" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.6.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
|
|
@ -22,4 +21,9 @@
|
|||
<Compile Include="..\shared\AssertNode.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\anglesharp\AngleSharpBuilder\AngleSharpBuilder.csproj" ReferenceOutputAssembly="false" />
|
||||
<Reference Include="AngleSharp" HintPath="..\..\src\anglesharp\AngleSharpBuilder\dist\AngleSharp.dll" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -121,6 +121,19 @@ namespace Microsoft.Blazor.Build.Test
|
|||
node => AssertNode.Text(node, "Third"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanRenderElements()
|
||||
{
|
||||
// Arrange/Act
|
||||
var component = CompileToComponent("<myelem>Hello</myelem>");
|
||||
|
||||
// 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);
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue