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 h1-h6 implementations. #53

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
6 changes: 6 additions & 0 deletions Sources/Slipstream/Documentation.docc/Views/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh
### Sections

- ``Body``
- ``H1``
- ``H2``
- ``H3``
- ``H4``
- ``H5``
- ``H6``

### Grouping content

Expand Down
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/Sections/H1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a top-level heading for a section.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// H1("Hello, world!")
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`h1-h6`](https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct H1<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "h1"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

/// Creates an H1 view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

/// Creates an H1 view with some static text.
public init(_ text: String) where Content == Text {
self.content = {
Text(text)
}
}
}
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/Sections/H2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a heading for a section.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// H2("Hello, world!")
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`h1-h6`](https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct H2<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "h2"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

/// Creates an H2 view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

/// Creates an H2 view with some static text.
public init(_ text: String) where Content == Text {
self.content = {
Text(text)
}
}
}
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/Sections/H3.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a heading for a section.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// H3("Hello, world!")
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`h1-h6`](https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct H3<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "h3"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

/// Creates an H3 view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

/// Creates an H3 view with some static text.
public init(_ text: String) where Content == Text {
self.content = {
Text(text)
}
}
}
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/Sections/H4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a heading for a section.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// H4("Hello, world!")
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`h1-h6`](https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct H4<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "h4"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

/// Creates an H4 view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

/// Creates an H4 view with some static text.
public init(_ text: String) where Content == Text {
self.content = {
Text(text)
}
}
}
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/Sections/H5.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a heading for a section.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// H5("Hello, world!")
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`h1-h6`](https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct H5<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "h5"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

/// Creates an H5 view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

/// Creates an H5 view with some static text.
public init(_ text: String) where Content == Text {
self.content = {
Text(text)
}
}
}
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/Sections/H6.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a heading for a section.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// H6("Hello, world!")
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`h1-h6`](https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct H6<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "h6"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

/// Creates an H6 view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

/// Creates an H6 view with some static text.
public init(_ text: String) where Content == Text {
self.content = {
Text(text)
}
}
}
14 changes: 14 additions & 0 deletions Tests/SlipstreamTests/Sites/CatalogSiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ private struct CatalogSite: View {
Body {
Container {
Text("Hello, world!")
H1("Heading 1")
H2 {
Text("Heading 2")
}
H3("Heading 3")
H4("Heading 4")
H5("Heading 5")
H6("Heading 6")
}
}
.id("root")
Expand All @@ -38,6 +46,12 @@ struct CatalogSiteTests {
<body id="root">
<div class="container">
Hello, world!
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
</div>
</body>
</html>
Expand Down
32 changes: 32 additions & 0 deletions Tests/SlipstreamTests/Views/W3C/HeadingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Testing

import Slipstream

struct HeadingTests {
@Test func emptyBlock() throws {
try #expect(renderHTML(H1 {}) == "<h1></h1>")
try #expect(renderHTML(H2 {}) == "<h2></h2>")
try #expect(renderHTML(H3 {}) == "<h3></h3>")
try #expect(renderHTML(H4 {}) == "<h4></h4>")
try #expect(renderHTML(H5 {}) == "<h5></h5>")
try #expect(renderHTML(H6 {}) == "<h6></h6>")
}

@Test func withText() throws {
try #expect(renderHTML(H1 { Text("Hello, world!") }) == "<h1>Hello, world!</h1>")
try #expect(renderHTML(H2 { Text("Hello, world!") }) == "<h2>Hello, world!</h2>")
try #expect(renderHTML(H3 { Text("Hello, world!") }) == "<h3>Hello, world!</h3>")
try #expect(renderHTML(H4 { Text("Hello, world!") }) == "<h4>Hello, world!</h4>")
try #expect(renderHTML(H5 { Text("Hello, world!") }) == "<h5>Hello, world!</h5>")
try #expect(renderHTML(H6 { Text("Hello, world!") }) == "<h6>Hello, world!</h6>")
}

@Test func attribute() throws {
try #expect(renderHTML(H1 {}.language("en")) == #"<h1 lang="en"></h1>"#)
try #expect(renderHTML(H2 {}.language("en")) == #"<h2 lang="en"></h2>"#)
try #expect(renderHTML(H3 {}.language("en")) == #"<h3 lang="en"></h3>"#)
try #expect(renderHTML(H4 {}.language("en")) == #"<h4 lang="en"></h4>"#)
try #expect(renderHTML(H5 {}.language("en")) == #"<h5 lang="en"></h5>"#)
try #expect(renderHTML(H6 {}.language("en")) == #"<h6 lang="en"></h6>"#)
}
}