about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--html/attributes.go5
-rw-r--r--html/attributes_test.go7
-rw-r--r--html/elements.go2
-rw-r--r--html/elements_test.go2
5 files changed, 16 insertions, 3 deletions
diff --git a/README.md b/README.md
index 31f0101..d0f51e8 100644
--- a/README.md
+++ b/README.md
@@ -137,9 +137,10 @@ For more complete examples, see [the examples directory](examples/).
 
 ### What's up with the specially named elements and attributes?
 
-Unfortunately, there are three main name clashes in HTML elements and attributes, so they need an `El` or `Attr` suffix,
+Unfortunately, there are four main name clashes in HTML elements and attributes, so they need an `El` or `Attr` suffix,
 respectively, to be able to co-exist in the same package in Go:
 
+- `data` (`DataEl`/`DataAttr`)
 - `form` (`FormEl`/`FormAttr`)
 - `style` (`StyleEl`/`StyleAttr`)
 - `title` (`TitleEl`/`TitleAttr`)
diff --git a/html/attributes.go b/html/attributes.go
index fabfbf0..23559f6 100644
--- a/html/attributes.go
+++ b/html/attributes.go
@@ -81,6 +81,11 @@ func Content(v string) g.Node {
 	return g.Attr("content", v)
 }
 
+// DataAttr attributes automatically have their name prefixed with "data-".
+func DataAttr(name, v string) g.Node {
+	return g.Attr("data-"+name, v)
+}
+
 func For(v string) g.Node {
 	return g.Attr("for", v)
 }
diff --git a/html/attributes_test.go b/html/attributes_test.go
index 23445db..b0f76c1 100644
--- a/html/attributes_test.go
+++ b/html/attributes_test.go
@@ -83,3 +83,10 @@ func TestAria(t *testing.T) {
 		assert.Equal(t, ` aria-selected="true"`, n)
 	})
 }
+
+func TestDataAttr(t *testing.T) {
+	t.Run("returns an attribute which name is prefixed with data-", func(t *testing.T) {
+		n := DataAttr("id", "partyhat")
+		assert.Equal(t, ` data-id="partyhat"`, n)
+	})
+}
diff --git a/html/elements.go b/html/elements.go
index c3fa396..18dbeb1 100644
--- a/html/elements.go
+++ b/html/elements.go
@@ -83,7 +83,7 @@ func ColGroup(children ...g.Node) g.NodeFunc {
 	return g.El("colgroup", children...)
 }
 
-func Data(children ...g.Node) g.NodeFunc {
+func DataEl(children ...g.Node) g.NodeFunc {
 	return g.El("data", children...)
 }
 
diff --git a/html/elements_test.go b/html/elements_test.go
index e9220e5..d7bd8bf 100644
--- a/html/elements_test.go
+++ b/html/elements_test.go
@@ -41,7 +41,7 @@ func TestSimpleElements(t *testing.T) {
 		"cite":       Cite,
 		"code":       Code,
 		"colgroup":   ColGroup,
-		"data":       Data,
+		"data":       DataEl,
 		"datalist":   DataList,
 		"details":    Details,
 		"dialog":     Dialog,