@@ -12,21 +12,19 @@ import HTMLKitUtilities
1212
1313struct HTMLElement : ExpressionMacro {
1414 static func expansion( of node: some FreestandingMacroExpansionSyntax , in context: some MacroExpansionContext ) throws -> ExprSyntax {
15- var dynamicVariables : [ String ] = [ ]
16- let ( string, isDynamic) : ( String , Bool ) = parse_macro ( context: context, expression: node. as ( MacroExpansionExprSyntax . self) !, isRoot: true , dynamicVariables: & dynamicVariables)
17- return isDynamic ? " \( raw: string) " : " \" \( raw: string) \" "
15+ return " \" \( raw: parse_macro ( context: context, expression: node. as ( MacroExpansionExprSyntax . self) !) ) \" "
1816 }
1917}
2018
2119private extension HTMLElement {
22- static func parse_macro( context: some MacroExpansionContext , expression: MacroExpansionExprSyntax , isRoot : Bool , dynamicVariables : inout [ String ] ) -> ( String , Bool ) {
23- guard let elementType: HTMLElementType = HTMLElementType ( rawValue: expression. macroName. text) else { return ( " \( expression) " , true ) }
20+ static func parse_macro( context: some MacroExpansionContext , expression: MacroExpansionExprSyntax ) -> String {
21+ guard let elementType: HTMLElementType = HTMLElementType ( rawValue: expression. macroName. text) else { return " \( expression) " }
2422 let childs : SyntaxChildren = expression. arguments. children ( viewMode: . all)
2523 if elementType == . escapeHTML {
26- return ( childs. compactMap ( {
24+ return childs. compactMap ( {
2725 guard let child: LabeledExprSyntax = $0. labeled else { return nil }
28- return parse_inner_html ( context: context, elementType: elementType, child: child, dynamicVariables : & dynamicVariables )
29- } ) . joined ( ) , false )
26+ return parse_inner_html ( context: context, elementType: elementType, child: child)
27+ } ) . joined ( )
3028 }
3129 let tag : String , isVoid : Bool
3230 var children : Slice < SyntaxChildren >
@@ -40,51 +38,44 @@ private extension HTMLElement {
4038 isVoid = elementType. isVoid
4139 children = childs. prefix ( childs. count)
4240 }
43- let data : ElementData = parse_arguments ( context: context, elementType: elementType, children: children, dynamicVariables : & dynamicVariables )
41+ let data : ElementData = parse_arguments ( context: context, elementType: elementType, children: children)
4442 var string : String = ( elementType == . html ? " <!DOCTYPE html> " : " " ) + " < " + tag + data. attributes + " > " + data. innerHTML
4543 if !isVoid {
4644 string += " </ " + tag + " > "
4745 }
48- return ( string, false )
49-
50- /*if isRoot {
51- return dynamicVariables.isEmpty ? (string, false) : ("DynamicString(string: \"" + string + "\").test", true)
52- //return dynamicVariables.isEmpty ? (string, false) : ("DynamicString(string: \"" + string + "\", values: [" + dynamicVariables.map({ $0.contains("\\(") ? "\"\($0)\"" : $0 }).joined(separator: ",") + "]).test", true)
53- } else {
54- return (string, false)
55- }*/
46+ return string
5647 }
57- static func parse_arguments( context: some MacroExpansionContext , elementType: HTMLElementType , children: Slice < SyntaxChildren > , dynamicVariables : inout [ String ] ) -> ElementData {
48+ static func parse_arguments( context: some MacroExpansionContext , elementType: HTMLElementType , children: Slice < SyntaxChildren > ) -> ElementData {
5849 var attributes : [ String ] = [ ] , innerHTML : [ String ] = [ ]
5950 for element in children {
6051 if let child: LabeledExprSyntax = element. labeled {
6152 if var key: String = child. label? . text {
6253 if key == " attributes " {
63- attributes. append ( contentsOf: parse_global_attributes ( context: context, elementType: elementType, array: child. expression. array!, dynamicVariables : & dynamicVariables ) )
54+ attributes. append ( contentsOf: parse_global_attributes ( context: context, elementType: elementType, array: child. expression. array!) )
6455 } else {
6556 if key == " acceptCharset " {
6657 key = " accept-charset "
6758 }
68- if let string: String = parse_attribute ( context: context, elementType: elementType, key: key, argument: child, dynamicVariables : & dynamicVariables ) {
59+ if let string: String = parse_attribute ( context: context, elementType: elementType, key: key, argument: child) {
6960 attributes. append ( key + ( string. isEmpty ? " " : " = \\ \" " + string + " \\ \" " ) )
7061 }
7162 }
7263 // inner html
73- } else if let inner_html: String = parse_inner_html ( context: context, elementType: elementType, child: child, dynamicVariables : & dynamicVariables ) {
64+ } else if let inner_html: String = parse_inner_html ( context: context, elementType: elementType, child: child) {
7465 innerHTML. append ( inner_html)
7566 }
7667 }
7768 }
7869 return ElementData ( attributes: attributes, innerHTML: innerHTML)
7970 }
80- static func parse_global_attributes( context: some MacroExpansionContext , elementType: HTMLElementType , array: ArrayExprSyntax , dynamicVariables : inout [ String ] ) -> [ String ] {
71+ static func parse_global_attributes( context: some MacroExpansionContext , elementType: HTMLElementType , array: ArrayExprSyntax ) -> [ String ] {
8172 var keys : Set < String > = [ ] , attributes : [ String ] = [ ]
8273 for element in array. elements {
8374 let function : FunctionCallExprSyntax = element. expression. as ( FunctionCallExprSyntax . self) !, key_argument : LabeledExprSyntax = function. arguments. first!, key_element : ExprSyntax = key_argument. expression
8475 var key : String = function. calledExpression. memberAccess!. declName. baseName. text, value : String ? = nil
8576 switch key {
8677 case " custom " , " data " :
87- var ( literalValue, returnType) : ( String , LiteralReturnType ) = parse_literal_value ( context: context, elementType: elementType, key: key, argument: function. arguments. last!, dynamicVariables : & dynamicVariables ) !
78+ var ( literalValue, returnType) : ( String , LiteralReturnType ) = parse_literal_value ( context: context, elementType: elementType, key: key, argument: function. arguments. last!) !
8879 if returnType == . string {
8980 literalValue. escapeHTML ( escapeAttributes: true )
9081 }
@@ -97,7 +88,7 @@ private extension HTMLElement {
9788 break
9889 case " event " :
9990 key = " on " + key_element. memberAccess!. declName. baseName. text
100- if var ( literalValue, returnType) : ( String , LiteralReturnType ) = parse_literal_value ( context: context, elementType: elementType, key: key, argument: function. arguments. last!, dynamicVariables : & dynamicVariables ) {
91+ if var ( literalValue, returnType) : ( String , LiteralReturnType ) = parse_literal_value ( context: context, elementType: elementType, key: key, argument: function. arguments. last!) {
10192 if returnType == . string {
10293 literalValue. escapeHTML ( escapeAttributes: true )
10394 }
@@ -108,7 +99,7 @@ private extension HTMLElement {
10899 }
109100 break
110101 default :
111- if let string: String = parse_attribute ( context: context, elementType: elementType, key: key, argument: key_argument, dynamicVariables : & dynamicVariables ) {
102+ if let string: String = parse_attribute ( context: context, elementType: elementType, key: key, argument: key_argument) {
112103 value = string
113104 }
114105 break
@@ -126,14 +117,14 @@ private extension HTMLElement {
126117 }
127118 return attributes
128119 }
129- static func parse_inner_html( context: some MacroExpansionContext , elementType: HTMLElementType , child: LabeledExprSyntax , dynamicVariables : inout [ String ] ) -> String ? {
120+ static func parse_inner_html( context: some MacroExpansionContext , elementType: HTMLElementType , child: LabeledExprSyntax ) -> String ? {
130121 if let macro: MacroExpansionExprSyntax = child. expression. macroExpansion {
131- var string : String = parse_macro ( context: context, expression: macro, isRoot : false , dynamicVariables : & dynamicVariables ) . 0
122+ var string : String = parse_macro ( context: context, expression: macro)
132123 if elementType == . escapeHTML {
133124 string. escapeHTML ( escapeAttributes: false )
134125 }
135126 return string
136- } else if var string: String = parse_literal_value ( context: context, elementType: elementType, key: " " , argument: child, dynamicVariables : & dynamicVariables ) ? . value {
127+ } else if var string: String = parse_literal_value ( context: context, elementType: elementType, key: " " , argument: child) ? . value {
137128 string. escapeHTML ( escapeAttributes: false )
138129 return string
139130 } else {
@@ -171,9 +162,9 @@ private extension HTMLElement {
171162 }
172163 }
173164
174- static func parse_attribute( context: some MacroExpansionContext , elementType: HTMLElementType , key: String , argument: LabeledExprSyntax , dynamicVariables : inout [ String ] ) -> String ? {
165+ static func parse_attribute( context: some MacroExpansionContext , elementType: HTMLElementType , key: String , argument: LabeledExprSyntax ) -> String ? {
175166 let expression : ExprSyntax = argument. expression
176- if var ( string, returnType) : ( String , LiteralReturnType ) = parse_literal_value ( context: context, elementType: elementType, key: key, argument: argument, dynamicVariables : & dynamicVariables ) {
167+ if var ( string, returnType) : ( String , LiteralReturnType ) = parse_literal_value ( context: context, elementType: elementType, key: key, argument: argument) {
177168 switch returnType {
178169 case . boolean: return string. elementsEqual ( " true " ) ? " " : nil
179170 case . string:
@@ -198,7 +189,7 @@ private extension HTMLElement {
198189 default : return " "
199190 }
200191 }
201- static func parse_literal_value( context: some MacroExpansionContext , elementType: HTMLElementType , key: String , argument: LabeledExprSyntax , dynamicVariables : inout [ String ] ) -> ( value: String , returnType: LiteralReturnType ) ? {
192+ static func parse_literal_value( context: some MacroExpansionContext , elementType: HTMLElementType , key: String , argument: LabeledExprSyntax ) -> ( value: String , returnType: LiteralReturnType ) ? {
202193 let expression : ExprSyntax = argument. expression
203194 if let boolean: String = expression. booleanLiteral? . literal. text {
204195 return ( boolean, . boolean)
@@ -286,7 +277,6 @@ private extension HTMLElement {
286277 }
287278 }
288279 if returnType == . interpolation || remaining_interpolation > 0 {
289- //dynamicVariables.append(string)
290280 if !string. contains ( " \\ ( " ) {
291281 string = " \\ ( " + string + " ) "
292282 }
0 commit comments