diff --git a/src/Microsoft.AspNet.Html.Abstractions/HtmlContentBuilderExtensions.cs b/src/Microsoft.AspNet.Html.Abstractions/HtmlContentBuilderExtensions.cs new file mode 100644 index 0000000000..bfd5e3ef03 --- /dev/null +++ b/src/Microsoft.AspNet.Html.Abstractions/HtmlContentBuilderExtensions.cs @@ -0,0 +1,136 @@ +// 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.Diagnostics; +using System.IO; +using Microsoft.Framework.WebEncoders; + +namespace Microsoft.AspNet.Html.Abstractions +{ + /// + /// Extension methods for . + /// + public static class HtmlContentBuilderExtensions + { + /// + /// Appends an . + /// + /// The . + /// The . + public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder) + { + builder.Append(HtmlEncodedString.NewLine); + return builder; + } + + /// + /// Appends an after appending the value. + /// The value is treated as unencoded as-provided, and will be HTML encoded before writing to output. + /// + /// The . + /// The to append. + /// The . + public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, string unencoded) + { + builder.Append(unencoded); + builder.Append(HtmlEncodedString.NewLine); + return builder; + } + + /// + /// Appends an after appending the value. + /// + /// The . + /// The to append. + /// The . + public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, IHtmlContent htmlContent) + { + builder.Append(htmlContent); + builder.Append(HtmlEncodedString.NewLine); + return builder; + } + + /// + /// Appends an after appending the value. + /// The value is treated as HTML encoded as-provided, and no further encoding will be performed. + /// + /// The . + /// The HTML encoded to append. + /// The . + public static IHtmlContentBuilder AppendLineEncoded(this IHtmlContentBuilder builder, string encoded) + { + builder.AppendEncoded(encoded); + builder.Append(HtmlEncodedString.NewLine); + return builder; + } + + /// + /// Sets the content to the value. The value is treated as unencoded as-provided, + /// and will be HTML encoded before writing to output. + /// + /// The . + /// The value that replaces the content. + /// The . + public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, string unencoded) + { + builder.Clear(); + builder.Append(unencoded); + return builder; + } + + /// + /// Sets the content to the value. + /// + /// The . + /// The value that replaces the content. + /// The . + public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, IHtmlContent content) + { + builder.Clear(); + builder.Append(content); + return builder; + } + + /// + /// Sets the content to the value. The value is treated as HTML encoded as-provided, and + /// no further encoding will be performed. + /// + /// The . + /// The HTML encoded that replaces the content. + /// The . + public static IHtmlContentBuilder SetContentEncoded(this IHtmlContentBuilder builder, string encoded) + { + builder.Clear(); + builder.AppendEncoded(encoded); + return builder; + } + + [DebuggerDisplay("{DebuggerToString()}")] + private class HtmlEncodedString : IHtmlContent + { + public static readonly IHtmlContent NewLine = new HtmlEncodedString(Environment.NewLine); + + private readonly string _value; + + public HtmlEncodedString(string value) + { + _value = value; + } + + public void WriteTo(TextWriter writer, IHtmlEncoder encoder) + { + writer.Write(_value); + } + + private string DebuggerToString() + { + using (var writer = new StringWriter()) + { + WriteTo(writer, HtmlEncoder.Default); + return writer.ToString(); + } + } + } + } +} diff --git a/src/Microsoft.AspNet.Html.Abstractions/IHtmlContentBuilder.cs b/src/Microsoft.AspNet.Html.Abstractions/IHtmlContentBuilder.cs index 0d558f2006..aea0291e70 100644 --- a/src/Microsoft.AspNet.Html.Abstractions/IHtmlContentBuilder.cs +++ b/src/Microsoft.AspNet.Html.Abstractions/IHtmlContentBuilder.cs @@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Html.Abstractions /// Appends a value. The value is treated as unencoded as-provided, and will be HTML /// encoded before writing to output. /// - /// The to append. + /// The to append. /// The . IHtmlContentBuilder Append(string unencoded); @@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Html.Abstractions /// Appends an HTML encoded value. The value is treated as HTML encoded as-provided, and /// no further encoding will be performed. /// - /// The HTML encoded to append. + /// The HTML encoded to append. /// The . IHtmlContentBuilder AppendEncoded(string encoded);