Introducing DocumentWriter
This commit is contained in:
parent
29d2defd11
commit
7730d33482
|
|
@ -0,0 +1,88 @@
|
|||
// 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 System;
|
||||
using Microsoft.AspNetCore.Razor.Evolution.Intermediate;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
||||
{
|
||||
internal class DefaultDocumentWriter : DocumentWriter
|
||||
{
|
||||
private readonly CSharpRenderingContext _context;
|
||||
private readonly RuntimeTarget _target;
|
||||
|
||||
private readonly PageStructureCSharpRenderer _renderer;
|
||||
|
||||
public DefaultDocumentWriter(RuntimeTarget target, CSharpRenderingContext context, PageStructureCSharpRenderer renderer)
|
||||
{
|
||||
if (target == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(target));
|
||||
}
|
||||
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
if (renderer == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(renderer));
|
||||
}
|
||||
|
||||
_target = target;
|
||||
_context = context;
|
||||
_renderer = renderer;
|
||||
}
|
||||
|
||||
public override void WriteDocument(DocumentIRNode node)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(node));
|
||||
}
|
||||
|
||||
var visitor = new Visitor(_target, _context, _renderer);
|
||||
_context.RenderChildren = visitor.RenderChildren;
|
||||
|
||||
visitor.VisitDocument(node);
|
||||
_context.RenderChildren = null;
|
||||
}
|
||||
|
||||
private class Visitor : RazorIRNodeVisitor
|
||||
{
|
||||
private readonly CSharpRenderingContext _context;
|
||||
private readonly RuntimeTarget _target;
|
||||
|
||||
private readonly PageStructureCSharpRenderer _renderer;
|
||||
|
||||
public Visitor(RuntimeTarget target, CSharpRenderingContext context, PageStructureCSharpRenderer renderer)
|
||||
{
|
||||
_target = target;
|
||||
_context = context;
|
||||
_renderer = renderer;
|
||||
}
|
||||
|
||||
public void RenderChildren(RazorIRNode node)
|
||||
{
|
||||
for (var i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
var child = node.Children[i];
|
||||
Visit(child);
|
||||
}
|
||||
}
|
||||
|
||||
public override void VisitDocument(DocumentIRNode node)
|
||||
{
|
||||
RenderChildren(node);
|
||||
}
|
||||
|
||||
public override void VisitDefault(RazorIRNode node)
|
||||
{
|
||||
// This is a temporary bridge to the renderer, which allows us to move functionality piecemeal
|
||||
// into this class.
|
||||
_renderer.Visit(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
// 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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
|
|
@ -18,16 +19,19 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
public IRuntimeTargetExtension[] Extensions { get; }
|
||||
|
||||
internal override PageStructureCSharpRenderer CreateRenderer(CSharpRenderingContext context)
|
||||
public override DocumentWriter CreateWriter(CSharpRenderingContext context)
|
||||
{
|
||||
PageStructureCSharpRenderer renderer;
|
||||
if (_options.DesignTimeMode)
|
||||
{
|
||||
return new DesignTimeCSharpRenderer(this, context);
|
||||
renderer = new DesignTimeCSharpRenderer(this, context);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new RuntimeCSharpRenderer(this, context);
|
||||
renderer = new RuntimeCSharpRenderer(this, context);
|
||||
}
|
||||
|
||||
return new DefaultDocumentWriter(this, context, renderer);
|
||||
}
|
||||
|
||||
public override TExtension GetExtension<TExtension>()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
// 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.AspNetCore.Razor.Evolution.Intermediate;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
||||
{
|
||||
public abstract class DocumentWriter
|
||||
{
|
||||
public abstract void WriteDocument(DocumentIRNode node);
|
||||
}
|
||||
}
|
||||
|
|
@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
}
|
||||
|
||||
internal abstract PageStructureCSharpRenderer CreateRenderer(CSharpRenderingContext context);
|
||||
public abstract DocumentWriter CreateWriter(CSharpRenderingContext context);
|
||||
|
||||
public abstract TExtension GetExtension<TExtension>() where TExtension : class, IRuntimeTargetExtension;
|
||||
|
||||
|
|
|
|||
|
|
@ -56,10 +56,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
renderingContext.IdGenerator = () => idValue.ToString();
|
||||
}
|
||||
|
||||
var renderer = target.CreateRenderer(renderingContext);
|
||||
renderingContext.RenderChildren = renderer.VisitDefault;
|
||||
|
||||
renderer.VisitDocument(irDocument);
|
||||
var documentWriter = target.CreateWriter(renderingContext);
|
||||
documentWriter.WriteDocument(irDocument);
|
||||
|
||||
var diagnostics = new List<RazorDiagnostic>();
|
||||
diagnostics.AddRange(syntaxTree.Diagnostics);
|
||||
|
|
|
|||
|
|
@ -28,35 +28,18 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateRenderer_DesignTime_CreatesDesignTimeRenderer()
|
||||
public void CreateWriter_CreatesDefaultDocumentWriter()
|
||||
{
|
||||
// Arrange
|
||||
var options = RazorParserOptions.CreateDefaultOptions();
|
||||
options.DesignTimeMode = true;
|
||||
|
||||
var target = new DefaultRuntimeTarget(options, Enumerable.Empty<IRuntimeTargetExtension>());
|
||||
|
||||
// Act
|
||||
var renderer = target.CreateRenderer(new CSharpRenderingContext());
|
||||
var writer = target.CreateWriter(new CSharpRenderingContext());
|
||||
|
||||
// Assert
|
||||
Assert.IsType<DesignTimeCSharpRenderer>(renderer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateRenderer_Runtime_CreatesRuntimeRenderer()
|
||||
{
|
||||
// Arrange
|
||||
var options = RazorParserOptions.CreateDefaultOptions();
|
||||
options.DesignTimeMode = false;
|
||||
|
||||
var target = new DefaultRuntimeTarget(options, Enumerable.Empty<IRuntimeTargetExtension>());
|
||||
|
||||
// Act
|
||||
var renderer = target.CreateRenderer(new CSharpRenderingContext());
|
||||
|
||||
// Assert
|
||||
Assert.IsType<RuntimeCSharpRenderer>(renderer);
|
||||
Assert.IsType<DefaultDocumentWriter>(writer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in New Issue