Skip to content

Commit 2addc8d

Browse files
committed
inline tags
1 parent 9133555 commit 2addc8d

File tree

3 files changed

+86
-21
lines changed

3 files changed

+86
-21
lines changed

CSHtml.Examples/BasicExample.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace CSHtml.Examples
88
{
99
public class BasicExamples
1010
{
11+
/// <summary>
12+
/// Example of a simple html using <see cref="HtmlBuilder"/>
13+
/// </summary>
1114
public static void WriteSimpleHtml()
1215
{
1316
HtmlBuilder builder = new HtmlBuilder();
@@ -31,7 +34,10 @@ public static void WriteSimpleHtml()
3134
Console.Write(builder.ToString());
3235
}
3336

34-
public static void MultipleTags()
37+
/// <summary>
38+
/// Example of how to add multiple attributes for an html tag
39+
/// </summary>
40+
public static void MultipleAttributes()
3541
{
3642
HtmlBuilder builder = new HtmlBuilder();
3743

@@ -41,12 +47,12 @@ public static void MultipleTags()
4147
{
4248
using (builder.AddTag(HtmlTag.H2))
4349
{
44-
builder.WriteLine("Span with multiple tags");
50+
builder.WriteLine("Span with multiple attributes");
4551
}
4652

4753
using (builder.AddTag(HtmlTag.P))
4854
{
49-
builder.WriteLine("Define a span with the source, width and height");
55+
builder.WriteLine("Define a span with source, width and height");
5056
}
5157

5258
using (builder.AddTag(HtmlTag.Span, (HtmlAttribute.Src, "my_icon.jpg"), (HtmlAttribute.Width, "100"), (HtmlAttribute.Height, "150")))
@@ -57,5 +63,35 @@ public static void MultipleTags()
5763

5864
Console.Write(builder.ToString());
5965
}
66+
67+
/// <summary>
68+
/// Example of how to add inline tags like images or hr
69+
/// </summary>
70+
public static void InlineTag()
71+
{
72+
HtmlBuilder builder = new HtmlBuilder();
73+
74+
using (builder.AddTag(HtmlTag.Html))
75+
{
76+
using (builder.AddTag(HtmlTag.Body, (HtmlAttribute.Class, "main_body")))
77+
{
78+
using (builder.AddTag(HtmlTag.P))
79+
{
80+
builder.WriteLine("Inline hr tag");
81+
}
82+
83+
builder.AddInlineTag(HtmlTag.Hr);
84+
85+
using (builder.AddTag(HtmlTag.P))
86+
{
87+
builder.WriteLine("Inline image tag");
88+
}
89+
90+
builder.AddInlineTag(HtmlTag.Img, (HtmlAttribute.Src, "my_image.jpg"), (HtmlAttribute.Width, "400"), (HtmlAttribute.Height, "500"));
91+
}
92+
}
93+
94+
Console.Write(builder.ToString());
95+
}
6096
}
6197
}

CSHtml/HtmlBuilder.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ public HtmlBuilder(TextWriter writer)
2020
this._writer = writer;
2121
}
2222

23+
24+
public void AddInlineTag(HtmlTag tag)
25+
{
26+
HtmlTagEntry.WriteInlineTag(this, tag.ToString().ToLower());
27+
}
28+
29+
public void AddInlineTag(HtmlTag tag, params (HtmlAttribute att, string value)[] arr)
30+
{
31+
HtmlTagEntry.WriteInlineTag(this, tag.ToString().ToLower(), arr);
32+
}
33+
34+
public void AddInlineTag(HtmlTag tag, params (string att, string value)[] arr)
35+
{
36+
HtmlTagEntry.WriteInlineTag(this, tag.ToString().ToLower(), arr);
37+
}
38+
2339
public HtmlTagEntry AddTag(HtmlTag tag)
2440
{
25-
var htag = new HtmlTagEntry(this, tag.ToString());
41+
var htag = new HtmlTagEntry(this, tag.ToString().ToLower());
2642

2743
this._indentLevel++;
2844
htag.OnDispose += this.onTagDisposed;
@@ -32,7 +48,7 @@ public HtmlTagEntry AddTag(HtmlTag tag)
3248

3349
public HtmlTagEntry AddTag(HtmlTag tag, params (HtmlAttribute att, string value)[] arr)
3450
{
35-
var htag = new HtmlTagEntry(this, tag.ToString(), arr);
51+
var htag = new HtmlTagEntry(this, tag.ToString().ToLower(), arr);
3652

3753
this._indentLevel++;
3854
htag.OnDispose += this.onTagDisposed;

CSHtml/HtmlTagEntry.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace CSHtml
45
{
@@ -12,46 +13,58 @@ public class HtmlTagEntry : IDisposable
1213

1314
internal HtmlTagEntry(HtmlBuilder builder, string name)
1415
{
15-
this.Name = name.ToLower();
16+
this.Name = name;
1617

1718
this._builder = builder;
19+
1820
this._builder.Write($"<{this.Name}>\n");
1921
}
2022

2123
internal HtmlTagEntry(HtmlBuilder builder, string name, params (HtmlAttribute att, string value)[] arr)
24+
: this(builder, name, arr.Select(a => (a.att.ToString().ToLower(), a.value)).ToArray())
25+
{
26+
}
27+
28+
internal HtmlTagEntry(HtmlBuilder builder, string name, params (string att, string value)[] arr)
2229
{
23-
this.Name = name.ToLower();
30+
this.Name = name;
2431

2532
this._builder = builder;
2633
this._builder.Write($"<{this.Name}");
2734

28-
if (arr != null)
29-
{
30-
foreach ((HtmlAttribute att, string value) in arr)
31-
{
32-
this._builder.Write($" {att.ToString().ToLower()}=\"{value}\"", false);
33-
}
34-
}
35+
writeAttributes(this._builder, arr);
3536

3637
this._builder.Write($">\n", false);
3738
}
3839

39-
internal HtmlTagEntry(HtmlBuilder builder, string name, params (string att, string value)[] arr)
40+
public static void WriteInlineTag(HtmlBuilder builder, string name)
41+
{
42+
builder.Write($"<{name}/>\n");
43+
}
44+
45+
public static void WriteInlineTag(HtmlBuilder builder, string name, params (HtmlAttribute att, string value)[] arr)
4046
{
41-
this.Name = name.ToLower();
47+
WriteInlineTag(builder, name, arr.Select(a => (a.att.ToString().ToLower(), a.value)).ToArray());
48+
}
4249

43-
this._builder = builder;
44-
this._builder.Write($"<{this.Name}");
50+
public static void WriteInlineTag(HtmlBuilder builder, string name, params (string att, string value)[] arr)
51+
{
52+
builder.Write($"<{name}");
4553

54+
writeAttributes(builder, arr.Select(a => (a.att.ToString().ToLower(), a.value)).ToArray());
55+
56+
builder.Write($"/>\n", false);
57+
}
58+
59+
protected static void writeAttributes(HtmlBuilder builder, params (string att, string value)[] arr)
60+
{
4661
if (arr != null)
4762
{
4863
foreach (var item in arr)
4964
{
50-
this._builder.Write($" {item.att.ToString().ToLower()}=\"{item.value}\"", false);
65+
builder.Write($" {item.att.ToString().ToLower()}=\"{item.value}\"", false);
5166
}
5267
}
53-
54-
this._builder.Write($">\n", false);
5568
}
5669

5770
/// <inheritdoc/>

0 commit comments

Comments
 (0)