Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Div implementation. #45

Merged
merged 1 commit into from
Aug 3, 2024
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
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>
""")
}
}