Skip to content

Commit

Permalink
html: Tr accept mixed Th and Td
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Dec 19, 2023
1 parent 7310caf commit 064a917
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
4 changes: 4 additions & 0 deletions html/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (e *Element) Href(href string) *Element {
return e.Attribute("href", href)
}

func (e *Element) Name(name string) *Element {
return e.Attribute("name", name)
}

func (e *Element) Src(src string) *Element {
return e.Attribute("src", src)
}
Expand Down
6 changes: 6 additions & 0 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@ type HTMLer interface {

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

func NewHTML() *Element { return NewElement("html") }

func A() *Element { return NewElement("a") }
func B() *Element { return NewElement("b") }
func Body() *Element { return NewElement("body") }
func Br() *Element { return NewElement("br") }
func Div() *Element { return NewElement("div") }
func Em() *Element { return NewElement("em") }
func Form() *Element { return NewElement("form") }
func H1() *Element { return NewElement("h1") }
func H2() *Element { return NewElement("h2") }
func Head() *Element { return NewElement("head") }
func I() *Element { return NewElement("i") }
func Img() *Element { return NewElement("img") }
func Input() *Element { return NewElement("input") }
func Label() *Element { return NewElement("label") }
func Li() *Element { return NewElement("li") }
func Meta() *Element { return NewElement("meta") }
func P() *Element { return NewElement("p") }
func Span() *Element { return NewElement("span") }
func Style() *Element { return NewElement("style") }
func Svg() *Element { return NewElement("svg") }
func Table() *Element { return NewElement("table") }
func Tbody() *Element { return NewElement("tbody") }
Expand Down
71 changes: 30 additions & 41 deletions html/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,57 @@ package html

import "strconv"

func Tr[T *TableHeader | *TableData](element ...T) *Element {
var _ HTMLer = new(TableCell)

type TableCell struct{ *Element }

func Tr(element ...*TableCell) *Element {
tr := NewElement("tr")
for _, i := range element {
tr.AppendContent(i)
}
return tr
}

var (
_ HTMLer = new(TableHeader)
_ HTMLer = new(TableData)
)

type (
TableHeader struct{ *Element }
TableData struct{ *Element }
)

func Th(content any) *TableHeader {
return &TableHeader{NewElement("th").Content(content)}
}

func (th *TableHeader) Abbr(abbr string) *TableHeader {
th.Element.Attribute("abbr", abbr)
return th
func Th(content any) *TableCell {
return &TableCell{NewElement("th").Content(content)}
}

func (th *TableHeader) Colspan(n uint) *TableHeader {
th.Element.Attribute("colspan", strconv.FormatUint(uint64(n), 10))
return th
func Td(content any) *TableCell {
return &TableCell{NewElement("td").Content(content)}
}

func (th *TableHeader) Headers(headers string) *TableHeader {
th.Element.Attribute("headers", headers)
return th
func (cell *TableCell) Abbr(abbr string) *TableCell {
cell.Element.Attribute("abbr", abbr)
return cell
}

func (th *TableHeader) Rowspan(n uint) *TableHeader {
th.Element.Attribute("rowspan", strconv.FormatUint(uint64(n), 10))
return th
func (cell *TableCell) Colspan(n uint) *TableCell {
cell.Element.Attribute("colspan", strconv.FormatUint(uint64(n), 10))
return cell
}

func (th *TableHeader) Scope(scope string) *TableHeader {
th.Element.Attribute("scope", scope)
return th
func (cell *TableCell) Headers(headers string) *TableCell {
cell.Element.Attribute("headers", headers)
return cell
}

func Td(content any) *TableData {
return &TableData{NewElement("td").Content(content)}
func (cell *TableCell) Rowspan(n uint) *TableCell {
cell.Element.Attribute("rowspan", strconv.FormatUint(uint64(n), 10))
return cell
}

func (td *TableData) Colspan(n uint) *TableData {
td.Element.Attribute("colspan", strconv.FormatUint(uint64(n), 10))
return td
func (cell *TableCell) Scope(scope string) *TableCell {
cell.Element.Attribute("scope", scope)
return cell
}

func (td *TableData) Headers(headers string) *TableData {
td.Element.Attribute("headers", headers)
return td
func (cell *TableCell) Class(class ...string) *TableCell {
cell.Element.Class(class...)
return cell
}

func (td *TableData) Rowspan(n uint) *TableData {
td.Element.Attribute("rowspan", strconv.FormatUint(uint64(n), 10))
return td
func (cell *TableCell) Style(style string) *TableCell {
cell.Element.Style(style)
return cell
}

0 comments on commit 064a917

Please sign in to comment.