Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doctype preamble to html tag #95

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<!DOCTYPE html>")
}

// Start with opening tag
builder.WriteString("<")
builder.WriteString(e.Tag)
Expand Down
2 changes: 1 addition & 1 deletion elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestBody(t *testing.T) {
}

func TestHtml(t *testing.T) {
expected := `<html lang="en"><head><meta charset="UTF-8"><title>Elem Page</title></head><body><p>Welcome to Elem!</p></body></html>`
expected := `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Elem Page</title></head><body><p>Welcome to Elem!</p></body></html>`
el := Html(attrs.Props{attrs.Lang: "en"},
Head(nil,
Meta(attrs.Props{attrs.Charset: "UTF-8"}),
Expand Down