From 9980a51c3c25a1e36fd95bf1fafeba7f5b7a34d3 Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Fri, 2 Aug 2024 23:26:17 -0400 Subject: [PATCH] Add Div implementation. (#45) Part of https://github.com/jverkoey/slipstream/issues/25. --- .../Documentation.docc/Views/W3C/W3CViews.md | 20 +++++++----- .../W3C/Elements/GroupingContent/Div.swift | 32 +++++++++++++++++++ .../Sites/CatalogSiteTests.swift | 8 +++-- .../SlipstreamTests/Views/W3C/DivTests.swift | 19 +++++++++++ 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 Sources/Slipstream/Views/W3C/Elements/GroupingContent/Div.swift create mode 100644 Tests/SlipstreamTests/Views/W3C/DivTests.swift diff --git a/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md b/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md index fc9b420..e76ebcb 100644 --- a/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md +++ b/Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md @@ -8,17 +8,21 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh ### Root element -- +- ``HTML`` ### Document metadata -- -- -- -- -- -- +- ``Head`` +- ``Title`` +- ``Stylesheet`` +- ``Meta`` +- ``Charset`` +- ``Viewport`` ### Sections -- +- ``Body`` + +### Grouping content + +- ``Div`` diff --git a/Sources/Slipstream/Views/W3C/Elements/GroupingContent/Div.swift b/Sources/Slipstream/Views/W3C/Elements/GroupingContent/Div.swift new file mode 100644 index 0000000..e618ecd --- /dev/null +++ b/Sources/Slipstream/Views/W3C/Elements/GroupingContent/Div.swift @@ -0,0 +1,32 @@ +/// A view that has no special meaning at all. +/// +/// A Div represents its children. It can be used with the ``View/class(_:)``, +/// ``View/language(_:)``, and ``View/title(_:)`` attributes to +/// mark up semantics common to a group of consecutive elements. +/// +/// ```swift +/// struct MySiteContent: View { +/// var body: some View { +/// Body { +/// Div { +/// Text("Hello, world!") +/// } +/// } +/// } +/// } +/// ``` +/// +/// - SeeAlso: W3C [`div`](https://html.spec.whatwg.org/multipage/grouping-content.html#the-div-element) specification. +@available(iOS 17.0, macOS 14.0, *) +public struct Div: W3CElement where Content: View { + @_documentation(visibility: private) + public let tagName: String = "div" + + @_documentation(visibility: private) + @ViewBuilder public let content: () -> Content + + /// Creates a Div 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 7d5b237..ff987ae 100644 --- a/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift +++ b/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift @@ -15,7 +15,9 @@ private struct CatalogSite: View { Stylesheet(URL(string: "/css/bootstrap.css")) } Body { - Text("Hello, world!") + Div { + Text("Hello, world!") + } } } } @@ -33,7 +35,9 @@ struct CatalogSiteTests { - Hello, world! +
+ Hello, world! +
""") diff --git a/Tests/SlipstreamTests/Views/W3C/DivTests.swift b/Tests/SlipstreamTests/Views/W3C/DivTests.swift new file mode 100644 index 0000000..6fb582a --- /dev/null +++ b/Tests/SlipstreamTests/Views/W3C/DivTests.swift @@ -0,0 +1,19 @@ +import Testing + +import Slipstream + +struct DivTests { + @Test func emptyBlock() throws { + try #expect(renderHTML(Div {}) == "
") + } + + @Test func withText() throws { + try #expect(renderHTML(Div { + Text("Hello, world!") + }) == """ +
+ Hello, world! +
+""") + } +}