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 228
/
TagHelperOutput.cs
454 lines (389 loc) · 15.4 KB
/
TagHelperOutput.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// 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.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// Class used to represent the output of an <see cref="ITagHelper"/>.
/// </summary>
public class TagHelperOutput : IHtmlContentContainer
{
private readonly Func<bool, HtmlEncoder, Task<TagHelperContent>> _getChildContentAsync;
private TagHelperContent _preElement;
private TagHelperContent _preContent;
private TagHelperContent _content;
private TagHelperContent _postContent;
private TagHelperContent _postElement;
private bool _wasSuppressOutputCalled;
// Internal for testing
internal TagHelperOutput(string tagName)
: this(
tagName,
new TagHelperAttributeList(),
(useCachedResult, encoder) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()))
{
}
/// <summary>
/// Instantiates a new instance of <see cref="TagHelperOutput"/>.
/// </summary>
/// <param name="tagName">The HTML element's tag name.</param>
/// <param name="attributes">The HTML attributes.</param>
/// <param name="getChildContentAsync">
/// A delegate used to execute children asynchronously with the given <see cref="HtmlEncoder"/> in scope and
/// return their rendered content.
/// </param>
public TagHelperOutput(
string tagName,
TagHelperAttributeList attributes,
Func<bool, HtmlEncoder, Task<TagHelperContent>> getChildContentAsync)
{
if (getChildContentAsync == null)
{
throw new ArgumentNullException(nameof(getChildContentAsync));
}
if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
TagName = tagName;
_getChildContentAsync = getChildContentAsync;
Attributes = attributes;
}
/// <summary>
/// The HTML element's tag name.
/// </summary>
/// <remarks>
/// A whitespace or <c>null</c> value results in no start or end tag being rendered.
/// </remarks>
public string TagName { get; set; }
/// <summary>
/// Content that precedes the HTML element.
/// </summary>
/// <remarks>Value is rendered before the HTML element.</remarks>
public TagHelperContent PreElement
{
get
{
if (_preElement == null)
{
_preElement = new DefaultTagHelperContent();
}
return _preElement;
}
}
/// <summary>
/// The HTML element's pre content.
/// </summary>
/// <remarks>Value is prepended to the <see cref="ITagHelper"/>'s final output.</remarks>
public TagHelperContent PreContent
{
get
{
if (_preContent == null)
{
_preContent = new DefaultTagHelperContent();
}
return _preContent;
}
}
/// <summary>
/// Get or set the HTML element's main content.
/// </summary>
/// <remarks>Value occurs in the <see cref="ITagHelper"/>'s final output after <see cref="PreContent"/> and
/// before <see cref="PostContent"/></remarks>
public TagHelperContent Content
{
get
{
if (_content == null)
{
_content = new DefaultTagHelperContent();
}
return _content;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_content = value;
}
}
/// <summary>
/// The HTML element's post content.
/// </summary>
/// <remarks>Value is appended to the <see cref="ITagHelper"/>'s final output.</remarks>
public TagHelperContent PostContent
{
get
{
if (_postContent == null)
{
_postContent = new DefaultTagHelperContent();
}
return _postContent;
}
}
/// <summary>
/// Content that follows the HTML element.
/// </summary>
/// <remarks>Value is rendered after the HTML element.</remarks>
public TagHelperContent PostElement
{
get
{
if (_postElement == null)
{
_postElement = new DefaultTagHelperContent();
}
return _postElement;
}
}
/// <summary>
/// <c>true</c> if <see cref="Content"/> has been set, <c>false</c> otherwise.
/// </summary>
public bool IsContentModified
{
get
{
return _wasSuppressOutputCalled || _content?.IsModified == true;
}
}
/// <summary>
/// Syntax of the element in the generated HTML.
/// </summary>
public TagMode TagMode { get; set; }
/// <summary>
/// The HTML element's attributes.
/// </summary>
/// <remarks>
/// MVC will HTML encode <see cref="string"/> values when generating the start tag. It will not HTML encode
/// a <c>Microsoft.AspNetCore.Mvc.Rendering.HtmlString</c> instance. MVC converts most other types to a
/// <see cref="string"/>, then HTML encodes the result.
/// </remarks>
public TagHelperAttributeList Attributes { get; }
/// <summary>
/// Clears the <see cref="TagHelperOutput"/> and updates its state with the provided values.
/// </summary>
/// <param name="tagName">The tag name to use.</param>
/// <param name="tagMode">The <see cref="TagMode"/> to use.</param>
public void Reinitialize(string tagName, TagMode tagMode)
{
TagName = tagName;
TagMode = tagMode;
Attributes.Clear();
_preElement?.Reinitialize();
_preContent?.Reinitialize();
_content?.Reinitialize();
_postContent?.Reinitialize();
_postElement?.Reinitialize();
_wasSuppressOutputCalled = false;
}
/// <summary>
/// Changes <see cref="TagHelperOutput"/> to generate nothing.
/// </summary>
/// <remarks>
/// Sets <see cref="TagName"/> to <c>null</c>, and clears <see cref="PreElement"/>, <see cref="PreContent"/>,
/// <see cref="Content"/>, <see cref="PostContent"/>, and <see cref="PostElement"/> to suppress output.
/// </remarks>
public void SuppressOutput()
{
TagName = null;
_wasSuppressOutputCalled = true;
_preElement?.Clear();
_preContent?.Clear();
_content?.Clear();
_postContent?.Clear();
_postElement?.Clear();
}
/// <summary>
/// Executes children asynchronously and returns their rendered content.
/// </summary>
/// <returns>A <see cref="Task"/> that on completion returns content rendered by children.</returns>
/// <remarks>
/// This method is memoized. Multiple calls will not cause children to re-execute with the page's original
/// <see cref="HtmlEncoder"/>.
/// </remarks>
public Task<TagHelperContent> GetChildContentAsync()
{
return GetChildContentAsync(useCachedResult: true, encoder: null);
}
/// <summary>
/// Executes children asynchronously and returns their rendered content.
/// </summary>
/// <param name="useCachedResult">
/// If <c>true</c>, multiple calls will not cause children to re-execute with the page's original
/// <see cref="HtmlEncoder"/>; returns cached content.
/// </param>
/// <returns>A <see cref="Task"/> that on completion returns content rendered by children.</returns>
public Task<TagHelperContent> GetChildContentAsync(bool useCachedResult)
{
return GetChildContentAsync(useCachedResult, encoder: null);
}
/// <summary>
/// Executes children asynchronously with the given <paramref name="encoder"/> in scope and returns their
/// rendered content.
/// </summary>
/// <param name="encoder">
/// The <see cref="HtmlEncoder"/> to use when the page handles non-<see cref="IHtmlContent"/> C# expressions.
/// If <c>null</c>, executes children with the page's current <see cref="HtmlEncoder"/>.
/// </param>
/// <returns>A <see cref="Task"/> that on completion returns content rendered by children.</returns>
/// <remarks>
/// This method is memoized. Multiple calls with the same <see cref="HtmlEncoder"/> instance will not cause
/// children to re-execute with that encoder in scope.
/// </remarks>
public Task<TagHelperContent> GetChildContentAsync(HtmlEncoder encoder)
{
return GetChildContentAsync(useCachedResult: true, encoder: encoder);
}
/// <summary>
/// Executes children asynchronously with the given <paramref name="encoder"/> in scope and returns their
/// rendered content.
/// </summary>
/// <param name="useCachedResult">
/// If <c>true</c>, multiple calls with the same <see cref="HtmlEncoder"/> will not cause children to
/// re-execute; returns cached content.
/// </param>
/// <param name="encoder">
/// The <see cref="HtmlEncoder"/> to use when the page handles non-<see cref="IHtmlContent"/> C# expressions.
/// If <c>null</c>, executes children with the page's current <see cref="HtmlEncoder"/>.
/// </param>
/// <returns>A <see cref="Task"/> that on completion returns content rendered by children.</returns>
public Task<TagHelperContent> GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
{
return _getChildContentAsync(useCachedResult, encoder);
}
void IHtmlContentContainer.CopyTo(IHtmlContentBuilder destination)
{
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
_preElement?.CopyTo(destination);
var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(TagName);
if (!isTagNameNullOrWhitespace)
{
destination.AppendHtml("<");
destination.AppendHtml(TagName);
// Perf: Avoid allocating enumerator
for (var i = 0; i < Attributes.Count; i++)
{
var attribute = Attributes[i];
destination.AppendHtml(" ");
attribute.CopyTo(destination);
}
if (TagMode == TagMode.SelfClosing)
{
destination.AppendHtml(" /");
}
destination.AppendHtml(">");
}
if (isTagNameNullOrWhitespace || TagMode == TagMode.StartTagAndEndTag)
{
_preContent?.CopyTo(destination);
_content?.CopyTo(destination);
_postContent?.CopyTo(destination);
}
if (!isTagNameNullOrWhitespace && TagMode == TagMode.StartTagAndEndTag)
{
destination.AppendHtml("</");
destination.AppendHtml(TagName);
destination.AppendHtml(">");
}
_postElement?.CopyTo(destination);
}
void IHtmlContentContainer.MoveTo(IHtmlContentBuilder destination)
{
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
_preElement?.MoveTo(destination);
var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(TagName);
if (!isTagNameNullOrWhitespace)
{
destination.AppendHtml("<");
destination.AppendHtml(TagName);
// Perf: Avoid allocating enumerator
for (var i = 0; i < Attributes.Count; i++)
{
var attribute = Attributes[i];
destination.AppendHtml(" ");
attribute.MoveTo(destination);
}
if (TagMode == TagMode.SelfClosing)
{
destination.AppendHtml(" /");
}
destination.AppendHtml(">");
}
if (isTagNameNullOrWhitespace || TagMode == TagMode.StartTagAndEndTag)
{
_preContent?.MoveTo(destination);
_content?.MoveTo(destination);
_postContent?.MoveTo(destination);
}
if (!isTagNameNullOrWhitespace && TagMode == TagMode.StartTagAndEndTag)
{
destination.AppendHtml("</");
destination.AppendHtml(TagName);
destination.AppendHtml(">");
}
_postElement?.MoveTo(destination);
// Depending on the code path we took, these might need to be cleared.
_preContent?.Clear();
_content?.Clear();
_postContent?.Clear();
Attributes.Clear();
}
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
_preElement?.WriteTo(writer, encoder);
var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(TagName);
if (!isTagNameNullOrWhitespace)
{
writer.Write("<");
writer.Write(TagName);
// Perf: Avoid allocating enumerator
for (var i = 0; i < Attributes.Count; i++)
{
var attribute = Attributes[i];
writer.Write(" ");
attribute.WriteTo(writer, encoder);
}
if (TagMode == TagMode.SelfClosing)
{
writer.Write(" /");
}
writer.Write(">");
}
if (isTagNameNullOrWhitespace || TagMode == TagMode.StartTagAndEndTag)
{
_preContent?.WriteTo(writer, encoder);
_content?.WriteTo(writer, encoder);
_postContent?.WriteTo(writer, encoder);
}
if (!isTagNameNullOrWhitespace && TagMode == TagMode.StartTagAndEndTag)
{
writer.Write("</");
writer.Write(TagName);
writer.Write(">");
}
_postElement?.WriteTo(writer, encoder);
}
}
}