Skip to content

Commit

Permalink
Add id modifier.
Browse files Browse the repository at this point in the history
Part of #27.
  • Loading branch information
jverkoey committed Aug 3, 2024
1 parent 7d1ada7 commit 7a561b9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Sources/Slipstream/Views/Fundamentals/AttributeModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public struct AttributeModifier<T: View>: ViewModifier {

/// A W3C global attribute, as defined in [3.2.3 Global attributes](https://html.spec.whatwg.org/multipage/dom.html#global-attributes).
public enum GlobalAttribute: String {
/// The id attribute specifies its element's unique identifier (ID).
///
/// There are no other restrictions on what form an ID can take; in particular,
/// IDs can consist of just digits, start with a digit, start with an
/// underscore, consist of just punctuation, etc.
case id

/// The lang attribute specifies the primary language for the view's contents
/// and for any of the view's attributes that contain text. Its value must be a
/// valid [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag),
Expand Down
11 changes: 11 additions & 0 deletions Sources/Slipstream/Views/W3C/Attributes/View+id.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extension View {
/// Sets the element's unique identifier (ID)..
///
/// - Parameter identifier: There are no other restrictions on what form an ID can take; in particular,
/// IDs can consist of just digits, start with a digit, start with an
/// underscore, consist of just punctuation, etc.
@available(iOS 17.0, macOS 14.0, *)
public func id(_ identifier: String) -> some View {
return modifier(AttributeModifier(.id, value: identifier))
}
}
22 changes: 22 additions & 0 deletions Tests/SlipstreamTests/Attributes/IDTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Testing

import Slipstream

private struct IDView: View {
var body: some View {
HTML {
Text("Bonjour!")
}
.id("root")
}
}

struct IDTests {
@Test func french() throws {
try #expect(renderHTML(IDView()) == """
<html id="root">
Bonjour!
</html>
""")
}
}
3 changes: 2 additions & 1 deletion Tests/SlipstreamTests/Sites/CatalogSiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private struct CatalogSite: View {
Text("Hello, world!")
}
}
.id("root")
}
}
}
Expand All @@ -34,7 +35,7 @@ struct CatalogSiteTests {
<meta name="description" content="Slipstream is a static website generator built in the Swift programming language and compatible with Tailwind CSS." />
<link rel="stylesheet" href="/css/bootstrap.css" />
</head>
<body>
<body id="root">
<div>
Hello, world!
</div>
Expand Down
File renamed without changes.

0 comments on commit 7a561b9

Please sign in to comment.