1
0

Add children varargs to h1-6, b, strong, i, em, img helpers (#15)

This makes it possible to add attributes.
This commit is contained in:
Markus Wüstenberg 2020-09-23 20:35:16 +02:00 committed by GitHub
parent 05c31515c6
commit eb2cfa10c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 33 deletions

View File

@ -68,28 +68,28 @@ func P(children ...g.Node) g.NodeFunc {
return g.El("p", children...)
}
func H1(text string) g.NodeFunc {
return g.El("h1", g.Text(text))
func H1(text string, children ...g.Node) g.NodeFunc {
return g.El("h1", prepend(g.Text(text), children)...)
}
func H2(text string) g.NodeFunc {
return g.El("h2", g.Text(text))
func H2(text string, children ...g.Node) g.NodeFunc {
return g.El("h2", prepend(g.Text(text), children)...)
}
func H3(text string) g.NodeFunc {
return g.El("h3", g.Text(text))
func H3(text string, children ...g.Node) g.NodeFunc {
return g.El("h3", prepend(g.Text(text), children)...)
}
func H4(text string) g.NodeFunc {
return g.El("h4", g.Text(text))
func H4(text string, children ...g.Node) g.NodeFunc {
return g.El("h4", prepend(g.Text(text), children)...)
}
func H5(text string) g.NodeFunc {
return g.El("h5", g.Text(text))
func H5(text string, children ...g.Node) g.NodeFunc {
return g.El("h5", prepend(g.Text(text), children)...)
}
func H6(text string) g.NodeFunc {
return g.El("h6", g.Text(text))
func H6(text string, children ...g.Node) g.NodeFunc {
return g.El("h6", prepend(g.Text(text), children)...)
}
func Ol(children ...g.Node) g.NodeFunc {
@ -104,24 +104,26 @@ func Li(children ...g.Node) g.NodeFunc {
return g.El("li", children...)
}
func B(text string) g.NodeFunc {
return g.El("b", g.Text(text))
func B(text string, children ...g.Node) g.NodeFunc {
return g.El("b", prepend(g.Text(text), children)...)
}
func Strong(text string) g.NodeFunc {
return g.El("strong", g.Text(text))
func Strong(text string, children ...g.Node) g.NodeFunc {
return g.El("strong", prepend(g.Text(text), children)...)
}
func I(text string) g.NodeFunc {
return g.El("i", g.Text(text))
func I(text string, children ...g.Node) g.NodeFunc {
return g.El("i", prepend(g.Text(text), children)...)
}
func Em(text string) g.NodeFunc {
return g.El("em", g.Text(text))
func Em(text string, children ...g.Node) g.NodeFunc {
return g.El("em", prepend(g.Text(text), children)...)
}
func Img(src, alt string) g.NodeFunc {
return g.El("img", g.Attr("src", src), g.Attr("alt", alt))
func Img(src, alt string, children ...g.Node) g.NodeFunc {
newChildren := prepend(g.Attr("alt", alt), children)
newChildren = prepend(g.Attr("src", src), newChildren)
return g.El("img", newChildren...)
}
func prepend(node g.Node, nodes []g.Node) []g.Node {

View File

@ -82,37 +82,37 @@ func TestP(t *testing.T) {
func TestH1(t *testing.T) {
t.Run("returns an h1 element", func(t *testing.T) {
assert.Equal(t, `<h1>hat</h1>`, el.H1("hat"))
assert.Equal(t, `<h1 id="headline">hat</h1>`, el.H1("hat", g.Attr("id", "headline")))
})
}
func TestH2(t *testing.T) {
t.Run("returns an h2 element", func(t *testing.T) {
assert.Equal(t, `<h2>hat</h2>`, el.H2("hat"))
assert.Equal(t, `<h2 id="headline">hat</h2>`, el.H2("hat", g.Attr("id", "headline")))
})
}
func TestH3(t *testing.T) {
t.Run("returns an h3 element", func(t *testing.T) {
assert.Equal(t, `<h3>hat</h3>`, el.H3("hat"))
assert.Equal(t, `<h3 id="headline">hat</h3>`, el.H3("hat", g.Attr("id", "headline")))
})
}
func TestH4(t *testing.T) {
t.Run("returns an h4 element", func(t *testing.T) {
assert.Equal(t, `<h4>hat</h4>`, el.H4("hat"))
assert.Equal(t, `<h4 id="headline">hat</h4>`, el.H4("hat", g.Attr("id", "headline")))
})
}
func TestH5(t *testing.T) {
t.Run("returns an h5 element", func(t *testing.T) {
assert.Equal(t, `<h5>hat</h5>`, el.H5("hat"))
assert.Equal(t, `<h5 id="headline">hat</h5>`, el.H5("hat", g.Attr("id", "headline")))
})
}
func TestH6(t *testing.T) {
t.Run("returns an h6 element", func(t *testing.T) {
assert.Equal(t, `<h6>hat</h6>`, el.H6("hat"))
assert.Equal(t, `<h6 id="headline">hat</h6>`, el.H6("hat", g.Attr("id", "headline")))
})
}
@ -136,30 +136,30 @@ func TestLi(t *testing.T) {
func TestB(t *testing.T) {
t.Run("returns a b element", func(t *testing.T) {
assert.Equal(t, `<b>hat</b>`, el.B("hat"))
assert.Equal(t, `<b id="text">hat</b>`, el.B("hat", g.Attr("id", "text")))
})
}
func TestStrong(t *testing.T) {
t.Run("returns a strong element", func(t *testing.T) {
assert.Equal(t, `<strong>hat</strong>`, el.Strong("hat"))
assert.Equal(t, `<strong id="text">hat</strong>`, el.Strong("hat", g.Attr("id", "text")))
})
}
func TestI(t *testing.T) {
t.Run("returns an i element", func(t *testing.T) {
assert.Equal(t, `<i>hat</i>`, el.I("hat"))
assert.Equal(t, `<i id="text">hat</i>`, el.I("hat", g.Attr("id", "text")))
})
}
func TestEm(t *testing.T) {
t.Run("returns an em element", func(t *testing.T) {
assert.Equal(t, `<em>hat</em>`, el.Em("hat"))
assert.Equal(t, `<em id="text">hat</em>`, el.Em("hat", g.Attr("id", "text")))
})
}
func TestImg(t *testing.T) {
t.Run("returns an img element with href and alt attributes", func(t *testing.T) {
assert.Equal(t, `<img src="hat.png" alt="hat"/>`, el.Img("hat.png", "hat"))
assert.Equal(t, `<img src="hat.png" alt="hat" id="image"/>`, el.Img("hat.png", "hat", g.Attr("id", "image")))
})
}