-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlElement.cs
47 lines (39 loc) · 1.11 KB
/
HtmlElement.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
using System.Collections.Generic;
using System.Text;
namespace BuilderPattern
{
class HtmlElement
{
public string Name, Text;
public List<HtmlElement> Elements = new List<HtmlElement>();
private const int indentSize = 2;
public HtmlElement()
{
}
public HtmlElement(string name, string text)
{
Name = name;
Text = text;
}
private string ToStringImpl(int indent)
{
var sb = new StringBuilder();
var i = new string(' ', indentSize * indent);
sb.Append($"{i}<{Name}>\n");
if (!string.IsNullOrWhiteSpace(Text))
{
sb.Append(new string(' ', indentSize * (indent + 1)));
sb.Append(Text);
sb.Append("\n");
}
foreach (var e in Elements)
sb.Append(e.ToStringImpl(indent + 1));
sb.Append($"{i}</{Name}>\n");
return sb.ToString();
}
public override string ToString()
{
return ToStringImpl(0);
}
}
}