From bb55077592ded4337f35acceecd54d82daf1dbe0 Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Fri, 2 Aug 2024 14:46:26 -0400 Subject: [PATCH] Add body element implementation. Part of https://github.com/jverkoey/slipstream/issues/25. --- .../Documentation.docc/Views/W3C/HTML.md | 7 ----- .../Documentation.docc/Views/W3C/W3CViews.md | 4 +++ .../Slipstream/Views/W3C/Elements/Body.swift | 28 +++++++++++++++++ .../Sites/CatalogSiteTests.swift | 11 +++++-- .../SlipstreamTests/Views/W3C/BodyTests.swift | 30 +++++++++++++++++++ .../SlipstreamTests/Views/W3C/HeadTests.swift | 9 +++++- 6 files changed, 79 insertions(+), 10 deletions(-) delete mode 100644 Sources/Slipstream/Documentation.docc/Views/W3C/HTML.md create mode 100644 Sources/Slipstream/Views/W3C/Elements/Body.swift create mode 100644 Tests/SlipstreamTests/Views/W3C/BodyTests.swift diff --git a/Sources/Slipstream/Documentation.docc/Views/W3C/HTML.md b/Sources/Slipstream/Documentation.docc/Views/W3C/HTML.md deleted file mode 100644 index 3aa5b52..0000000 --- a/Sources/Slipstream/Documentation.docc/Views/W3C/HTML.md +++ /dev/null @@ -1,7 +0,0 @@ -# ``HTML`` - -## Topics - -### Creating an HTML view - -- ``HTML/init(content:)`` diff --git a/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md b/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md index 7d6b74b..e67bb76 100644 --- a/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md +++ b/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md @@ -13,3 +13,7 @@ The complete W3C HTML elements standard can be found [here](https://www.w3.org/T ### Document metadata - + +### Sections + +- diff --git a/Sources/Slipstream/Views/W3C/Elements/Body.swift b/Sources/Slipstream/Views/W3C/Elements/Body.swift new file mode 100644 index 0000000..00f797e --- /dev/null +++ b/Sources/Slipstream/Views/W3C/Elements/Body.swift @@ -0,0 +1,28 @@ +/// A view that represents the body of a document (as opposed to the document’s metadata). +/// +/// Web pages use ``Body`` to define the content of the web page. +/// +/// ```swift +/// struct MySiteContent: View { +/// var body: some View { +/// Body { +/// Text("Hello, world!") +/// } +/// } +/// } +/// ``` +/// +/// - SeeAlso: W3C [`body`](https://www.w3.org/TR/2012/WD-html-markup-20121025/body.html#body) specification. +@available(iOS 17.0, macOS 14.0, *) +public struct Body: W3CElement where Content: View { + @_documentation(visibility: private) + public let tagName: String = "body" + + @_documentation(visibility: private) + @ViewBuilder public let content: () -> Content + + /// Creates a Body view. + public init(@ViewBuilder content: @escaping () -> Content) { + self.content = content + } +} diff --git a/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift b/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift index f2b14ba..3dbfc23 100644 --- a/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift +++ b/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift @@ -6,7 +6,11 @@ import Slipstream private struct CatalogSite: View { var body: some View { HTML { - Text("Hello, world!") + Head { + } + Body { + Text("Hello, world!") + } } } } @@ -15,7 +19,10 @@ struct CatalogSiteTests { @Test func rendered() throws { try #expect(renderHTML(CatalogSite()) == """ - Hello, world! + + + Hello, world! + """) } diff --git a/Tests/SlipstreamTests/Views/W3C/BodyTests.swift b/Tests/SlipstreamTests/Views/W3C/BodyTests.swift new file mode 100644 index 0000000..9974e04 --- /dev/null +++ b/Tests/SlipstreamTests/Views/W3C/BodyTests.swift @@ -0,0 +1,30 @@ +import Testing + +import Slipstream + +struct BodyTests { + @Test func emptyBlock() throws { + try #expect(renderHTML(Body {}) == "") + } + + @Test func withText() throws { + try #expect(renderHTML(HTML { + Head { + } + Body { + Text("Hello, world!") + } + }) == """ + + + + Hello, world! + + +""") + } + + @Test func attribute() throws { + try #expect(renderHTML(Body {}.language("en")) == #""#) + } +} diff --git a/Tests/SlipstreamTests/Views/W3C/HeadTests.swift b/Tests/SlipstreamTests/Views/W3C/HeadTests.swift index a0cc07d..4a8cd9c 100644 --- a/Tests/SlipstreamTests/Views/W3C/HeadTests.swift +++ b/Tests/SlipstreamTests/Views/W3C/HeadTests.swift @@ -10,11 +10,18 @@ struct HeadTests { @Test func withText() throws { try #expect(renderHTML(HTML { Head { + Text("Hello, world!") } }) == """ - + + Hello, world! + """) } + + @Test func attribute() throws { + try #expect(renderHTML(Head {}.language("en")) == #""#) + } }