Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attributes["hx-on::after-request"] does not preserve double colon #267

Open
davidchambers opened this issue Apr 11, 2024 · 3 comments
Open

Comments

@davidchambers
Copy link

I'm trying to use the approach suggested in bigskysoftware/htmx#1698 (comment) to reset a form after successful submission.

I'm having trouble with attributes["hx-on::after-request"]: only one colon appears in the rendered HTML.

I'm using .replace("hx-on:after-request", "hx-on::after-request") as a workaround. Can you suggest a better solution?

@severn-everett
Copy link
Contributor

Would you be able to give a more detailed list of reproduction steps? This code that gets inserted into AttributesTest.kt on the master branch passes:

    @Test
    fun testDoubleColon() {
        val html = buildString {
            appendHTML(false).form {
                attributes["hx-on::after-request"] = "if(event.detail.successful) this.reset()"
            }
        }
        assertEquals("<form hx-on::after-request=\"if(event.detail.successful) this.reset()\"></form>", html)
    }

@davidchambers
Copy link
Author

Here's a simplified version of my starting point:

createHTMLDocument().html {
    body {
        form {
            attributes["hx-on::after-request"] = "if (event.detail.successful) this.reset()"
        }
    }
}.serialize()

This crashes with java.lang.RuntimeException: Namespace for prefix 'hx-on' has not been declared.

I made the following change:

 createHTMLDocument().html {
+    attributes["xmlns:hx-on"] = "http://www.w3.org/1999/xhtml"
     body {
         form {
             attributes["hx-on::after-request"] = "if (event.detail.successful) this.reset()"
         }
     }
 }.serialize()

This version doesn't crash, but the resulting HTML is missing a colon.

Interestingly, replacing "hx-on::after-request" with "hx-on:htmx:after-request" does not change the resulting HTML.

@severn-everett
Copy link
Contributor

The immediate cause is that Java's XML parsing library views the colons as part of a namespace-based declaration, so it (unsuccessfully) attempts to look up the namespace hx-on as your POC demonstrates. I'd defer to the lib owners like @e5l as to whether this is functionality that should be supported, but in the meantime, you could follow HTMX's recommendation to use dashes instead of colons, e.g. hx-on--after-request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants