Skip to content

Commit 4480079

Browse files
added compile safety when declaring values for an attribute that takes an array, and...
- `attributionsrc` -> `attributionSrc`
1 parent 82c281b commit 4480079

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

Sources/HTMLKit/HTMLKit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public macro custom<T: ExpressibleByStringLiteral>(tag: String, isVoid: Bool, at
2525
public macro a<T: ExpressibleByStringLiteral>(
2626
attributes: [HTMLElementAttribute] = [],
2727

28-
attributionsrc: [T]? = nil,
28+
attributionSrc: [T]? = nil,
2929
download: HTMLElementAttribute.Extra.download? = nil,
3030
href: T? = nil,
3131
hreflang: T? = nil,
@@ -434,7 +434,7 @@ public macro img<T: ExpressibleByStringLiteral>(
434434
attributes: [HTMLElementAttribute] = [],
435435

436436
alt: T? = nil,
437-
attributionsrc: [T]? = nil,
437+
attributionSrc: [T]? = nil,
438438
crossorigin: HTMLElementAttribute.Extra.crossorigin? = nil,
439439
decoding: HTMLElementAttribute.Extra.decoding? = nil,
440440
elementtiming: T? = nil,
@@ -753,7 +753,7 @@ public macro script<T: ExpressibleByStringLiteral>(
753753
attributes: [HTMLElementAttribute] = [],
754754

755755
async: Bool = false,
756-
attributionsrc: [T]? = nil,
756+
attributionSrc: [T]? = nil,
757757
blocking: HTMLElementAttribute.Extra.blocking? = nil,
758758
crossorigin: HTMLElementAttribute.Extra.crossorigin? = nil,
759759
defer: Bool = false,

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private extension HTMLElement {
2828
if key == "acceptCharset" {
2929
key = "accept-charset"
3030
}
31-
if let string:String = parse_attribute(elementType: elementType, key: key, expression: child.expression) {
31+
if let string:String = parse_attribute(context: context, elementType: elementType, key: key, expression: child.expression) {
3232
attributes.append(key + (string.isEmpty ? "" : "=\\\"" + string + "\\\""))
3333
}
3434
}
@@ -68,7 +68,7 @@ private extension HTMLElement {
6868
value = function.arguments.last!.expression.stringLiteral!.string
6969
break
7070
default:
71-
if let string:String = parse_attribute(elementType: elementType, key: key, expression: key_element) {
71+
if let string:String = parse_attribute(context: context, elementType: elementType, key: key, expression: key_element) {
7272
value = string
7373
}
7474
break
@@ -128,17 +128,24 @@ private extension HTMLElement {
128128
}
129129
}
130130

131-
static func parse_attribute(elementType: HTMLElementType, key: String, expression: ExprSyntax) -> String? {
131+
static func parse_attribute(context: some MacroExpansionContext, elementType: HTMLElementType, key: String, expression: ExprSyntax) -> String? {
132132
if let (string, returnType):(String, LiteralReturnType) = parse_literal_value(elementType: elementType, key: key, expression: expression) {
133133
switch returnType {
134134
case .boolean: return string.elementsEqual("true") ? "" : nil
135135
case .string: return string
136136
case .interpolation: return "\\(" + string + ")"
137137
}
138138
}
139+
let separator:String = get_separator(key: key)
140+
let string_return_logic:(ExprSyntax, String) -> String = {
141+
if $1.contains(separator) {
142+
context.diagnose(Diagnostic(node: $0, message: ErrorDiagnostic(id: "characterNotAllowedInDeclaration", message: "Character \"" + separator + "\" is not allowed when declaring values for \"" + key + "\".")))
143+
}
144+
return $1
145+
}
139146
if let value:String = expression.array?.elements.compactMap({
140147
if let string:String = $0.expression.stringLiteral?.string {
141-
return string
148+
return string_return_logic($0.expression, string)
142149
}
143150
if let string:String = $0.expression.integerLiteral?.literal.text {
144151
return string
@@ -147,7 +154,7 @@ private extension HTMLElement {
147154
return HTMLElementAttribute.Extra.htmlValue(enumName: enumName(elementType: elementType, key: key), for: string)
148155
}
149156
return nil
150-
}).joined(separator: get_separator(key: key)) {
157+
}).joined(separator: separator) {
151158
return value
152159
}
153160
func member(_ value: String) -> String {

Tests/HTMLKitTests/HTMLKitTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ extension HTMLKitTests {
151151
"""
152152
]) == "<script>bro</script>")*/
153153
}
154+
155+
/*@Test func not_allowed() {
156+
let _:StaticString = #a(
157+
attributes: [
158+
.class(["lets go"])
159+
],
160+
attributionSrc: ["lets go"],
161+
ping: ["lets go"]
162+
)
163+
let _:StaticString = #input(
164+
accept: ["lets,go"],
165+
autocomplete: ["lets go"]
166+
)
167+
let _:StaticString = #link(
168+
imagesizes: ["lets,go"],
169+
imagesrcset: ["lets,go"],
170+
rel: ["lets go"],
171+
sizes: ["lets,go"]
172+
)
173+
}*/
154174
}
155175

156176
extension HTMLKitTests {

0 commit comments

Comments
 (0)