@@ -43,10 +43,15 @@ public extension HTMLElementAttribute {
43
43
case trigger( String )
44
44
case vals( String )
45
45
46
+ case ws( WebSocket )
47
+
46
48
public init ? ( rawValue: String ) {
47
49
guard rawValue. last == " ) " else { return nil }
48
50
let start : String . Index = rawValue. startIndex, end : String . Index = rawValue. index ( before: rawValue. endIndex) , end_minus_one : String . Index = rawValue. index ( before: end)
49
51
let key : Substring = rawValue. split ( separator: " ( " ) [ 0 ]
52
+ func literal( ) -> String {
53
+ return String ( rawValue [ rawValue. index ( start, offsetBy: key. count + 2 ) ..< end] )
54
+ }
50
55
func string( ) -> String {
51
56
return String ( rawValue [ rawValue. index ( start, offsetBy: key. count + 2 ) ..< end_minus_one] )
52
57
}
@@ -65,37 +70,92 @@ public extension HTMLElementAttribute {
65
70
case " disinherit " : self = . disinherit( string ( ) )
66
71
case " encoding " : self = . encoding( string ( ) )
67
72
case " ext " : self = . ext( string ( ) )
68
- //case "headers": self = .headers(js: Bool, [String : String])
73
+ //case "headers": self = .headers(js: Bool, [String : String]) // TODO: fix
69
74
case " history " : self = . history( enumeration ( ) )
70
75
case " historyElt " : self = . historyElt( boolean ( ) )
71
76
case " include " : self = . include( string ( ) )
72
77
case " indicator " : self = . indicator( string ( ) )
73
78
case " inherit " : self = . inherit( string ( ) )
74
- // case "params": self = .params(enumeration() )
79
+ case " params " : self = . params( Params ( rawValue : literal ( ) ) ! )
75
80
case " patch " : self = . patch( string ( ) )
76
81
case " preserve " : self = . preserve( boolean ( ) )
77
82
case " prompt " : self = . prompt( string ( ) )
78
83
case " put " : self = . put( string ( ) )
79
- // case "replaceURL": self = .replaceURL(enumeration() )
80
- //case "request": self = .request(js: Bool, timeout: Int, credentials: Bool, noHeaders: Bool)
81
- //case "sync": self = .sync(String, strategy: SyncStrategy?)
84
+ case " replaceURL " : self = . replaceURL( URL ( rawValue : literal ( ) ) ! )
85
+ //case "request": self = .request(js: Bool, timeout: Int, credentials: Bool, noHeaders: Bool) // TODO: fix
86
+ //case "sync": self = .sync(String, strategy: SyncStrategy?) // TODO: fix
82
87
case " validate " : self = . validate( enumeration ( ) )
83
88
84
89
case " get " : self = . get( string ( ) )
85
90
case " post " : self = . post( string ( ) )
86
- //case "on": self = .on(Event, String)
87
- //case "pushURL": self = .pushURL(enumeration())
91
+ case " on " :
92
+ let string : String = literal ( )
93
+ let values : [ Substring ] = string. split ( separator: " , " )
94
+ let event_string : String = String ( values [ 0 ] )
95
+ var value : String = String ( string [ values [ 1 ] . startIndex... ] )
96
+ while ( value. first? . isWhitespace ?? false ) || value. first == " \" " {
97
+ value. removeFirst ( )
98
+ }
99
+ value. removeLast ( )
100
+ let event : Event = Event ( rawValue: event_string) !
101
+ self = . on( event, value)
102
+ break
103
+ case " pushURL " : self = . pushURL( URL ( rawValue: string ( ) ) !)
88
104
case " select " : self = . select( string ( ) )
89
105
case " selectOOB " : self = . selectOOB( string ( ) )
90
106
case " swap " : self = . swap( enumeration ( ) )
91
107
case " swapOOB " : self = . swapOOB( string ( ) )
92
108
case " target " : self = . target( string ( ) )
93
109
case " trigger " : self = . trigger( string ( ) )
94
110
case " vals " : self = . vals( string ( ) )
111
+
112
+ case " ws " : self = . ws( WebSocket ( rawValue: literal ( ) ) !)
95
113
default : return nil
96
114
}
97
115
}
98
116
117
+ public var key : String {
118
+ switch self {
119
+ case . boost( _) : return " boost "
120
+ case . confirm( _) : return " confirm "
121
+ case . delete( _) : return " delete "
122
+ case . disable( _) : return " disable "
123
+ case . disabledElt( _) : return " disable-elt "
124
+ case . disinherit( _) : return " disinherit "
125
+ case . encoding( _) : return " encoding "
126
+ case . ext( _) : return " ext "
127
+ case . headers( _, _) : return " headers "
128
+ case . history( _) : return " history "
129
+ case . historyElt( _) : return " historyElt "
130
+ case . include( _) : return " include "
131
+ case . indicator( _) : return " indicator "
132
+ case . inherit( _) : return " inherit "
133
+ case . params( _) : return " params "
134
+ case . patch( _) : return " patch "
135
+ case . preserve( _) : return " preserve "
136
+ case . prompt( _) : return " prompt "
137
+ case . put( _) : return " put "
138
+ case . replaceURL( _) : return " replace-url "
139
+ case . request( _, _, _, _) : return " request "
140
+ case . sync( _, _) : return " sync "
141
+ case . validate( _) : return " validate "
142
+
143
+ case . get( _) : return " get "
144
+ case . post( _) : return " post "
145
+ case . on( let event, _) : return " on: " + event. rawValue
146
+ case . pushURL( _) : return " push-url "
147
+ case . select( _) : return " select "
148
+ case . selectOOB( _) : return " select-oob "
149
+ case . swap( _) : return " swap "
150
+ case . swapOOB( _) : return " swap-oob "
151
+ case . target( _) : return " target "
152
+ case . trigger( _) : return " trigger "
153
+ case . vals( _) : return " vals "
154
+
155
+ case . ws( let value) : return " ws- " + value. key
156
+ }
157
+ }
158
+
99
159
public var htmlValue : String {
100
160
switch self {
101
161
case . boost( let value) : return value. rawValue
@@ -106,8 +166,8 @@ public extension HTMLElementAttribute {
106
166
case . disinherit( let value) : return value
107
167
case . encoding( let value) : return value
108
168
case . ext( let value) : return value
109
- case . headers( let js, let headers) :
110
- return js ? " " : headers. map ( { " \" " + $0. key + " \" : \" " + $0. value + " \" " } ) . joined ( separator: " , " )
169
+ case . headers( let js, let headers) : // TODO: fix
170
+ return js ? " " : " { " + headers. map ( { " \\ \ "" + $0. key + " \\ \ ": \\ \ "" + $0. value + " \\ \ "" } ) . joined ( separator: " , " ) + " } "
111
171
case . history( let value) : return value. rawValue
112
172
case . historyElt( _) : return " "
113
173
case . include( let value) : return value
@@ -120,7 +180,7 @@ public extension HTMLElementAttribute {
120
180
case . put( let value) : return value
121
181
case . replaceURL( let url) : return url. htmlValue
122
182
case . request( let js, let timeout, let credentials, let noHeaders) :
123
- return " "
183
+ return " " // TODO: fix
124
184
case . sync( let selector, let strategy) :
125
185
return selector + ( strategy == nil ? " " : " : " + strategy!. htmlValue)
126
186
case . validate( let value) : return value. rawValue
@@ -136,6 +196,8 @@ public extension HTMLElementAttribute {
136
196
case . target( let value) : return value
137
197
case . trigger( let value) : return value
138
198
case . vals( let value) : return value
199
+
200
+ case . ws( let value) : return value. htmlValue
139
201
}
140
202
}
141
203
}
@@ -223,6 +285,25 @@ public extension HTMLElementAttribute.HTMX {
223
285
case not( [ String ] )
224
286
case list( [ String ] )
225
287
288
+ public init ? ( rawValue: String ) {
289
+ let key : Substring = rawValue. split ( separator: " ( " ) [ 0 ]
290
+ func array_string( ) -> [ String ] {
291
+ let string : String = String ( rawValue [ rawValue. index ( rawValue. startIndex, offsetBy: key. count + 2 ) ..< rawValue. index ( before: rawValue. endIndex) ] )
292
+ let ranges : [ Range < String . Index > ] = try ! string. ranges ( of: Regex ( " \" ([^ \" ]+) \" " ) ) // TODO: fix? (doesn't parse correctly if the string contains escaped quotation marks)
293
+ return ranges. map ( {
294
+ let item : String = String ( string [ $0] )
295
+ return String ( item [ item. index ( after: item. startIndex) ..< item. index ( before: item. endIndex) ] )
296
+ } )
297
+ }
298
+ switch key {
299
+ case " all " : self = . all
300
+ case " none " : self = . none
301
+ case " not " : self = . not( array_string ( ) )
302
+ case " list " : self = . list( array_string ( ) )
303
+ default : return nil
304
+ }
305
+ }
306
+
226
307
public var htmlValue : String {
227
308
switch self {
228
309
case . all: return " * "
@@ -266,6 +347,20 @@ public extension HTMLElementAttribute.HTMX {
266
347
case `true`, `false`
267
348
case url( String )
268
349
350
+ public init ? ( rawValue: String ) {
351
+ let key : Substring = rawValue. split ( separator: " ( " ) [ 0 ]
352
+ let end : String . Index = rawValue. index ( before: rawValue. endIndex) , end_minus_one : String . Index = rawValue. index ( before: end)
353
+ func string( ) -> String {
354
+ return String ( rawValue [ rawValue. index ( rawValue. startIndex, offsetBy: key. count + 2 ) ..< end_minus_one] )
355
+ }
356
+ switch key {
357
+ case " true " : self = . true
358
+ case " false " : self = . false
359
+ case " url " : self = . url( string ( ) )
360
+ default : return nil
361
+ }
362
+ }
363
+
269
364
public var htmlValue : String {
270
365
switch self {
271
366
case . true : return " true "
@@ -274,4 +369,52 @@ public extension HTMLElementAttribute.HTMX {
274
369
}
275
370
}
276
371
}
372
+ }
373
+
374
+ // MARK: WebSocket
375
+ public extension HTMLElementAttribute . HTMX {
376
+ enum WebSocket {
377
+ case connect( String )
378
+ case send( String )
379
+
380
+ public init ? ( rawValue: String ) {
381
+ guard rawValue. last == " ) " else { return nil }
382
+ let start : String . Index = rawValue. startIndex, end : String . Index = rawValue. index ( before: rawValue. endIndex) , end_minus_one : String . Index = rawValue. index ( before: end)
383
+ let key : Substring = rawValue. split ( separator: " ( " ) [ 0 ]
384
+ func string( ) -> String {
385
+ return String ( rawValue [ rawValue. index ( start, offsetBy: key. count + 2 ) ..< end_minus_one] )
386
+ }
387
+ switch key {
388
+ case " connect " : self = . connect( string ( ) )
389
+ case " send " : self = . send( string ( ) )
390
+ default : return nil
391
+ }
392
+ }
393
+
394
+ public var key : String {
395
+ switch self {
396
+ case . connect( _) : return " connect "
397
+ case . send( _) : return " send "
398
+ }
399
+ }
400
+
401
+ public var htmlValue : String {
402
+ switch self {
403
+ case . connect( let value) : return value
404
+ case . send( let value) : return value
405
+ }
406
+ }
407
+
408
+ public enum Event : String {
409
+ case wsConnecting
410
+ case wsOpen
411
+ case wsClose
412
+ case wsError
413
+ case wsBeforeMessage
414
+ case wsAfterMessage
415
+ case wsConfigSend
416
+ case wsBeforeSend
417
+ case wsAfterSend
418
+ }
419
+ }
277
420
}
0 commit comments