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 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing buffering in TagHelperOutput.PreContent, Content, PostCon…
…tent.
- Loading branch information
sornaks
committed
Mar 4, 2015
1 parent
7ecce1b
commit bd9d57d
Showing
26 changed files
with
1,159 additions
and
469 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.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,150 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.Framework.Internal; | ||
|
||
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Default concrete <see cref="TagHelperContent"/>. | ||
/// </summary> | ||
public class DefaultTagHelperContent : TagHelperContent, ITextWriterCopyable | ||
{ | ||
private readonly BufferEntryCollection _buffer; | ||
|
||
/// <summary> | ||
/// Instantiates a new instance of <see cref="DefaultTagHelperContent"/>. | ||
/// </summary> | ||
public DefaultTagHelperContent() | ||
{ | ||
_buffer = new BufferEntryCollection(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool IsModified | ||
{ | ||
get | ||
{ | ||
return _buffer.IsModified; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool IsWhiteSpace | ||
{ | ||
get | ||
{ | ||
foreach (var value in _buffer) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool IsEmpty | ||
{ | ||
get | ||
{ | ||
foreach (var value in _buffer) | ||
{ | ||
if (!string.IsNullOrEmpty(value)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override TagHelperContent SetContent(string value) | ||
{ | ||
Clear(); | ||
Append(value); | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override TagHelperContent SetContent(TagHelperContent tagHelperContent) | ||
{ | ||
Clear(); | ||
Append(tagHelperContent); | ||
return this; | ||
} | ||
|
||
|
||
/// <inheritdoc /> | ||
public override TagHelperContent Append(string value) | ||
{ | ||
_buffer.Add(value); | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override TagHelperContent Append(TagHelperContent tagHelperContent) | ||
{ | ||
if (tagHelperContent != null) | ||
{ | ||
foreach (var value in tagHelperContent) | ||
{ | ||
Append(value); | ||
} | ||
} | ||
|
||
// If Append() was called with an empty TagHelperContent IsModified should | ||
// still be true. If the content was not already modified, it means it is empty. | ||
// So the Clear() method can be used to indirectly set the IsModified. | ||
if (!IsModified) | ||
{ | ||
Clear(); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override TagHelperContent Clear() | ||
{ | ||
_buffer.Clear(); | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string GetContent() | ||
{ | ||
return string.Join(string.Empty, _buffer); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void CopyTo([NotNull] TextWriter writer) | ||
{ | ||
foreach (var value in _buffer) | ||
{ | ||
writer.Write(value); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
{ | ||
return GetContent(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IEnumerator<string> GetEnumerator() | ||
{ | ||
// The enumerator is exposed so that SetContent(TagHelperContent) and Append(TagHelperContent) | ||
// can use this to iterate through the values of the buffer. | ||
return _buffer.GetEnumerator(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Microsoft.AspNet.Razor.Runtime/TagHelpers/ITextWriterCopyable.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,19 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.IO; | ||
|
||
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Specifies the contract for copying to a <see cref="TextWriter"/>. | ||
/// </summary> | ||
public interface ITextWriterCopyable | ||
{ | ||
/// <summary> | ||
/// Copies to the <paramref name="writer"/>. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="TextWriter"/> target.</param> | ||
void CopyTo(TextWriter writer); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.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,78 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNet.Razor.Runtime.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Abstract class used to buffer content returned by <see cref="ITagHelper"/>s. | ||
/// </summary> | ||
public abstract class TagHelperContent : IEnumerable<string> | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether the content was modifed. | ||
/// </summary> | ||
public abstract bool IsModified { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the content is empty. | ||
/// </summary> | ||
public abstract bool IsEmpty { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the content is whitespace. | ||
/// </summary> | ||
public abstract bool IsWhiteSpace { get; } | ||
|
||
/// <summary> | ||
/// Sets the content. | ||
/// </summary> | ||
/// <param name="value">The <see cref="string"/> that replaces the content.</param> | ||
/// <returns>A reference to this instance after the set operation has completed.</returns> | ||
public abstract TagHelperContent SetContent(string value); | ||
|
||
/// <summary> | ||
/// Sets the content. | ||
/// </summary> | ||
/// <param name="tagHelperContent">The <see cref="TagHelperContent"/> that replaces the content.</param> | ||
/// <returns>A reference to this instance after the set operation has completed.</returns> | ||
public abstract TagHelperContent SetContent(TagHelperContent tagHelperContent); | ||
|
||
/// <summary> | ||
/// Appends <paramref name="value"/> to the existing content. | ||
/// </summary> | ||
/// <param name="value">The <see cref="string"/> to be appended.</param> | ||
/// <returns>A reference to this instance after the append operation has completed.</returns> | ||
public abstract TagHelperContent Append(string value); | ||
|
||
/// <summary> | ||
/// Appends <paramref name="tagHelperContent"/> to the existing content. | ||
/// </summary> | ||
/// <param name="tagHelperContent">The <see cref="TagHelperContent"/> to be appended.</param> | ||
/// <returns>A reference to this instance after the append operation has completed.</returns> | ||
public abstract TagHelperContent Append(TagHelperContent tagHelperContent); | ||
|
||
/// <summary> | ||
/// Clears the content. | ||
/// </summary> | ||
/// <returns>A reference to this instance after the clear operation has completed.</returns> | ||
public abstract TagHelperContent Clear(); | ||
|
||
/// <summary> | ||
/// Gets the content. | ||
/// </summary> | ||
/// <returns>A <see cref="string"/> containing the content.</returns> | ||
public abstract string GetContent(); | ||
|
||
/// <inheritdoc /> | ||
public abstract IEnumerator<string> GetEnumerator(); | ||
|
||
/// <inheritdoc /> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.