boosted forms that issue a `GET` (and only a `GET`) and have no `action` attribute or an empty `action` attribute should clear the existing parameters of the current path when submitting lmao
This commit is contained in:
Carson Gross 2024-12-11 16:42:20 -07:00
parent 815c117088
commit 232667d2c6
2 changed files with 47 additions and 4 deletions

View File

@ -2321,8 +2321,13 @@ var htmx = (function() {
const rawAttribute = getRawAttribute(elt, 'method')
verb = (/** @type HttpVerb */(rawAttribute ? rawAttribute.toLowerCase() : 'get'))
path = getRawAttribute(elt, 'action')
if (verb === 'get' && path && path.includes('?')) {
path = path.replace(/\?[^#]+/, '')
if (path == null || path === '') {
// if there is no action attribute on the form set path to current href before the
// following logic to properly clear parameters on a GET (not on a POST!)
path = getDocument().location.href
}
if (verb === 'get' && path.includes('?')) {
path = path.replace(/\?[^#]+/, '');
}
}
triggerSpecs.forEach(function(triggerSpec) {

View File

@ -144,8 +144,33 @@ describe('hx-boost attribute', function() {
div.innerHTML.should.equal('Boosted!')
})
it('form get with an unset action property', function() {
it('form get with an unset action properly submits', function() {
this.server.respondWith('GET', /\/*/, function(xhr) {
xhr.respond(200, {}, 'Boosted!')
})
var div = make('<div hx-target="this" hx-boost="true"><form id="f1" method="get"><button id="b1">Submit</button></form></div>')
var btn = byId('b1')
btn.click()
this.server.respond()
div.innerHTML.should.equal('Boosted!')
})
it('form get with no action properly clears existing parameters on submit', function() {
/// add a foo=bar to the current url
var path = location.href;
if (!path.includes("foo=bar")) {
if (!path.includes("?")) {
path += "?foo=bar";
} else {
path += "&foo=bar";
}
}
history.replaceState({ htmx: true }, '', path);
this.server.respondWith('GET', /\/*/, function(xhr) {
// foo should not be present because the form is a get with no action
should.equal(undefined, getParameters(xhr).foo)
xhr.respond(200, {}, 'Boosted!')
})
@ -157,8 +182,21 @@ describe('hx-boost attribute', function() {
div.innerHTML.should.equal('Boosted!')
})
it('form get with an empty action property', function() {
it('form get with an empty action properly clears existing parameters on submit', function() {
/// add a foo=bar to the current url
var path = location.href;
if (!path.includes("foo=bar")) {
if (!path.includes("?")) {
path += "?foo=bar";
} else {
path += "&foo=bar";
}
}
history.replaceState({ htmx: true }, '', path);
this.server.respondWith('GET', /\/*/, function(xhr) {
// foo should not be present because the form is a get with no action
should.equal(undefined, getParameters(xhr).foo)
xhr.respond(200, {}, 'Boosted!')
})