Skip to content

Commit

Permalink
# update code
Browse files Browse the repository at this point in the history
# code method: add style for element
  • Loading branch information
Maniceraf committed Jun 14, 2024
1 parent 0637ae9 commit 18bf135
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
26 changes: 13 additions & 13 deletions EasyHtmlToolkit.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public static void Example1()

var th = new HtmlTh("Month");

tr.AddChild(th);
tr.AddChildren(th);

th = new HtmlTh("Savings");

tr.AddChild(th);
tr.AddChildren(th);

tableHeader.AddChild(tr);
tableHeader.AddChildren(tr);

var tableBody = new HtmlTableBody();

Expand All @@ -38,24 +38,24 @@ public static void Example1()

var td = new HtmlTd("January");

tr.AddChild(td);
tr.AddChildren(td);

td = new HtmlTd("$100");

tr.AddChild(td);
tr.AddChildren(td);

tableBody.AddChild(tr);
tableBody.AddChildren(tr);

table.AddChild(tableHeader);
table.AddChild(tableBody);
table.AddChildren(tableHeader);
table.AddChildren(tableBody);

table.AddAttribute("style", "width:100%; border: 1px solid black");

doc.Body.AddChild(table);
doc.Body.AddChildren(table);

doc.AddStyleToHead(@"table, th, td {
border: 1px solid black;
}");
doc.AddStyle(@"table, th, td {
border: 1px solid black;
}");

doc.SaveToFile1("D:\\", "test1.html");
}
Expand All @@ -73,7 +73,7 @@ public static void Example2()
button.AddAttribute("accesskey", "b");

// Add the button to the document body
document.Body.AddChild(button);
document.Body.AddChildren(button);

// Save the HTML document to a file
document.SaveToDownloadsFolder();
Expand Down
2 changes: 1 addition & 1 deletion EasyHtmlToolkit/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void AddStyle(string css)
{
var styleElement = new HtmlElement("style");
styleElement.InnerText = css;
Head.AddChild(styleElement);
Head.AddChildren(styleElement);
}

public override string ToString()
Expand Down
18 changes: 13 additions & 5 deletions EasyHtmlToolkit/Models/HtmlElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System;
using System.Text;

namespace EasyHtmlToolkit.Models
{
Expand All @@ -7,11 +8,12 @@ public class HtmlElement(string tagName)
public string TagName { get; set; } = tagName;
public string? InnerText { get; set; }
public Dictionary<string, string> Attributes { get; set; } = [];
public List<HtmlElement> Children { get; set; } = [];
public Dictionary<string, string> Styles { get; set; } = [];
public List<HtmlElement> Childrens { get; set; } = [];

public void AddChild(HtmlElement child)
public void AddChildren(HtmlElement children)
{
Children.Add(child);
Childrens.Add(children);
}

public void AddAttribute(string key, string value)
Expand Down Expand Up @@ -53,6 +55,12 @@ public override string ToString()
var sb = new StringBuilder();
sb.Append($"<{TagName}");

var style = string.Join(";", Styles.Select(x => $"{x.Key}: {x.Value}"));
if (!string.IsNullOrEmpty(style))
{
sb.Append($" style=\"{style}\"");
}

foreach (var attribute in Attributes)
{
sb.Append($" {attribute.Key}=\"{attribute.Value}\"");
Expand All @@ -65,7 +73,7 @@ public override string ToString()
sb.Append(InnerText);
}

foreach (var child in Children)
foreach (var child in Childrens)
{
sb.Append(child.ToString());
}
Expand Down

0 comments on commit 18bf135

Please sign in to comment.