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 body element implementation. #34

Merged
merged 1 commit into from
Aug 2, 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
7 changes: 0 additions & 7 deletions Sources/Slipstream/Documentation.docc/Views/W3C/HTML.md

This file was deleted.

4 changes: 4 additions & 0 deletions Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ The complete W3C HTML elements standard can be found [here](https://www.w3.org/T
### Document metadata

- <doc:Head>

### Sections

- <doc:Body>
28 changes: 28 additions & 0 deletions Sources/Slipstream/Views/W3C/Elements/Body.swift
Original file line number Diff line number Diff line change
@@ -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<Content>: 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
}
}
11 changes: 9 additions & 2 deletions Tests/SlipstreamTests/Sites/CatalogSiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import Slipstream
private struct CatalogSite: View {
var body: some View {
HTML {
Text("Hello, world!")
Head {
}
Body {
Text("Hello, world!")
}
}
}
}
Expand All @@ -15,7 +19,10 @@ struct CatalogSiteTests {
@Test func rendered() throws {
try #expect(renderHTML(CatalogSite()) == """
<html>
Hello, world!
<head></head>
<body>
Hello, world!
</body>
</html>
""")
}
Expand Down
30 changes: 30 additions & 0 deletions Tests/SlipstreamTests/Views/W3C/BodyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Testing

import Slipstream

struct BodyTests {
@Test func emptyBlock() throws {
try #expect(renderHTML(Body {}) == "<body></body>")
}

@Test func withText() throws {
try #expect(renderHTML(HTML {
Head {
}
Body {
Text("Hello, world!")
}
}) == """
<html>
<head></head>
<body>
Hello, world!
</body>
</html>
""")
}

@Test func attribute() throws {
try #expect(renderHTML(Body {}.language("en")) == #"<body lang="en"></body>"#)
}
}
9 changes: 8 additions & 1 deletion Tests/SlipstreamTests/Views/W3C/HeadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ struct HeadTests {
@Test func withText() throws {
try #expect(renderHTML(HTML {
Head {
Text("Hello, world!")
}
}) == """
<html>
<head></head>
<head>
Hello, world!
</head>
</html>
""")
}

@Test func attribute() throws {
try #expect(renderHTML(Head {}.language("en")) == #"<head lang="en"></head>"#)
}
}