Skip to content

Commit

Permalink
Add head element implementation.
Browse files Browse the repository at this point in the history
Part of #25.
  • Loading branch information
jverkoey committed Aug 2, 2024
1 parent 6da46ea commit 0b3226b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
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 @@ -9,3 +9,7 @@ The complete W3C HTML elements standard can be found [here](https://www.w3.org/T
### Root element

- <doc:HTML>

### Document metadata

- <doc:Head>
12 changes: 9 additions & 3 deletions Sources/Slipstream/Views/W3C/Elements/HTML.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import SwiftSoup

/// The html element represents the root of a document.
/// A view that represents the root of an HTML document.
///
/// Every web page starts with an HTML view.
///
/// ```swift
/// struct MySite: View {
/// var body: some View {
/// HTML {
/// // Your site's content.
/// Head {
/// }
/// Body {
/// }
/// }
/// }
/// }
/// ```
///
/// View the full [W3C specification](https://www.w3.org/TR/2012/WD-html-markup-20121025/html.html#html).
/// - SeeAlso: W3C [`html`](https://www.w3.org/TR/2012/WD-html-markup-20121025/html.html#html) specification.
///
/// ## See Also
///
/// - ``View/language(_:)``
@available(iOS 17.0, macOS 14.0, *)
public struct HTML<Content>: View where Content: View {
/// Creates an HTML view.
///
/// - Parameter content: Should include a single ``Head`` view followed by a single ``Body`` view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
Expand Down
32 changes: 32 additions & 0 deletions Sources/Slipstream/Views/W3C/Elements/Head.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import SwiftSoup

/// A view that defines the document's metadata container.
///
/// Every web page starts with an HTML view.
///
/// ```swift
/// struct MySiteMetadata: View {
/// var body: some View {
/// Head {
/// // Add document metadata here.
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`head`](https://www.w3.org/TR/2012/WD-html-markup-20121025/head.html#head) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct Head<Content>: View where Content: View {
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

@_documentation(visibility: private)
public func render(_ container: Element, environment: EnvironmentValues) throws {
let head = Element(.init("head"), "")
try self.content().render(head, environment: environment)
try container.addChildren(head)
}

private let content: () -> Content
}
20 changes: 20 additions & 0 deletions Tests/SlipstreamTests/Views/W3C/HeadTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Testing

import Slipstream

struct HeadTests {
@Test func emptyBlock() throws {
try #expect(renderHTML(Head {}) == "<head></head>")
}

@Test func withText() throws {
try #expect(renderHTML(HTML {
Head {
}
}) == """
<html>
<head></head>
</html>
""")
}
}

0 comments on commit 0b3226b

Please sign in to comment.