Skip to content

Commit

Permalink
Add Div implementation. (#45)
Browse files Browse the repository at this point in the history
Part of #25.
  • Loading branch information
jverkoey authored Aug 3, 2024
1 parent 2b3fad2 commit 9980a51
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
20 changes: 12 additions & 8 deletions Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh

### Root element

- <doc:HTML>
- ``HTML``

### Document metadata

- <doc:Head>
- <doc:Title>
- <doc:Stylesheet>
- <doc:Meta>
- <doc:Charset>
- <doc:Viewport>
- ``Head``
- ``Title``
- ``Stylesheet``
- ``Meta``
- ``Charset``
- ``Viewport``

### Sections

- <doc:Body>
- ``Body``

### Grouping content

- ``Div``
32 changes: 32 additions & 0 deletions Sources/Slipstream/Views/W3C/Elements/GroupingContent/Div.swift
Original file line number Diff line number Diff line change
@@ -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<Content>: 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
}
}
8 changes: 6 additions & 2 deletions Tests/SlipstreamTests/Sites/CatalogSiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ private struct CatalogSite: View {
Stylesheet(URL(string: "/css/bootstrap.css"))
}
Body {
Text("Hello, world!")
Div {
Text("Hello, world!")
}
}
}
}
Expand All @@ -33,7 +35,9 @@ struct CatalogSiteTests {
<link rel="stylesheet" href="/css/bootstrap.css" />
</head>
<body>
Hello, world!
<div>
Hello, world!
</div>
</body>
</html>
""")
Expand Down
19 changes: 19 additions & 0 deletions Tests/SlipstreamTests/Views/W3C/DivTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Testing

import Slipstream

struct DivTests {
@Test func emptyBlock() throws {
try #expect(renderHTML(Div {}) == "<div></div>")
}

@Test func withText() throws {
try #expect(renderHTML(Div {
Text("Hello, world!")
}) == """
<div>
Hello, world!
</div>
""")
}
}

0 comments on commit 9980a51

Please sign in to comment.