fix(swap): find oob targets in the document when the context element is detached - #3998
Open
yfwmaniish wants to merge 1 commit into
Open
fix(swap): find oob targets in the document when the context element is detached#3998yfwmaniish wants to merge 1 commit into
yfwmaniish wants to merge 1 commit into
Conversation
…is detached swap() picks the rootNode for hx-swap-oob target lookup by calling getRootNode() on the request's contextElement (the element that issued it). That was fine when contextElement is still in the document - it's how bigskysoftware#2846 made OOB swaps work inside shadow roots - but if contextElement has since been detached (its own region re-rendered while the request was in flight), getRootNode() returns its orphaned subtree's own root, not the document. oobSwap()'s selector lookup against that orphaned root finds nothing, so every OOB fragment in the response is silently dropped (htmx:oobErrorNoTarget) even though its target is still live in the document, and even though the response's main swap still applies normally. Falling back to the document when contextElement.isConnected is false fixes the drop while leaving the connected-trigger / shadow-root case bigskysoftware#2846 added completely untouched. Fixes bigskysoftware#3959.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3959.
What
swap()picks therootNodeused forhx-swap-oobtarget lookup by callinggetRootNode()on the request'scontextElement(the element that issued it):That's correct while
contextElementis still in the document - it's exactly what #2846 needed to make OOB swaps work inside shadow roots. But ifcontextElementhas since been detached (its own region re-rendered while the request was in flight - e.g. a row in a list whose ownhx-getis pending when the list itself gets replaced),elt.getRootNode()on a detached node returns its orphaned subtree's own root, not the document.oobSwap()'squerySelectorAllExt(rootNode, selector, false)then searches that orphaned root and finds nothing, so every OOB fragment in the response gets silently dropped (htmx:oobErrorNoTargetin the console is the only sign) - even though its target is still live in the document, and even though the response's main swap applies normally.Fix
Falls back to the document when
contextElementis disconnected, leaving the connected-trigger / shadow-root behavior #2846 added completely untouched.Testing
test/attributes/hx-swap-oob.js: click a trigger, detach it (.remove()) before the response lands, and assert a separate, still-in-document OOB target still receives its content.src/htmx.js, kept the test - it fails with the target'sinnerHTMLstaying empty (the drop), all other 33 tests in the file still pass. Restored.eslintclean on both changed files.PR targets
devper CONTRIBUTING.md.