about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMarkus Wüstenberg2020-09-23 20:35:16 +0200
committerGitHub2020-09-23 20:35:16 +0200
commiteb2cfa10c2cfc763b18df4879d06223f2efaddfd (patch)
tree26807c8c4aa9450c7697c649d2b700ec4efd04cc
parent05c31515c6e5d5a9633b3af4b58eb46456e0632c (diff)
downloadgomponents-eb2cfa10c2cfc763b18df4879d06223f2efaddfd.tar.lz
gomponents-eb2cfa10c2cfc763b18df4879d06223f2efaddfd.tar.zst
gomponents-eb2cfa10c2cfc763b18df4879d06223f2efaddfd.zip
Add children varargs to h1-6, b, strong, i, em, img helpers (#15)
This makes it possible to add attributes.
-rw-r--r--el/elements.go46
-rw-r--r--el/elements_test.go22
2 files changed, 35 insertions, 33 deletions
diff --git a/el/elements.go b/el/elements.go
index a0ea3d5..6745d1c 100644
--- a/el/elements.go
+++ b/el/elements.go
@@ -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 {
diff --git a/el/elements_test.go b/el/elements_test.go
index 0eee5f3..a24e963 100644
--- a/el/elements_test.go
+++ b/el/elements_test.go
@@ -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")))
 	})
 }