@@ -14,7 +14,8 @@ public extension HTMLKitUtilities {
1414 static func escapeHTML( expansion: MacroExpansionExprSyntax , context: some MacroExpansionContext ) -> String {
1515 return expansion. arguments. children ( viewMode: . all) . compactMap ( {
1616 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 {
1819 return nil
1920 }
2021 if var element: HTMLElement = c as? HTMLElement {
@@ -59,18 +60,19 @@ public extension HTMLKitUtilities {
5960
6061 case . string:
6162 return " \" \( raw: string) \" "
62- case . custom( let encoded) :
63+ case . custom( let encoded, _ ) :
6364 return " \( raw: encoded. replacingOccurrences ( of: " $0 " , with: string) ) "
6465 }
6566 }
6667
6768 // MARK: Parse Arguments
6869 static func parseArguments(
6970 context: some MacroExpansionContext ,
71+ encoding: HTMLEncoding ,
7072 children: SyntaxChildren ,
7173 otherAttributes: [ String : String ] = [ : ]
7274 ) -> ElementData {
73- var encoding : HTMLEncoding = HTMLEncoding . string
75+ var encoding : HTMLEncoding = encoding
7476 var global_attributes : [ HTMLElementAttribute ] = [ ]
7577 var attributes : [ String : Any ] = [ : ]
7678 var innerHTML : [ CustomStringConvertible ] = [ ]
@@ -83,7 +85,12 @@ public extension HTMLKitUtilities {
8385 if let key: String = child. expression. memberAccess? . declName. baseName. text {
8486 encoding = HTMLEncoding ( rawValue: key) ?? . string
8587 } 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+ }
8794 }
8895 } else if key == " lookupFiles " {
8996 lookupFiles = Set ( child. expression. array!. elements. compactMap ( { $0. expression. stringLiteral? . string } ) )
@@ -99,15 +106,20 @@ public extension HTMLKitUtilities {
99106 } else if let string: LiteralReturnType = parse_literal_value ( context: context, key: key, expression: child. expression, lookupFiles: lookupFiles) {
100107 switch string {
101108 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 )
103110 case . int( let i) : attributes [ key] = i
104111 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+ }
106118 }
107119 }
108120 }
109121 // 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) {
111123 innerHTML. append ( inner_html)
112124 }
113125 }
@@ -132,7 +144,7 @@ public extension HTMLKitUtilities {
132144 context. diagnose ( Diagnostic ( node: first_expression, message: DiagnosticMsg ( id: " spacesNotAllowedInAttributeDeclaration " , message: " Spaces are not allowed in attribute declaration. " ) ) )
133145 } else if keys. contains ( key) {
134146 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) {
136148 attributes. append ( attr)
137149 key = attr. key
138150 keys. insert ( key)
@@ -152,6 +164,7 @@ public extension HTMLKitUtilities {
152164 // MARK: Parse Inner HTML
153165 static func parseInnerHTML(
154166 context: some MacroExpansionContext ,
167+ encoding: HTMLEncoding ,
155168 child: LabeledExprSyntax ,
156169 lookupFiles: Set < String >
157170 ) -> CustomStringConvertible ? {
@@ -160,7 +173,7 @@ public extension HTMLKitUtilities {
160173 return escapeHTML ( expansion: expansion, context: context)
161174 }
162175 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) {
164177 return element
165178 } else if let string: String = parse_literal_value ( context: context, key: " " , expression: child. expression, lookupFiles: lookupFiles) ? . value ( key: " " ) {
166179 return string
@@ -171,9 +184,9 @@ public extension HTMLKitUtilities {
171184 }
172185
173186 // 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 ? {
175188 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)
177190 }
178191}
179192extension HTMLKitUtilities {
@@ -217,7 +230,7 @@ extension HTMLKitUtilities {
217230 guard macro. macroName. text == " html " else {
218231 return ( " \( macro) " , . string)
219232 }
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) )
221234 return ( data. innerHTML. map ( { String ( describing: $0) } ) . joined ( ) , data. encoding)
222235 }
223236}
0 commit comments