@@ -14,7 +14,8 @@ public extension HTMLKitUtilities {
14
14
static func escapeHTML( expansion: MacroExpansionExprSyntax , context: some MacroExpansionContext ) -> String {
15
15
return expansion. arguments. children ( viewMode: . all) . compactMap ( {
16
16
guard let child: LabeledExprSyntax = $0. labeled,
17
- var c: CustomStringConvertible = HTMLKitUtilities . parseInnerHTML ( context: context, child: child, lookupFiles: [ ] ) else {
17
+ // TODO: fix the below encoding?
18
+ var c: CustomStringConvertible = HTMLKitUtilities . parseInnerHTML ( context: context, encoding: . string, child: child, lookupFiles: [ ] ) else {
18
19
return nil
19
20
}
20
21
if var element: HTMLElement = c as? HTMLElement {
@@ -59,18 +60,19 @@ public extension HTMLKitUtilities {
59
60
60
61
case . string:
61
62
return " \" \( raw: string) \" "
62
- case . custom( let encoded) :
63
+ case . custom( let encoded, _ ) :
63
64
return " \( raw: encoded. replacingOccurrences ( of: " $0 " , with: string) ) "
64
65
}
65
66
}
66
67
67
68
// MARK: Parse Arguments
68
69
static func parseArguments(
69
70
context: some MacroExpansionContext ,
71
+ encoding: HTMLEncoding ,
70
72
children: SyntaxChildren ,
71
73
otherAttributes: [ String : String ] = [ : ]
72
74
) -> ElementData {
73
- var encoding : HTMLEncoding = HTMLEncoding . string
75
+ var encoding : HTMLEncoding = encoding
74
76
var global_attributes : [ HTMLElementAttribute ] = [ ]
75
77
var attributes : [ String : Any ] = [ : ]
76
78
var innerHTML : [ CustomStringConvertible ] = [ ]
@@ -83,7 +85,12 @@ public extension HTMLKitUtilities {
83
85
if let key: String = child. expression. memberAccess? . declName. baseName. text {
84
86
encoding = HTMLEncoding ( rawValue: key) ?? . string
85
87
} else if let custom: FunctionCallExprSyntax = child. expression. functionCall {
86
- encoding = . custom( custom. arguments. first!. expression. stringLiteral!. string)
88
+ let logic : String = custom. arguments. first!. expression. stringLiteral!. string
89
+ if custom. arguments. count == 1 {
90
+ encoding = . custom( logic)
91
+ } else {
92
+ encoding = . custom( logic, stringDelimiter: custom. arguments. last!. expression. stringLiteral!. string)
93
+ }
87
94
}
88
95
} else if key == " lookupFiles " {
89
96
lookupFiles = Set ( child. expression. array!. elements. compactMap ( { $0. expression. stringLiteral? . string } ) )
@@ -99,15 +106,20 @@ public extension HTMLKitUtilities {
99
106
} else if let string: LiteralReturnType = parse_literal_value ( context: context, key: key, expression: child. expression, lookupFiles: lookupFiles) {
100
107
switch string {
101
108
case . boolean( let b) : attributes [ key] = b
102
- case . string( let s ) , . interpolation( let s ) : attributes [ key] = s
109
+ case . string( _ ) , . interpolation( _ ) : attributes [ key] = string . value ( key : key )
103
110
case . int( let i) : attributes [ key] = i
104
111
case . float( let f) : attributes [ key] = f
105
- case . array( let a) : attributes [ key] = a
112
+ case . array( _) :
113
+ let escaped : LiteralReturnType = string. escapeArray ( )
114
+ switch escaped {
115
+ case . array( let a) : attributes [ key] = a
116
+ default : break
117
+ }
106
118
}
107
119
}
108
120
}
109
121
// inner html
110
- } else if let inner_html: CustomStringConvertible = parseInnerHTML ( context: context, child: child, lookupFiles: lookupFiles) {
122
+ } else if let inner_html: CustomStringConvertible = parseInnerHTML ( context: context, encoding : encoding , child: child, lookupFiles: lookupFiles) {
111
123
innerHTML. append ( inner_html)
112
124
}
113
125
}
@@ -132,7 +144,7 @@ public extension HTMLKitUtilities {
132
144
context. diagnose ( Diagnostic ( node: first_expression, message: DiagnosticMsg ( id: " spacesNotAllowedInAttributeDeclaration " , message: " Spaces are not allowed in attribute declaration. " ) ) )
133
145
} else if keys. contains ( key) {
134
146
global_attribute_already_defined ( context: context, attribute: key, node: first_expression)
135
- } else if let attr: HTMLElementAttribute = HTMLElementAttribute . init ( context: context, key: key, function) {
147
+ } else if let attr: HTMLElementAttribute = HTMLElementAttribute ( context: context, key: key, function) {
136
148
attributes. append ( attr)
137
149
key = attr. key
138
150
keys. insert ( key)
@@ -152,6 +164,7 @@ public extension HTMLKitUtilities {
152
164
// MARK: Parse Inner HTML
153
165
static func parseInnerHTML(
154
166
context: some MacroExpansionContext ,
167
+ encoding: HTMLEncoding ,
155
168
child: LabeledExprSyntax ,
156
169
lookupFiles: Set < String >
157
170
) -> CustomStringConvertible ? {
@@ -160,7 +173,7 @@ public extension HTMLKitUtilities {
160
173
return escapeHTML ( expansion: expansion, context: context)
161
174
}
162
175
return " " // TODO: fix?
163
- } else if let element: HTMLElement = parse_element ( context: context, expr: child. expression) {
176
+ } else if let element: HTMLElement = parse_element ( context: context, encoding : encoding , expr: child. expression) {
164
177
return element
165
178
} else if let string: String = parse_literal_value ( context: context, key: " " , expression: child. expression, lookupFiles: lookupFiles) ? . value ( key: " " ) {
166
179
return string
@@ -171,9 +184,9 @@ public extension HTMLKitUtilities {
171
184
}
172
185
173
186
// MARK: Parse element
174
- static func parse_element( context: some MacroExpansionContext , expr: ExprSyntax ) -> HTMLElement ? {
187
+ static func parse_element( context: some MacroExpansionContext , encoding : HTMLEncoding , expr: ExprSyntax ) -> HTMLElement ? {
175
188
guard let function: FunctionCallExprSyntax = expr. functionCall else { return nil }
176
- return HTMLElementValueType . parse_element ( context: context, function)
189
+ return HTMLElementValueType . parse_element ( context: context, encoding : encoding , function)
177
190
}
178
191
}
179
192
extension HTMLKitUtilities {
@@ -217,7 +230,7 @@ extension HTMLKitUtilities {
217
230
guard macro. macroName. text == " html " else {
218
231
return ( " \( macro) " , . string)
219
232
}
220
- let data : HTMLKitUtilities . ElementData = HTMLKitUtilities . parseArguments ( context: context, children: macro. arguments. children ( viewMode: . all) )
233
+ let data : HTMLKitUtilities . ElementData = HTMLKitUtilities . parseArguments ( context: context, encoding : . string , children: macro. arguments. children ( viewMode: . all) )
221
234
return ( data. innerHTML. map ( { String ( describing: $0) } ) . joined ( ) , data. encoding)
222
235
}
223
236
}
0 commit comments