File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 3
3
package htmx
4
4
5
5
import (
6
+ "io"
7
+
6
8
g "github.com/maragudk/gomponents"
7
9
)
8
10
@@ -18,6 +20,12 @@ func Get(url string) g.Node {
18
20
return attr ("get" , url )
19
21
}
20
22
23
+ // On handles any event with a script inline.
24
+ // See https://htmx.org/attributes/hx-on
25
+ func On (name string , v string ) g.Node {
26
+ return & rawAttr {name : "on:" + name , value : v }
27
+ }
28
+
21
29
// Post to the specified URL.
22
30
// See https://htmx.org/attributes/hx-post
23
31
func Post (url string ) g.Node {
@@ -195,3 +203,18 @@ func Validate(v string) g.Node {
195
203
func attr (name , value string ) g.Node {
196
204
return g .Attr ("hx-" + name , value )
197
205
}
206
+
207
+ // rawAttr is an attribute that doesn't escape its value.
208
+ type rawAttr struct {
209
+ name string
210
+ value string
211
+ }
212
+
213
+ func (r * rawAttr ) Render (w io.Writer ) error {
214
+ _ , err := w .Write ([]byte (" hx-" + r .name + `="` + r .value + `"` ))
215
+ return err
216
+ }
217
+
218
+ func (r * rawAttr ) Type () g.NodeType {
219
+ return g .AttributeType
220
+ }
Original file line number Diff line number Diff line change @@ -60,3 +60,15 @@ func ExampleGet() {
60
60
_ = n .Render (os .Stdout )
61
61
// Output: <button hx-post="/clicked" hx-swap="outerHTML"></button>
62
62
}
63
+
64
+ func TestOn (t * testing.T ) {
65
+ t .Run (`should output hx-on:click="alert('hat')"` , func (t * testing.T ) {
66
+ n := g .El ("div" , hx .On ("click" , "alert('hat')" ))
67
+ assert .Equal (t , `<div hx-on:click="alert('hat')"></div>` , n )
68
+ })
69
+
70
+ t .Run (`should output hx-on::before-request="alert('hat')"` , func (t * testing.T ) {
71
+ n := g .El ("div" , hx .On (":before-request" , "alert('hat')" ))
72
+ assert .Equal (t , `<div hx-on::before-request="alert('hat')"></div>` , n )
73
+ })
74
+ }
You can’t perform that action at this time.
0 commit comments