Skip to content

Commit 32d152b

Browse files
fixes #4
1 parent e7f6e00 commit 32d152b

File tree

9 files changed

+103
-51
lines changed

9 files changed

+103
-51
lines changed

Sources/HTMLKitUtilities/HTMLEncoding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public enum HTMLEncoding {
7575
}
7676
}
7777

78-
public var stringDelimiter : String {
78+
public func stringDelimiter(forMacro: Bool) -> String {
7979
switch self {
8080
case .string:
81-
return "\\\""
81+
return forMacro ? "\\\"" : "\""
8282
case .utf8Bytes, .utf16Bytes, .utf8CString, .foundationData, .byteBuffer:
8383
return "\""
8484
case .custom(_, let delimiter):

Sources/HTMLKitUtilities/LiteralElements.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ public struct custom : HTMLElement {
155155
public var escaped:Bool = false
156156
public let tag:String
157157
private var encoding:HTMLEncoding = .string
158+
private var fromMacro:Bool = false
158159
public var attributes:[HTMLElementAttribute]
159160
public var innerHTML:[CustomStringConvertible]
160161

161162
public init(_ context: some MacroExpansionContext, _ encoding: HTMLEncoding, _ children: SyntaxChildren) {
162163
self.encoding = encoding
164+
fromMacro = true
163165
let data:HTMLKitUtilities.ElementData = HTMLKitUtilities.parseArguments(context: context, encoding: encoding, children: children)
164166
tag = data.attributes["tag"] as? String ?? ""
165167
isVoid = data.attributes["isVoid"] as? Bool ?? false
@@ -182,8 +184,8 @@ public struct custom : HTMLElement {
182184

183185
public var description : String {
184186
let attributes_string:String = self.attributes.compactMap({
185-
guard let v:String = $0.htmlValue(encoding) else { return nil }
186-
let delimiter:String = $0.htmlValueDelimiter(encoding)
187+
guard let v:String = $0.htmlValue(encoding: encoding, forMacro: fromMacro) else { return nil }
188+
let delimiter:String = $0.htmlValueDelimiter(encoding: encoding, forMacro: fromMacro)
187189
return $0.key + ($0.htmlValueIsVoidable && v.isEmpty ? "" : "=\(delimiter)\(v)\(delimiter)")
188190
}).joined(separator: " ")
189191
let l:String, g:String

Sources/HTMLKitUtilities/attributes/HTMLElementAttribute.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,21 @@ public enum HTMLElementAttribute : Hashable {
178178
}
179179

180180
// MARK: htmlValue
181-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
181+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
182182
switch self {
183183
case .accesskey(let value): return value
184-
case .ariaattribute(let value): return value?.htmlValue(encoding)
184+
case .ariaattribute(let value): return value?.htmlValue(encoding: encoding, forMacro: forMacro)
185185
case .role(let value): return value?.rawValue
186186
case .autocapitalize(let value): return value?.rawValue
187187
case .autofocus(let value): return value == true ? "" : nil
188188
case .class(let value): return value?.joined(separator: " ")
189-
case .contenteditable(let value): return value?.htmlValue(encoding)
189+
case .contenteditable(let value): return value?.htmlValue(encoding: encoding, forMacro: forMacro)
190190
case .data(_, let value): return value
191191
case .dir(let value): return value?.rawValue
192192
case .draggable(let value): return value?.rawValue
193193
case .enterkeyhint(let value): return value?.rawValue
194194
case .exportparts(let value): return value?.joined(separator: ",")
195-
case .hidden(let value): return value?.htmlValue(encoding)
195+
case .hidden(let value): return value?.htmlValue(encoding: encoding, forMacro: forMacro)
196196
case .id(let value): return value
197197
case .inert(let value): return value == true ? "" : nil
198198
case .inputmode(let value): return value?.rawValue
@@ -217,7 +217,7 @@ public enum HTMLElementAttribute : Hashable {
217217

218218
case .trailingSlash: return nil
219219

220-
case .htmx(let htmx): return htmx?.htmlValue(encoding)
220+
case .htmx(let htmx): return htmx?.htmlValue(encoding: encoding, forMacro: forMacro)
221221
case .custom(_, let value): return value
222222
case .event(_, let value): return value
223223
}
@@ -236,14 +236,14 @@ public enum HTMLElementAttribute : Hashable {
236236
}
237237

238238
// MARK: htmlValueDelimiter
239-
public func htmlValueDelimiter(_ encoding: HTMLEncoding) -> String {
239+
public func htmlValueDelimiter(encoding: HTMLEncoding, forMacro: Bool) -> String {
240240
switch self {
241241
case .htmx(let v):
242242
switch v {
243243
case .request(_, _, _, _), .headers(_, _): return "'"
244-
default: return encoding.stringDelimiter
244+
default: return encoding.stringDelimiter(forMacro: forMacro)
245245
}
246-
default: return encoding.stringDelimiter
246+
default: return encoding.stringDelimiter(forMacro: forMacro)
247247
}
248248
}
249249
}
@@ -331,7 +331,7 @@ public extension HTMLElementAttribute {
331331
}
332332
}
333333

334-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
334+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
335335
switch self {
336336
case .centimeters(let v),
337337
.millimeters(let v),

Sources/HTMLKitUtilities/attributes/HTMLElementAttributeExtra.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public protocol HTMLInitializable : Hashable {
1313
init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax)
1414

1515
var key : String { get }
16-
func htmlValue(_ encoding: HTMLEncoding) -> String?
16+
func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String?
1717
var htmlValueIsVoidable : Bool { get }
1818
}
1919
public extension HTMLInitializable where Self: RawRepresentable, RawValue == String {
2020
var key : String { rawValue }
21-
func htmlValue(_ encoding: HTMLEncoding) -> String? { rawValue }
21+
func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? { rawValue }
2222
var htmlValueIsVoidable : Bool { false }
2323

2424
init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
@@ -302,7 +302,7 @@ public extension HTMLElementAttribute.Extra {
302302
}
303303
}
304304

305-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
305+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
306306
func unwrap<T>(_ value: T?) -> String? {
307307
guard let value:T = value else { return nil }
308308
return "\(value)"
@@ -596,7 +596,7 @@ public extension HTMLElementAttribute.Extra {
596596
}
597597
}
598598

599-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
599+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
600600
switch self {
601601
case .showModal: return "show-modal"
602602
case .close: return "close"
@@ -615,7 +615,7 @@ public extension HTMLElementAttribute.Extra {
615615
case `true`, `false`
616616
case plaintextOnly
617617

618-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
618+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
619619
switch self {
620620
case .plaintextOnly: return "plaintext-only"
621621
default: return rawValue
@@ -633,7 +633,7 @@ public extension HTMLElementAttribute.Extra {
633633
case anonymous
634634
case useCredentials
635635

636-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
636+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
637637
switch self {
638638
case .useCredentials: return "use-credentials"
639639
default: return rawValue
@@ -681,7 +681,7 @@ public extension HTMLElementAttribute.Extra {
681681
}
682682
}
683683

684-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
684+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
685685
switch self {
686686
case .empty: return ""
687687
case .filename(let value): return value
@@ -735,7 +735,7 @@ public extension HTMLElementAttribute.Extra {
735735
case multipartFormData
736736
case textPlain
737737

738-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
738+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
739739
switch self {
740740
case .applicationXWWWFormURLEncoded: return "application/x-www-form-urlencoded"
741741
case .multipartFormData: return "multipart/form-data"
@@ -759,7 +759,7 @@ public extension HTMLElementAttribute.Extra {
759759
case `true`
760760
case untilFound
761761

762-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
762+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
763763
switch self {
764764
case .true: return ""
765765
case .untilFound: return "until-found"
@@ -775,7 +775,7 @@ public extension HTMLElementAttribute.Extra {
775775
case xUACompatible
776776
case refresh
777777

778-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
778+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
779779
switch self {
780780
case .contentSecurityPolicy: return "content-security-policy"
781781
case .contentType: return "content-type"
@@ -797,7 +797,7 @@ public extension HTMLElementAttribute.Extra {
797797
case datetimeLocal
798798
case email, file, hidden, image, month, number, password, radio, range, reset, search, submit, tel, text, time, url, week
799799

800-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
800+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
801801
switch self {
802802
case .datetimeLocal: return "datetime-local"
803803
default: return rawValue
@@ -819,7 +819,7 @@ public extension HTMLElementAttribute.Extra {
819819
enum numberingtype : String, HTMLInitializable {
820820
case a, A, i, I, one
821821

822-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
822+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
823823
switch self {
824824
case .one: return "1"
825825
default: return rawValue
@@ -853,7 +853,7 @@ public extension HTMLElementAttribute.Extra {
853853
case strictOriginWhenCrossOrigin
854854
case unsafeURL
855855

856-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
856+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
857857
switch self {
858858
case .noReferrer: return "no-referrer"
859859
case .noReferrerWhenDowngrade: return "no-referrer-when-downgrade"
@@ -877,7 +877,7 @@ public extension HTMLElementAttribute.Extra {
877877
case search, stylesheet, tag
878878
case termsOfService
879879

880-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
880+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
881881
switch self {
882882
case .dnsPrefetch: return "dns-prefetch"
883883
case .privacyPolicy: return "privacy-policy"
@@ -904,7 +904,7 @@ public extension HTMLElementAttribute.Extra {
904904
case allowTopNavigationByUserActivation
905905
case allowTopNavigationToCustomProtocols
906906

907-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
907+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
908908
switch self {
909909
case .allowDownloads: return "allow-downloads"
910910
case .allowForms: return "allow-forms"

Sources/HTMLKitUtilities/attributes/HTMX.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public extension HTMLElementAttribute {
158158
}
159159

160160
// MARK: htmlValue
161-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
161+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
162162
switch self {
163163
case .boost(let value): return value?.rawValue
164164
case .confirm(let value): return value
@@ -169,7 +169,7 @@ public extension HTMLElementAttribute {
169169
case .encoding(let value): return value
170170
case .ext(let value): return value
171171
case .headers(let js, let headers):
172-
let delimiter:String = encoding.stringDelimiter
172+
let delimiter:String = encoding.stringDelimiter(forMacro: forMacro)
173173
let value:String = headers.map({ item in
174174
delimiter + item.key + delimiter + ":" + delimiter + item.value + delimiter
175175
}).joined(separator: ",")
@@ -179,14 +179,14 @@ public extension HTMLElementAttribute {
179179
case .include(let value): return value
180180
case .indicator(let value): return value
181181
case .inherit(let value): return value
182-
case .params(let params): return params?.htmlValue(encoding)
182+
case .params(let params): return params?.htmlValue(encoding: encoding, forMacro: forMacro)
183183
case .patch(let value): return value
184184
case .preserve(let value): return value ?? false ? "" : nil
185185
case .prompt(let value): return value
186186
case .put(let value): return value
187-
case .replaceURL(let url): return url?.htmlValue(encoding)
187+
case .replaceURL(let url): return url?.htmlValue(encoding: encoding, forMacro: forMacro)
188188
case .request(let js, let timeout, let credentials, let noHeaders):
189-
let delimiter:String = encoding.stringDelimiter
189+
let delimiter:String = encoding.stringDelimiter(forMacro: forMacro)
190190
if let timeout:String = timeout {
191191
return js ? "js: timeout:\(timeout)" : "{" + delimiter + "timeout" + delimiter + ":\(timeout)}"
192192
} else if let credentials:String = credentials {
@@ -197,14 +197,14 @@ public extension HTMLElementAttribute {
197197
return ""
198198
}
199199
case .sync(let selector, let strategy):
200-
return selector + (strategy == nil ? "" : ":" + strategy!.htmlValue(encoding)!)
200+
return selector + (strategy == nil ? "" : ":" + strategy!.htmlValue(encoding: encoding, forMacro: forMacro)!)
201201
case .validate(let value): return value?.rawValue
202202

203203
case .get(let value): return value
204204
case .post(let value): return value
205205
case .on(_, let value): return value
206206
case .onevent(_, let value): return value
207-
case .pushURL(let url): return url?.htmlValue(encoding)
207+
case .pushURL(let url): return url?.htmlValue(encoding: encoding, forMacro: forMacro)
208208
case .select(let value): return value
209209
case .selectOOB(let value): return value
210210
case .swap(let swap): return swap?.rawValue
@@ -213,8 +213,8 @@ public extension HTMLElementAttribute {
213213
case .trigger(let value): return value
214214
case .vals(let value): return value
215215

216-
case .sse(let value): return value?.htmlValue(encoding)
217-
case .ws(let value): return value?.htmlValue(encoding)
216+
case .sse(let value): return value?.htmlValue(encoding: encoding, forMacro: forMacro)
217+
case .ws(let value): return value?.htmlValue(encoding: encoding, forMacro: forMacro)
218218
}
219219
}
220220

Sources/HTMLKitUtilities/attributes/HTMXAttributes.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public extension HTMLElementAttribute.HTMX {
143143
}
144144
}
145145

146-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
146+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
147147
switch self {
148148
case .all: return "*"
149149
case .none: return "none"
@@ -195,7 +195,7 @@ public extension HTMLElementAttribute.HTMX {
195195
}
196196
}
197197

198-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
198+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
199199
switch self {
200200
case .drop: return "drop"
201201
case .abort: return "abort"
@@ -229,7 +229,7 @@ public extension HTMLElementAttribute.HTMX {
229229
}
230230
}
231231

232-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
232+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
233233
switch self {
234234
case .true: return "true"
235235
case .false: return "false"
@@ -266,7 +266,7 @@ public extension HTMLElementAttribute.HTMX {
266266
}
267267
}
268268

269-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
269+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
270270
switch self {
271271
case .connect(let value),
272272
.swap(let value),
@@ -303,7 +303,7 @@ public extension HTMLElementAttribute.HTMX {
303303
}
304304
}
305305

306-
public func htmlValue(_ encoding: HTMLEncoding) -> String? {
306+
public func htmlValue(encoding: HTMLEncoding, forMacro: Bool) -> String? {
307307
switch self {
308308
case .connect(let value): return value
309309
case .send(let value): return value ?? false ? "" : nil

0 commit comments

Comments
 (0)