// 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.Globalization;
using System.IO;
using System.Text.Encodings.Web;
namespace Microsoft.AspNetCore.Html
{
///
/// Extension methods for .
///
public static class HtmlContentBuilderExtensions
{
///
/// Appends the specified to the existing content after replacing each format
/// item with the HTML encoded representation of the corresponding item in the
/// array.
///
/// The .
///
/// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// The format string is assumed to be HTML encoded as-provided, and no further encoding will be performed.
///
///
/// The object array to format. Each element in the array will be formatted and then HTML encoded.
///
/// A reference to this instance after the append operation has completed.
public static IHtmlContentBuilder AppendFormat(
this IHtmlContentBuilder builder,
string format,
params object[] args)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
builder.AppendHtml(new HtmlFormattableString(format, args));
return builder;
}
///
/// Appends the specified to the existing content with information from the
/// after replacing each format item with the HTML encoded
/// representation of the corresponding item in the array.
///
/// The .
/// An object that supplies culture-specific formatting information.
///
/// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
/// The format string is assumed to be HTML encoded as-provided, and no further encoding will be performed.
///
///
/// The object array to format. Each element in the array will be formatted and then HTML encoded.
///
/// A reference to this instance after the append operation has completed.
public static IHtmlContentBuilder AppendFormat(
this IHtmlContentBuilder builder,
IFormatProvider formatProvider,
string format,
params object[] args)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
builder.AppendHtml(new HtmlFormattableString(formatProvider, format, args));
return builder;
}
///
/// Appends an .
///
/// The .
/// The .
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AppendHtml(HtmlString.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)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Append(unencoded);
builder.AppendHtml(HtmlString.NewLine);
return builder;
}
///
/// Appends an after appending the value.
///
/// The .
/// The to append.
/// The .
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, IHtmlContent content)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AppendHtml(content);
builder.AppendHtml(HtmlString.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 AppendHtmlLine(this IHtmlContentBuilder builder, string encoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AppendHtml(encoded);
builder.AppendHtml(HtmlString.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)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Clear();
builder.Append(unencoded);
return builder;
}
///
/// Sets the content to the value.
///
/// The .
/// The value that replaces the content.
/// The .
public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, IHtmlContent content)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Clear();
builder.AppendHtml(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 SetHtmlContent(this IHtmlContentBuilder builder, string encoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Clear();
builder.AppendHtml(encoded);
return builder;
}
}
}