Add LinkStylesheet and LinkPreload components (#79) These are often used in the `<head>` part of the HTML.
Markus Wüstenberg markus@maragu.dk
Tue, 08 Jun 2021 16:52:57 +0200
4 files changed, 27 insertions(+), 0 deletions(-)
M components/elements.go → components/elements.go
@@ -8,3 +8,11 @@ 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)) +}
M components/elements_test.go → components/elements_test.go
@@ -14,3 +14,17 @@ n := c.InputHidden("id", "partyhat", g.Attr("class", "hat")) assert.Equal(t, `<input type="hidden" name="id" value="partyhat" class="hat">`, 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, `<link rel="stylesheet" href="style.css" media="print">`, 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, `<link rel="preload" href="party.woff2" as="font" type="font/woff2">`, n) + }) +}
M html/attributes.go → 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) }
M html/attributes_test.go → html/attributes_test.go
@@ -36,6 +36,7 @@ cases := map[string]func(string) g.Node{ "accept": Accept, "action": Action, "alt": Alt, + "as": As, "autocomplete": AutoComplete, "charset": Charset, "class": Class,