@@ -19,42 +19,40 @@ import struct NIOCore.ByteBuffer
1919enum HTMLElementMacro : ExpressionMacro {
2020 static func expansion( of node: some FreestandingMacroExpansionSyntax , in context: some MacroExpansionContext ) throws -> ExprSyntax {
2121 let string : String = expand_macro ( context: context, macro: node. macroExpansion!)
22- var set : Set < HTMLElementType ? > = [ . htmlUTF8Bytes , . htmlUTF16Bytes , . htmlUTF8CString , . htmlByteBuffer ]
22+ let encoding : HTMLEncoding = HTMLEncoding . string
2323
24- #if canImport(Foundation)
25- set. insert ( . htmlData)
26- #endif
27-
28- let elementType : HTMLElementType ? = HTMLElementType ( rawValue: node. macroName. text)
29- if set. contains ( elementType) {
24+ func has_no_interpolation( ) -> Bool {
3025 let has_interpolation : Bool = !string. ranges ( of: try ! Regex ( " \\ ((.*) \\ ) " ) ) . isEmpty
3126 guard !has_interpolation else {
3227 context. diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " interpolationNotAllowedForDataType " , message: " String Interpolation is not allowed for this data type. Runtime values get converted to raw text, which is not the expected result. " ) ) )
33- return " "
34- }
35- func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
36- return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
28+ return false
3729 }
38- switch elementType {
39- case . htmlUTF8Bytes:
40- return " \( raw: bytes ( [ UInt8] ( string. utf8) ) ) "
41- case . htmlUTF16Bytes:
42- return " \( raw: bytes ( [ UInt16] ( string. utf16) ) ) "
43- case . htmlUTF8CString:
44- return " \( raw: string. utf8CString) "
30+ return true
31+ }
32+ func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
33+ return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
34+ }
35+ switch encoding {
36+ case . utf8Bytes:
37+ guard has_no_interpolation ( ) else { return " " }
38+ return " \( raw: bytes ( [ UInt8] ( string. utf8) ) ) "
39+ case . utf16Bytes:
40+ guard has_no_interpolation ( ) else { return " " }
41+ return " \( raw: bytes ( [ UInt16] ( string. utf16) ) ) "
42+ case . utf8CString:
43+ return " \( raw: string. utf8CString) "
4544
46- #if canImport(Foundation)
47- case . htmlData:
48- return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
49- #endif
45+ case . foundationData:
46+ guard has_no_interpolation ( ) else { return " " }
47+ return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
5048
51- case . htmlByteBuffer:
52- return " ByteBuffer(bytes: \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
49+ case . byteBuffer:
50+ guard has_no_interpolation ( ) else { return " " }
51+ return " ByteBuffer(bytes: \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
5352
54- default : break
55- }
53+ case . string :
54+ return " \" \( raw : string ) \" "
5655 }
57- return " \" \( raw: string) \" "
5856 }
5957}
6058
@@ -65,56 +63,14 @@ private extension HTMLElementMacro {
6563 return " \( macro) "
6664 }
6765 let children : SyntaxChildren = macro. arguments. children ( viewMode: . all)
68- if elementType == . escapeHTML {
66+ /* if elementType == .escapeHTML {
6967 let array:[CustomStringConvertible] = children.compactMap({
7068 guard let child:LabeledExprSyntax = $0.labeled else { return nil }
7169 return HTMLKitUtilities.parse_inner_html(context: context, child: child, lookupFiles: [])
7270 })
7371 return array.map({ String(describing: $0) }).joined()
74- }
72+ }*/
7573 let data : HTMLKitUtilities . ElementData = HTMLKitUtilities . parse_arguments ( context: context, children: children)
7674 return data. innerHTML. map ( { String ( describing: $0) } ) . joined ( )
7775 }
78- }
79-
80- extension HTMLElementAttribute . Extra {
81- enum CSSUnitType : String {
82- case centimeters
83- case millimeters
84- case inches
85- case pixels
86- case points
87- case picas
88-
89- case em
90- case ex
91- case ch
92- case rem
93- case viewportWidth
94- case viewportHeight
95- case viewportMin
96- case viewportMax
97- case percent
98-
99- var suffix : String {
100- switch self {
101- case . centimeters: return " cm "
102- case . millimeters: return " mm "
103- case . inches: return " in "
104- case . pixels: return " px "
105- case . points: return " pt "
106- case . picas: return " pc "
107-
108- case . em: return " em "
109- case . ex: return " ex "
110- case . ch: return " ch "
111- case . rem: return " rem "
112- case . viewportWidth: return " vw "
113- case . viewportHeight: return " vh "
114- case . viewportMin: return " vmin "
115- case . viewportMax: return " vmax "
116- case . percent: return " % "
117- }
118- }
119- }
12076}
0 commit comments