From 56e5dee3668669c5f8e15695098661ff1d0f73e0 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Fri, 15 Dec 2023 18:51:45 +0100 Subject: [PATCH] Add doctype preamble to html tag The doctype preamble needs to be emmitted in order to ensure browsers don't switch to legacy/quirks mode rendering. Fixes: #91 --- elem.go | 7 +++++++ elements_test.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/elem.go b/elem.go index 75ac219..cf2d086 100644 --- a/elem.go +++ b/elem.go @@ -112,6 +112,13 @@ type Element struct { } func (e *Element) RenderTo(builder *strings.Builder) { + // The HTML tag needs a doctype preamble in order to ensure + // browsers don't render in legacy/quirks mode + // https://developer.mozilla.org/en-US/docs/Glossary/Doctype + if e.Tag == "html" { + builder.WriteString("") + } + // Start with opening tag builder.WriteString("<") builder.WriteString(e.Tag) diff --git a/elements_test.go b/elements_test.go index 6c6f341..6ec6fa4 100644 --- a/elements_test.go +++ b/elements_test.go @@ -18,7 +18,7 @@ func TestBody(t *testing.T) { } func TestHtml(t *testing.T) { - expected := `Elem Page

Welcome to Elem!

` + expected := `Elem Page

Welcome to Elem!

` el := Html(attrs.Props{attrs.Lang: "en"}, Head(nil, Meta(attrs.Props{attrs.Charset: "UTF-8"}),