Skip to content

Commit

Permalink
Add body element implementation. (#34)
Browse files Browse the repository at this point in the history
Part of #25.
jverkoey authored Aug 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ec21cc1 commit ab262a2
Showing 6 changed files with 79 additions and 10 deletions.
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
@@ -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
@@ -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()) == """
<html>
Hello, world!
<head></head>
<body>
Hello, world!
</body>
</html>
""")
}
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
@@ -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>"#)
}
}

0 comments on commit ab262a2

Please sign in to comment.