From 8959c03151df760848941701c62f4a2376a0496c Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 6 Nov 2026 11:21:16 -0500 Subject: [PATCH] Special-case `` element `[open]` attribute According to the MDN documentation for the [HTMLDialogElement.open()][] attribute: > *Warning*: While the open property is technically not read-only and can > be set directly, doing so is strongly discouraged by the HTML > specification, as it can break normal dialog interactions in unexpected > ways. For example, the `close` event won't fire when programmatically > setting `open` to `false`, and subsequent calls to the `close()` and > `requestClose()` methods will have no effect. Instead, it's better to > use methods such as `show()`, `showModal()`, `close()`, and > `requestClose()` to change the value of the `open` attribute. > > -- MDN documentation for `HTMLDialogElement.open` property value Prior to this commit, `Idiomorph.morph` invocations that resulted in the removal of the `[open]` attribute violated this guidance. If the `[open]` attribute was present (regardless of its provenance: `HTMLDialogElement.show()`, `HTMLDialogElement.showModal()`, or being rendered with `[open]`), the morph would remove the attribute itself in a way that circumvents the internal state management of the `` element. This commit introduces a special-case conditional that skips the explicit removal of the `[open]` in favor of invoking [HTMLDialogElement.close()][] directly. Since morphs occur without end-user involvement, `.close()` is invoked rather than [HTMLDialogElement.requestClose()][]. [HTMLDialogElement.open()]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/open#value [HTMLDialogElement.close()]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/close [HTMLDialogElement.requestClose()]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/requestClose --- CHANGELOG.md | 3 +++ src/idiomorph.js | 8 ++++++++ test/core.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c926bb..a005e0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Changed: + * Invoke `HTMLDialogElement.close` instead of removing `[open]` attribute. (@seanpdoyle) + * Removed: * Move the htmx extension out of tree into [htmx-ext-morph](https://github.com/bigskysoftware/htmx-extensions/tree/main/src/morph). (@botandrose) #111 * Remove the undocumented `head.block` option. I hereby commit `Idiomorph.morph()` to being a sync function. Please open an issue if you miss this. (@botandrose) diff --git a/src/idiomorph.js b/src/idiomorph.js index 20fe99a..1a4d5ea 100644 --- a/src/idiomorph.js +++ b/src/idiomorph.js @@ -698,6 +698,14 @@ var Idiomorph = (function () { if (ignoreAttribute(oldAttribute.name, oldElt, "remove", ctx)) { continue; } + if ( + oldAttribute.name === "open" && + oldElt instanceof HTMLDialogElement && + newElt instanceof HTMLDialogElement + ) { + oldElt.close(); + continue; + } oldElt.removeAttribute(oldAttribute.name); } } diff --git a/test/core.js b/test/core.js index d543b5f..e3e2938 100644 --- a/test/core.js +++ b/test/core.js @@ -842,4 +842,39 @@ describe("Core morphing tests", function () { warn.called.should.equal(false); }); }); + + describe("open dialog elements", function () { + beforeEach(function () { + clearWorkArea(); + }); + + it("invokes HTMLDialogElement.close when removing [open] attribute", function () { + let html = "

Foo

"; + let dialog = make(html); + getWorkArea().append(dialog); + dialog.showModal(); + let close = sinon.spy(dialog, "close"); + Idiomorph.morph(dialog, html); + dialog.open.should.equal(false, "removes [open] attribute"); + close.calledOnce.should.equal(true, "invokes HTMLDialogElement.close()"); + }); + + it("does not invoke HTMLDialogElement.close when cancelling the removal of the [open] attribute", function () { + let html = "

Foo

"; + let dialog = make(html); + getWorkArea().append(dialog); + dialog.showModal(); + let close = sinon.spy(dialog, "close"); + Idiomorph.morph(dialog, html, { + callbacks: { + beforeAttributeUpdated: (attr, to, updatetype) => false, + }, + }); + dialog.open.should.equal(true, "retains [open] attribute"); + close.called.should.equal( + false, + "does not invoke HTMLDialogElement.close()", + ); + }); + }); });