fix: escape IDs in restorePreservedElements, selectOOB, and anchor scroll - #3982
Open
n0vdd wants to merge 1 commit into
Open
fix: escape IDs in restorePreservedElements, selectOOB, and anchor scroll#3982n0vdd wants to merge 1 commit into
n0vdd wants to merge 1 commit into
Conversation
…roll
Three call sites build a CSS selector from a raw element id without
CSS.escape(), causing failures when the id contains CSS-special characters
(dots, slashes, colons, etc.).
- restorePreservedElements: find('#' + preservedElt.id) breaks on the
moveBefore path (Chrome) with dotted ids — the selector parses as
id + class, matches nothing, throws TypeError on .parentNode.
- selectOOB: fragment.querySelector('#' + id) — same pattern.
- anchor scroll: resolveTarget('#' + swapOptions.anchor) — same pattern.
The sibling call sites in oobSwap (fixed by bigskysoftware#3304) and handleAttributes
(fixed by bigskysoftware#3752) already use CSS.escape() correctly.
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.
Description
Three remaining call sites build a CSS selector from a raw element id without
CSS.escape(), causing failures when the id contains CSS-special characters (.,/,:, etc.):restorePreservedElements(line ~1521) —find('#' + preservedElt.id)→querySelector('#' + id). On browsers withmoveBefore(Chrome), htmx moves the preserved element into the pantry div duringhandlePreservedElements, thenrestorePreservedElementslooks it back up by id to put it in place. A dotted id likepayment.parcelasbecomes the selector#payment.parcelas(id=payment AND class=parcelas), matches nothing, and the next line throwsTypeError: Cannot read properties of null (reading 'parentNode'), aborting the swap.selectOOBhandling (line ~1929) —fragment.querySelector('#' + id). Same pattern: the id fromhx-select-oobis interpolated raw.Anchor scroll (line ~2004) —
resolveTarget('#' + swapOptions.anchor)→find()→querySelector(). An anchor id with special chars hits the same parse error.The sibling call sites in
oobSwap(fixed by #3304) andhandleAttributes(fixed by #3752) already useCSS.escape()correctly — these three were missed.Repro (restorePreservedElements — the most visible one)
On Chrome (moveBefore path): swap replaces the content,
handlePreservedElementsstashes the input in the pantry viagetElementById(works fine — getElementById treats id as opaque), thenrestorePreservedElementstriesquerySelector('#payment.amount')→ no match → TypeError on.parentNode. The preserved element is stranded in the pantry div and all subsequent swaps on the page may break.On Firefox/Safari (replaceChild path):
handlePreservedElementstakes the else branch and callsreplaceChilddirectly —restorePreservedElementsis never called, so the bug is latent.Corresponding issues: the same bug class as #1537 (oobSwap) and the settle-lookup in #3752, applied to the three remaining unescaped sites.
Testing
handles hx-preserve on elements with dotted IDstotest/attributes/hx-preserve.jshandles elements with IDs containing dots in hx-select-oobtotest/attributes/hx-select-oob.jsChecklist
devfor source changes)npm run test:chrome) and verified that it succeeded