This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved ExecuteTagHelpersIRNode from renderer to writer
- Loading branch information
1 parent
0481cc6
commit 0228fd2
Showing
10 changed files
with
273 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RedirectedTagHelperWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 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 | ||
{ | ||
internal class RedirectedTagHelperWriter : TagHelperWriter | ||
{ | ||
private readonly TagHelperWriter _previous; | ||
private readonly string _textWriter; | ||
|
||
public RedirectedTagHelperWriter(TagHelperWriter previous, string textWriter) | ||
{ | ||
_previous = previous; | ||
_textWriter = textWriter; | ||
} | ||
|
||
public string ExecutionContextVariableName { get; set; } = "__tagHelperExecutionContext"; | ||
|
||
public string ExecutionContextOutputPropertyName { get; set; } = "Output"; | ||
|
||
public string ExecutionContextSetOutputContentAsyncMethodName { get; set; } = "SetOutputContentAsync"; | ||
|
||
public string RunnerVariableName { get; set; } = "__tagHelperRunner"; | ||
|
||
public string RunnerRunAsyncMethodName { get; set; } = "RunAsync"; | ||
|
||
public string ScopeManagerVariableName { get; set; } = "__tagHelperScopeManager"; | ||
|
||
public string ScopeManagerEndMethodName { get; set; } = "End"; | ||
|
||
public string TagHelperOutputIsContentModifiedPropertyName { get; set; } = "IsContentModified"; | ||
|
||
public string WriteTagHelperOutputMethod { get; set; } = "WriteTo"; | ||
|
||
public override void WriteAddTagHelperHtmlAttribute(CSharpRenderingContext context, AddTagHelperHtmlAttributeIRNode node) | ||
{ | ||
_previous.WriteAddTagHelperHtmlAttribute(context, node); | ||
} | ||
|
||
public override void WriteCreateTagHelper(CSharpRenderingContext context, CreateTagHelperIRNode node) | ||
{ | ||
_previous.WriteCreateTagHelper(context, node); | ||
} | ||
|
||
public override void WriteDeclareTagHelperFields(CSharpRenderingContext context, DeclareTagHelperFieldsIRNode node) | ||
{ | ||
_previous.WriteDeclareTagHelperFields(context, node); | ||
} | ||
|
||
public override void WriteExecuteTagHelpers(CSharpRenderingContext context, ExecuteTagHelpersIRNode node) | ||
{ | ||
if (context.Options.DesignTimeMode) | ||
{ | ||
_previous.WriteExecuteTagHelpers(context, node); | ||
return; | ||
} | ||
|
||
context.Writer | ||
.Write("await ") | ||
.WriteStartInstanceMethodInvocation( | ||
RunnerVariableName, | ||
RunnerRunAsyncMethodName) | ||
.Write(ExecutionContextVariableName) | ||
.WriteEndMethodInvocation(); | ||
|
||
var tagHelperOutputAccessor = $"{ExecutionContextVariableName}.{ExecutionContextOutputPropertyName}"; | ||
|
||
context.Writer | ||
.Write("if (!") | ||
.Write(tagHelperOutputAccessor) | ||
.Write(".") | ||
.Write(TagHelperOutputIsContentModifiedPropertyName) | ||
.WriteLine(")"); | ||
|
||
using (context.Writer.BuildScope()) | ||
{ | ||
context.Writer | ||
.Write("await ") | ||
.WriteInstanceMethodInvocation( | ||
ExecutionContextVariableName, | ||
ExecutionContextSetOutputContentAsyncMethodName); | ||
} | ||
|
||
context.Writer | ||
.WriteStartMethodInvocation(WriteTagHelperOutputMethod) | ||
.Write(_textWriter) | ||
.WriteParameterSeparator() | ||
.Write(tagHelperOutputAccessor) | ||
.WriteEndMethodInvocation() | ||
.WriteStartAssignment(ExecutionContextVariableName) | ||
.WriteInstanceMethodInvocation( | ||
ScopeManagerVariableName, | ||
ScopeManagerEndMethodName); | ||
} | ||
|
||
public override void WriteInitializeTagHelperStructure(CSharpRenderingContext context, InitializeTagHelperStructureIRNode node) | ||
{ | ||
_previous.WriteInitializeTagHelperStructure(context, node); | ||
} | ||
|
||
public override void WriteSetTagHelperProperty(CSharpRenderingContext context, SetTagHelperPropertyIRNode node) | ||
{ | ||
_previous.WriteSetTagHelperProperty(context, node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...Microsoft.AspNetCore.Razor.Evolution.Test/CodeGeneration/RedirectedTagHelperWriterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 Microsoft.AspNetCore.Razor.Evolution.Intermediate; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration | ||
{ | ||
public class RedirectedTagHelperWriterTest | ||
{ | ||
// In design time this will not include the 'text writer' parameter. | ||
[Fact] | ||
public void WriteExecuteTagHelpers_DesignTime_DoesNormalWrite() | ||
{ | ||
// Arrange | ||
var writer = new RedirectedTagHelperWriter(new DesignTimeTagHelperWriter(), "test_writer") | ||
{ | ||
WriteTagHelperOutputMethod = "Test", | ||
}; | ||
|
||
var context = new CSharpRenderingContext() | ||
{ | ||
Options = RazorParserOptions.CreateDefaultOptions(), | ||
Writer = new Legacy.CSharpCodeWriter(), | ||
}; | ||
|
||
context.Options.DesignTimeMode = true; | ||
|
||
var node = new ExecuteTagHelpersIRNode(); | ||
|
||
// Act | ||
writer.WriteExecuteTagHelpers(context, node); | ||
|
||
// Assert | ||
var csharp = context.Writer.Builder.ToString(); | ||
Assert.Empty(csharp); | ||
} | ||
|
||
[Fact] | ||
public void WriteExecuteTagHelpers_Runtime_RendersWithRedirectWriter() | ||
{ | ||
// Arrange | ||
var writer = new RedirectedTagHelperWriter(new RuntimeTagHelperWriter(), "test_writer") | ||
{ | ||
WriteTagHelperOutputMethod = "Test", | ||
}; | ||
|
||
var context = new CSharpRenderingContext() | ||
{ | ||
Options = RazorParserOptions.CreateDefaultOptions(), | ||
Writer = new Legacy.CSharpCodeWriter(), | ||
}; | ||
|
||
var node = new ExecuteTagHelpersIRNode(); | ||
|
||
// Act | ||
writer.WriteExecuteTagHelpers(context, node); | ||
|
||
// Assert | ||
var csharp = context.Writer.Builder.ToString(); | ||
Assert.Equal( | ||
@"await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); | ||
if (!__tagHelperExecutionContext.Output.IsContentModified) | ||
{ | ||
await __tagHelperExecutionContext.SetOutputContentAsync(); | ||
} | ||
Test(test_writer, __tagHelperExecutionContext.Output); | ||
__tagHelperExecutionContext = __tagHelperScopeManager.End(); | ||
", | ||
csharp, | ||
ignoreLineEndingDifferences: true); | ||
} | ||
} | ||
} |
Oops, something went wrong.