-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathNodePrintExtensions.cs
More file actions
140 lines (122 loc) · 4.01 KB
/
NodePrintExtensions.cs
File metadata and controls
140 lines (122 loc) · 4.01 KB
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
using System.Text;
using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Html;
using AngleSharp.Text;
using Bunit.Diffing;
namespace Bunit;
/// <summary>
/// Helper methods for pretty printing markup from <see cref="INode"/> and <see cref="INodeList"/>.
/// </summary>
public static class NodePrintExtensions
{
/// <summary>
/// Writes the serialization of the node guided by the formatter.
/// </summary>
/// <param name="nodes">The nodes to serialize.</param>
/// <param name="writer">The output target of the serialization.</param>
/// <param name="formatter">The formatter to use.</param>
public static void ToHtml(this IEnumerable<INode> nodes, TextWriter writer, IMarkupFormatter formatter)
{
if (nodes is null)
throw new ArgumentNullException(nameof(nodes));
foreach (var node in nodes)
{
node.ToHtml(writer, formatter);
}
}
/// <summary>
/// Uses the <see cref="DiffMarkupFormatter"/> to generate a HTML markup string
/// from a <see cref="IEnumerable{INode}"/> <paramref name="nodes"/>.
/// The generated HTML markup will NOT include the internal Blazor attributes
/// added to elements.
/// </summary>
public static string ToDiffMarkup(this IEnumerable<INode> nodes)
{
if (nodes is null)
throw new ArgumentNullException(nameof(nodes));
using var sw = new StringWriter();
nodes.ToHtml(sw, new DiffMarkupFormatter());
return sw.ToString();
}
/// <summary>
/// Uses the <see cref="DiffMarkupFormatter"/> to generate a HTML markup string
/// from a <see cref="IMarkupFormattable"/> <paramref name="markupFormattable"/>.
/// The generated HTML markup will NOT include the internal Blazor attributes
/// added to elements.
/// </summary>
public static string ToDiffMarkup(this IMarkupFormattable markupFormattable)
{
if (markupFormattable is null)
throw new ArgumentNullException(nameof(markupFormattable));
using var sw = new StringWriter();
markupFormattable.ToHtml(sw, new DiffMarkupFormatter());
return sw.ToString();
}
/// <summary>
/// Uses the <see cref="PrettyMarkupFormatter"/> to generate a HTML markup string
/// from a <see cref="IEnumerable{INode}"/> <paramref name="nodes"/>.
/// </summary>
public static string ToMarkup(this IEnumerable<INode> nodes)
{
if (nodes is null)
throw new ArgumentNullException(nameof(nodes));
using var sw = new StringWriter();
var formatter = new PrettyMarkupFormatter
{
NewLine = Environment.NewLine,
Indentation = " ",
};
nodes.ToHtml(sw, formatter);
return sw.ToString();
}
/// <summary>
/// Uses the <see cref="PrettyMarkupFormatter"/> to generate a HTML markup
/// from a <see cref="IMarkupFormattable"/> <paramref name="markupFormattable"/>.
/// </summary>
public static string ToMarkup(this IMarkupFormattable markupFormattable)
{
if (markupFormattable is null)
throw new ArgumentNullException(nameof(markupFormattable));
using var sw = new StringWriter();
var formatter = new PrettyMarkupFormatter
{
NewLine = Environment.NewLine,
Indentation = " ",
};
markupFormattable.ToHtml(sw, formatter);
return sw.ToString();
}
/// <summary>
/// Converts an <see cref="IElement"/> into a HTML markup string,
/// with only its tag and attributes included in the output. All
/// child nodes are skipped.
/// </summary>
public static string ToMarkupElementOnly(this IElement element)
{
if (element is null)
throw new ArgumentNullException(nameof(element));
var diffMarkupFormatter = new DiffMarkupFormatter();
var result = new StringBuilder();
result.Append(Symbols.LessThan);
var prefix = element.Prefix;
var name = element.LocalName;
var tag = !string.IsNullOrEmpty(prefix) ? string.Concat(prefix, ":", name) : name;
result.Append(tag);
foreach (var attribute in element.Attributes)
{
result.Append(' ').Append(diffMarkupFormatter.ConvertToString(attribute));
}
if (element.HasChildNodes)
{
result.Append(Symbols.GreaterThan);
result.Append("...");
result.Append("</").Append(tag).Append('>');
}
else
{
result.Append(" />");
}
return result.ToString();
}
}