Skip to content

Commit aa0be5a

Browse files
jakvbseffect-bot
andauthored
fix(ui): only give HTML forms submit handling when hydrating (#11203)
Co-authored-by: Effect Bot <tech-ops@effectful.co>
1 parent a55d273 commit aa0be5a

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎.changeset/hydrate-svg-form.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Restrict hydration's submit handling to HTML forms so it no longer cancels submit events on SVG or MathML elements named `form`.

‎packages/effect/src/ui/html/HtmlHydrate.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,10 @@ const between = (
548548
}
549549
}
550550
}
551-
if ((node as Element).localName === "form") {
551+
if (
552+
(node as Element).localName === "form" &&
553+
(node as Element).namespaceURI === "http://www.w3.org/1999/xhtml"
554+
) {
552555
const env = inst.env
553556
if (
554557
inst.tree.served?.(inst) !== undefined && env.ctx === undefined &&

‎packages/effect/test/ui/html/HtmlHydrate.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,30 @@ describe("HtmlHydrate", () => {
596596
])
597597
}).pipe(Effect.scoped))
598598

599+
it.effect("creates a missing SVG child as SVG and leaves a MathML form's submits alone", () =>
600+
Effect.gen(function*() {
601+
const Icon = Html.component({
602+
name: "Icon",
603+
model: Schema.Null,
604+
message: Schema.Never
605+
}).make({
606+
init: () => null,
607+
update: ($) => $.model,
608+
// Keep the form before the missing title so hydration adopts it.
609+
view: (h) => h.div(h.el("math", h.form.attr("method", "dialog")), h.el("svg", h.el("title", "Chart")))
610+
})
611+
const rendered = yield* HtmlServer.renderToHtml(Icon, {})
612+
const app = yield* container(rendered.html.replace("<title>Chart</title>", ""))
613+
// The test DOM parses <math> children as HTML; browsers use MathML.
614+
const form = document.createElementNS("http://www.w3.org/1998/Math/MathML", "form")
615+
form.setAttribute("method", "dialog")
616+
app.querySelector("math")!.replaceChildren(form)
617+
yield* HtmlHydrate.hydrateEffect(Icon, {}, app, rendered.state)
618+
assert.strictEqual(app.querySelector("title")!.namespaceURI, "http://www.w3.org/2000/svg")
619+
assert.strictEqual(app.querySelector("math")!.firstElementChild, form)
620+
assert.isTrue(submitted(form))
621+
}).pipe(Effect.scoped))
622+
599623
it.effect("replaces a stale element with text inside comment markers while keeping surrounding siblings", () =>
600624
Effect.gen(function*() {
601625
const Texty = Html.component({

0 commit comments

Comments
 (0)