Repository: maragudk/gomponents
Pull request #15 (2020-09-23 18:31:43)

Add children varargs to h1-6, b, strong, i, em, img helpers
This makes it possible to add attributes.

codecov[bot]: # [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=h1) Report
> Merging [#15](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=desc) into [master](https://codecov.io/gh/maragudk/gomponents/commit/05c31515c6e5d5a9633b3af4b58eb46456e0632c?el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/maragudk/gomponents/pull/15/graphs/tree.svg?width=650&height=150&src=pr&token=JYN032TYAC)](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #15   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines           81        83    +2     
=========================================
+ Hits            81        83    +2     
```


| [Impacted Files](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [el/elements.go](https://codecov.io/gh/maragudk/gomponents/pull/15/diff?src=pr&el=tree#diff-ZWwvZWxlbWVudHMuZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=footer). Last update [05c3151...57b2b82](https://codecov.io/gh/maragudk/gomponents/pull/15?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
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 {


Test patch:
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")))
 	})
 }