diff options
author | Markus Wüstenberg | 2020-12-22 11:46:49 +0100 |
---|---|---|
committer | GitHub | 2020-12-22 11:46:49 +0100 |
commit | d41c4e5a85362dec7844a9093b43720eb0e70e72 (patch) | |
tree | 8b9fc056e2721b7403e40852de7abb998dcad993 | |
parent | b4918d5a63434e162e5547cf0e8461fcd1931765 (diff) | |
download | gomponents-d41c4e5a85362dec7844a9093b43720eb0e70e72.tar.lz gomponents-d41c4e5a85362dec7844a9093b43720eb0e70e72.tar.zst gomponents-d41c4e5a85362dec7844a9093b43720eb0e70e72.zip |
Add data- attribute helper (#61)
Fixes #49.
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | html/attributes.go | 5 | ||||
-rw-r--r-- | html/attributes_test.go | 7 | ||||
-rw-r--r-- | html/elements.go | 2 | ||||
-rw-r--r-- | html/elements_test.go | 2 |
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, |