From 0ad24b1dd51ee4f0bc1291537c216f15b86e4147 Mon Sep 17 00:00:00 2001 From: Chris Griffin Date: Tue, 17 Feb 2026 14:35:01 -0800 Subject: [PATCH 1/3] Added ability to set attributes on the body tag. Added tests. --- Sources/Elementary/HtmlDocument.swift | 8 +++++++- Tests/ElementaryTests/CompositionRenderingTest.swift | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/Elementary/HtmlDocument.swift b/Sources/Elementary/HtmlDocument.swift index a28c69b..13975e6 100644 --- a/Sources/Elementary/HtmlDocument.swift +++ b/Sources/Elementary/HtmlDocument.swift @@ -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] { get } + /// The text directionality (`ltr`, `rtl`, `auto`) of the HTML document. /// /// By default this attribute is not set. @@ -46,12 +49,15 @@ public protocol HTMLDocument: HTML { // which would cause confusing issues when adopters provide a property of a non-optional type. private let defaultUndefinedLanguage = "" private let defaultUndefinedDirection = "" +private let defaultUndefinedAttributes: [HTMLAttribute] = [] public extension HTMLDocument { /// The default value for the `lang` property is an empty string and will not be rendered in the HTML. 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] { defaultUndefinedAttributes } } // NOTE: this is a bit messy after the renaming of var content to var body @@ -92,7 +98,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) diff --git a/Tests/ElementaryTests/CompositionRenderingTest.swift b/Tests/ElementaryTests/CompositionRenderingTest.swift index 7e38301..617f1b4 100644 --- a/Tests/ElementaryTests/CompositionRenderingTest.swift +++ b/Tests/ElementaryTests/CompositionRenderingTest.swift @@ -5,7 +5,7 @@ final class CompositionRenderingTests: XCTestCase { func testRendersADocument() async throws { try await HTMLAssertEqual( MyPage(text: "my text"), - #"Foo

Hello, world!

my text

"# + #"Foo

Hello, world!

my text

"# ) } @@ -47,6 +47,7 @@ struct MyPage: HTMLDocument { var title = "Foo" var lang = "en" + var bodyAttributes: [HTMLAttribute] = [.class("my-class"), .id("42")] var head: some HTML { meta(.name(.author), .content("Me")) From 314cd7e752af64cdffcaa3059807bd92ab3b1d51 Mon Sep 17 00:00:00 2001 From: Chris Griffin Date: Wed, 18 Feb 2026 09:56:54 -0800 Subject: [PATCH 2/3] Changed tabs to spaces. --- Sources/Elementary/HtmlDocument.swift | 10 +++++----- Tests/ElementaryTests/CompositionRenderingTest.swift | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/Elementary/HtmlDocument.swift b/Sources/Elementary/HtmlDocument.swift index 13975e6..ac7dabe 100644 --- a/Sources/Elementary/HtmlDocument.swift +++ b/Sources/Elementary/HtmlDocument.swift @@ -32,8 +32,8 @@ public protocol HTMLDocument: HTML { /// By default this attribute is not set. var lang: String { get } - /// Attributes for the body tag. - var bodyAttributes: [HTMLAttribute] { get } + /// Attributes for the body tag. + var bodyAttributes: [HTMLAttribute] { get } /// The text directionality (`ltr`, `rtl`, `auto`) of the HTML document. /// @@ -56,8 +56,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] { defaultUndefinedAttributes } + /// The default value for the `bodyAttribute` property is an empty array. + var bodyAttributes: [HTMLAttribute] { defaultUndefinedAttributes } } // NOTE: this is a bit messy after the renaming of var content to var body @@ -98,7 +98,7 @@ public extension HTMLDocument { Elementary.title { self.title } self.head } - Elementary.body(attributes: self.bodyAttributes) { self.body } + Elementary.body(attributes: self.bodyAttributes) { self.body } } .attributes(.lang(lang), when: lang != defaultUndefinedLanguage) .attributes(.dir(dir), when: dir.value != defaultUndefinedDirection) diff --git a/Tests/ElementaryTests/CompositionRenderingTest.swift b/Tests/ElementaryTests/CompositionRenderingTest.swift index 617f1b4..cf5dce4 100644 --- a/Tests/ElementaryTests/CompositionRenderingTest.swift +++ b/Tests/ElementaryTests/CompositionRenderingTest.swift @@ -47,7 +47,7 @@ struct MyPage: HTMLDocument { var title = "Foo" var lang = "en" - var bodyAttributes: [HTMLAttribute] = [.class("my-class"), .id("42")] + var bodyAttributes: [HTMLAttribute] = [.class("my-class"), .id("42")] var head: some HTML { meta(.name(.author), .content("Me")) From 77586816351fb41727300559dedeba0c77b8b9cc Mon Sep 17 00:00:00 2001 From: Chris Griffin Date: Wed, 18 Feb 2026 13:49:19 -0800 Subject: [PATCH 3/3] Removed `defaultUndefinedAttributes` replaced by direct assignment. --- Sources/Elementary/HtmlDocument.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/Elementary/HtmlDocument.swift b/Sources/Elementary/HtmlDocument.swift index ac7dabe..afc1684 100644 --- a/Sources/Elementary/HtmlDocument.swift +++ b/Sources/Elementary/HtmlDocument.swift @@ -49,7 +49,6 @@ public protocol HTMLDocument: HTML { // which would cause confusing issues when adopters provide a property of a non-optional type. private let defaultUndefinedLanguage = "" private let defaultUndefinedDirection = "" -private let defaultUndefinedAttributes: [HTMLAttribute] = [] public extension HTMLDocument { /// The default value for the `lang` property is an empty string and will not be rendered in the HTML. @@ -57,7 +56,7 @@ public extension HTMLDocument { /// 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] { defaultUndefinedAttributes } + var bodyAttributes: [HTMLAttribute] { [] } } // NOTE: this is a bit messy after the renaming of var content to var body