diff options
author | Markus Wüstenberg | 2020-10-29 12:03:43 +0100 |
---|---|---|
committer | GitHub | 2020-10-29 12:03:43 +0100 |
commit | 3df42084aed213fc9e760b53b5132da63b1818b7 (patch) | |
tree | 51fbb6cc46f71762b711012a4455d0d228f992ad /components | |
parent | 13701c4f668eba27956a8ac554a1fe272245d210 (diff) | |
download | gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.tar.lz gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.tar.zst gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.zip |
Add HTML5 document template (#36)
Diffstat (limited to 'components')
-rw-r--r-- | components/documents.go | 40 | ||||
-rw-r--r-- | components/documents_test.go | 33 |
2 files changed, 73 insertions, 0 deletions
diff --git a/components/documents.go b/components/documents.go new file mode 100644 index 0000000..65f3e63 --- /dev/null +++ b/components/documents.go @@ -0,0 +1,40 @@ +package components + +import ( + g "github.com/maragudk/gomponents" + "github.com/maragudk/gomponents/attr" + "github.com/maragudk/gomponents/el" +) + +// DocumentProps for HTML5. +// Title is set no matter what, Description and Language elements only if the strings are non-empty. +type DocumentProps struct { + Title string + Description string + Language string + Head []g.Node + Body []g.Node +} + +// HTML5 document template. +func HTML5(p DocumentProps) g.NodeFunc { + var lang, description g.Node + if p.Language != "" { + lang = attr.Lang(p.Language) + } + if p.Description != "" { + description = el.Meta(attr.Name("description"), attr.Content(p.Description)) + } + return el.Document( + el.HTML(lang, + el.Head( + el.Meta(attr.Charset("utf-8")), + el.Meta(attr.Name("viewport"), attr.Content("width=device-width, initial-scale=1")), + el.Title(p.Title), + description, + g.Group(p.Head), + ), + el.Body(g.Group(p.Body)), + ), + ) +} diff --git a/components/documents_test.go b/components/documents_test.go new file mode 100644 index 0000000..2aabd5a --- /dev/null +++ b/components/documents_test.go @@ -0,0 +1,33 @@ +package components_test + +import ( + "testing" + + g "github.com/maragudk/gomponents" + "github.com/maragudk/gomponents/assert" + "github.com/maragudk/gomponents/attr" + c "github.com/maragudk/gomponents/components" + "github.com/maragudk/gomponents/el" +) + +func TestHTML5(t *testing.T) { + t.Run("returns an html5 document template", func(t *testing.T) { + e := c.HTML5(c.DocumentProps{ + Title: "Hat", + Description: "Love hats.", + Language: "en", + Head: []g.Node{el.Link(attr.Rel("stylesheet"), attr.Href("/hat.css"))}, + Body: []g.Node{el.Div()}, + }) + + assert.Equal(t, `<!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Hat</title><meta name="description" content="Love hats." /><link rel="stylesheet" href="/hat.css" /></head><body><div /></body></html>`, e) + }) + + t.Run("returns no language, description, and extra head/body elements if empty", func(t *testing.T) { + e := c.HTML5(c.DocumentProps{ + Title: "Hat", + }) + + assert.Equal(t, `<!doctype html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Hat</title></head><body /></html>`, e) + }) +} |