@@ -12,21 +12,19 @@ import HTMLKitUtilities
12
12
13
13
struct HTMLElement : ExpressionMacro {
14
14
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) !) ) \" "
18
16
}
19
17
}
20
18
21
19
private 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) " }
24
22
let childs : SyntaxChildren = expression. arguments. children ( viewMode: . all)
25
23
if elementType == . escapeHTML {
26
- return ( childs. compactMap ( {
24
+ return childs. compactMap ( {
27
25
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 ( )
30
28
}
31
29
let tag : String , isVoid : Bool
32
30
var children : Slice < SyntaxChildren >
@@ -40,51 +38,44 @@ private extension HTMLElement {
40
38
isVoid = elementType. isVoid
41
39
children = childs. prefix ( childs. count)
42
40
}
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)
44
42
var string : String = ( elementType == . html ? " <!DOCTYPE html> " : " " ) + " < " + tag + data. attributes + " > " + data. innerHTML
45
43
if !isVoid {
46
44
string += " </ " + tag + " > "
47
45
}
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
56
47
}
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 {
58
49
var attributes : [ String ] = [ ] , innerHTML : [ String ] = [ ]
59
50
for element in children {
60
51
if let child: LabeledExprSyntax = element. labeled {
61
52
if var key: String = child. label? . text {
62
53
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!) )
64
55
} else {
65
56
if key == " acceptCharset " {
66
57
key = " accept-charset "
67
58
}
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) {
69
60
attributes. append ( key + ( string. isEmpty ? " " : " = \\ \" " + string + " \\ \" " ) )
70
61
}
71
62
}
72
63
// 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) {
74
65
innerHTML. append ( inner_html)
75
66
}
76
67
}
77
68
}
78
69
return ElementData ( attributes: attributes, innerHTML: innerHTML)
79
70
}
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 ] {
81
72
var keys : Set < String > = [ ] , attributes : [ String ] = [ ]
82
73
for element in array. elements {
83
74
let function : FunctionCallExprSyntax = element. expression. as ( FunctionCallExprSyntax . self) !, key_argument : LabeledExprSyntax = function. arguments. first!, key_element : ExprSyntax = key_argument. expression
84
75
var key : String = function. calledExpression. memberAccess!. declName. baseName. text, value : String ? = nil
85
76
switch key {
86
77
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!) !
88
79
if returnType == . string {
89
80
literalValue. escapeHTML ( escapeAttributes: true )
90
81
}
@@ -97,7 +88,7 @@ private extension HTMLElement {
97
88
break
98
89
case " event " :
99
90
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!) {
101
92
if returnType == . string {
102
93
literalValue. escapeHTML ( escapeAttributes: true )
103
94
}
@@ -108,7 +99,7 @@ private extension HTMLElement {
108
99
}
109
100
break
110
101
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) {
112
103
value = string
113
104
}
114
105
break
@@ -126,14 +117,14 @@ private extension HTMLElement {
126
117
}
127
118
return attributes
128
119
}
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 ? {
130
121
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)
132
123
if elementType == . escapeHTML {
133
124
string. escapeHTML ( escapeAttributes: false )
134
125
}
135
126
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 {
137
128
string. escapeHTML ( escapeAttributes: false )
138
129
return string
139
130
} else {
@@ -171,9 +162,9 @@ private extension HTMLElement {
171
162
}
172
163
}
173
164
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 ? {
175
166
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) {
177
168
switch returnType {
178
169
case . boolean: return string. elementsEqual ( " true " ) ? " " : nil
179
170
case . string:
@@ -198,7 +189,7 @@ private extension HTMLElement {
198
189
default : return " "
199
190
}
200
191
}
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 ) ? {
202
193
let expression : ExprSyntax = argument. expression
203
194
if let boolean: String = expression. booleanLiteral? . literal. text {
204
195
return ( boolean, . boolean)
@@ -286,7 +277,6 @@ private extension HTMLElement {
286
277
}
287
278
}
288
279
if returnType == . interpolation || remaining_interpolation > 0 {
289
- //dynamicVariables.append(string)
290
280
if !string. contains ( " \\ ( " ) {
291
281
string = " \\ ( " + string + " ) "
292
282
}
0 commit comments