From 7c0f2e4cbb4da6d7074bd78be9c55d3495c0dad7 Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Tue, 8 Jun 2021 16:52:57 +0200 Subject: Add LinkStylesheet and LinkPreload components (#79) These are often used in the `
` part of the HTML.--- components/elements.go | 8 ++++++++ components/elements_test.go | 14 ++++++++++++++ html/attributes.go | 4 ++++ html/attributes_test.go | 1 + 4 files changed, 27 insertions(+) diff --git a/components/elements.go b/components/elements.go index f4e347d..c9e1427 100644 --- a/components/elements.go +++ b/components/elements.go @@ -8,3 +8,11 @@ import ( func InputHidden(name, value string, children ...g.Node) g.Node { return Input(Type("hidden"), Name(name), Value(value), g.Group(children)) } + +func LinkStylesheet(href string, children ...g.Node) g.Node { + return Link(Rel("stylesheet"), Href(href), g.Group(children)) +} + +func LinkPreload(href, as string, children ...g.Node) g.Node { + return Link(Rel("preload"), Href(href), As(as), g.Group(children)) +} diff --git a/components/elements_test.go b/components/elements_test.go index 334ed2c..7dc4eec 100644 --- a/components/elements_test.go +++ b/components/elements_test.go @@ -14,3 +14,17 @@ func TestInputHidden(t *testing.T) { assert.Equal(t, ``, n) }) } + +func TestLinkStylesheet(t *testing.T) { + t.Run("returns a link element with rel stylesheet and the given href", func(t *testing.T) { + n := c.LinkStylesheet("style.css", g.Attr("media", "print")) + assert.Equal(t, ``, n) + }) +} + +func TestLinkPreload(t *testing.T) { + t.Run("returns a link element with rel preload and the given href and as", func(t *testing.T) { + n := c.LinkPreload("party.woff2", "font", g.Attr("type", "font/woff2")) + assert.Equal(t, ``, n) + }) +} diff --git a/html/attributes.go b/html/attributes.go index 23559f6..ab575d8 100644 --- a/html/attributes.go +++ b/html/attributes.go @@ -61,6 +61,10 @@ func Aria(name, v string) g.Node { return g.Attr("aria-"+name, v) } +func As(v string) g.Node { + return g.Attr("as", v) +} + func AutoComplete(v string) g.Node { return g.Attr("autocomplete", v) } diff --git a/html/attributes_test.go b/html/attributes_test.go index b0f76c1..1d0b312 100644 --- a/html/attributes_test.go +++ b/html/attributes_test.go @@ -36,6 +36,7 @@ func TestSimpleAttributes(t *testing.T) { "accept": Accept, "action": Action, "alt": Alt, + "as": As, "autocomplete": AutoComplete, "charset": Charset, "class": Class, -- cgit 1.4.1