Skip to content

Commit f6be316

Browse files
authored
html: Tr accept mixed Th and Td (#28)
1 parent 7310caf commit f6be316

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

html/element.go

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func (e *Element) Href(href string) *Element {
2727
return e.Attribute("href", href)
2828
}
2929

30+
func (e *Element) Name(name string) *Element {
31+
return e.Attribute("name", name)
32+
}
33+
3034
func (e *Element) Src(src string) *Element {
3135
return e.Attribute("src", src)
3236
}

html/html.go

+6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@ type HTMLer interface {
1515

1616
func Background() *Element { return NewElement("") }
1717

18+
func NewHTML() *Element { return NewElement("html") }
19+
1820
func A() *Element { return NewElement("a") }
1921
func B() *Element { return NewElement("b") }
22+
func Body() *Element { return NewElement("body") }
2023
func Br() *Element { return NewElement("br") }
2124
func Div() *Element { return NewElement("div") }
2225
func Em() *Element { return NewElement("em") }
2326
func Form() *Element { return NewElement("form") }
2427
func H1() *Element { return NewElement("h1") }
2528
func H2() *Element { return NewElement("h2") }
29+
func Head() *Element { return NewElement("head") }
2630
func I() *Element { return NewElement("i") }
2731
func Img() *Element { return NewElement("img") }
2832
func Input() *Element { return NewElement("input") }
2933
func Label() *Element { return NewElement("label") }
3034
func Li() *Element { return NewElement("li") }
35+
func Meta() *Element { return NewElement("meta") }
3136
func P() *Element { return NewElement("p") }
3237
func Span() *Element { return NewElement("span") }
38+
func Style() *Element { return NewElement("style") }
3339
func Svg() *Element { return NewElement("svg") }
3440
func Table() *Element { return NewElement("table") }
3541
func Tbody() *Element { return NewElement("tbody") }

html/table.go

+30-41
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,57 @@ package html
22

33
import "strconv"
44

5-
func Tr[T *TableHeader | *TableData](element ...T) *Element {
5+
var _ HTMLer = new(TableCell)
6+
7+
type TableCell struct{ *Element }
8+
9+
func Tr(element ...*TableCell) *Element {
610
tr := NewElement("tr")
711
for _, i := range element {
812
tr.AppendContent(i)
913
}
1014
return tr
1115
}
1216

13-
var (
14-
_ HTMLer = new(TableHeader)
15-
_ HTMLer = new(TableData)
16-
)
17-
18-
type (
19-
TableHeader struct{ *Element }
20-
TableData struct{ *Element }
21-
)
22-
23-
func Th(content any) *TableHeader {
24-
return &TableHeader{NewElement("th").Content(content)}
25-
}
26-
27-
func (th *TableHeader) Abbr(abbr string) *TableHeader {
28-
th.Element.Attribute("abbr", abbr)
29-
return th
17+
func Th(content any) *TableCell {
18+
return &TableCell{NewElement("th").Content(content)}
3019
}
3120

32-
func (th *TableHeader) Colspan(n uint) *TableHeader {
33-
th.Element.Attribute("colspan", strconv.FormatUint(uint64(n), 10))
34-
return th
21+
func Td(content any) *TableCell {
22+
return &TableCell{NewElement("td").Content(content)}
3523
}
3624

37-
func (th *TableHeader) Headers(headers string) *TableHeader {
38-
th.Element.Attribute("headers", headers)
39-
return th
25+
func (cell *TableCell) Abbr(abbr string) *TableCell {
26+
cell.Element.Attribute("abbr", abbr)
27+
return cell
4028
}
4129

42-
func (th *TableHeader) Rowspan(n uint) *TableHeader {
43-
th.Element.Attribute("rowspan", strconv.FormatUint(uint64(n), 10))
44-
return th
30+
func (cell *TableCell) Colspan(n uint) *TableCell {
31+
cell.Element.Attribute("colspan", strconv.FormatUint(uint64(n), 10))
32+
return cell
4533
}
4634

47-
func (th *TableHeader) Scope(scope string) *TableHeader {
48-
th.Element.Attribute("scope", scope)
49-
return th
35+
func (cell *TableCell) Headers(headers string) *TableCell {
36+
cell.Element.Attribute("headers", headers)
37+
return cell
5038
}
5139

52-
func Td(content any) *TableData {
53-
return &TableData{NewElement("td").Content(content)}
40+
func (cell *TableCell) Rowspan(n uint) *TableCell {
41+
cell.Element.Attribute("rowspan", strconv.FormatUint(uint64(n), 10))
42+
return cell
5443
}
5544

56-
func (td *TableData) Colspan(n uint) *TableData {
57-
td.Element.Attribute("colspan", strconv.FormatUint(uint64(n), 10))
58-
return td
45+
func (cell *TableCell) Scope(scope string) *TableCell {
46+
cell.Element.Attribute("scope", scope)
47+
return cell
5948
}
6049

61-
func (td *TableData) Headers(headers string) *TableData {
62-
td.Element.Attribute("headers", headers)
63-
return td
50+
func (cell *TableCell) Class(class ...string) *TableCell {
51+
cell.Element.Class(class...)
52+
return cell
6453
}
6554

66-
func (td *TableData) Rowspan(n uint) *TableData {
67-
td.Element.Attribute("rowspan", strconv.FormatUint(uint64(n), 10))
68-
return td
55+
func (cell *TableCell) Style(style string) *TableCell {
56+
cell.Element.Style(style)
57+
return cell
6958
}

0 commit comments

Comments
 (0)