-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple extended selectors for hx-include, hx-trigger from, and hx-disabled-elt #2902
Support multiple extended selectors for hx-include, hx-trigger from, and hx-disabled-elt #2902
Conversation
After discussion with @1cg, here's the direction this PR should take:
I'll work on these changes when I have the chance |
8e23149
to
4466697
Compare
You might be able to add support for the let selectors = selector
forEach(selectors.match(/<.*?\/>/g), function(enclosed) {
selectors = selectors.replace(enclosed, enclosed.replaceAll(',', '%2C'))
})
const parts = selectors.split(',') Trying to split on the commas but ignore the commas in the enclosed tags is very complex to do with just a regex so here I've just used the most basic regex to find all enclosed portions of the full selectors input string and then for each one replace all the commas with an encoded comma and update this in the main selectors string. Finally you can just add the reverse of this comma encoding in the normalizeSelector() function cf10b71 example commit One thing to note is the next/previous/closest/find extended selectors only return a single node by design so supporting multiple css selectors via the list |
I'm fine w/ a loop-based mini-lexer, the logic isn't too brutal (basically count "<" and "/>") and ignore commas when count > 0) but I'm happy with whatever solution y'all come up w/ |
31dd615
to
48b2ec2
Compare
Looks good @Telroshan! was wondering how efficient this new version was compared to my more hacky solution i posted earlier cf10b71 and checking just the , parsing code on measurethat.net i got this: Checked test: my-version x 774,172 ops/sec ±7.65% (48 runs sampled) So your version is about the same speed just slightly behind mine but then i found that 90% of the performance is lost in the single line: } else if (selector.substring(i, i + 2) === '/>') { updating this to } else if (char === '/' && selector[i+1] === '>') { got me to 3.2million ops/sec |
Damn, that's an interesting benchmark, thanks for looking into it @MichaelWest22 ! I wouldn't have expected to be such a difference compared to using substring |
fun fact. unlike most languages there is no such thing as out of bounds array access error on strings/arrays in javascript. strings/arrays get auto extended with undefined if you read out of bounds so such checks are never needed if you already deal with the undefined case as you could also do } else if (char === '>' && selector[i-1] === '/') {
or if you have to keep the out of bounds check for some reason:
} else if (char === '>' && i > 0 && selector[i-1] === '/') { |
@Telroshan can you update this PR against |
Support multiple extended selectors for hx-include Additional test for nested standard selector Add @MichaelWest22 hx-disabled-elt multiple selector test Add hx-trigger `from` test with multiple extended selectors Simplify Include bigskysoftware#2915 fix Update htmx.js Split for readability Don't apply global to previous selectors Rewrite loop, restore global recursive call, minimize diff Use break for better readability Co-Authored-By: MichaelWest22 <[email protected]>
1e00251
to
c3b1c59
Compare
Sure, done @1cg |
Description
hx-include
will include all elements matching the given selector, but currently only supports 1 extended selector at a time, meaning thatwill work, and include values from all elements that match any of these 2 selectors.
Using 1 extended selector works well, like
will retrieve the first child input's value, and works as expected.
However, the following won't work:
Htmx will currently simply parse the first
find
keyword and useinput, previous input
as the selector to test against previous elements. So, the second partprevious input
won't work at all, as itsprevious
keyword won't even be interpreted by htmx.It will instead be processed as a standard selector, meaning searching for an
input
under aprevious
tag (which ofc, doesn't exist)Explanations
This PR does the following:
querySelectorAll
as it used to.Corresponding issues: #2645 #2660 #2610
Testing
Added 2 test cases
Checklist
master
for website changes,dev
forsource changes)
approved via an issue
npm run test
) and verified that it succeeded