@@ -102,17 +102,19 @@ enum HTMLElements : DeclarationMacro {
102102 initializers += " let data:HTMLKitUtilities.ElementData = HTMLKitUtilities.parse_arguments(context: context, children: children \( other_attributes_string) ) \n "
103103 initializers += " self.attributes = data.globalAttributes \n "
104104 for (key, value_type, _) in attributes {
105+ var key_literal : String = key
106+ if key_literal. first == " ` " {
107+ key_literal. removeFirst ( )
108+ key_literal. removeLast ( )
109+ }
105110 var value : String = " as? \( value_type) "
106111 switch value_type {
107112 case " Bool " :
108113 value += " ?? false "
109114 default :
110- if value_type. first == " [ " {
111- value += " ?? [] "
112- }
113115 break
114116 }
115- initializers += " self. \( key) = data.attributes[ \" \( key ) \" ] " + value + " \n "
117+ initializers += " self. \( key) = data.attributes[ \" \( key_literal ) \" ] " + value + " \n "
116118 }
117119 initializers += " self.innerHTML = data.innerHTML \n "
118120 initializers += " \n } "
@@ -130,6 +132,7 @@ enum HTMLElements : DeclarationMacro {
130132 key_literal. removeFirst ( )
131133 key_literal. removeLast ( )
132134 }
135+ let variable_name : String = key_literal
133136 if key_literal == " httpEquiv " {
134137 key_literal = " http-equiv "
135138 } else if key_literal == " acceptCharset " {
@@ -138,30 +141,34 @@ enum HTMLElements : DeclarationMacro {
138141 if value_type == " Bool " {
139142 attributes_func += " \n if \( key) { items.append( \" \( key_literal) \" ) } "
140143 } else if value_type. first == " [ " {
141- attributes_func += " \n if ! \( key ) .isEmpty { \n let string :String = "
144+ attributes_func += " \n if let _ \( variable_name ) :String = "
142145 let separator : String = separator ( key: key)
143146 switch value_type {
144147 case " [String] " :
145- attributes_func += " \( key) .map({ $0.escapingHTML(escapeAttributes: true) }) "
148+ attributes_func += " \( key) ? .map({ $0.escapingHTML(escapeAttributes: true) })"
146149 break
147150 case " [Int] " , " [Float] " :
148- attributes_func += " \( key) .map({ \" \\ ($0) \" }) "
151+ attributes_func += " \( key) ? .map({ \" \\ ($0) \" }) "
149152 break
150153 default :
151- attributes_func += " \( key) .compactMap({ return $0.htmlValue?.escapingHTML(escapeAttributes: true) }) "
154+ attributes_func += " \( key) ? .compactMap({ return $0.htmlValue?.escapingHTML(escapeAttributes: true) })"
152155 break
153156 }
154- attributes_func += " .joined(separator: \" \( separator) \" ) \n "
155- attributes_func += #"items.append(" \#( key_literal) =\\\"" + string + "\\\"")}"#
156- attributes_func += " \n "
157+ attributes_func += " .joined(separator: \" \( separator) \" ) \n { \n "
158+ attributes_func += #"let k:String = _ \#( variable_name) .isEmpty ? "" : "=\\\"" + _ \#( variable_name) + "\\\"""#
159+ attributes_func += " \n items.append( \" \( key_literal) \" + k) "
160+ attributes_func += " \n } "
157161 } else if value_type == " String " || value_type == " Int " || value_type == " Float " || value_type == " Double " {
158162 attributes_func += " \n "
159163 let value : String = value_type == " String " ? key + " .escapingHTML(escapeAttributes: true) " : " String(describing: \( key) ) "
160164 attributes_func += #"if let \#( key) { items.append(" \#( key) =\\\"" + \#( value) + "\\\"") }"#
161165 attributes_func += " \n "
162166 } else {
163167 attributes_func += " \n "
164- attributes_func += #"if let v:String = \#( key) ?.htmlValue { items.append(" \#( key_literal) =\\\"" + v + "\\\"") }"#
168+ attributes_func += " if let \( key) , let v:String = \( key) .htmlValue { \n "
169+ attributes_func += " let s:String = \( key) .htmlValueIsVoidable && v.isEmpty ? \" \" : \" = \\ \" \" + v + \" \\ \" \" \n "
170+ attributes_func += #"items.append(" \#( key_literal) " + s)"#
171+ attributes_func += " \n } "
165172 }
166173 }
167174 attributes_func += " \n return (items.isEmpty ? \" \" : \" \" ) + items.joined(separator: \" \" ) \n } \n "
@@ -188,7 +195,7 @@ enum HTMLElements : DeclarationMacro {
188195 case " array " :
189196 isArray = true
190197 let ( of_type, _, of_type_literal) : ( String , String , HTMLElementValueType ) = parse_value_type ( isArray: & isArray, key: key, expr. as ( FunctionCallExprSyntax . self) !. arguments. first!. expression)
191- return ( " [ " + of_type + " ] " , " = [] " , . array( of: of_type_literal) )
198+ return ( " [ " + of_type + " ] " , " ? = nil " , . array( of: of_type_literal) )
192199 case " attribute " :
193200 return ( " HTMLElementAttribute.Extra. \( key) " , isArray ? " " : " ? = nil " , . attribute)
194201 case " otherAttribute " :
0 commit comments