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

Commit

Permalink
Added PushWriter and PopWriter to RazorPageBase
Browse files Browse the repository at this point in the history
- Removed WriteXYZTo methods
  • Loading branch information
ajaybhargavb committed May 11, 2017
1 parent 42a4e9a commit 5eabae5
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 153 deletions.
196 changes: 48 additions & 148 deletions src/Microsoft.AspNetCore.Mvc.Razor/RazorPageBase.cs
Expand Up @@ -28,6 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// </summary>
public abstract class RazorPageBase : IRazorPage
{
private readonly Stack<TextWriter> _textWriterStack = new Stack<TextWriter>();
private StringWriter _valueBuffer;
private ITagHelperFactory _tagHelperFactory;
private IViewBufferScope _bufferScope;
Expand Down Expand Up @@ -275,6 +276,25 @@ public string EndWriteTagHelperAttribute()
return content;
}

// Internal for unit testing.
protected internal virtual void PushWriter(TextWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

_textWriterStack.Push(ViewContext.Writer);
ViewContext.Writer = writer;
}

// Internal for unit testing.
protected internal virtual TextWriter PopWriter()
{
ViewContext.Writer = _textWriterStack.Pop();
return ViewContext.Writer;
}

public virtual string Href(string contentPath)
{
if (contentPath == null)
Expand Down Expand Up @@ -335,63 +355,14 @@ public virtual void DefineSection(string name, RenderAsyncDelegate section)
/// <param name="value">The <see cref="object"/> to write.</param>
public virtual void Write(object value)
{
WriteTo(Output, value);
}

/// <summary>
/// Writes the specified <paramref name="value"/> with HTML encoding to <paramref name="writer"/>.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
/// <param name="value">The <see cref="object"/> to write.</param>
/// <remarks>
/// <paramref name="value"/>s of type <see cref="IHtmlContent"/> are written using
/// <see cref="IHtmlContent.WriteTo(TextWriter, HtmlEncoder)"/>.
/// For all other types, the encoded result of <see cref="object.ToString"/> is written to the
/// <paramref name="writer"/>.
/// </remarks>
public virtual void WriteTo(TextWriter writer, object value)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

WriteTo(writer, HtmlEncoder, value);
}

/// <summary>
/// Writes the specified <paramref name="value"/> with HTML encoding to given <paramref name="writer"/>.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
/// <param name="encoder">
/// The <see cref="System.Text.Encodings.Web.HtmlEncoder"/> to use when encoding <paramref name="value"/>.
/// </param>
/// <param name="value">The <see cref="object"/> to write.</param>
/// <remarks>
/// <paramref name="value"/>s of type <see cref="IHtmlContent"/> are written using
/// <see cref="IHtmlContent.WriteTo(TextWriter, HtmlEncoder)"/>.
/// For all other types, the encoded result of <see cref="object.ToString"/> is written to the
/// <paramref name="writer"/>.
/// </remarks>
public static void WriteTo(TextWriter writer, HtmlEncoder encoder, object value)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}

if (value == null || value == HtmlString.Empty)
{
return;
}

var htmlContent = value as IHtmlContent;
if (htmlContent != null)
var writer = Output;
var encoder = HtmlEncoder;
if (value is IHtmlContent htmlContent)
{
var bufferedWriter = writer as ViewBufferTextWriter;
if (bufferedWriter == null || !bufferedWriter.IsBuffering)
Expand All @@ -400,8 +371,7 @@ public static void WriteTo(TextWriter writer, HtmlEncoder encoder, object value)
}
else
{
var htmlContentContainer = value as IHtmlContentContainer;
if (htmlContentContainer != null)
if (value is IHtmlContentContainer htmlContentContainer)
{
// This is likely another ViewBuffer.
htmlContentContainer.MoveTo(bufferedWriter.Buffer);
Expand All @@ -417,26 +387,17 @@ public static void WriteTo(TextWriter writer, HtmlEncoder encoder, object value)
return;
}

WriteTo(writer, encoder, value.ToString());
Write(value.ToString());
}

/// <summary>
/// Writes the specified <paramref name="value"/> with HTML encoding to <paramref name="writer"/>.
/// Writes the specified <paramref name="value"/> with HTML encoding to <see cref="Output"/>.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
/// <param name="value">The <see cref="string"/> to write.</param>
public virtual void WriteTo(TextWriter writer, string value)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

WriteTo(writer, HtmlEncoder, value);
}

private static void WriteTo(TextWriter writer, HtmlEncoder encoder, string value)
public virtual void Write(string value)
{
var writer = Output;
var encoder = HtmlEncoder;
if (!string.IsNullOrEmpty(value))
{
// Perf: Encode right away instead of writing it character-by-character.
Expand All @@ -452,42 +413,18 @@ private static void WriteTo(TextWriter writer, HtmlEncoder encoder, string value
/// <param name="value">The <see cref="object"/> to write.</param>
public virtual void WriteLiteral(object value)
{
WriteLiteralTo(Output, value);
}

/// <summary>
/// Writes the specified <paramref name="value"/> without HTML encoding to the <paramref name="writer"/>.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
/// <param name="value">The <see cref="object"/> to write.</param>
public virtual void WriteLiteralTo(TextWriter writer, object value)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

if (value != null)
{
WriteLiteralTo(writer, value.ToString());
}
WriteLiteral(value.ToString());
}

/// <summary>
/// Writes the specified <paramref name="value"/> without HTML encoding to <see cref="Output"/>.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
/// <param name="value">The <see cref="string"/> to write.</param>
public virtual void WriteLiteralTo(TextWriter writer, string value)
public virtual void WriteLiteral(string value)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

if (!string.IsNullOrEmpty(value))
{
writer.Write(value);
Output.Write(value);
}
}

Expand All @@ -499,23 +436,6 @@ public virtual void WriteLiteralTo(TextWriter writer, string value)
int suffixOffset,
int attributeValuesCount)
{
BeginWriteAttributeTo(Output, name, prefix, prefixOffset, suffix, suffixOffset, attributeValuesCount);
}

public virtual void BeginWriteAttributeTo(
TextWriter writer,
string name,
string prefix,
int prefixOffset,
string suffix,
int suffixOffset,
int attributeValuesCount)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

if (prefix == null)
{
throw new ArgumentNullException(nameof(prefix));
Expand All @@ -532,7 +452,7 @@ public virtual void WriteLiteralTo(TextWriter writer, string value)
// null or false. Consequently defer the prefix generation until we encounter the attribute value.
if (attributeValuesCount != 1)
{
WritePositionTaggedLiteral(writer, prefix, prefixOffset);
WritePositionTaggedLiteral(prefix, prefixOffset);
}
}

Expand All @@ -543,18 +463,6 @@ public virtual void WriteLiteralTo(TextWriter writer, string value)
int valueOffset,
int valueLength,
bool isLiteral)
{
WriteAttributeValueTo(Output, prefix, prefixOffset, value, valueOffset, valueLength, isLiteral);
}

public void WriteAttributeValueTo(
TextWriter writer,
string prefix,
int prefixOffset,
object value,
int valueOffset,
int valueLength,
bool isLiteral)
{
if (_attributeInfo.AttributeValuesCount == 1)
{
Expand All @@ -566,7 +474,7 @@ public virtual void WriteLiteralTo(TextWriter writer, string value)
}

// We are not omitting the attribute. Write the prefix.
WritePositionTaggedLiteral(writer, _attributeInfo.Prefix, _attributeInfo.PrefixOffset);
WritePositionTaggedLiteral(_attributeInfo.Prefix, _attributeInfo.PrefixOffset);

if (IsBoolTrueWithEmptyPrefixValue(prefix, value))
{
Expand All @@ -582,32 +490,22 @@ public virtual void WriteLiteralTo(TextWriter writer, string value)
{
if (!string.IsNullOrEmpty(prefix))
{
WritePositionTaggedLiteral(writer, prefix, prefixOffset);
WritePositionTaggedLiteral(prefix, prefixOffset);
}

BeginContext(valueOffset, valueLength, isLiteral);

WriteUnprefixedAttributeValueTo(writer, value, isLiteral);
WriteUnprefixedAttributeValue(value, isLiteral);

EndContext();
}
}

public virtual void EndWriteAttribute()
{
EndWriteAttributeTo(Output);
}

public virtual void EndWriteAttributeTo(TextWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

if (!_attributeInfo.Suppressed)
{
WritePositionTaggedLiteral(writer, _attributeInfo.Suffix, _attributeInfo.SuffixOffset);
WritePositionTaggedLiteral(_attributeInfo.Suffix, _attributeInfo.SuffixOffset);
}
}

Expand Down Expand Up @@ -668,12 +566,14 @@ public virtual void EndWriteAttributeTo(TextWriter writer)
_valueBuffer = new StringWriter();
}

PushWriter(_valueBuffer);
if (!string.IsNullOrEmpty(prefix))
{
WriteLiteralTo(_valueBuffer, prefix);
WriteLiteral(prefix);
}

WriteUnprefixedAttributeValueTo(_valueBuffer, value, isLiteral);
WriteUnprefixedAttributeValue(value, isLiteral);
PopWriter();
}
}

Expand Down Expand Up @@ -738,33 +638,33 @@ public virtual HtmlString SetAntiforgeryCookieAndHeader()
return HtmlString.Empty;
}

private void WriteUnprefixedAttributeValueTo(TextWriter writer, object value, bool isLiteral)
private void WriteUnprefixedAttributeValue(object value, bool isLiteral)
{
var stringValue = value as string;

// The extra branching here is to ensure that we call the Write*To(string) overload where possible.
if (isLiteral && stringValue != null)
{
WriteLiteralTo(writer, stringValue);
WriteLiteral(stringValue);
}
else if (isLiteral)
{
WriteLiteralTo(writer, value);
WriteLiteral(value);
}
else if (stringValue != null)
{
WriteTo(writer, stringValue);
Write(stringValue);
}
else
{
WriteTo(writer, value);
Write(value);
}
}

private void WritePositionTaggedLiteral(TextWriter writer, string value, int position)
private void WritePositionTaggedLiteral(string value, int position)
{
BeginContext(position, value.Length, isLiteral: true);
WriteLiteralTo(writer, value);
WriteLiteral(value);
EndContext();
}

Expand Down

0 comments on commit 5eabae5

Please sign in to comment.