This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
HtmlContentBuilderExtensions.cs
223 lines (200 loc) · 9.16 KB
/
HtmlContentBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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
{
/// <summary>
/// Extension methods for <see cref="IHtmlContentBuilder"/>.
/// </summary>
public static class HtmlContentBuilderExtensions
{
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content after replacing each format
/// item with the HTML encoded <see cref="string"/> representation of the corresponding item in the
/// <paramref name="args"/> array.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="format">
/// The composite format <see cref="string"/> (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.
/// </param>
/// <param name="args">
/// The object array to format. Each element in the array will be formatted and then HTML encoded.
/// </param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
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;
}
/// <summary>
/// Appends the specified <paramref name="format"/> to the existing content with information from the
/// <paramref name="formatProvider"/> after replacing each format item with the HTML encoded
/// <see cref="string"/> representation of the corresponding item in the <paramref name="args"/> array.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">
/// The composite format <see cref="string"/> (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.
/// </param>
/// <param name="args">
/// The object array to format. Each element in the array will be formatted and then HTML encoded.
/// </param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
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;
}
/// <summary>
/// Appends an <see cref="Environment.NewLine"/>.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AppendHtml(HtmlString.NewLine);
return builder;
}
/// <summary>
/// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="string"/> value.
/// The value is treated as unencoded as-provided, and will be HTML encoded before writing to output.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="unencoded">The <see cref="string"/> to append.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
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;
}
/// <summary>
/// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="IHtmlContent"/> value.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="content">The <see cref="IHtmlContent"/> to append.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
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;
}
/// <summary>
/// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="string"/> value.
/// The value is treated as HTML encoded as-provided, and no further encoding will be performed.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="encoded">The HTML encoded <see cref="string"/> to append.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
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;
}
/// <summary>
/// Sets the content to the <see cref="string"/> value. The value is treated as unencoded as-provided,
/// and will be HTML encoded before writing to output.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="unencoded">The <see cref="string"/> value that replaces the content.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, string unencoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Clear();
builder.Append(unencoded);
return builder;
}
/// <summary>
/// Sets the content to the <see cref="IHtmlContent"/> value.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="content">The <see cref="IHtmlContent"/> value that replaces the content.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, IHtmlContent content)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Clear();
builder.AppendHtml(content);
return builder;
}
/// <summary>
/// Sets the content to the <see cref="string"/> value. The value is treated as HTML encoded as-provided, and
/// no further encoding will be performed.
/// </summary>
/// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
/// <param name="encoded">The HTML encoded <see cref="string"/> that replaces the content.</param>
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, string encoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Clear();
builder.AppendHtml(encoded);
return builder;
}
}
}