Skip to content
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: 6 additions & 1 deletion Sources/Elementary/HtmlDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public protocol HTMLDocument: HTML {
/// By default this attribute is not set.
var lang: String { get }

/// Attributes for the body tag.
var bodyAttributes: [HTMLAttribute<HTMLTag.body>] { get }

/// The text directionality (`ltr`, `rtl`, `auto`) of the HTML document.
///
/// By default this attribute is not set.
Expand All @@ -52,6 +55,8 @@ public extension HTMLDocument {
var lang: String { defaultUndefinedLanguage }
/// The default value for the `dir` property is an empty string and will not be rendered in the HTML.
var dir: HTMLAttributeValue.Direction { .init(value: defaultUndefinedDirection) }
/// The default value for the `bodyAttribute` property is an empty array.
var bodyAttributes: [HTMLAttribute<HTMLTag.body>] { [] }
}

// NOTE: this is a bit messy after the renaming of var content to var body
Expand Down Expand Up @@ -92,7 +97,7 @@ public extension HTMLDocument {
Elementary.title { self.title }
self.head
}
Elementary.body { self.body }
Elementary.body(attributes: self.bodyAttributes) { self.body }
}
.attributes(.lang(lang), when: lang != defaultUndefinedLanguage)
.attributes(.dir(dir), when: dir.value != defaultUndefinedDirection)
Expand Down
3 changes: 2 additions & 1 deletion Tests/ElementaryTests/CompositionRenderingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ final class CompositionRenderingTests: XCTestCase {
func testRendersADocument() async throws {
try await HTMLAssertEqual(
MyPage(text: "my text"),
#"<!DOCTYPE html><html lang="en"><head><title>Foo</title><meta name="author" content="Me"><meta name="description" content="Test page"></head><body><div><h1>Hello, world!</h1><p>my text</p></div></body></html>"#
#"<!DOCTYPE html><html lang="en"><head><title>Foo</title><meta name="author" content="Me"><meta name="description" content="Test page"></head><body class="my-class" id="42"><div><h1>Hello, world!</h1><p>my text</p></div></body></html>"#
)
}

Expand Down Expand Up @@ -47,6 +47,7 @@ struct MyPage: HTMLDocument {

var title = "Foo"
var lang = "en"
var bodyAttributes: [HTMLAttribute<HTMLTag.body>] = [.class("my-class"), .id("42")]

var head: some HTML {
meta(.name(.author), .content("Me"))
Expand Down