Moved scope writer logic to BasicWriter
This commit is contained in:
parent
27b73d737b
commit
f099232ca4
|
|
@ -24,5 +24,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
|
||||||
public abstract void WriteCSharpExpressionAttributeValue(CSharpRenderingContext context, CSharpExpressionAttributeValueIRNode node);
|
public abstract void WriteCSharpExpressionAttributeValue(CSharpRenderingContext context, CSharpExpressionAttributeValueIRNode node);
|
||||||
|
|
||||||
public abstract void WriteCSharpCodeAttributeValue(CSharpRenderingContext context, CSharpCodeAttributeValueIRNode node);
|
public abstract void WriteCSharpCodeAttributeValue(CSharpRenderingContext context, CSharpCodeAttributeValueIRNode node);
|
||||||
|
|
||||||
|
public abstract void BeginWriterScope(CSharpRenderingContext context, string writer);
|
||||||
|
|
||||||
|
public abstract void EndWriterScope(CSharpRenderingContext context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -269,5 +269,15 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
|
||||||
{
|
{
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void BeginWriterScope(CSharpRenderingContext context, string writer)
|
||||||
|
{
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void EndWriterScope(CSharpRenderingContext context)
|
||||||
|
{
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -269,7 +269,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
|
||||||
|
|
||||||
using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: ValueWriterName))
|
using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: ValueWriterName))
|
||||||
{
|
{
|
||||||
context.Writer.WriteMethodInvocation(PushWriterMethod, ValueWriterName);
|
BeginWriterScope(context, ValueWriterName);
|
||||||
|
|
||||||
for (var i = 0; i < node.Children.Count; i++)
|
for (var i = 0; i < node.Children.Count; i++)
|
||||||
{
|
{
|
||||||
|
|
@ -310,7 +310,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.Writer.WriteMethodInvocation(PopWriterMethod);
|
EndWriterScope(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.Writer.WriteEndMethodInvocation(false);
|
context.Writer.WriteEndMethodInvocation(false);
|
||||||
|
|
@ -364,5 +364,15 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
|
||||||
charactersConsumed += textToRender.Length;
|
charactersConsumed += textToRender.Length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void BeginWriterScope(CSharpRenderingContext context, string writer)
|
||||||
|
{
|
||||||
|
context.Writer.WriteMethodInvocation(PushWriterMethod, writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void EndWriterScope(CSharpRenderingContext context)
|
||||||
|
{
|
||||||
|
context.Writer.WriteMethodInvocation(PopWriterMethod);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
public class TemplateTargetExtension : ITemplateTargetExtension
|
public class TemplateTargetExtension : ITemplateTargetExtension
|
||||||
{
|
{
|
||||||
public static readonly string DefaultTemplateTypeName = "Template";
|
public static readonly string DefaultTemplateTypeName = "Template";
|
||||||
public static readonly string DefaultPushWriterMethod = "PushWriter";
|
|
||||||
public static readonly string DefaultPopWriterMethod = "PopWriter";
|
|
||||||
|
|
||||||
public string TemplateTypeName { get; set; } = DefaultTemplateTypeName;
|
public string TemplateTypeName { get; set; } = DefaultTemplateTypeName;
|
||||||
|
|
||||||
public string PushWriterMethod { get; set; } = DefaultPushWriterMethod;
|
|
||||||
|
|
||||||
public string PopWriterMethod { get; set; } = DefaultPopWriterMethod;
|
|
||||||
|
|
||||||
public void WriteTemplate(CSharpRenderingContext context, TemplateIRNode node)
|
public void WriteTemplate(CSharpRenderingContext context, TemplateIRNode node)
|
||||||
{
|
{
|
||||||
const string ItemParameterName = "item";
|
const string ItemParameterName = "item";
|
||||||
|
|
@ -28,17 +22,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
|
|
||||||
using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName))
|
using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName))
|
||||||
{
|
{
|
||||||
if (!context.Options.DesignTime)
|
context.BasicWriter.BeginWriterScope(context, TemplateWriterName);
|
||||||
{
|
|
||||||
context.Writer.WriteMethodInvocation(PushWriterMethod, TemplateWriterName);
|
|
||||||
}
|
|
||||||
|
|
||||||
context.RenderChildren(node);
|
context.RenderChildren(node);
|
||||||
|
|
||||||
if (!context.Options.DesignTime)
|
context.BasicWriter.EndWriterScope(context);
|
||||||
{
|
|
||||||
context.Writer.WriteMethodInvocation(PopWriterMethod);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context.Writer.WriteEndMethodInvocation(endLine: false);
|
context.Writer.WriteEndMethodInvocation(endLine: false);
|
||||||
|
|
|
||||||
|
|
@ -433,6 +433,7 @@ if (true) { }
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteHtmlContent_RendersContentCorrectly()
|
public void WriteHtmlContent_RendersContentCorrectly()
|
||||||
{
|
{
|
||||||
|
// Arrange
|
||||||
var writer = new RuntimeBasicWriter();
|
var writer = new RuntimeBasicWriter();
|
||||||
var context = new CSharpRenderingContext()
|
var context = new CSharpRenderingContext()
|
||||||
{
|
{
|
||||||
|
|
@ -462,6 +463,7 @@ if (true) { }
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteHtmlContent_LargeStringLiteral_UsesMultipleWrites()
|
public void WriteHtmlContent_LargeStringLiteral_UsesMultipleWrites()
|
||||||
{
|
{
|
||||||
|
// Arrange
|
||||||
var writer = new RuntimeBasicWriter();
|
var writer = new RuntimeBasicWriter();
|
||||||
var context = new CSharpRenderingContext()
|
var context = new CSharpRenderingContext()
|
||||||
{
|
{
|
||||||
|
|
@ -493,6 +495,7 @@ WriteLiteral(@""{1}"");
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteHtmlAttribute_RendersCorrectly()
|
public void WriteHtmlAttribute_RendersCorrectly()
|
||||||
{
|
{
|
||||||
|
// Arrange
|
||||||
var writer = new RuntimeBasicWriter();
|
var writer = new RuntimeBasicWriter();
|
||||||
var context = GetCSharpRenderingContext(writer);
|
var context = GetCSharpRenderingContext(writer);
|
||||||
|
|
||||||
|
|
@ -519,6 +522,7 @@ EndWriteAttribute();
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteHtmlAttributeValue_RendersCorrectly()
|
public void WriteHtmlAttributeValue_RendersCorrectly()
|
||||||
{
|
{
|
||||||
|
// Arrange
|
||||||
var writer = new RuntimeBasicWriter();
|
var writer = new RuntimeBasicWriter();
|
||||||
var context = GetCSharpRenderingContext(writer);
|
var context = GetCSharpRenderingContext(writer);
|
||||||
|
|
||||||
|
|
@ -543,6 +547,7 @@ EndWriteAttribute();
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteCSharpExpressionAttributeValue_RendersCorrectly()
|
public void WriteCSharpExpressionAttributeValue_RendersCorrectly()
|
||||||
{
|
{
|
||||||
|
// Arrange
|
||||||
var writer = new RuntimeBasicWriter();
|
var writer = new RuntimeBasicWriter();
|
||||||
var context = GetCSharpRenderingContext(writer);
|
var context = GetCSharpRenderingContext(writer);
|
||||||
|
|
||||||
|
|
@ -571,6 +576,7 @@ WriteAttributeValue("" "", 27, false, 28, 6, false);
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteCSharpCodeAttributeValue_BuffersResult()
|
public void WriteCSharpCodeAttributeValue_BuffersResult()
|
||||||
{
|
{
|
||||||
|
// Arrange
|
||||||
var writer = new RuntimeBasicWriter();
|
var writer = new RuntimeBasicWriter();
|
||||||
var context = GetCSharpRenderingContext(writer);
|
var context = GetCSharpRenderingContext(writer);
|
||||||
|
|
||||||
|
|
@ -602,6 +608,50 @@ WriteAttributeValue("" "", 27, false, 28, 6, false);
|
||||||
ignoreLineEndingDifferences: true);
|
ignoreLineEndingDifferences: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BeginWriterScope_UsesSpecifiedWriter_RendersCorrectly()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var writer = new RuntimeBasicWriter()
|
||||||
|
{
|
||||||
|
PushWriterMethod = "TestPushWriter"
|
||||||
|
};
|
||||||
|
var context = GetCSharpRenderingContext(writer);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
writer.BeginWriterScope(context, "MyWriter");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var csharp = context.Writer.Builder.ToString();
|
||||||
|
Assert.Equal(
|
||||||
|
@"TestPushWriter(MyWriter);
|
||||||
|
",
|
||||||
|
csharp,
|
||||||
|
ignoreLineEndingDifferences: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void EndWriterScope_RendersCorrectly()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var writer = new RuntimeBasicWriter()
|
||||||
|
{
|
||||||
|
PopWriterMethod = "TestPopWriter"
|
||||||
|
};
|
||||||
|
var context = GetCSharpRenderingContext(writer);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
writer.EndWriterScope(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var csharp = context.Writer.Builder.ToString();
|
||||||
|
Assert.Equal(
|
||||||
|
@"TestPopWriter();
|
||||||
|
",
|
||||||
|
csharp,
|
||||||
|
ignoreLineEndingDifferences: true);
|
||||||
|
}
|
||||||
|
|
||||||
private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer)
|
private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer)
|
||||||
{
|
{
|
||||||
var options = RazorCodeGenerationOptions.CreateDefault();
|
var options = RazorCodeGenerationOptions.CreateDefault();
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,16 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
|
|
||||||
var extension = new TemplateTargetExtension()
|
var extension = new TemplateTargetExtension()
|
||||||
{
|
{
|
||||||
TemplateTypeName = "global::TestTemplate",
|
TemplateTypeName = "global::TestTemplate"
|
||||||
PushWriterMethod = "TestPushWriter",
|
|
||||||
PopWriterMethod = "TestPopWriter"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var context = new CSharpRenderingContext()
|
var context = new CSharpRenderingContext()
|
||||||
{
|
{
|
||||||
BasicWriter = new RuntimeBasicWriter(),
|
BasicWriter = new RuntimeBasicWriter()
|
||||||
|
{
|
||||||
|
PushWriterMethod = "TestPushWriter",
|
||||||
|
PopWriterMethod = "TestPopWriter"
|
||||||
|
},
|
||||||
TagHelperWriter = new RuntimeTagHelperWriter(),
|
TagHelperWriter = new RuntimeTagHelperWriter(),
|
||||||
Writer = new CSharpCodeWriter(),
|
Writer = new CSharpCodeWriter(),
|
||||||
Options = RazorCodeGenerationOptions.CreateDefault(),
|
Options = RazorCodeGenerationOptions.CreateDefault(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue