Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Moved HtmlContentIRNode from renderer to writer
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed Apr 7, 2017
1 parent ea3b7b0 commit fe60c24
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ public override void VisitCSharpStatement(CSharpStatementIRNode node)
Context.BasicWriter.WriteCSharpStatement(Context, node);
}

public override void VisitHtml(HtmlContentIRNode node)
{
Context.BasicWriter.WriteHtmlContent(Context, node);
}

public override void VisitDeclareTagHelperFields(DeclareTagHelperFieldsIRNode node)
{
Context.TagHelperWriter.WriteDeclareTagHelperFields(Context, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public override void WriteHtmlAttribute(CSharpRenderingContext context, HtmlAttr

public override void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIRNode node)
{
throw new NotImplementedException();
// Do nothing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@

namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{
internal class RedirectedBasicWriter : BasicWriter
internal class RedirectedRuntimeBasicWriter : RuntimeBasicWriter
{
private readonly BasicWriter _previous;
private readonly string _textWriter;

public RedirectedBasicWriter(BasicWriter previous, string textWriter)
public RedirectedRuntimeBasicWriter(string textWriter)
{
_previous = previous;
_textWriter = textWriter;
}

public string WriteCSharpExpressionMethod { get; set; } = "WriteTo";
public new string WriteCSharpExpressionMethod { get; set; } = "WriteTo";

public new string WriteHtmlContentMethod { get; set; } = "WriteLiteralTo";

public override void WriteCSharpExpression(CSharpRenderingContext context, CSharpExpressionIRNode node)
{
if (context.Options.DesignTimeMode)
{
_previous.WriteCSharpExpression(context, node);
return;
}

IDisposable linePragmaScope = null;
if (node.Source != null)
{
Expand Down Expand Up @@ -58,19 +52,35 @@ public override void WriteCSharpExpression(CSharpRenderingContext context, CShar
linePragmaScope?.Dispose();
}

public override void WriteCSharpStatement(CSharpRenderingContext context, CSharpStatementIRNode node)
public override void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIRNode node)
{
_previous.WriteCSharpStatement(context, node);
}
const int MaxStringLiteralLength = 1024;

public override void WriteHtmlAttribute(CSharpRenderingContext context, HtmlAttributeIRNode node)
{
_previous.WriteHtmlAttribute(context, node);
}
var charactersConsumed = 0;

public override void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIRNode node)
{
_previous.WriteHtmlContent(context, node);
// Render the string in pieces to avoid Roslyn OOM exceptions at compile time: https://github.com/aspnet/External/issues/54
while (charactersConsumed < node.Content.Length)
{
string textToRender;
if (node.Content.Length <= MaxStringLiteralLength)
{
textToRender = node.Content;
}
else
{
var charactersToSubstring = Math.Min(MaxStringLiteralLength, node.Content.Length - charactersConsumed);
textToRender = node.Content.Substring(charactersConsumed, charactersToSubstring);
}

context.Writer
.WriteStartMethodInvocation(WriteHtmlContentMethod)
.Write(_textWriter)
.WriteParameterSeparator()
.WriteStringLiteral(textToRender)
.WriteEndMethodInvocation();

charactersConsumed += textToRender.Length;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 RedirectedRuntimeTagHelperWriter : RuntimeTagHelperWriter
{
private readonly string _textWriter;

public RedirectedRuntimeTagHelperWriter(string textWriter)
{
_textWriter = textWriter;
}

public new string WriteTagHelperOutputMethod { get; set; } = "WriteTo";

public override void WriteExecuteTagHelpers(CSharpRenderingContext context, ExecuteTagHelpersIRNode node)
{
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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class RuntimeBasicWriter : BasicWriter
{
public string WriteCSharpExpressionMethod { get; set; } = "Write";

public string WriteHtmlContentMethod { get; set; } = "WriteLiteral";

public override void WriteCSharpExpression(CSharpRenderingContext context, CSharpExpressionIRNode node)
{
if (context == null)
Expand Down Expand Up @@ -102,7 +104,31 @@ public override void WriteHtmlAttribute(CSharpRenderingContext context, HtmlAttr

public override void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIRNode node)
{
throw new NotImplementedException();
const int MaxStringLiteralLength = 1024;

var charactersConsumed = 0;

// Render the string in pieces to avoid Roslyn OOM exceptions at compile time: https://github.com/aspnet/External/issues/54
while (charactersConsumed < node.Content.Length)
{
string textToRender;
if (node.Content.Length <= MaxStringLiteralLength)
{
textToRender = node.Content;
}
else
{
var charactersToSubstring = Math.Min(MaxStringLiteralLength, node.Content.Length - charactersConsumed);
textToRender = node.Content.Substring(charactersConsumed, charactersToSubstring);
}

context.Writer
.WriteStartMethodInvocation(WriteHtmlContentMethod)
.WriteStringLiteral(textToRender)
.WriteEndMethodInvocation();

charactersConsumed += textToRender.Length;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public override void VisitChecksum(ChecksumIRNode node)

public override void VisitHtml(HtmlContentIRNode node)
{
// We can't remove this yet, because it's still used recursively in a few places.
const int MaxStringLiteralLength = 1024;

var charactersConsumed = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 Microsoft.AspNetCore.Razor.Evolution.Intermediate;

namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
Expand All @@ -23,15 +24,22 @@ public void WriteTemplate(CSharpRenderingContext context, TemplateIRNode node)
var initialRenderingConventions = context.RenderingConventions;
context.RenderingConventions = new CSharpRedirectRenderingConventions(TemplateWriterName, context.Writer);

using (context.Push(new RedirectedBasicWriter(context.BasicWriter, TemplateWriterName)))
using (context.Push(new RedirectedTagHelperWriter(context.TagHelperWriter, TemplateWriterName)))
IDisposable basicWriterScope = null;
IDisposable tagHelperWriterScope = null;
if (!context.Options.DesignTimeMode)
{
using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName))
{
context.RenderChildren(node);
}
basicWriterScope = context.Push(new RedirectedRuntimeBasicWriter(TemplateWriterName));
tagHelperWriterScope = context.Push(new RedirectedRuntimeTagHelperWriter(TemplateWriterName));
}

using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName))
{
context.RenderChildren(node);
}

basicWriterScope?.Dispose();
tagHelperWriterScope?.Dispose();

context.RenderingConventions = initialRenderingConventions;

context.Writer.WriteEndMethodInvocation(endLine: false);
Expand Down
Loading

0 comments on commit fe60c24

Please sign in to comment.