-
Notifications
You must be signed in to change notification settings - Fork 56
/
LazyHtmlAttributes.cs
35 lines (31 loc) · 1.15 KB
/
LazyHtmlAttributes.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
using Microsoft.AspNetCore.Html;
using System;
using System.IO;
using System.Text.Encodings.Web;
namespace ChameleonForms
{
/// <summary>
/// HtmlAttributes class that evaluates a given HTML generator when .ToHtmlString() is called.
/// </summary>
public class LazyHtmlAttributes : HtmlAttributes
{
private readonly Func<HtmlAttributes, IHtmlContent> _htmlGenerator;
/// <summary>
/// Construct a LazyHtmlAttributes class.
/// </summary>
/// <param name="htmlGenerator">The generator to use to generate the HTML when .ToHtmlString() is called</param>
public LazyHtmlAttributes(Func<HtmlAttributes, IHtmlContent> htmlGenerator)
{
if (htmlGenerator == null)
throw new ArgumentNullException("htmlGenerator");
_htmlGenerator = htmlGenerator;
}
/// <summary>
/// Invokes the given HTML generator to return HTML.
/// </summary>
public override void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
_htmlGenerator(this).WriteTo(writer, encoder);
}
}
}