Repository navigation
Conversation
htmx.find(elt, selector) and htmx.findAll(elt, selector) ran plain CSS selectors on elt's root node, so they returned matches from the whole document. The docs say they search within the element, and htmx 2 did. With an element argument, plain selectors now match within it. Extended selectors (closest, next, find, global, ...) keep working relative to the element. hx-targets relied on the old behavior to resolve its selector like hx-target, so it now uses a new internal API, findAllExt. Fixes bigskysoftware#4173 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
htmx.find(elt, selector)andhtmx.findAll(elt, selector)ran plain CSS selectors on the element's root node, so they returned matches from the whole document. The reference page says the element is the one to "search within", and htmx 2 usedelt.querySelectorAll(selector).Changes:
findAllpasses a newscopedflag to__findAllExt. With an element argument, plain selectors are matched within it. Extended selectors (closest,next,find,global, ...) still resolve relative to the element, as before.findreturns the first result offindAll.hx-targetscalledhtmx.findAll(ctx.sourceElement, selector)to resolve its selector likehx-target(whole document). It now calls a new internal API,api.findAllExt, so its behavior is unchanged. I added it to the two extension API lists thatcheck_extension_api.pychecks.Corresponding issue: #4173
Testing
test/tests/unit/__findAllExt.js:findAll(elt, selector)andfind(elt, selector)only match withinelt, and extended selectors still work relative toelt. The first two fail without the fix.test/tests/ext/hx-targets.js(the extension had no tests): a plain selector outside the source element and an extended selector. The first test fails ifhx-targetskeeps callinghtmx.findAll.npm run test: 1855 passed, 0 failed, 4 skipped, coverage 100% (Chromium, Windows).python3 src/scripts/content/check.pypasses.Checklist
four-devfor htmx 4 source changes)approved via an issue
npm run test) and verified that it succeededThis PR was generated with AI (Claude Code).