|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
| 6 | +using System.Globalization; |
6 | 7 | using System.IO;
|
7 | 8 | using System.Linq;
|
8 | 9 | using System.Security.Claims;
|
@@ -250,25 +251,88 @@ public TagHelperContent EndTagHelperWritingScope()
|
250 | 251 | }
|
251 | 252 |
|
252 | 253 | /// <summary>
|
253 |
| - /// Writes an <see cref="ITextWriterCopyable"/> to the <see cref="Output"/>. |
| 254 | + /// Writes the content of a specified <paramref name="tagHelperExecutionContext"/>. |
254 | 255 | /// </summary>
|
255 |
| - /// <param name="copyableTextWriter">Contains the data to be written.</param> |
256 |
| - public void Write(ITextWriterCopyable copyableTextWriter) |
| 256 | + /// <param name="tagHelperExecutionContext">The execution context containing the content.</param> |
| 257 | + /// <returns> |
| 258 | + /// A <see cref="Task"/> that on completion writes the <paramref name="tagHelperExecutionContext"/> content. |
| 259 | + /// </returns> |
| 260 | + public async Task WriteTagHelperAsync([NotNull] TagHelperExecutionContext tagHelperExecutionContext) |
257 | 261 | {
|
258 |
| - WriteTo(Output, copyableTextWriter); |
| 262 | + await WriteTagHelperToAsync(Output, tagHelperExecutionContext); |
259 | 263 | }
|
260 | 264 |
|
261 | 265 | /// <summary>
|
262 |
| - /// Writes an <see cref="ITextWriterCopyable"/> to the <paramref name="writer"/>. |
| 266 | + /// Writes the content of a specified <paramref name="tagHelperExecutionContext"/> to the specified |
| 267 | + /// <paramref name="writer"/>. |
263 | 268 | /// </summary>
|
264 |
| - /// <param name="writer">The <see cref="TextWriter"/> to which the |
265 |
| - /// <paramref name="copyableTextWriter"/> is written.</param> |
266 |
| - /// <param name="copyableTextWriter">Contains the data to be written.</param> |
267 |
| - public void WriteTo([NotNull] TextWriter writer, ITextWriterCopyable copyableTextWriter) |
| 269 | + /// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param> |
| 270 | + /// <param name="tagHelperExecutionContext">The execution context containing the content.</param> |
| 271 | + /// <returns> |
| 272 | + /// A <see cref="Task"/> that on completion writes the <paramref name="tagHelperExecutionContext"/> content |
| 273 | + /// to the <paramref name="writer"/>. |
| 274 | + /// </returns> |
| 275 | + public async Task WriteTagHelperToAsync( |
| 276 | + [NotNull] TextWriter writer, |
| 277 | + [NotNull] TagHelperExecutionContext tagHelperExecutionContext) |
268 | 278 | {
|
269 |
| - if (copyableTextWriter != null) |
| 279 | + var tagHelperOutput = tagHelperExecutionContext.Output; |
| 280 | + var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(tagHelperOutput.TagName); |
| 281 | + |
| 282 | + if (!isTagNameNullOrWhitespace) |
270 | 283 | {
|
271 |
| - copyableTextWriter.CopyTo(writer); |
| 284 | + writer.Write('<'); |
| 285 | + writer.Write(tagHelperOutput.TagName); |
| 286 | + |
| 287 | + foreach (var attribute in tagHelperOutput.Attributes) |
| 288 | + { |
| 289 | + var value = HtmlEncoder.HtmlEncode(attribute.Value); |
| 290 | + writer.Write(' '); |
| 291 | + writer.Write(attribute.Key); |
| 292 | + writer.Write("=\""); |
| 293 | + writer.Write(value); |
| 294 | + writer.Write('"'); |
| 295 | + } |
| 296 | + |
| 297 | + if (tagHelperOutput.SelfClosing) |
| 298 | + { |
| 299 | + writer.Write(" /"); |
| 300 | + } |
| 301 | + |
| 302 | + writer.Write('>'); |
| 303 | + } |
| 304 | + |
| 305 | + if (isTagNameNullOrWhitespace || !tagHelperOutput.SelfClosing) |
| 306 | + { |
| 307 | + WriteTagHelperContentTo(writer, tagHelperOutput.PreContent); |
| 308 | + if (tagHelperOutput.IsContentModified) |
| 309 | + { |
| 310 | + WriteTagHelperContentTo(writer, tagHelperOutput.Content); |
| 311 | + } |
| 312 | + else if (tagHelperExecutionContext.ChildContentRetrieved) |
| 313 | + { |
| 314 | + var childContent = await tagHelperExecutionContext.GetChildContentAsync(); |
| 315 | + WriteTagHelperContentTo(writer, childContent); |
| 316 | + } |
| 317 | + else |
| 318 | + { |
| 319 | + await tagHelperExecutionContext.ExecuteChildContentAsync(); |
| 320 | + } |
| 321 | + |
| 322 | + WriteTagHelperContentTo(writer, tagHelperOutput.PostContent); |
| 323 | + } |
| 324 | + |
| 325 | + if (!isTagNameNullOrWhitespace && !tagHelperOutput.SelfClosing) |
| 326 | + { |
| 327 | + writer.Write(string.Format(CultureInfo.InvariantCulture, "</{0}>", tagHelperOutput.TagName)); |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + private void WriteTagHelperContentTo(TextWriter writer, TagHelperContent content) |
| 332 | + { |
| 333 | + foreach (var entry in content) |
| 334 | + { |
| 335 | + writer.Write(entry); |
272 | 336 | }
|
273 | 337 | }
|
274 | 338 |
|
@@ -310,20 +374,7 @@ public virtual void WriteTo([NotNull] TextWriter writer, object value)
|
310 | 374 | }
|
311 | 375 | else
|
312 | 376 | {
|
313 |
| - // This path is called when GetChildContentAsync() is called in tag helper |
314 |
| - // and content is not set. |
315 |
| - var tagHelperContent = value as TagHelperContent; |
316 |
| - if (tagHelperContent != null) |
317 |
| - { |
318 |
| - foreach (var entry in tagHelperContent) |
319 |
| - { |
320 |
| - writer.Write(entry); |
321 |
| - } |
322 |
| - } |
323 |
| - else |
324 |
| - { |
325 |
| - WriteTo(writer, value.ToString()); |
326 |
| - } |
| 377 | + WriteTo(writer, value.ToString()); |
327 | 378 | }
|
328 | 379 | }
|
329 | 380 | }
|
|
0 commit comments