Repository: maragudk/gomponents
Pull request #17 (2020-09-23 20:06:56)

Add Textf function
Like Text, but calls fmt.Sprintf before rendering.

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

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

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


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/17?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/17?src=pr&el=footer). Last update [3de9270...8d8c9f0](https://codecov.io/gh/maragudk/gomponents/pull/17?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index b817ad0..23b29b7 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,7 @@ func Page(title, path string) g.Node {
 			el.Body(
 				Navbar(path),
 				el.H1(title),
-				el.P(g.Text(fmt.Sprintf("Welcome to the page at %v.", path))),
+				el.P(g.Textf("Welcome to the page at %v.", path)),
 			),
 		),
 	)
diff --git a/gomponents.go b/gomponents.go
index d160b54..21d3e75 100644
--- a/gomponents.go
+++ b/gomponents.go
@@ -109,6 +109,13 @@ func Text(t string) NodeFunc {
 	}
 }
 
+// Textf creates a text DOM Node that Renders the interpolated and escaped string t.
+func Textf(format string, a ...interface{}) NodeFunc {
+	return func() string {
+		return template.HTMLEscaper(fmt.Sprintf(format, a...))
+	}
+}
+
 // Raw creates a raw Node that just Renders the unescaped string t.
 func Raw(t string) NodeFunc {
 	return func() string {


Test patch:
diff --git a/gomponents_test.go b/gomponents_test.go
index 78b69f0..ee0b91d 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -81,6 +81,13 @@ func TestText(t *testing.T) {
 	})
 }
 
+func TestTextf(t *testing.T) {
+	t.Run("renders interpolated and escaped text", func(t *testing.T) {
+		e := g.Textf("<%v/>", "div")
+		assert.Equal(t, "&lt;div/&gt;", e)
+	})
+}
+
 func TestRaw(t *testing.T) {
 	t.Run("renders raw text", func(t *testing.T) {
 		e := g.Raw("<div/>")