Skip to content

Commit 5bf3348

Browse files
authored
Make some attributes more typesafe (#201)
* Make position attribute more typesafe * Make center attribute more typesafe * Make radius attribute more typesafe * Make viewbox attribute more typesafe * Add missing conformance of the position attribute * Deprecate the old modifiers * Revise the formatting * Make sizes attribute more typesafe * Make sourceset attribute more typesafe * Make media attribute more typesafe * Make accept attribute more typesafe * Refactor the code a bit
1 parent d3d7015 commit 5bf3348

File tree

12 files changed

+1044
-116
lines changed

12 files changed

+1044
-116
lines changed

Sources/HTMLKit/Abstraction/Attributes/BasicAttributes.swift

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,52 @@ public protocol AcceptAttribute: Attribute {
4747
/// ```swift
4848
/// Input()
4949
/// .type(.file)
50-
/// .accept("image/png, image/jpeg")
50+
/// .accept(["image/png", "image/jpeg"])
5151
/// ```
5252
///
53-
/// - Parameter value: The file types to pick from.
53+
/// - Parameter specifiers: The file types to pick from.
5454
///
5555
/// - Returns: The element
56-
func accept(_ value: String) -> Self
56+
func accept(_ specifiers: [String]) -> Self
57+
58+
/// Filter accepted file types for upload.
59+
///
60+
/// ```swift
61+
/// Input()
62+
/// .type(.file)
63+
/// .accept("image/png", "image/jpeg")
64+
/// ```
65+
///
66+
/// - Parameter specifiers: The file types to pick from.
67+
///
68+
/// - Returns: The element
69+
func accept(_ specifiers: String...) -> Self
70+
71+
/// Filter accepted file types for upload.
72+
///
73+
/// ```swift
74+
/// Input()
75+
/// .type(.file)
76+
/// .accept([.ogg, .mpeg])
77+
/// ```
78+
///
79+
/// - Parameter specifiers: The file types to pick from.
80+
///
81+
/// - Returns: The element
82+
func accept(_ specifiers: [Values.Media]) -> Self
83+
84+
/// Filter accepted file types for upload.
85+
///
86+
/// ```swift
87+
/// Input()
88+
/// .type(.file)
89+
/// .accept(.ogg, .mpeg)
90+
/// ```
91+
///
92+
/// - Parameter specifiers: The file types to pick from.
93+
///
94+
/// - Returns: The element
95+
func accept(_ specifiers: Values.Media...) -> Self
5796
}
5897

5998
extension AcceptAttribute where Self: ContentNode {
@@ -2113,14 +2152,34 @@ public protocol MediaAttribute: Attribute {
21132152
///
21142153
/// ```swift
21152154
/// Link()
2116-
/// .reference("https://...")
2117-
/// .media("print")
2155+
/// .reference("...css")
2156+
/// .media([
2157+
/// MediaQuery(target: .screen, features: .orientation(.portrait)),
2158+
/// MediaQuery(target: .print, features: .resolution("300dpi"))
2159+
/// ])
21182160
/// ```
21192161
///
2120-
/// - Parameter value: The media to be considered.
2162+
/// - Parameter queries: The media to be considered.
21212163
///
21222164
/// - Returns: The element
2123-
func media(_ value: String) -> Self
2165+
func media(_ queries: [MediaQuery]) -> Self
2166+
2167+
2168+
/// Specify the media the ressource is optimized for.
2169+
///
2170+
/// ```swift
2171+
/// Link()
2172+
/// .reference("...css")
2173+
/// .media(
2174+
/// MediaQuery(target: .screen, features: .orientation(.portrait)),
2175+
/// MediaQuery(target: .print, features: .resolution("300dpi"))
2176+
/// )
2177+
/// ```
2178+
///
2179+
/// - Parameter queries: The media to be considered.
2180+
///
2181+
/// - Returns: The element
2182+
func media(_ queries: MediaQuery...) -> Self
21242183
}
21252184

21262185
extension MediaAttribute where Self: ContentNode {
@@ -3139,29 +3198,31 @@ extension SizeAttribute where Self: EmptyNode {
31393198
@_documentation(visibility: internal)
31403199
public protocol SizesAttribute: Attribute {
31413200

3201+
associatedtype SizesValueType
3202+
31423203
/// Describe different sizes for different viewport sizes.
31433204
///
31443205
/// ```swift
31453206
/// Link()
3146-
/// .sizes(16x16)
3207+
/// .sizes("16x16", "32x32")
31473208
/// ```
31483209
///
3149-
/// - Parameter size: The sizes to take into consideration.
3210+
/// - Parameter candidates: The sizes to take into consideration.
31503211
///
31513212
/// - Returns: The element
3152-
func sizes(_ size: Int) -> Self
3213+
func sizes(_ candidates: [SizesValueType]) -> Self
31533214
}
31543215

31553216
extension SizesAttribute where Self: ContentNode {
31563217

3157-
internal func mutate(sizes value: Int) -> Self {
3218+
internal func mutate(sizes value: String) -> Self {
31583219
return self.mutate(key: "sizes", value: value)
31593220
}
31603221
}
31613222

31623223
extension SizesAttribute where Self: EmptyNode {
31633224

3164-
internal func mutate(sizes value: Int) -> Self {
3225+
internal func mutate(sizes value: String) -> Self {
31653226
return self.mutate(key: "sizes", value: value)
31663227
}
31673228
}
@@ -4062,21 +4123,33 @@ extension LoadingAttribute where Self: EmptyNode {
40624123
@_documentation(visibility: internal)
40634124
public protocol SourceSetAttribute: Attribute {
40644125

4065-
/// Set a source set for a picture element.
4126+
/// Define a set of sources for a picture element.
40664127
///
40674128
/// ```swift
40684129
/// Picture {
40694130
/// Source()
4070-
/// .sourceSet("https://...")
4131+
/// .sourceSet([SourceCandidate("...webp", width: 1024), SourceCandidate("...webp", width: 1680)])
4132+
/// }
4133+
/// ```
4134+
///
4135+
/// - Parameter candidates: The candidates to choose from.
4136+
///
4137+
/// - Returns: The element.
4138+
func sourceSet(_ candidates: [SourceCandidate]) -> Self
4139+
4140+
/// Define a set of sources for a picture element.
4141+
///
4142+
/// ```swift
4143+
/// Picture {
40714144
/// Source()
4072-
/// .sourceSet("https://...")
4145+
/// .sourceSet(SourceCandidate("...webp", width: 1024), SourceCandidate("...webp", width: 1680))
40734146
/// }
40744147
/// ```
40754148
///
4076-
/// - Parameter url: The url path to load from.
4149+
/// - Parameter candidates: The candidates to choose from.
40774150
///
40784151
/// - Returns: The element.
4079-
func sourceSet(_ url: String) -> Self
4152+
func sourceSet(_ candidates: SourceCandidate...) -> Self
40804153
}
40814154

40824155
extension SourceSetAttribute where Self: ContentNode {

0 commit comments

Comments
 (0)