Skip to content

Commit 446ae68

Browse files
authored
Add several useful global attributes (#67)
* Add several useful global attributes * Withdraw anchor attribute * Changes requested by PR review: - Remove non-global autofocus - Add values to role - Fix true and false values for draggable - Add popover values * Fix values for role attribute * Remove built-in attributes for role * Fix parameter-less popover attribute
1 parent b307b6b commit 446ae68

File tree

1 file changed

+83
-16
lines changed

1 file changed

+83
-16
lines changed

Sources/Elementary/HtmlAttributes.swift

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,43 @@ public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
3838
static func tabindex(_ index: Int) -> Self {
3939
HTMLAttribute(name: "tabindex", value: "\(index)")
4040
}
41+
42+
static func role(_ role: HTMLAttributeValue.Role) -> Self {
43+
HTMLAttribute(name: "role", value: role.rawValue)
44+
}
45+
46+
static var popover: Self {
47+
HTMLAttribute(name: "popover", value: nil)
48+
}
49+
50+
static func popover(_ popover: HTMLAttributeValue.Popover) -> Self {
51+
HTMLAttribute(name: "popover", value: popover.value)
52+
}
53+
54+
static var inert: Self {
55+
HTMLAttribute(name: "inert", value: nil)
56+
}
57+
58+
static func contenteditable(_ value: HTMLAttributeValue.ContentEditable) -> Self {
59+
HTMLAttribute(name: "contenteditable", value: value.value)
60+
}
61+
62+
static func draggable(_ value: HTMLAttributeValue.Draggable) -> Self {
63+
HTMLAttribute(name: "draggable", value: value.value)
64+
}
65+
66+
static func `is`(_ value: String) -> Self {
67+
HTMLAttribute(name: "is", value: value)
68+
}
69+
70+
static func slot(_ value: String) -> Self {
71+
HTMLAttribute(name: "slot", value: value)
72+
}
73+
74+
static var autofocus: Self {
75+
HTMLAttribute(name: "autofocus", value: nil)
76+
}
77+
4178
}
4279

4380
// dir attribute
@@ -51,12 +88,58 @@ public extension HTMLAttributeValue {
5188
}
5289
}
5390

91+
public extension HTMLAttributeValue {
92+
struct ContentEditable {
93+
var value: String
94+
95+
public static var `true`: Self { .init(value: "true") }
96+
public static var `false`: Self { .init(value: "false") }
97+
public static var plaintextOnly: Self { .init(value: "plaintext-only") }
98+
}
99+
}
100+
101+
public extension HTMLAttributeValue {
102+
struct Popover {
103+
var value: String
104+
105+
public static var auto: Self { .init(value: "auto") }
106+
public static var hint: Self { .init(value: "hint") }
107+
public static var manual: Self { .init(value: "manual") }
108+
}
109+
}
110+
111+
public extension HTMLAttributeValue {
112+
// MDN docs describe draggable as having an enumerated value, but currently
113+
// the only valid values are "true" and "false"
114+
struct Draggable {
115+
var value: String
116+
117+
public static var `true`: Self { .init(value: "true") }
118+
public static var `false`: Self { .init(value: "false") }
119+
}
120+
}
121+
54122
public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
55123
static func dir(_ value: HTMLAttributeValue.Direction) -> Self {
56124
HTMLAttribute(name: "dir", value: value.value)
57125
}
58126
}
59127

128+
// role attribute
129+
public extension HTMLAttributeValue {
130+
struct Role: ExpressibleByStringLiteral, RawRepresentable {
131+
public var rawValue: String
132+
133+
public init(rawValue: String) {
134+
self.rawValue = rawValue
135+
}
136+
137+
public init(stringLiteral value: String) {
138+
rawValue = value
139+
}
140+
}
141+
}
142+
60143
// meta tag attributes
61144
public extension HTMLAttribute where Tag == HTMLTag.meta {
62145
struct Name: Sendable, ExpressibleByStringLiteral {
@@ -215,22 +298,6 @@ public extension HTMLAttribute where Tag: HTMLTrait.Attributes.target {
215298
}
216299
}
217300

218-
// autofocus attribute
219-
public extension HTMLTrait.Attributes {
220-
protocol autofocus {}
221-
}
222-
223-
extension HTMLTag.button: HTMLTrait.Attributes.autofocus {}
224-
extension HTMLTag.input: HTMLTrait.Attributes.autofocus {}
225-
extension HTMLTag.select: HTMLTrait.Attributes.autofocus {}
226-
extension HTMLTag.textarea: HTMLTrait.Attributes.autofocus {}
227-
228-
public extension HTMLAttribute where Tag: HTMLTrait.Attributes.autofocus {
229-
static var autofocus: Self {
230-
HTMLAttribute(name: "autofocus", value: nil)
231-
}
232-
}
233-
234301
// charset attribute
235302
public extension HTMLTrait.Attributes {
236303
protocol charset {}

0 commit comments

Comments
 (0)