Skip to content

Commit

Permalink
Don't apply global to previous selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Telroshan committed Sep 22, 2024
1 parent 5ff9fb5 commit 3b38d3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,14 @@ var htmx = (function() {
let selector = normalizeSelector(parts[i])

if (selector.indexOf('global ') === 0) {
// Previous implementation didn't support `global` at another position than the start
// One would expect that `input, global button` would not apply `global` to `input` though
if (!global && unprocessedParts.length > 0) {
const standardSelector = unprocessedParts.join(',')
const rootNode = asParentNode(getRootNode(elt, false))
result.push(...toArray(rootNode.querySelectorAll(standardSelector)))
unprocessedParts.length = 0
}
global = true
selector = selector.substring(7)
}
Expand Down Expand Up @@ -1186,9 +1194,9 @@ var htmx = (function() {
}

if (unprocessedParts.length > 0) {
const selector = unprocessedParts.join(',')
const standardSelector = unprocessedParts.join(',')
const rootNode = asParentNode(getRootNode(elt, global))
result.push(...toArray(rootNode.querySelectorAll(selector)))
result.push(...toArray(rootNode.querySelectorAll(standardSelector)))
}

return result
Expand Down
22 changes: 22 additions & 0 deletions test/core/shadowdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,4 +1313,26 @@ describe('Core htmx Shadow DOM Tests', function() {
window.foo.should.equal(true)
delete window.foo
})

it('global selector modifier does not apply to previous selectors', function() {
this.server.respondWith('POST', '/include', function(xhr) {
var params = getParameters(xhr)
params.i1.should.equal('test')
should.equal(params.i2, undefined)
params.i3.should.equal('bar')
params.i4.should.equal('test2')
xhr.respond(200, {}, 'Clicked!')
})
make('<div>' +
'<input id="i1" name="i1" value="test"/>' +
'<input name="i2" value="foo"/>' +
'<button id="btn" hx-post="/include" hx-include="#i1, next input, global #i4"></button>' +
'</div>' +
'<input name="i3" value="bar"/>')
getWorkArea().innerHTML += '<input name="i4" value="test2" id="i4"/>'
var btn = byId('btn')
btn.click()
this.server.respond()
btn.innerHTML.should.equal('Clicked!')
})
})

0 comments on commit 3b38d3c

Please sign in to comment.