Skip to content

Commit 7b1ef12

Browse files
added missing command attribute to button macro
1 parent 45ef2d6 commit 7b1ef12

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

Sources/HTMLKit/HTMLKit.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public macro br<T: ExpressibleByStringLiteral>(
178178
public macro button<T: ExpressibleByStringLiteral>(
179179
attributes: [HTMLElementAttribute] = [],
180180

181+
command: HTMLElementAttribute.Extra.command? = nil,
181182
disabled: Bool = false,
182183
form: T? = nil,
183184
formaction: T? = nil,

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private extension HTMLElement {
324324
}
325325
if let function:FunctionCallExprSyntax = expression.functionCall {
326326
switch key {
327-
case "download", "height", "width":
327+
case "command", "download", "height", "width":
328328
var value:String = "\(function)"
329329
value = String(value[value.index(after: value.startIndex)...])
330330
value = HTMLElementAttribute.Extra.htmlValue(enumName: enumName(elementType: elementType, key: key), for: value)
@@ -628,6 +628,7 @@ extension StringLiteralExprSyntax {
628628
extension HTMLElementAttribute.Extra {
629629
static func htmlValue(enumName: String, for enumCase: String) -> String { // only need to check the ones where the htmlValue is different from the rawValue
630630
switch enumName {
631+
case "command": return command(rawValue: enumCase)!.htmlValue
631632
case "contenteditable": return contenteditable(rawValue: enumCase)!.htmlValue
632633
case "crossorigin": return crossorigin(rawValue: enumCase)!.htmlValue
633634
case "download": return download(rawValue: enumCase)!.htmlValue

Sources/HTMLKitUtilities/HTMLKitUtilities.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,43 @@ public extension HTMLElementAttribute.Extra {
160160
case user, environment
161161
}
162162

163+
enum command {
164+
case showModal
165+
case close
166+
case showPopover
167+
case hidePopover
168+
case togglePopover
169+
case custom(String)
170+
171+
public init?(rawValue: String) {
172+
switch rawValue {
173+
case "showModal": self = .showModal
174+
case "close": self = .close
175+
case "showPopover": self = .showPopover
176+
case "hidePopover": self = .hidePopover
177+
case "togglePopover": self = .togglePopover
178+
default:
179+
if rawValue.starts(with: "custom(\"") && rawValue.hasSuffix("\")") {
180+
let value:String = String(rawValue[rawValue.index(rawValue.startIndex, offsetBy: 8)..<rawValue.index(rawValue.endIndex, offsetBy: -2)])
181+
self = .custom(value)
182+
} else {
183+
return nil
184+
}
185+
}
186+
}
187+
188+
public var htmlValue : String {
189+
switch self {
190+
case .showModal: return "show-modal"
191+
case .close: return "close"
192+
case .showPopover: return "show-popover"
193+
case .hidePopover: return "hide-popover"
194+
case .togglePopover: return "toggle-popover"
195+
case .custom(let value): return "--" + value
196+
}
197+
}
198+
}
199+
163200
enum contenteditable : String {
164201
case `true`, `false`
165202
case plaintextOnly

Tests/HTMLKitTests/ElementTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,16 @@ extension ElementTests {
129129

130130
string = #audio(crossorigin: .useCredentials)
131131
#expect(string == "<audio crossorigin=\"use-credentials\"></audio>")
132+
133+
string = #audio(disableremoteplayback: true)
134+
#expect(string == "<audio disableremoteplayback></audio>")
135+
136+
string = #audio(preload: .auto)
137+
#expect(string == "<audio preload=\"auto\"></audio>")
132138
}
133139

134140
// MARK: button
141+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
135142
@Test func button() {
136143
var string:StaticString = #button(type: .submit)
137144
#expect(string == "<button type=\"submit\"></button>")
@@ -144,6 +151,21 @@ extension ElementTests {
144151

145152
string = #button(formenctype: .textPlain, formmethod: .get, type: .reset)
146153
#expect(string == "<button formenctype=\"text/plain\" formmethod=\"get\" type=\"reset\"></button>")
154+
155+
string = #button(command: .showModal)
156+
#expect(string == "<button command=\"show-modal\"></button>")
157+
158+
string = #button(command: .showPopover)
159+
#expect(string == "<button command=\"show-popover\"></button>")
160+
161+
string = #button(command: .hidePopover)
162+
#expect(string == "<button command=\"hide-popover\"></button>")
163+
164+
string = #button(command: .togglePopover)
165+
#expect(string == "<button command=\"toggle-popover\"></button>")
166+
167+
string = #button(command: .custom("bingbong"))
168+
#expect(string == "<button command=\"--bingbong\"></button>")
147169
}
148170

149171
// MARK: canvas

0 commit comments

Comments
 (0)