wimdeblauwe / htmx-spring-boot

Spring Boot and Thymeleaf helpers for working with htmx
Apache License 2.0
521 stars 49 forks source link

Is hx:on supported? #125

Open jeffreyhwang opened 4 months ago

jeffreyhwang commented 4 months ago

Looking at the https://github.com/wimdeblauwe/htmx-spring-boot/blob/69fb0e1604a4fc97d97681df9a37f69b3054e1c5/htmx-spring-boot-thymeleaf/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/thymeleaf/HtmxDialect.java#L27-55, it doesn't look like hx:on is supported.

When I try to use it in thymeleaf, it looks like hx:on is being rendered as hx:on, not hx-on.

Alternative that works is using th:hx-on::before-request, but I was wondering if there is a way to use hx:on to keep things consistent.

Thank you!

wimdeblauwe commented 1 week ago

I have just built a working prototype for this, but then I stumbled upon https://github.com/thymeleaf/thymeleaf/issues/705. It seems support for things like:

<div th:onclick="|doSomething('${mydata}');|">

is no longer supported, because it can lead to unsafe things. I think we should respect this, and also not add support for hx:on in this library.

That said, I am still a bit on the fence since you can today already use th:hx-on::before-request, so if we would add support for having hx:on::before-request, it would not matter that much.

In all honesty, I am not sure it is that useful, because if you do this for example:

<div id="hx-on-div" hx:on:click="|console.log('${stringVariable}')|"></div>

It would render as:

<div id="hx-on-div" hx-on:click="console.log(&#39;someString&#39;)"></div>

(Assuming the value of stringVariable in the model is someString)

@xhaggi @checketts I would love to hear your input on this as well.

xhaggi commented 1 week ago

I don't use Thymeleaf in my projects, so it's up you.

wimdeblauwe commented 5 days ago

@jeffreyhwang Do you have an example where you might actually use this? I feel that you can't actually use this in a concrete example because of the escaping that happens, but I might be wrong.